]> git.tdb.fi Git - r2c2.git/blob - server.h
badced0dbd7d8dd612cf690319e8515c9f5ed63a
[r2c2.git] / server.h
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 #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 "libr2c2/trainstatus.h"
17 #include "packets.h"
18 #include "protocol.h"
19
20 namespace R2C2 {
21
22 class Server
23 {
24 private:
25         struct Connection: Msp::Net::PacketReceiver<DriverStatePacket>,
26                 Msp::Net::PacketReceiver<TrainControlPacket>,
27                 Msp::Net::PacketReceiver<TrainFunctionPacket>,
28                 Msp::Net::PacketReceiver<TrainRoutePacket>
29         {
30                 Server &server;
31                 Msp::Net::StreamSocket *socket;
32                 Msp::Net::Communicator comm;
33                 bool stale;
34
35                 Connection(Server &, Msp::Net::StreamSocket *);
36                 ~Connection();
37
38                 void handshake_done();
39                 void end_of_file();
40                 virtual void receive(const DriverStatePacket &);
41                 virtual void receive(const TrainControlPacket &);
42                 virtual void receive(const TrainFunctionPacket &);
43                 virtual void receive(const TrainRoutePacket &);
44                 void error(const std::string &);
45         };
46
47         Protocol proto;
48         Layout &layout;
49         Msp::Net::StreamListenSocket listen_sock;
50         Msp::IO::EventDispatcher *event_disp;
51         std::vector<Connection *> connections;
52
53 public:
54         Server(Layout &);
55         void use_event_dispatcher(Msp::IO::EventDispatcher &);
56 private:
57         void incoming_connection();
58
59         void driver_state_changed();
60         void emergency(const std::string &);
61         void train_added(Train &);
62         void train_control_changed(const Train &, const std::string &, float);
63         void train_function_changed(const Train &, unsigned, bool);
64         void train_route_changed(const Train &, const Route *);
65         void train_status_changed(const Train &, const std::string &);
66
67         template<typename P>
68         void send(const P &);
69 };
70
71 } // namespace R2C2
72
73 #endif