From f419f8cb2e3768789cb23593cfa7c850acc0fe72 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli (think)" Date: Thu, 16 Aug 2012 17:11:17 +0200 Subject: crc-dabmod: RemoteControl cleanup --- src/RemoteControl.cpp | 7 +++-- src/RemoteControl.h | 69 +++++++++++++++++++++--------------------- src/testremotecontrol/test.cpp | 30 ++++++++++++++---- 3 files changed, 62 insertions(+), 44 deletions(-) diff --git a/src/RemoteControl.cpp b/src/RemoteControl.cpp index b534914..53b7204 100644 --- a/src/RemoteControl.cpp +++ b/src/RemoteControl.cpp @@ -130,9 +130,10 @@ RemoteControllerTelnet::dispatch_command(tcp::socket& socket, string command) else if (cmd.size() == 2) { try { stringstream ss; - list params = get_param_list_(cmd[1]); - for (list::iterator it = params.begin(); it != params.end(); it++) { - ss << *it << " "; + + list< vector > params = get_parameter_descriptions_(cmd[1]); + for (list< vector >::iterator it = params.begin(); it != params.end(); it++) { + ss << (*it)[0] << " : " << (*it)[1] << endl; } reply(socket, ss.str()); } diff --git a/src/RemoteControl.h b/src/RemoteControl.h index 8b0a30f..ab3f6fb 100644 --- a/src/RemoteControl.h +++ b/src/RemoteControl.h @@ -26,6 +26,7 @@ #define _REMOTECONTROL_H #include +#include #include #include #include @@ -78,6 +79,9 @@ class RemoteControllable { /* Return a list of possible parameters that can be set */ virtual list get_supported_parameters() = 0; + /* Return a mapping of the descriptions of all parameters */ + virtual std::list< std::vector > get_parameter_descriptions() = 0; + /* Base function to set parameters. */ virtual void set_parameter(string parameter, string value) = 0; @@ -133,54 +137,49 @@ class RemoteControllerTelnet : public BaseRemoteController { return all_tokens; } - list get_param_list_(string controllable) { + RemoteControllable* get_controllable_(string name) { for (list::iterator it = cohort_.begin(); it != cohort_.end(); it++) { - if ((*it)->get_rc_name() == controllable) + if ((*it)->get_rc_name() == name) { - return (*it)->get_supported_parameters(); + return *it; } } throw ParameterError("Module name unknown"); } - list< vector > get_param_list_values_(string controllable) { + list< vector > get_parameter_descriptions_(string name) { + RemoteControllable* controllable = get_controllable_(name); + return controllable->get_parameter_descriptions(); + } + + list get_param_list_(string name) { + RemoteControllable* controllable = get_controllable_(name); + return controllable->get_supported_parameters(); + } + + list< vector > get_param_list_values_(string name) { + RemoteControllable* controllable = get_controllable_(name); + list< vector > allparams; - for (list::iterator it = cohort_.begin(); it != cohort_.end(); it++) { - if ((*it)->get_rc_name() == controllable) - { - list params = (*it)->get_supported_parameters(); - for (list::iterator it2 = params.begin(); it2 != params.end(); it2++) { - vector item; - item.push_back(*it2); - item.push_back((*it)->get_parameter(*it2)); - - allparams.push_back(item); - } - return allparams; - } + list params = controllable->get_supported_parameters(); + for (list::iterator it = params.begin(); it != params.end(); it++) { + vector item; + item.push_back(*it); + item.push_back(controllable->get_parameter(*it)); + + allparams.push_back(item); } - throw ParameterError("Module name unknown"); + return allparams; } - string get_param_(string controllable, string param) { - for (list::iterator it = cohort_.begin(); it != cohort_.end(); it++) { - if ((*it)->get_rc_name() == controllable) - { - return (*it)->get_parameter(param); - } - } - throw ParameterError("Module name unknown"); + string get_param_(string name, string param) { + RemoteControllable* controllable = get_controllable_(name); + return controllable->get_parameter(param); } - void set_param_(string controllable, string param, string value) { - for (list::iterator it = cohort_.begin(); it != cohort_.end(); it++) { - if ((*it)->get_rc_name() == controllable) - { - (*it)->set_parameter(param, value); - return; - } - } - throw ParameterError("Module name unknown"); + void set_param_(string name, string param, string value) { + RemoteControllable* controllable = get_controllable_(name); + return controllable->set_parameter(param, value); } bool running_; diff --git a/src/testremotecontrol/test.cpp b/src/testremotecontrol/test.cpp index 7733a96..84032fc 100644 --- a/src/testremotecontrol/test.cpp +++ b/src/testremotecontrol/test.cpp @@ -1,24 +1,38 @@ #include +#include #include #include "RemoteControl.h" using namespace std; +#define BUILD_FOO(p) { \ + vector p; \ + p.push_back(#p); \ + p.push_back("That's the" #p); \ + parameters_.push_back(p); \ +} + class TestControllable : public RemoteControllable { public: TestControllable(string name) { name_ = name; - parameterlist_.push_back("foo"); - parameterlist_.push_back("bar"); - parameterlist_.push_back("baz"); + + BUILD_FOO(foo); + BUILD_FOO(bar); + BUILD_FOO(baz); + } - std::string get_rc_name() { return name_; }; + string get_rc_name() { return name_; }; list get_supported_parameters() { - return parameterlist_; + list parameterlist; + for (list< vector >::iterator it = parameters_.begin(); it != parameters_.end(); it++) { + parameterlist.push_back((*it)[0]); + } + return parameterlist; } void set_parameter(string parameter, string value) { @@ -81,12 +95,16 @@ class TestControllable : public RemoteControllable return ss.str(); } + std::list< std::vector > get_parameter_descriptions() { + return parameters_; + } + private: long foo_; std::string bar_; std::string name_; double baz_; - std::list parameterlist_; + std::list< std::vector > parameters_; }; -- cgit v1.2.3