summaryrefslogtreecommitdiffstats
path: root/src/testremotecontrol/test.cpp
blob: 84032fcd31c8ceed03788138bbbfc11b1ecb2df6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <string>
#include <map>
#include <unistd.h>
#include "RemoteControl.h"

using namespace std;

#define BUILD_FOO(p) {   \
  vector<string> 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;
            
            BUILD_FOO(foo);
            BUILD_FOO(bar);
            BUILD_FOO(baz);

        }

        string get_rc_name() { return name_; };

        list<string> get_supported_parameters() {
            list<string> parameterlist;
            for (list< vector<string> >::iterator it = parameters_.begin(); it != parameters_.end(); it++) {
                parameterlist.push_back((*it)[0]);
            }
            return parameterlist;
        }

        void set_parameter(string parameter, string value) {
            if (parameter == "foo") {
                stringstream ss(value);
                ss >> foo_;
            }
            else if (parameter == "bar") {
                bar_ = value;
            }
            else if (parameter == "baz") {
                stringstream ss(value);
                ss >> baz_;
            }
            else {
                stringstream ss;
                ss << "Parameter '" << parameter << "' is not exported by controllable " << get_rc_name();
                throw ParameterError(ss.str());
            }
        }

        void set_parameter(string parameter, double value) {
            if (parameter == "baz") {
                baz_ = value;
            }
            else {
                stringstream ss;
                ss << "Parameter '" << parameter << "' is not a double in controllable " << get_rc_name();
                throw ParameterError(ss.str());
            }
        }

        void set_parameter(string parameter, long value) {
            if (parameter == "foo") {
                foo_ = value;
            }
            else {
                stringstream ss;
                ss << "Parameter '" << parameter << "' is not a long in controllable " << get_rc_name();
                throw ParameterError(ss.str());
            }
        }

        string get_parameter(string parameter) {
            stringstream ss;
            if (parameter == "foo") {
                ss << foo_;
            }
            else if (parameter == "bar") {
                ss << bar_;
            }
            else if (parameter == "baz") {
                ss << baz_;
            }
            else {
                stringstream ss;
                ss << "Parameter '" << parameter << "' is not exported by controllable " << get_rc_name();
                throw ParameterError(ss.str());
            }
            return ss.str();
        }

        std::list< std::vector<std::string> > get_parameter_descriptions() {
            return parameters_;
        }

    private:
        long foo_;
        std::string bar_;
        std::string name_;
        double baz_;
        std::list< std::vector<std::string> > parameters_;

};

int main()
{
    RemoteControllerTelnet rc (2121);
    TestControllable t("test1");
    TestControllable t2("test2");

    t.enrol_at(rc);
    t2.enrol_at(rc);

    rc.start();

    std::cerr << "Thread has been launched" << std::endl;

    sleep(100);

    std::cerr << "Stop" << std::endl;

    rc.stop();

    return 0;
}