3 This file is part of R²C²
4 Copyright © 2009-2011 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
15 Client::Client(const Catalogue &c):
28 for(map<unsigned, NetTrain *>::iterator i=trains.begin(); i!=trains.end(); ++i)
32 void Client::use_event_dispatcher(Msp::IO::EventDispatcher &ed)
36 event_disp->add(*socket);
39 void Client::connect(const Net::SockAddr &addr)
41 socket = new Net::StreamSocket(addr.get_family());
42 socket->connect(addr);
44 event_disp->add(*socket);
45 comm = new Net::Communicator(*socket, proto, *this);
48 void Client::set_power(bool p)
50 DriverStatePacket pkt;
56 void Client::set_halt(bool h)
58 DriverStatePacket pkt;
64 NetTrain &Client::get_train(unsigned addr) const
66 map<unsigned, NetTrain *>::const_iterator i = trains.find(addr);
68 throw KeyError("Unknown train");
72 void Client::receive(const DriverStatePacket &pkt)
77 signal_power_changed.emit(power);
82 signal_halt_changed.emit(halt);
86 void Client::receive(const EmergencyPacket &pkt)
88 signal_emergency.emit(pkt.message);
91 void Client::receive(const TrainInfoPacket &pkt)
93 NetTrain *train = new NetTrain(*this, pkt);
94 trains[pkt.address] = train;
95 signal_train_added.emit(*train);
98 void Client::receive(const TrainControlPacket &pkt)
100 get_train(pkt.address).process_packet(pkt);
103 void Client::receive(const TrainFunctionPacket &pkt)
105 get_train(pkt.address).process_packet(pkt);
108 void Client::receive(const TrainStatusPacket &pkt)
110 get_train(pkt.address).process_packet(pkt);
113 void Client::receive(const RouteInfoPacket &pkt)
115 routes.push_back(pkt.name);
118 void Client::receive(const TrainRoutePacket &pkt)
120 get_train(pkt.address).process_packet(pkt);
123 void Client::receive(const ErrorPacket &pkt)
125 signal_error.emit(pkt.message);