From: Mikko Rasa Date: Wed, 24 Nov 2010 18:27:08 +0000 (+0000) Subject: Add control enumeration to Controller X-Git-Url: http://git.tdb.fi/?p=r2c2.git;a=commitdiff_plain;h=bd174b180d22a68bfa2caa35fb31d8b50fa3a750 Add control enumeration to Controller Send control states when a new client connects --- diff --git a/source/libr2c2/aicontrol.cpp b/source/libr2c2/aicontrol.cpp index 2d48bf7..7829937 100644 --- a/source/libr2c2/aicontrol.cpp +++ b/source/libr2c2/aicontrol.cpp @@ -32,9 +32,24 @@ AIControl::~AIControl() delete next_ctrl; } +const char *AIControl::enumerate_controls(unsigned index) const +{ + if(index==0) + return target_speed.name.c_str(); + else + { + for(--index;; ++index) + { + const char *ret = next_ctrl->enumerate_controls(index-1); + if(!ret || ret!=target_speed.name) + return ret; + } + } +} + void AIControl::set_control(const string &n, float v) { - if(n=="speed") + if(n==target_speed.name) { if(v && !train.is_active()) train.set_active(true); @@ -57,7 +72,7 @@ void AIControl::set_control(const string &n, float v) const Controller::Control &AIControl::get_control(const string &n) const { - if(n=="speed") + if(n==target_speed.name) return target_speed; else return next_ctrl->get_control(n); diff --git a/source/libr2c2/aicontrol.h b/source/libr2c2/aicontrol.h index 9e0808e..8d9ff11 100644 --- a/source/libr2c2/aicontrol.h +++ b/source/libr2c2/aicontrol.h @@ -35,6 +35,7 @@ public: AIControl(Train &, Controller *); virtual ~AIControl(); + virtual const char *enumerate_controls(unsigned) const; virtual void set_control(const std::string &, float); virtual const Control &get_control(const std::string &) const; diff --git a/source/libr2c2/controller.h b/source/libr2c2/controller.h index 6cabade..92564e4 100644 --- a/source/libr2c2/controller.h +++ b/source/libr2c2/controller.h @@ -55,6 +55,7 @@ protected: public: virtual ~Controller() { } + virtual const char *enumerate_controls(unsigned) const = 0; virtual void set_control(const std::string &, float) = 0; virtual const Control &get_control(const std::string &) const = 0; diff --git a/source/libr2c2/simplecontroller.cpp b/source/libr2c2/simplecontroller.cpp index 7f8b336..e7d9352 100644 --- a/source/libr2c2/simplecontroller.cpp +++ b/source/libr2c2/simplecontroller.cpp @@ -23,14 +23,24 @@ SimpleController::SimpleController(): target_speed.set(0); } +const char *SimpleController::enumerate_controls(unsigned index) const +{ + if(index==0) + return target_speed.name.c_str(); + else if(index==1) + return reverse.name.c_str(); + else + return 0; +} + void SimpleController::set_control(const string &name, float v) { - if(name=="speed") + if(name==target_speed.name) { target_speed.set(v); signal_control_changed.emit(target_speed); } - else if(name=="reverse") + else if(name==reverse.name) { if(target_speed.value || speed) throw InvalidState("Must be stopped to change reverse"); @@ -41,9 +51,9 @@ void SimpleController::set_control(const string &name, float v) const Controller::Control &SimpleController::get_control(const string &name) const { - if(name=="speed") + if(name==target_speed.name) return target_speed; - else if(name=="reverse") + else if(name==reverse.name) return reverse; else throw KeyError("Unknown control", name); diff --git a/source/libr2c2/simplecontroller.h b/source/libr2c2/simplecontroller.h index 140f13d..1b3d7dd 100644 --- a/source/libr2c2/simplecontroller.h +++ b/source/libr2c2/simplecontroller.h @@ -24,6 +24,7 @@ private: public: SimpleController(); + virtual const char *enumerate_controls(unsigned) const; virtual void set_control(const std::string &, float); virtual const Control &get_control(const std::string &) const; diff --git a/source/network/server.cpp b/source/network/server.cpp index 404faa9..174e812 100644 --- a/source/network/server.cpp +++ b/source/network/server.cpp @@ -139,7 +139,18 @@ void Server::Connection::handshake_done() pkt.name = train.get_name(); comm.send(pkt); } - // XXX Need control enumeration to send control packets + for(unsigned j=0;; ++j) + { + const char *name = train.get_controller().enumerate_controls(j); + if(!name) + break; + + TrainControlPacket pkt; + pkt.address = train.get_address(); + pkt.control = name; + pkt.value = train.get_control(name); + comm.send(pkt); + } { TrainFunctionPacket pkt; pkt.address = train.get_address();