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