]> git.tdb.fi Git - r2c2.git/blob - source/network/client.h
Emit signals from loco status command only when all data has been gathered
[r2c2.git] / source / network / client.h
1 /* $Id$
2
3 This file is part of R²C²
4 Copyright © 2009  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: public Msp::Net::PacketReceiver<TrainInfoPacket>,
21         Msp::Net::PacketReceiver<TrainFunctionPacket>,
22         Msp::Net::PacketReceiver<TrainControlPacket>,
23         Msp::Net::PacketReceiver<TrainStatusPacket>,
24         Msp::Net::PacketReceiver<RouteInfoPacket>,
25         Msp::Net::PacketReceiver<TrainRoutePacket>,
26         Msp::Net::PacketReceiver<ErrorPacket>
27 {
28 public:
29         sigc::signal<void, NetTrain &> signal_train_added;
30         sigc::signal<void, const std::string &> signal_error;
31
32 private:
33         const Catalogue &catalogue;
34         Protocol proto;
35         Msp::Net::StreamSocket *socket;
36         Msp::Net::Communicator *comm;
37         Msp::IO::EventDispatcher *event_disp;
38         std::list<std::string> routes;
39         std::map<unsigned, NetTrain *> trains;
40
41 public:
42         Client(const Catalogue &);
43         ~Client();
44
45         void use_event_dispatcher(Msp::IO::EventDispatcher &);
46         void connect(const Msp::Net::SockAddr &);
47
48         template<typename P>
49         void send(const P &pkt)
50         { if(comm) comm->send(pkt); }
51
52         const Catalogue &get_catalogue() const { return catalogue; }
53         const std::list<std::string> &get_routes() const { return routes; }
54         NetTrain &get_train(unsigned) const;
55         const std::map<unsigned, NetTrain *> &get_trains() const { return trains; }
56
57 private:
58         virtual void receive(const TrainInfoPacket &);
59         virtual void receive(const TrainControlPacket &);
60         virtual void receive(const TrainFunctionPacket &);
61         virtual void receive(const TrainStatusPacket &);
62         virtual void receive(const RouteInfoPacket &);
63         virtual void receive(const TrainRoutePacket &);
64         virtual void receive(const ErrorPacket &);
65 };
66
67 } // namespace R2C2
68
69 #endif