]> git.tdb.fi Git - r2c2.git/blob - source/network/server.h
Separate train routing logic to a class derived from TrainAI
[r2c2.git] / source / network / server.h
1 #ifndef R2C2_NET_SERVER_H_
2 #define R2C2_NET_SERVER_H_
3
4 #include <msp/io/eventdispatcher.h>
5 #include <msp/net/communicator.h>
6 #include <msp/net/streamsocket.h>
7 #include <msp/net/streamserversocket.h>
8 #include "libr2c2/layout.h"
9 #include "libr2c2/trainai.h"
10 #include "packets.h"
11 #include "protocol.h"
12
13 namespace R2C2 {
14
15 class Server
16 {
17 private:
18         struct Connection: Msp::Net::PacketReceiver<DriverStatePacket>,
19                 Msp::Net::PacketReceiver<TrainControlPacket>,
20                 Msp::Net::PacketReceiver<TrainFunctionPacket>,
21                 Msp::Net::PacketReceiver<TrainRoutePacket>
22         {
23                 Server &server;
24                 Msp::Net::StreamSocket *socket;
25                 Msp::Net::Communicator comm;
26                 bool stale;
27
28                 Connection(Server &, Msp::Net::StreamSocket *);
29                 ~Connection();
30
31                 void handshake_done();
32                 void end_of_file();
33                 virtual void receive(const DriverStatePacket &);
34                 virtual void receive(const TrainControlPacket &);
35                 virtual void receive(const TrainFunctionPacket &);
36                 virtual void receive(const TrainRoutePacket &);
37                 void error(const std::string &);
38         };
39
40         Protocol proto;
41         Layout &layout;
42         Msp::Net::StreamServerSocket listen_sock;
43         Msp::IO::EventDispatcher *event_disp;
44         std::vector<Connection *> connections;
45
46 public:
47         Server(Layout &);
48         void use_event_dispatcher(Msp::IO::EventDispatcher &);
49 private:
50         void incoming_connection();
51
52         void driver_state_changed();
53         void emergency(const std::string &);
54         void train_added(Train &);
55         void train_control_changed(const Train &, const std::string &, float);
56         void train_function_changed(const Train &, unsigned, bool);
57         void train_ai_event(const Train &, TrainAI &, const TrainAI::Message &);
58         void train_status_changed(const Train &, const std::string &);
59
60         template<typename P>
61         void send(const P &);
62 };
63
64 } // namespace R2C2
65
66 #endif