]> git.tdb.fi Git - r2c2.git/blob - source/network/client.cpp
Emit signals from loco status command only when all data has been gathered
[r2c2.git] / source / network / client.cpp
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 #include "client.h"
9
10 using namespace std;
11 using namespace Msp;
12
13 namespace R2C2 {
14
15 Client::Client(const Catalogue &c):
16         catalogue(c),
17         socket(0),
18         comm(0),
19         event_disp(0)
20 { }
21
22 Client::~Client()
23 {
24         delete comm;
25         delete socket;
26         for(map<unsigned, NetTrain *>::iterator i=trains.begin(); i!=trains.end(); ++i)
27                 delete i->second;
28 }
29
30 void Client::use_event_dispatcher(Msp::IO::EventDispatcher &ed)
31 {
32         event_disp = &ed;
33         if(socket)
34                 event_disp->add(*socket);
35 }
36
37 void Client::connect(const Net::SockAddr &addr)
38 {
39         socket = new Net::StreamSocket(addr.get_family());
40         socket->connect(addr);
41         if(event_disp)
42                 event_disp->add(*socket);
43         comm = new Net::Communicator(*socket, proto, *this);
44 }
45
46 NetTrain &Client::get_train(unsigned addr) const
47 {
48         map<unsigned, NetTrain *>::const_iterator i = trains.find(addr);
49         if(i==trains.end())
50                 throw KeyError("Unknown train");
51         return *i->second;
52 }
53
54 void Client::receive(const TrainInfoPacket &pkt)
55 {
56         NetTrain *train = new NetTrain(*this, pkt);
57         trains[pkt.address] = train;
58         signal_train_added.emit(*train);
59 }
60
61 void Client::receive(const TrainControlPacket &pkt)
62 {
63         get_train(pkt.address).process_packet(pkt);
64 }
65
66 void Client::receive(const TrainFunctionPacket &pkt)
67 {
68         get_train(pkt.address).process_packet(pkt);
69 }
70
71 void Client::receive(const TrainStatusPacket &pkt)
72 {
73         get_train(pkt.address).process_packet(pkt);
74 }
75
76 void Client::receive(const RouteInfoPacket &pkt)
77 {
78         routes.push_back(pkt.name);
79 }
80
81 void Client::receive(const TrainRoutePacket &pkt)
82 {
83         get_train(pkt.address).process_packet(pkt);
84 }
85
86 void Client::receive(const ErrorPacket &pkt)
87 {
88         signal_error.emit(pkt.message);
89 }
90
91 } // namespace R2C2