]> git.tdb.fi Git - r2c2.git/blobdiff - source/network/client.cpp
Export AI control parameters over the network
[r2c2.git] / source / network / client.cpp
index d418b8cad92c719d7d0a5f0cb217b8ee6ba89fea..15d0b407691fbb3e526f365aa877b55d105aa053 100644 (file)
@@ -1,10 +1,4 @@
-/* $Id$
-
-This file is part of R²C²
-Copyright © 2009  Mikkosoft Productions, Mikko Rasa
-Distributed under the GPL
-*/
-
+#include <msp/core/maputils.h>
 #include "client.h"
 
 using namespace std;
@@ -12,11 +6,13 @@ using namespace Msp;
 
 namespace R2C2 {
 
-Client::Client(const Catalogue &c):
+Client::Client(Catalogue &c):
        catalogue(c),
        socket(0),
        comm(0),
-       event_disp(0)
+       event_disp(0),
+       power(false),
+       halt(false)
 { }
 
 Client::~Client()
@@ -43,12 +39,44 @@ 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);
-       if(i==trains.end())
-               throw KeyError("Unknown train");
-       return *i->second;
+       return *get_item(trains, addr);
+}
+
+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)
@@ -68,6 +96,11 @@ void Client::receive(const TrainFunctionPacket &pkt)
        get_train(pkt.address).process_packet(pkt);
 }
 
+void Client::receive(const TrainAIControlPacket &pkt)
+{
+       get_train(pkt.address).process_packet(pkt);
+}
+
 void Client::receive(const TrainStatusPacket &pkt)
 {
        get_train(pkt.address).process_packet(pkt);