]> git.tdb.fi Git - r2c2.git/commitdiff
Send and receive power/halt states over network
authorMikko Rasa <tdb@tdb.fi>
Sat, 19 Feb 2011 20:46:01 +0000 (20:46 +0000)
committerMikko Rasa <tdb@tdb.fi>
Sat, 19 Feb 2011 20:46:01 +0000 (20:46 +0000)
Send emergency messages over network
Only send non-temporary routes

source/network/client.cpp
source/network/client.h
source/network/packets.h
source/network/protocol.cpp
source/network/server.cpp
source/network/server.h
source/remote/remote.cpp
source/remote/remote.h

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);
index 02295dfa32690aeb9a2d2610269fd4132ce83254..220cdee1e79046dd61d0e0b93f64c878f0ee213e 100644 (file)
@@ -17,7 +17,9 @@ Distributed under the GPL
 
 namespace R2C2 {
 
-class Client: public Msp::Net::PacketReceiver<TrainInfoPacket>,
+class Client: Msp::Net::PacketReceiver<DriverStatePacket>,
+       Msp::Net::PacketReceiver<EmergencyPacket>,
+       Msp::Net::PacketReceiver<TrainInfoPacket>,
        Msp::Net::PacketReceiver<TrainFunctionPacket>,
        Msp::Net::PacketReceiver<TrainControlPacket>,
        Msp::Net::PacketReceiver<TrainStatusPacket>,
@@ -26,6 +28,9 @@ class Client: public Msp::Net::PacketReceiver<TrainInfoPacket>,
        Msp::Net::PacketReceiver<ErrorPacket>
 {
 public:
+       sigc::signal<void, bool> signal_power_changed;
+       sigc::signal<void, bool> signal_halt_changed;
+       sigc::signal<void, const std::string &> signal_emergency;
        sigc::signal<void, NetTrain &> signal_train_added;
        sigc::signal<void, const std::string &> signal_error;
 
@@ -35,6 +40,8 @@ private:
        Msp::Net::StreamSocket *socket;
        Msp::Net::Communicator *comm;
        Msp::IO::EventDispatcher *event_disp;
+       bool power;
+       bool halt;
        std::list<std::string> routes;
        std::map<unsigned, NetTrain *> trains;
 
@@ -51,10 +58,14 @@ public:
 
        const Catalogue &get_catalogue() const { return catalogue; }
        const std::list<std::string> &get_routes() const { return routes; }
+       void set_power(bool);
+       void set_halt(bool);
        NetTrain &get_train(unsigned) const;
        const std::map<unsigned, NetTrain *> &get_trains() const { return trains; }
 
 private:
+       virtual void receive(const DriverStatePacket &);
+       virtual void receive(const EmergencyPacket &);
        virtual void receive(const TrainInfoPacket &);
        virtual void receive(const TrainControlPacket &);
        virtual void receive(const TrainFunctionPacket &);
index 8001b8c1baa721beab957a34459e843d3c063219..0cd56dc8de828e66461a2871628f8b3eb24f0a92 100644 (file)
@@ -12,6 +12,17 @@ Distributed under the GPL
 
 namespace R2C2 {
 
+struct DriverStatePacket
+{
+       char power;
+       char halt;
+};
+
+struct EmergencyPacket
+{
+       std::string message;
+};
+
 struct TrainInfoPacket
 {
        unsigned address;
index cfbc0fdc2a1eac66c53205ef8475468129f33a60..f688b486442d38a2d51f9ee3a520ba6a7485ac08 100644 (file)
@@ -12,6 +12,9 @@ namespace R2C2 {
 
 Protocol::Protocol()
 {
+       add<DriverStatePacket>() (&DriverStatePacket::power)
+               (&DriverStatePacket::halt);
+       add<EmergencyPacket>() (&EmergencyPacket::message);
        add<TrainInfoPacket>() (&TrainInfoPacket::address)
                (&TrainInfoPacket::loco_type) (&TrainInfoPacket::name);
        add<TrainControlPacket>() (&TrainControlPacket::address)
index 174e812148b6f5c46f7fdb24fea04b67f8e90e5f..43d22ffef9016f26b376e691d840840bea13a940 100644 (file)
@@ -6,6 +6,7 @@ Distributed under the GPL
 */
 
 #include <msp/net/inet.h>
+#include "libr2c2/driver.h"
 #include "libr2c2/route.h"
 #include "libr2c2/train.h"
 #include "libr2c2/vehicletype.h"
@@ -21,6 +22,10 @@ Server::Server(Layout &l):
        listen_sock(Net::INET),
        event_disp(0)
 {
+       layout.get_driver().signal_power.connect(sigc::hide(sigc::mem_fun(this, &Server::driver_state_changed)));
+       layout.get_driver().signal_halt.connect(sigc::hide(sigc::mem_fun(this, &Server::driver_state_changed)));
+       layout.signal_emergency.connect(sigc::mem_fun(this, &Server::emergency));
+
        layout.signal_train_added.connect(sigc::mem_fun(this, &Server::train_added));
 
        const map<unsigned, Train *> &trains = layout.get_trains();
@@ -45,6 +50,21 @@ void Server::incoming_connection()
        connections.push_back(new Connection(*this, sock));
 }
 
+void Server::driver_state_changed()
+{
+       DriverStatePacket pkt;
+       pkt.power = layout.get_driver().get_power();
+       pkt.halt = layout.get_driver().is_halted();
+       send(pkt);
+}
+
+void Server::emergency(const string &msg)
+{
+       EmergencyPacket pkt;
+       pkt.message = msg;
+       send(pkt);
+}
+
 void Server::train_added(Train &train)
 {
        train.signal_control_changed.connect(sigc::bind<0>(sigc::mem_fun(this, &Server::train_control_changed), sigc::ref(train)));
@@ -119,14 +139,22 @@ Server::Connection::~Connection()
 
 void Server::Connection::handshake_done()
 {
-       const set<Route *> &routes = server.layout.get_routes();
-       for(set<Route *>::const_iterator i=routes.begin(); i!=routes.end(); ++i)
        {
-               RouteInfoPacket pkt;
-               pkt.name = (*i)->get_name();
+               DriverStatePacket pkt;
+               pkt.power = server.layout.get_driver().get_power();
+               pkt.halt = server.layout.get_driver().is_halted();
                comm.send(pkt);
        }
 
+       const set<Route *> &routes = server.layout.get_routes();
+       for(set<Route *>::const_iterator i=routes.begin(); i!=routes.end(); ++i)
+               if(!(*i)->is_temporary())
+               {
+                       RouteInfoPacket pkt;
+                       pkt.name = (*i)->get_name();
+                       comm.send(pkt);
+               }
+
        const map<unsigned, Train *> &trains = server.layout.get_trains();
        for(map<unsigned, Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
        {
@@ -179,6 +207,15 @@ void Server::Connection::end_of_file()
        stale = true;
 }
 
+void Server::Connection::receive(const DriverStatePacket &pkt)
+{
+       Driver &driver = server.layout.get_driver();
+       if(pkt.power!=driver.get_power())
+               driver.set_power(pkt.power);
+       if(pkt.halt!=driver.is_halted())
+               driver.halt(pkt.halt);
+}
+
 void Server::Connection::receive(const TrainControlPacket &pkt)
 {
        try
index ced08aac06eb2df544a9dd8e741cf6cdcfc75e2f..dc7543fe293278f09be6fb673731108500bae8bf 100644 (file)
@@ -21,9 +21,10 @@ namespace R2C2 {
 class Server
 {
 private:
-       struct Connection: private Msp::Net::PacketReceiver<TrainControlPacket>,
-               private Msp::Net::PacketReceiver<TrainFunctionPacket>,
-               private Msp::Net::PacketReceiver<TrainRoutePacket>
+       struct Connection: Msp::Net::PacketReceiver<DriverStatePacket>,
+               Msp::Net::PacketReceiver<TrainControlPacket>,
+               Msp::Net::PacketReceiver<TrainFunctionPacket>,
+               Msp::Net::PacketReceiver<TrainRoutePacket>
        {
                Server &server;
                Msp::Net::StreamSocket *socket;
@@ -35,6 +36,7 @@ private:
 
                void handshake_done();
                void end_of_file();
+               virtual void receive(const DriverStatePacket &);
                virtual void receive(const TrainControlPacket &);
                virtual void receive(const TrainFunctionPacket &);
                virtual void receive(const TrainRoutePacket &);
@@ -53,6 +55,8 @@ public:
 private:
        void incoming_connection();
 
+       void driver_state_changed();
+       void emergency(const std::string &);
        void train_added(Train &);
        void train_control_changed(const Train &, const std::string &, float);
        void train_function_changed(const Train &, unsigned, bool);
index c53bddda1c2f08c4578b09cca3e05bf93c66ca5f..21689867a8f58c02a908537e919e883b30bfc123 100644 (file)
@@ -9,6 +9,7 @@ Distributed under the GPL
 #include <gtkmm/separator.h>
 #include <msp/net/resolve.h>
 #include <msp/time/units.h>
+#include "mainpanel.h"
 #include "remote.h"
 #include "trainpanel.h"
 
@@ -43,8 +44,11 @@ Remote::Remote(int argc, char **argv):
        window.add(*manage(scroll));
        scroll->set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
 
-       train_box = new Gtk::VBox(false, 5);
-       scroll->add(*manage(train_box));
+       panel_box = new Gtk::VBox(false, 5);
+       scroll->add(*manage(panel_box));
+
+       main_panel = new MainPanel(*this, client);
+       panel_box->pack_start(*manage(main_panel), false, true);
 
        window.show_all();
 }
@@ -57,13 +61,11 @@ void Remote::tick()
 
 void Remote::train_added(R2C2::NetTrain &t)
 {
+       Gtk::HSeparator *sep = new Gtk::HSeparator;
+       panel_box->pack_start(*manage(sep), false, true);
+       sep->show();
+
        TrainPanel *panel = new TrainPanel(*this, client, t);
-       if(!train_panels.empty())
-       {
-               Gtk::HSeparator *sep = new Gtk::HSeparator;
-               train_box->pack_start(*manage(sep), false, true);
-               sep->show();
-       }
-       train_box->pack_start(*manage(panel), false, true);
+       panel_box->pack_start(*manage(panel), false, true);
        train_panels.push_back(panel);
 }
index 0d6d413d2e2db93ff38a6874a1cbfba1c9f34797..a9ab92b2c6f876973c4872ea7c21974e5bd6f16f 100644 (file)
@@ -14,6 +14,7 @@ Distributed under the GPL
 #include <msp/core/application.h>
 #include "network/client.h"
 
+class MainPanel;
 class TrainPanel;
 
 class Remote: public Msp::Application
@@ -24,7 +25,8 @@ private:
        R2C2::Client client;
        Gtk::Main gtk;
        Gtk::Window window;
-       Gtk::Box *train_box;
+       Gtk::Box *panel_box;
+       MainPanel *main_panel;
        std::vector<TrainPanel *> train_panels;
 
        static Msp::Application::RegApp<Remote> reg;