]> git.tdb.fi Git - r2c2.git/blob - server.cpp
df40b3f11ea23d03bf9ca1cb2bb3cdb36b36d6f5
[r2c2.git] / server.cpp
1 /* $Id$
2
3 This file is part of R²C²
4 Copyright © 2009-2011  Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <msp/net/inet.h>
9 #include "libr2c2/driver.h"
10 #include "libr2c2/route.h"
11 #include "libr2c2/train.h"
12 #include "libr2c2/vehicletype.h"
13 #include "server.h"
14
15 using namespace std;
16 using namespace Msp;
17
18 namespace R2C2 {
19
20 Server::Server(Layout &l):
21         layout(l),
22         listen_sock(Net::INET),
23         event_disp(0)
24 {
25         layout.get_driver().signal_power.connect(sigc::hide(sigc::mem_fun(this, &Server::driver_state_changed)));
26         layout.get_driver().signal_halt.connect(sigc::hide(sigc::mem_fun(this, &Server::driver_state_changed)));
27         layout.signal_emergency.connect(sigc::mem_fun(this, &Server::emergency));
28
29         layout.signal_train_added.connect(sigc::mem_fun(this, &Server::train_added));
30
31         const map<unsigned, Train *> &trains = layout.get_trains();
32         for(map<unsigned, Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
33                 train_added(*i->second);
34
35         listen_sock.listen(Net::InetAddr(0, 8315), 4);
36         listen_sock.signal_data_available.connect(sigc::mem_fun(this, &Server::incoming_connection));
37 }
38
39 void Server::use_event_dispatcher(IO::EventDispatcher &ed)
40 {
41         event_disp = &ed;
42         event_disp->add(listen_sock);
43 }
44
45 void Server::incoming_connection()
46 {
47         Net::StreamSocket *sock = listen_sock.accept();
48         if(event_disp)
49                 event_disp->add(*sock);
50         connections.push_back(new Connection(*this, sock));
51 }
52
53 void Server::driver_state_changed()
54 {
55         DriverStatePacket pkt;
56         pkt.power = layout.get_driver().get_power();
57         pkt.halt = layout.get_driver().is_halted();
58         send(pkt);
59 }
60
61 void Server::emergency(const string &msg)
62 {
63         EmergencyPacket pkt;
64         pkt.message = msg;
65         send(pkt);
66 }
67
68 void Server::train_added(Train &train)
69 {
70         train.signal_control_changed.connect(sigc::bind<0>(sigc::mem_fun(this, &Server::train_control_changed), sigc::ref(train)));
71         train.signal_function_changed.connect(sigc::bind<0>(sigc::mem_fun(this, &Server::train_function_changed), sigc::ref(train)));
72         train.signal_route_changed.connect(sigc::bind<0>(sigc::mem_fun(this, &Server::train_route_changed), sigc::ref(train)));
73         train.signal_status_changed.connect(sigc::bind<0>(sigc::mem_fun(this, &Server::train_status_changed), sigc::ref(train)));
74
75         TrainInfoPacket pkt;
76         pkt.address = train.get_address();
77         pkt.loco_type = train.get_locomotive_type().get_article_number().str();
78         pkt.name = train.get_name();
79         send(pkt);
80
81         TrainStatus *status = new TrainStatus(train);
82         status->set_tag("server:status");
83         status->signal_changed.connect(sigc::bind<0>(sigc::mem_fun(this, &Server::train_status_changed), sigc::ref(train)));
84 }
85
86 void Server::train_control_changed(const Train &train, const string &control, float value)
87 {
88         TrainControlPacket pkt;
89         pkt.address = train.get_address();
90         pkt.control = control;
91         pkt.value = value;
92         send(pkt);
93 }
94
95 void Server::train_function_changed(const Train &train, unsigned, bool)
96 {
97         TrainFunctionPacket pkt;
98         pkt.address = train.get_address();
99         pkt.functions = train.get_functions();
100         send(pkt);
101 }
102
103 void Server::train_route_changed(const Train &train, const Route *route)
104 {
105         TrainRoutePacket pkt;
106         pkt.address = train.get_address();
107         if(route)
108                 pkt.route = route->get_name();
109         send(pkt);
110 }
111
112 void Server::train_status_changed(const Train &train, const string &status)
113 {
114         TrainStatusPacket pkt;
115         pkt.address = train.get_address();
116         pkt.status = status;
117         send(pkt);
118 }
119
120 template<typename P>
121 void Server::send(const P &pkt)
122 {
123         for(vector<Connection *>::const_iterator i=connections.begin(); i!=connections.end(); ++i)
124                 if(!(*i)->stale && (*i)->comm.is_handshake_done())
125                 {
126                         try
127                         {
128                                 (*i)->comm.send(pkt);
129                         }
130                         catch(...)
131                         {
132                                 (*i)->stale = true;
133                         }
134                 }
135 }
136
137
138 Server::Connection::Connection(Server &s, Net::StreamSocket *o):
139         server(s),
140         socket(o),
141         comm(*socket, server.proto, *this)
142 {
143         socket->signal_end_of_file.connect(sigc::mem_fun(this, &Connection::end_of_file));
144         comm.signal_handshake_done.connect(sigc::mem_fun(this, &Connection::handshake_done));
145         comm.initiate_handshake();
146 }
147
148 Server::Connection::~Connection()
149 {
150         delete socket;
151 }
152
153 void Server::Connection::handshake_done()
154 {
155         {
156                 DriverStatePacket pkt;
157                 pkt.power = server.layout.get_driver().get_power();
158                 pkt.halt = server.layout.get_driver().is_halted();
159                 comm.send(pkt);
160         }
161
162         const set<Route *> &routes = server.layout.get_routes();
163         for(set<Route *>::const_iterator i=routes.begin(); i!=routes.end(); ++i)
164                 if(!(*i)->is_temporary())
165                 {
166                         RouteInfoPacket pkt;
167                         pkt.name = (*i)->get_name();
168                         comm.send(pkt);
169                 }
170
171         const map<unsigned, Train *> &trains = server.layout.get_trains();
172         for(map<unsigned, Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
173         {
174                 const Train &train = *i->second;
175
176                 {
177                         TrainInfoPacket pkt;
178                         pkt.address = train.get_address();
179                         pkt.loco_type = train.get_locomotive_type().get_article_number().str();
180                         pkt.name = train.get_name();
181                         comm.send(pkt);
182                 }
183                 for(unsigned j=0;; ++j)
184                 {
185                         const char *name = train.get_controller().enumerate_controls(j);
186                         if(!name)
187                                 break;
188
189                         TrainControlPacket pkt;
190                         pkt.address = train.get_address();
191                         pkt.control = name;
192                         pkt.value = train.get_control(name);
193                         comm.send(pkt);
194                 }
195                 {
196                         TrainFunctionPacket pkt;
197                         pkt.address = train.get_address();
198                         pkt.functions = train.get_functions();
199                         comm.send(pkt);
200                 }
201                 if(TrainStatus *status = dynamic_cast<TrainStatus *>(train.get_tagged_ai("server:status")))
202                 {
203                         TrainStatusPacket pkt;
204                         pkt.address = train.get_address();
205                         pkt.status = status->get_status();
206                         comm.send(pkt);
207                 }
208                 if(train.get_route())
209                 {
210                         TrainRoutePacket pkt;
211                         pkt.address = train.get_address();
212                         pkt.route = train.get_route()->get_name();
213                         comm.send(pkt);
214                 }
215         }
216 }
217
218 void Server::Connection::end_of_file()
219 {
220         socket->close();
221         stale = true;
222 }
223
224 void Server::Connection::receive(const DriverStatePacket &pkt)
225 {
226         Driver &driver = server.layout.get_driver();
227         if(pkt.power!=driver.get_power())
228                 driver.set_power(pkt.power);
229         if(pkt.halt!=driver.is_halted())
230                 driver.halt(pkt.halt);
231 }
232
233 void Server::Connection::receive(const TrainControlPacket &pkt)
234 {
235         try
236         {
237                 Train &train = server.layout.get_train(pkt.address);
238                 train.set_control(pkt.control, pkt.value);
239         }
240         catch(const Exception &e)
241         {
242                 error(e.what());
243         }
244 }
245
246 void Server::Connection::receive(const TrainFunctionPacket &pkt)
247 {
248         try
249         {
250                 Train &train = server.layout.get_train(pkt.address);
251                 for(unsigned i=0; i<9; ++i)
252                         if(((pkt.functions^train.get_functions())>>i)&1)
253                                 train.set_function(i, (pkt.functions>>i)&1);
254         }
255         catch(const Exception &e)
256         {
257                 error(e.what());
258         }
259 }
260
261 void Server::Connection::receive(const TrainRoutePacket &pkt)
262 {
263         try
264         {
265                 Train &train = server.layout.get_train(pkt.address);
266                 if(pkt.route.empty())
267                         train.set_route(0);
268                 else
269                 {
270                         Route &route = server.layout.get_route(pkt.route);
271                         train.set_route(&route);
272                 }
273         }
274         catch(const Exception &e)
275         {
276                 error(e.what());
277         }
278 }
279
280 void Server::Connection::error(const string &msg)
281 {
282         ErrorPacket pkt;
283         pkt.message = msg;
284         comm.send(pkt);
285 }
286
287 } // namespace R2C2