Working C++ IoTSlave for both IoTSet and IoTRelation; threading works for C++ LifxLig...
[iot2.git] / benchmarks / drivers / Cpp / LifxLightBulb / LightBulb_Skeleton.hpp
1 #ifndef _LIGHTBULB_SKELETON_HPP__
2 #define _LIGHTBULB_SKELETON_HPP__
3 #include <iostream>
4 #include <fstream>
5 #include "LightBulb.hpp"
6
7 #include <vector>
8 #include <set>
9 #include "IoTRMICall.hpp"
10 #include "IoTRMIObject.hpp"
11
12 using namespace std;
13
14 class LightBulb_Skeleton : public LightBulb
15 {
16         private:
17
18         LightBulb *mainObj;
19         vector<int> ports;
20         string callbackAddress;
21         IoTRMIObject *rmiObj;
22
23         const static int object0Id = 0; //LightBulbSmart
24         static set<int> set0Allowed;
25         
26         ofstream log;
27         public:
28
29         LightBulb_Skeleton(LightBulb *_mainObj, string _callbackAddress, int _port) {
30                 bool _bResult = false;
31                 mainObj = _mainObj;
32                 callbackAddress = _callbackAddress;
33                 // Logging
34                 int i=0;
35                 string file = "LightBulb_Skeleton_cpp" + to_string(i) + ".log";
36                 while (ifstream(file.c_str())) {
37                         i++;
38                         file = "LightBulb_Skeleton_cpp" + to_string(i) + ".log";
39                 }
40                 log.open(file);
41                 log << "Callback address: " << callbackAddress << endl;
42                 log << "Port: " << _port << endl;
43                 rmiObj = new IoTRMIObject(_port, &_bResult);
44                 log << "Established connection with slave! Wait request invoke now..." << endl;
45                 ___waitRequestInvokeMethod();
46         }
47
48         ~LightBulb_Skeleton() {
49                 if (rmiObj != NULL) {
50                         delete rmiObj;
51                         rmiObj = NULL;
52                 }
53         }
54         
55         void init() {
56                 mainObj->init();
57         }
58
59         void turnOff() {
60                 mainObj->turnOff();
61         }
62
63         void turnOn() {
64                 mainObj->turnOn();
65         }
66
67         bool getState() {
68                 return mainObj->getState();
69         }
70
71         void setColor(double _hue, double _saturation, double _brightness) {
72                 mainObj->setColor(_hue, _saturation, _brightness);
73         }
74
75         void setTemperature(int _temperature) {
76                 mainObj->setTemperature(_temperature);
77         }
78
79         double getBrightness() {
80                 return mainObj->getBrightness();
81         }
82
83         double getHue() {
84                 return mainObj->getHue();
85         }
86
87         double getSaturation() {
88                 return mainObj->getSaturation();
89         }
90
91         int getTemperature() {
92                 return mainObj->getTemperature();
93         }
94
95         double getBrightnessRangeLowerBound() {
96                 return mainObj->getBrightnessRangeLowerBound();
97         }
98
99         double getBrightnessRangeUpperBound() {
100                 return mainObj->getBrightnessRangeUpperBound();
101         }
102
103         double getHueRangeLowerBound() {
104                 return mainObj->getHueRangeLowerBound();
105         }
106
107         double getHueRangeUpperBound() {
108                 return mainObj->getHueRangeUpperBound();
109         }
110
111         double getSaturationRangeLowerBound() {
112                 return mainObj->getSaturationRangeLowerBound();
113         }
114
115         double getSaturationRangeUpperBound() {
116                 return mainObj->getSaturationRangeUpperBound();
117         }
118
119         int getTemperatureRangeLowerBound() {
120                 return mainObj->getTemperatureRangeLowerBound();
121         }
122
123         int getTemperatureRangeUpperBound() {
124                 return mainObj->getTemperatureRangeUpperBound();
125         }
126
127         void ___init() {
128                 string paramCls[] = {  };
129                 int numParam = 0;
130                 void* paramObj[] = {  };
131                 rmiObj->getMethodParams(paramCls, numParam, paramObj);
132                 init();
133         }
134
135         void ___turnOff() {
136                 string paramCls[] = {  };
137                 int numParam = 0;
138                 void* paramObj[] = {  };
139                 rmiObj->getMethodParams(paramCls, numParam, paramObj);
140                 turnOff();
141         }
142
143         void ___turnOn() {
144                 string paramCls[] = {  };
145                 int numParam = 0;
146                 void* paramObj[] = {  };
147                 rmiObj->getMethodParams(paramCls, numParam, paramObj);
148                 turnOn();
149         }
150
151         void ___getState() {
152                 string paramCls[] = {  };
153                 int numParam = 0;
154                 void* paramObj[] = {  };
155                 rmiObj->getMethodParams(paramCls, numParam, paramObj);
156                 bool retVal = getState();
157                 void* retObj = &retVal;
158                 rmiObj->sendReturnObj(retObj, "boolean");
159         }
160
161         void ___setColor() {
162                 string paramCls[] = { "double", "double", "double" };
163                 int numParam = 3;
164                 double _hue;
165                 double _saturation;
166                 double _brightness;
167                 void* paramObj[] = { &_hue, &_saturation, &_brightness };
168                 rmiObj->getMethodParams(paramCls, numParam, paramObj);
169                 setColor(_hue, _saturation, _brightness);
170         }
171
172         void ___setTemperature() {
173                 string paramCls[] = { "int" };
174                 int numParam = 1;
175                 int _temperature;
176                 void* paramObj[] = { &_temperature };
177                 rmiObj->getMethodParams(paramCls, numParam, paramObj);
178                 setTemperature(_temperature);
179         }
180
181         void ___getBrightness() {
182                 string paramCls[] = {  };
183                 int numParam = 0;
184                 void* paramObj[] = {  };
185                 rmiObj->getMethodParams(paramCls, numParam, paramObj);
186                 double retVal = getBrightness();
187                 void* retObj = &retVal;
188                 rmiObj->sendReturnObj(retObj, "double");
189         }
190
191         void ___getHue() {
192                 string paramCls[] = {  };
193                 int numParam = 0;
194                 void* paramObj[] = {  };
195                 rmiObj->getMethodParams(paramCls, numParam, paramObj);
196                 double retVal = getHue();
197                 void* retObj = &retVal;
198                 rmiObj->sendReturnObj(retObj, "double");
199         }
200
201         void ___getSaturation() {
202                 string paramCls[] = {  };
203                 int numParam = 0;
204                 void* paramObj[] = {  };
205                 rmiObj->getMethodParams(paramCls, numParam, paramObj);
206                 double retVal = getSaturation();
207                 void* retObj = &retVal;
208                 rmiObj->sendReturnObj(retObj, "double");
209         }
210
211         void ___getTemperature() {
212                 string paramCls[] = {  };
213                 int numParam = 0;
214                 void* paramObj[] = {  };
215                 rmiObj->getMethodParams(paramCls, numParam, paramObj);
216                 int retVal = getTemperature();
217                 void* retObj = &retVal;
218                 rmiObj->sendReturnObj(retObj, "int");
219         }
220
221         void ___getBrightnessRangeLowerBound() {
222                 string paramCls[] = {  };
223                 int numParam = 0;
224                 void* paramObj[] = {  };
225                 rmiObj->getMethodParams(paramCls, numParam, paramObj);
226                 double retVal = getBrightnessRangeLowerBound();
227                 void* retObj = &retVal;
228                 rmiObj->sendReturnObj(retObj, "double");
229         }
230
231         void ___getBrightnessRangeUpperBound() {
232                 string paramCls[] = {  };
233                 int numParam = 0;
234                 void* paramObj[] = {  };
235                 rmiObj->getMethodParams(paramCls, numParam, paramObj);
236                 double retVal = getBrightnessRangeUpperBound();
237                 void* retObj = &retVal;
238                 rmiObj->sendReturnObj(retObj, "double");
239         }
240
241         void ___getHueRangeLowerBound() {
242                 string paramCls[] = {  };
243                 int numParam = 0;
244                 void* paramObj[] = {  };
245                 rmiObj->getMethodParams(paramCls, numParam, paramObj);
246                 double retVal = getHueRangeLowerBound();
247                 void* retObj = &retVal;
248                 rmiObj->sendReturnObj(retObj, "double");
249         }
250
251         void ___getHueRangeUpperBound() {
252                 string paramCls[] = {  };
253                 int numParam = 0;
254                 void* paramObj[] = {  };
255                 rmiObj->getMethodParams(paramCls, numParam, paramObj);
256                 double retVal = getHueRangeUpperBound();
257                 void* retObj = &retVal;
258                 rmiObj->sendReturnObj(retObj, "double");
259         }
260
261         void ___getSaturationRangeLowerBound() {
262                 string paramCls[] = {  };
263                 int numParam = 0;
264                 void* paramObj[] = {  };
265                 rmiObj->getMethodParams(paramCls, numParam, paramObj);
266                 double retVal = getSaturationRangeLowerBound();
267                 void* retObj = &retVal;
268                 rmiObj->sendReturnObj(retObj, "double");
269         }
270
271         void ___getSaturationRangeUpperBound() {
272                 string paramCls[] = {  };
273                 int numParam = 0;
274                 void* paramObj[] = {  };
275                 rmiObj->getMethodParams(paramCls, numParam, paramObj);
276                 double retVal = getSaturationRangeUpperBound();
277                 void* retObj = &retVal;
278                 rmiObj->sendReturnObj(retObj, "double");
279         }
280
281         void ___getTemperatureRangeLowerBound() {
282                 string paramCls[] = {  };
283                 int numParam = 0;
284                 void* paramObj[] = {  };
285                 rmiObj->getMethodParams(paramCls, numParam, paramObj);
286                 int retVal = getTemperatureRangeLowerBound();
287                 void* retObj = &retVal;
288                 rmiObj->sendReturnObj(retObj, "int");
289         }
290
291         void ___getTemperatureRangeUpperBound() {
292                 string paramCls[] = {  };
293                 int numParam = 0;
294                 void* paramObj[] = {  };
295                 rmiObj->getMethodParams(paramCls, numParam, paramObj);
296                 int retVal = getTemperatureRangeUpperBound();
297                 void* retObj = &retVal;
298                 rmiObj->sendReturnObj(retObj, "int");
299         }
300
301         void ___waitRequestInvokeMethod() {
302                 while (true) {
303                         log << "Getting into the while loop" << endl;
304                         rmiObj->getMethodBytes();
305                         log << "Getting method bytes now" << endl;
306                         log << "Method len: " << rmiObj->getMethodBytesLen() << endl;
307                         int _objectId = rmiObj->getObjectId();
308                         log << "Object Id: " << _objectId << endl;
309                         int methodId = rmiObj->getMethodId();
310                         log << "Method Id: " << methodId << endl;
311                         if (_objectId == object0Id) {
312                                 if (set0Allowed.find(methodId) == set0Allowed.end()) {
313                                         cerr << "Object with object Id: " << _objectId << "  is not allowed to access method: " << methodId << endl;
314                                         return;
315                                 }
316                         }
317                         else {
318                                 cerr << "Object Id: " << _objectId << " not recognized!" << endl;
319                                 return;
320                         }
321                         switch (methodId) {
322                                 case 0: ___init(); break;
323                                 case 1: ___turnOff(); break;
324                                 case 2: ___turnOn(); break;
325                                 case 3: ___getState(); break;
326                                 case 4: ___setColor(); break;
327                                 case 5: ___setTemperature(); break;
328                                 case 6: ___getBrightness(); break;
329                                 case 7: ___getHue(); break;
330                                 case 8: ___getSaturation(); break;
331                                 case 9: ___getTemperature(); break;
332                                 case 10: ___getBrightnessRangeLowerBound(); break;
333                                 case 11: ___getBrightnessRangeUpperBound(); break;
334                                 case 12: ___getHueRangeLowerBound(); break;
335                                 case 13: ___getHueRangeUpperBound(); break;
336                                 case 14: ___getSaturationRangeLowerBound(); break;
337                                 case 15: ___getSaturationRangeUpperBound(); break;
338                                 case 16: ___getTemperatureRangeLowerBound(); break;
339                                 case 17: ___getTemperatureRangeUpperBound(); break;
340                                 default: 
341                                 cerr << "Method Id " << methodId << " not recognized!" << endl;
342                                 throw exception();
343                         }
344                 }
345                 log.close();
346         }
347
348 };
349 set<int> LightBulb_Skeleton::set0Allowed { 2, 10, 1, 3, 11, 8, 12, 7, 13, 9, 6, 16, 17, 4, 0, 14, 15, 5 };
350
351 #endif