]> git.tdb.fi Git - r2c2.git/blob - source/network/server.h
Add networking library and a remote control program
[r2c2.git] / source / network / server.h
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
4 Copyright © 2009  Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #ifndef MARKLINNET_SERVER_H_
9 #define MARKLINNET_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 "libmarklin/trafficmanager.h"
16 #include "packets.h"
17 #include "protocol.h"
18
19 namespace Marklin {
20
21 class Server
22 {
23 private:
24         struct Connection: private Msp::Net::PacketReceiver<TrainSpeedPacket>,
25                 private Msp::Net::PacketReceiver<TrainFunctionPacket>
26         {
27                 Server &server;
28                 Msp::Net::StreamSocket *socket;
29                 Msp::Net::Communicator comm;
30                 bool stale;
31
32                 Connection(Server &, Msp::Net::StreamSocket *);
33                 ~Connection();
34
35                 void handshake_done();
36                 void end_of_file();
37                 virtual void receive(const TrainSpeedPacket &);
38                 virtual void receive(const TrainFunctionPacket &);
39         };
40
41         Protocol proto;
42         TrafficManager &trfc_mgr;
43         Msp::Net::StreamListenSocket listen_sock;
44         Msp::IO::EventDispatcher *event_disp;
45         std::vector<Connection *> connections;
46
47 public:
48         Server(TrafficManager &);
49         void use_event_dispatcher(Msp::IO::EventDispatcher &);
50 private:
51         void incoming_connection();
52
53         void train_added(Train &);
54         void train_speed_changed(const Train &, unsigned);
55         void train_function_changed(const Train &, unsigned, bool);
56         void train_status_changed(const Train &, const std::string &);
57
58         template<typename P>
59         void send(const P &);
60 };
61
62 } // namespace Marklin
63
64 #endif