]> git.tdb.fi Git - r2c2.git/commitdiff
Add control enumeration to Controller
authorMikko Rasa <tdb@tdb.fi>
Wed, 24 Nov 2010 18:27:08 +0000 (18:27 +0000)
committerMikko Rasa <tdb@tdb.fi>
Wed, 24 Nov 2010 18:27:08 +0000 (18:27 +0000)
Send control states when a new client connects

source/libr2c2/aicontrol.cpp
source/libr2c2/aicontrol.h
source/libr2c2/controller.h
source/libr2c2/simplecontroller.cpp
source/libr2c2/simplecontroller.h
source/network/server.cpp

index 2d48bf764a3f498047a078a34965461fc2985c2d..782993752ab94cd504261b5522a7211b936b92b5 100644 (file)
@@ -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);
index 9e0808e1b821803179e627c9f1d7536c226b3c49..8d9ff11591eba395c414b6197d99652454eb06d6 100644 (file)
@@ -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;
 
index 6cabadec7c3d37d0bfff30438b3377af21ebaa4a..92564e461ba0a5507075a75079f2c1750293b0e8 100644 (file)
@@ -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;
 
index 7f8b3362087ed0cee7a37eb03c55875cc31a4f20..e7d9352ca048bd8377570476662860d1662d5fd3 100644 (file)
@@ -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);
index 140f13d3ee849c3a12f8affc888f6b3b250ee0ed..1b3d7dd651a527f7e14151600997e9acada13887 100644 (file)
@@ -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;
 
index 404faa94a6fa83826f46403fd7ff08110d1d231d..174e812148b6f5c46f7fdb24fea04b67f8e90e5f 100644 (file)
@@ -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();