]> git.tdb.fi Git - r2c2.git/blob - client.h
a9ced6fc4f02e7cbb6cd17fcd283f754c32ed0ee
[r2c2.git] / client.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_CLIENT_H_
9 #define R2C2_NET_CLIENT_H_
10
11 #include <msp/io/eventdispatcher.h>
12 #include <msp/net/communicator.h>
13 #include "libr2c2/catalogue.h"
14 #include "packets.h"
15 #include "protocol.h"
16 #include "train.h"
17
18 namespace R2C2 {
19
20 class Client: Msp::Net::PacketReceiver<DriverStatePacket>,
21         Msp::Net::PacketReceiver<EmergencyPacket>,
22         Msp::Net::PacketReceiver<TrainInfoPacket>,
23         Msp::Net::PacketReceiver<TrainFunctionPacket>,
24         Msp::Net::PacketReceiver<TrainControlPacket>,
25         Msp::Net::PacketReceiver<TrainStatusPacket>,
26         Msp::Net::PacketReceiver<RouteInfoPacket>,
27         Msp::Net::PacketReceiver<TrainRoutePacket>,
28         Msp::Net::PacketReceiver<ErrorPacket>
29 {
30 public:
31         sigc::signal<void, bool> signal_power_changed;
32         sigc::signal<void, bool> signal_halt_changed;
33         sigc::signal<void, const std::string &> signal_emergency;
34         sigc::signal<void, NetTrain &> signal_train_added;
35         sigc::signal<void, const std::string &> signal_error;
36
37 private:
38         const Catalogue &catalogue;
39         Protocol proto;
40         Msp::Net::StreamSocket *socket;
41         Msp::Net::Communicator *comm;
42         Msp::IO::EventDispatcher *event_disp;
43         bool power;
44         bool halt;
45         std::list<std::string> routes;
46         std::map<unsigned, NetTrain *> trains;
47
48 public:
49         Client(const Catalogue &);
50         ~Client();
51
52         void use_event_dispatcher(Msp::IO::EventDispatcher &);
53         void connect(const Msp::Net::SockAddr &);
54
55         template<typename P>
56         void send(const P &pkt)
57         { if(comm) comm->send(pkt); }
58
59         const Catalogue &get_catalogue() const { return catalogue; }
60         const std::list<std::string> &get_routes() const { return routes; }
61         void set_power(bool);
62         void set_halt(bool);
63         NetTrain &get_train(unsigned) const;
64         const std::map<unsigned, NetTrain *> &get_trains() const { return trains; }
65
66 private:
67         virtual void receive(const DriverStatePacket &);
68         virtual void receive(const EmergencyPacket &);
69         virtual void receive(const TrainInfoPacket &);
70         virtual void receive(const TrainControlPacket &);
71         virtual void receive(const TrainFunctionPacket &);
72         virtual void receive(const TrainStatusPacket &);
73         virtual void receive(const RouteInfoPacket &);
74         virtual void receive(const TrainRoutePacket &);
75         virtual void receive(const ErrorPacket &);
76 };
77
78 } // namespace R2C2
79
80 #endif