]> git.tdb.fi Git - r2c2.git/blob - source/network/client.h
Add networking library and a remote control program
[r2c2.git] / source / network / client.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_CLIENT_H_
9 #define MARKLINNET_CLIENT_H_
10
11 #include <msp/io/eventdispatcher.h>
12 #include <msp/net/communicator.h>
13 #include "libmarklin/catalogue.h"
14 #include "packets.h"
15 #include "protocol.h"
16 #include "train.h"
17
18 namespace Marklin {
19
20 class Client: public Msp::Net::PacketReceiver<TrainInfoPacket>,
21         Msp::Net::PacketReceiver<TrainSpeedPacket>,
22         Msp::Net::PacketReceiver<TrainFunctionPacket>,
23         Msp::Net::PacketReceiver<TrainStatusPacket>
24 {
25 public:
26         sigc::signal<void, NetTrain &> signal_train_added;
27
28 private:
29         const Catalogue &catalogue;
30         Protocol proto;
31         Msp::Net::StreamSocket *socket;
32         Msp::Net::Communicator *comm;
33         Msp::IO::EventDispatcher *event_disp;
34         std::map<unsigned, NetTrain *> trains;
35
36 public:
37         Client(const Catalogue &);
38         ~Client();
39
40         void use_event_dispatcher(Msp::IO::EventDispatcher &);
41         void connect(const Msp::Net::SockAddr &);
42
43         template<typename P>
44         void send(const P &pkt)
45         { if(comm) comm->send(pkt); }
46
47         const Catalogue &get_catalogue() const { return catalogue; }
48         NetTrain &get_train(unsigned);
49
50 private:
51         virtual void receive(const TrainInfoPacket &);
52         virtual void receive(const TrainSpeedPacket &);
53         virtual void receive(const TrainFunctionPacket &);
54         virtual void receive(const TrainStatusPacket &);
55 };
56
57 } // namespace Marklin
58
59 #endif