3 This file is part of R²C²
4 Copyright © 2009-2011 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
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"
20 Server::Server(Layout &l):
22 listen_sock(Net::INET),
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));
29 layout.signal_train_added.connect(sigc::mem_fun(this, &Server::train_added));
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);
35 listen_sock.listen(Net::InetAddr(0, 8315), 4);
36 listen_sock.signal_data_available.connect(sigc::mem_fun(this, &Server::incoming_connection));
39 void Server::use_event_dispatcher(IO::EventDispatcher &ed)
42 event_disp->add(listen_sock);
45 void Server::incoming_connection()
47 Net::StreamSocket *sock = listen_sock.accept();
49 event_disp->add(*sock);
50 connections.push_back(new Connection(*this, sock));
53 void Server::driver_state_changed()
55 DriverStatePacket pkt;
56 pkt.power = layout.get_driver().get_power();
57 pkt.halt = layout.get_driver().is_halted();
61 void Server::emergency(const string &msg)
68 void Server::train_added(Train &train)
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)));
76 pkt.address = train.get_address();
77 pkt.loco_type = train.get_locomotive_type().get_article_number().str();
78 pkt.name = train.get_name();
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)));
86 void Server::train_control_changed(const Train &train, const string &control, float value)
88 TrainControlPacket pkt;
89 pkt.address = train.get_address();
90 pkt.control = control;
95 void Server::train_function_changed(const Train &train, unsigned, bool)
97 TrainFunctionPacket pkt;
98 pkt.address = train.get_address();
99 pkt.functions = train.get_functions();
103 void Server::train_route_changed(const Train &train, const Route *route)
105 TrainRoutePacket pkt;
106 pkt.address = train.get_address();
108 pkt.route = route->get_name();
112 void Server::train_status_changed(const Train &train, const string &status)
114 TrainStatusPacket pkt;
115 pkt.address = train.get_address();
121 void Server::send(const P &pkt)
123 for(vector<Connection *>::const_iterator i=connections.begin(); i!=connections.end(); ++i)
124 if(!(*i)->stale && (*i)->comm.is_handshake_done())
128 (*i)->comm.send(pkt);
138 Server::Connection::Connection(Server &s, Net::StreamSocket *o):
141 comm(*socket, server.proto, *this)
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();
148 Server::Connection::~Connection()
153 void Server::Connection::handshake_done()
156 DriverStatePacket pkt;
157 pkt.power = server.layout.get_driver().get_power();
158 pkt.halt = server.layout.get_driver().is_halted();
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())
167 pkt.name = (*i)->get_name();
171 const map<unsigned, Train *> &trains = server.layout.get_trains();
172 for(map<unsigned, Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
174 const Train &train = *i->second;
178 pkt.address = train.get_address();
179 pkt.loco_type = train.get_locomotive_type().get_article_number().str();
180 pkt.name = train.get_name();
183 for(unsigned j=0;; ++j)
185 const char *name = train.get_controller().enumerate_controls(j);
189 TrainControlPacket pkt;
190 pkt.address = train.get_address();
192 pkt.value = train.get_control(name);
196 TrainFunctionPacket pkt;
197 pkt.address = train.get_address();
198 pkt.functions = train.get_functions();
201 if(TrainStatus *status = dynamic_cast<TrainStatus *>(train.get_tagged_ai("server:status")))
203 TrainStatusPacket pkt;
204 pkt.address = train.get_address();
205 pkt.status = status->get_status();
208 if(train.get_route())
210 TrainRoutePacket pkt;
211 pkt.address = train.get_address();
212 pkt.route = train.get_route()->get_name();
218 void Server::Connection::end_of_file()
224 void Server::Connection::receive(const DriverStatePacket &pkt)
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);
233 void Server::Connection::receive(const TrainControlPacket &pkt)
237 Train &train = server.layout.get_train(pkt.address);
238 train.set_control(pkt.control, pkt.value);
240 catch(const Exception &e)
246 void Server::Connection::receive(const TrainFunctionPacket &pkt)
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);
255 catch(const Exception &e)
261 void Server::Connection::receive(const TrainRoutePacket &pkt)
265 Train &train = server.layout.get_train(pkt.address);
266 if(pkt.route.empty())
270 Route &route = server.layout.get_route(pkt.route);
271 train.set_route(&route);
274 catch(const Exception &e)
280 void Server::Connection::error(const string &msg)