]> git.tdb.fi Git - r2c2.git/blob - source/network/server.h
Send and receive power/halt states over network
[r2c2.git] / source / network / server.h
1 /* $Id$
2
3 This file is part of R²C²
4 Copyright © 2009-2010  Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #ifndef R2C2_NET_SERVER_H_
9 #define R2C2_NET_SERVER_H_
10
11 #include <msp/io/eventdispatcher.h>
12 #include <msp/net/communicator.h>
13 #include <msp/net/streamsocket.h>
14 #include <msp/net/streamlistensocket.h>
15 #include "libr2c2/layout.h"
16 #include "packets.h"
17 #include "protocol.h"
18
19 namespace R2C2 {
20
21 class Server
22 {
23 private:
24         struct Connection: Msp::Net::PacketReceiver<DriverStatePacket>,
25                 Msp::Net::PacketReceiver<TrainControlPacket>,
26                 Msp::Net::PacketReceiver<TrainFunctionPacket>,
27                 Msp::Net::PacketReceiver<TrainRoutePacket>
28         {
29                 Server &server;
30                 Msp::Net::StreamSocket *socket;
31                 Msp::Net::Communicator comm;
32                 bool stale;
33
34                 Connection(Server &, Msp::Net::StreamSocket *);
35                 ~Connection();
36
37                 void handshake_done();
38                 void end_of_file();
39                 virtual void receive(const DriverStatePacket &);
40                 virtual void receive(const TrainControlPacket &);
41                 virtual void receive(const TrainFunctionPacket &);
42                 virtual void receive(const TrainRoutePacket &);
43                 void error(const std::string &);
44         };
45
46         Protocol proto;
47         Layout &layout;
48         Msp::Net::StreamListenSocket listen_sock;
49         Msp::IO::EventDispatcher *event_disp;
50         std::vector<Connection *> connections;
51
52 public:
53         Server(Layout &);
54         void use_event_dispatcher(Msp::IO::EventDispatcher &);
55 private:
56         void incoming_connection();
57
58         void driver_state_changed();
59         void emergency(const std::string &);
60         void train_added(Train &);
61         void train_control_changed(const Train &, const std::string &, float);
62         void train_function_changed(const Train &, unsigned, bool);
63         void train_route_changed(const Train &, const Route *);
64         void train_status_changed(const Train &, const std::string &);
65
66         template<typename P>
67         void send(const P &);
68 };
69
70 } // namespace R2C2
71
72 #endif