]> git.tdb.fi Git - r2c2.git/blobdiff - source/network/client.cpp
Send and receive power/halt states over network
[r2c2.git] / source / network / client.cpp
index d418b8cad92c719d7d0a5f0cb217b8ee6ba89fea..33e7feac274ea9628d6688ed9758fd81a3305dc4 100644 (file)
@@ -16,7 +16,9 @@ Client::Client(const Catalogue &c):
        catalogue(c),
        socket(0),
        comm(0),
-       event_disp(0)
+       event_disp(0),
+       power(false),
+       halt(false)
 { }
 
 Client::~Client()
@@ -43,6 +45,22 @@ void Client::connect(const Net::SockAddr &addr)
        comm = new Net::Communicator(*socket, proto, *this);
 }
 
+void Client::set_power(bool p)
+{
+       DriverStatePacket pkt;
+       pkt.power = p;
+       pkt.halt = halt;
+       send(pkt);
+}
+
+void Client::set_halt(bool h)
+{
+       DriverStatePacket pkt;
+       pkt.power = power;
+       pkt.halt = h;
+       send(pkt);
+}
+
 NetTrain &Client::get_train(unsigned addr) const
 {
        map<unsigned, NetTrain *>::const_iterator i = trains.find(addr);
@@ -51,6 +69,25 @@ NetTrain &Client::get_train(unsigned addr) const
        return *i->second;
 }
 
+void Client::receive(const DriverStatePacket &pkt)
+{
+       if(pkt.power!=power)
+       {
+               power = pkt.power;
+               signal_power_changed.emit(power);
+       }
+       if(pkt.halt!=halt)
+       {
+               halt = pkt.halt;
+               signal_halt_changed.emit(halt);
+       }
+}
+
+void Client::receive(const EmergencyPacket &pkt)
+{
+       signal_emergency.emit(pkt.message);
+}
+
 void Client::receive(const TrainInfoPacket &pkt)
 {
        NetTrain *train = new NetTrain(*this, pkt);