]> git.tdb.fi Git - r2c2.git/blob - client.cpp
7c583255b2b7b29f04b714c0590257f959c78afb
[r2c2.git] / client.cpp
1 #include "client.h"
2
3 using namespace std;
4 using namespace Msp;
5
6 namespace R2C2 {
7
8 Client::Client(const Catalogue &c):
9         catalogue(c),
10         socket(0),
11         comm(0),
12         event_disp(0),
13         power(false),
14         halt(false)
15 { }
16
17 Client::~Client()
18 {
19         delete comm;
20         delete socket;
21         for(map<unsigned, NetTrain *>::iterator i=trains.begin(); i!=trains.end(); ++i)
22                 delete i->second;
23 }
24
25 void Client::use_event_dispatcher(Msp::IO::EventDispatcher &ed)
26 {
27         event_disp = &ed;
28         if(socket)
29                 event_disp->add(*socket);
30 }
31
32 void Client::connect(const Net::SockAddr &addr)
33 {
34         socket = new Net::StreamSocket(addr.get_family());
35         socket->connect(addr);
36         if(event_disp)
37                 event_disp->add(*socket);
38         comm = new Net::Communicator(*socket, proto, *this);
39 }
40
41 void Client::set_power(bool p)
42 {
43         DriverStatePacket pkt;
44         pkt.power = p;
45         pkt.halt = halt;
46         send(pkt);
47 }
48
49 void Client::set_halt(bool h)
50 {
51         DriverStatePacket pkt;
52         pkt.power = power;
53         pkt.halt = h;
54         send(pkt);
55 }
56
57 NetTrain &Client::get_train(unsigned addr) const
58 {
59         map<unsigned, NetTrain *>::const_iterator i = trains.find(addr);
60         if(i==trains.end())
61                 throw KeyError("Unknown train");
62         return *i->second;
63 }
64
65 void Client::receive(const DriverStatePacket &pkt)
66 {
67         if(pkt.power!=power)
68         {
69                 power = pkt.power;
70                 signal_power_changed.emit(power);
71         }
72         if(pkt.halt!=halt)
73         {
74                 halt = pkt.halt;
75                 signal_halt_changed.emit(halt);
76         }
77 }
78
79 void Client::receive(const EmergencyPacket &pkt)
80 {
81         signal_emergency.emit(pkt.message);
82 }
83
84 void Client::receive(const TrainInfoPacket &pkt)
85 {
86         NetTrain *train = new NetTrain(*this, pkt);
87         trains[pkt.address] = train;
88         signal_train_added.emit(*train);
89 }
90
91 void Client::receive(const TrainControlPacket &pkt)
92 {
93         get_train(pkt.address).process_packet(pkt);
94 }
95
96 void Client::receive(const TrainFunctionPacket &pkt)
97 {
98         get_train(pkt.address).process_packet(pkt);
99 }
100
101 void Client::receive(const TrainStatusPacket &pkt)
102 {
103         get_train(pkt.address).process_packet(pkt);
104 }
105
106 void Client::receive(const RouteInfoPacket &pkt)
107 {
108         routes.push_back(pkt.name);
109 }
110
111 void Client::receive(const TrainRoutePacket &pkt)
112 {
113         get_train(pkt.address).process_packet(pkt);
114 }
115
116 void Client::receive(const ErrorPacket &pkt)
117 {
118         signal_error.emit(pkt.message);
119 }
120
121 } // namespace R2C2