From: Mikko Rasa Date: Sat, 19 Feb 2011 20:46:01 +0000 (+0000) Subject: Send and receive power/halt states over network X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=fadc35133b1ebbd98af948c1fa529dcde6fddcfd;p=r2c2.git Send and receive power/halt states over network Send emergency messages over network Only send non-temporary routes --- diff --git a/source/network/client.cpp b/source/network/client.cpp index d418b8c..33e7fea 100644 --- a/source/network/client.cpp +++ b/source/network/client.cpp @@ -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::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); diff --git a/source/network/client.h b/source/network/client.h index 02295df..220cdee 100644 --- a/source/network/client.h +++ b/source/network/client.h @@ -17,7 +17,9 @@ Distributed under the GPL namespace R2C2 { -class Client: public Msp::Net::PacketReceiver, +class Client: Msp::Net::PacketReceiver, + Msp::Net::PacketReceiver, + Msp::Net::PacketReceiver, Msp::Net::PacketReceiver, Msp::Net::PacketReceiver, Msp::Net::PacketReceiver, @@ -26,6 +28,9 @@ class Client: public Msp::Net::PacketReceiver, Msp::Net::PacketReceiver { public: + sigc::signal signal_power_changed; + sigc::signal signal_halt_changed; + sigc::signal signal_emergency; sigc::signal signal_train_added; sigc::signal 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 routes; std::map trains; @@ -51,10 +58,14 @@ public: const Catalogue &get_catalogue() const { return catalogue; } const std::list &get_routes() const { return routes; } + void set_power(bool); + void set_halt(bool); NetTrain &get_train(unsigned) const; const std::map &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 &); diff --git a/source/network/packets.h b/source/network/packets.h index 8001b8c..0cd56dc 100644 --- a/source/network/packets.h +++ b/source/network/packets.h @@ -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; diff --git a/source/network/protocol.cpp b/source/network/protocol.cpp index cfbc0fd..f688b48 100644 --- a/source/network/protocol.cpp +++ b/source/network/protocol.cpp @@ -12,6 +12,9 @@ namespace R2C2 { Protocol::Protocol() { + add() (&DriverStatePacket::power) + (&DriverStatePacket::halt); + add() (&EmergencyPacket::message); add() (&TrainInfoPacket::address) (&TrainInfoPacket::loco_type) (&TrainInfoPacket::name); add() (&TrainControlPacket::address) diff --git a/source/network/server.cpp b/source/network/server.cpp index 174e812..43d22ff 100644 --- a/source/network/server.cpp +++ b/source/network/server.cpp @@ -6,6 +6,7 @@ Distributed under the GPL */ #include +#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 &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 &routes = server.layout.get_routes(); - for(set::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 &routes = server.layout.get_routes(); + for(set::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 &trains = server.layout.get_trains(); for(map::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 diff --git a/source/network/server.h b/source/network/server.h index ced08aa..dc7543f 100644 --- a/source/network/server.h +++ b/source/network/server.h @@ -21,9 +21,10 @@ namespace R2C2 { class Server { private: - struct Connection: private Msp::Net::PacketReceiver, - private Msp::Net::PacketReceiver, - private Msp::Net::PacketReceiver + struct Connection: Msp::Net::PacketReceiver, + Msp::Net::PacketReceiver, + Msp::Net::PacketReceiver, + Msp::Net::PacketReceiver { 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); diff --git a/source/remote/remote.cpp b/source/remote/remote.cpp index c53bddd..2168986 100644 --- a/source/remote/remote.cpp +++ b/source/remote/remote.cpp @@ -9,6 +9,7 @@ Distributed under the GPL #include #include #include +#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); } diff --git a/source/remote/remote.h b/source/remote/remote.h index 0d6d413..a9ab92b 100644 --- a/source/remote/remote.h +++ b/source/remote/remote.h @@ -14,6 +14,7 @@ Distributed under the GPL #include #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 train_panels; static Msp::Application::RegApp reg;