96b37b45276782a977dd4db14e742f6104253a7c
[iot2.git] / iotjava / iotrmi / C++ / sample / TestClass_Skeleton.hpp
1 #ifndef _TESTCLASS_SKELETON_HPP__
2 #define _TESTCLASS_SKELETON_HPP__
3
4 #include <iostream>
5 #include <exception>
6 #include <set>
7 #include "../IoTRMIObject.hpp"
8 #include "../IoTRMICall.hpp"
9 #include "CallBack_CBStub.hpp"
10 #include "TestClassInterface.hpp"
11
12 using namespace std;
13
14 class TestClass_Skeleton : public TestClassInterface {
15         public:
16                 TestClass_Skeleton(TestClassInterface* _tc, int _port);
17                 ~TestClass_Skeleton();
18
19                 void                    ___waitRequestInvokeMethod();
20                 void                    setA(int _int);
21                 void                    setB(float _float);
22                 void                    setC(string _string);
23                 string                  sumArray(vector<string> newA);
24                 //int64_t               sumArray(vector<int> newA);
25                 int                             setAndGetA(int newA);
26                 int                             setACAndGetA(string newC, int newA);
27                 void                    registerCallback(CallBackInterface* _cb);
28                 void                    registerCallback(vector<CallBackInterface*> _cb);
29                 int                             callBack();
30                 vector<data>    handleStruct(vector<data> vecData);
31                 vector<EnumC>   handleEnum(vector<EnumC> vecEn);
32                 
33                 void                    ___setA();
34                 void                    ___setB();
35                 void                    ___setC();
36                 void                    ___sumArray();
37                 //int64_t               ____sumArray();
38                 void                    ___setAndGetA();
39                 void                    ___setACAndGetA();
40                 void                    ___registerCallback();
41                 void                    ____registerCallback();
42                 // For array of callbacks
43                 void                    ___regCB();
44                 void                    ___callBack();
45                 // For array of structs
46                 int                             ___structSize();
47                 void                    ___handleStruct(int structsize1);
48                 int                             ___enumSize();
49                 void                    ___handleEnum(int enumsize1);
50
51         private:                
52                 TestClassInterface                      *tc;
53                 IoTRMIObject                            *rmiObj;
54                 // Permission setup
55                 const static int                        object0Id = 0;
56                 //const static int                      object0Permission[];
57                 const static set<int>           set0Allowed;
58                 
59                 IoTRMICall                                      *rmiCall;
60                 static int                                      objIdCnt;
61                 vector<CallBackInterface*>      vecCBObj;
62                 //CallBackInterface cbstub;
63 };
64
65
66 // Permission setup
67 //const int TestClass_Skeleton::object0Id = 0;
68 //const int TestClass_Skeleton::object0Permission[] = {0, 1, 2, 3, 4, 5};
69 const set<int> TestClass_Skeleton::set0Allowed {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
70
71 int TestClass_Skeleton::objIdCnt = 0;
72
73
74 TestClass_Skeleton::TestClass_Skeleton(TestClassInterface* _tc, int _port) {
75
76         bool _bResult = false;
77         tc = _tc;
78         rmiObj = new IoTRMIObject(_port, &_bResult);
79         ___waitRequestInvokeMethod();
80 }
81
82
83 TestClass_Skeleton::~TestClass_Skeleton() {
84
85         if (rmiObj != NULL) {
86                 delete rmiObj;
87                 rmiObj = NULL;
88         }
89         if (rmiCall != NULL) {
90                 delete rmiCall;
91                 rmiCall = NULL;
92         }
93         for(CallBackInterface* cb : vecCBObj) {
94                 delete cb;
95                 cb = NULL;
96         }
97 }
98
99
100 void TestClass_Skeleton::setA(int _int) {
101
102         tc->setA(_int);
103 }
104
105
106 void TestClass_Skeleton::___setA() {
107
108         string paramCls[] = { "int" };
109         int numParam = 1;
110         int param1 = 0;
111         void* paramObj[] = { &param1 };
112         rmiObj->getMethodParams(paramCls, numParam, paramObj);
113         setA(param1);
114 }
115
116
117 void TestClass_Skeleton::setB(float _float) {
118
119         tc->setB(_float);
120 }
121
122
123 void TestClass_Skeleton::___setB() {
124
125         string paramCls[] = { "float" };
126         int numParam = 1;
127         float param1 = 0.0;
128         void* paramObj[] = { &param1 };
129         rmiObj->getMethodParams(paramCls, numParam, paramObj);
130         setB(param1);
131 }
132
133
134 void TestClass_Skeleton::setC(string _string) {
135
136         tc->setC(_string);
137 }
138
139
140 void TestClass_Skeleton::___setC() {
141
142         string paramCls[] = { "string" };
143         int numParam = 1;
144         string param1 = "";
145         void* paramObj[] = { &param1 };
146         rmiObj->getMethodParams(paramCls, numParam, paramObj);
147         setC(param1);
148 }
149
150
151 string TestClass_Skeleton::sumArray(vector<string> newA) {
152
153         return tc->sumArray(newA);
154 }
155
156
157 void TestClass_Skeleton::___sumArray() {
158
159         string paramCls[] = { "string[]" };
160         int numParam = 1;
161         vector<string> param1;
162         void* paramObj[] = { &param1 };
163         rmiObj->getMethodParams(paramCls, numParam, paramObj);
164         string retVal = sumArray(param1);
165         void* retObj = &retVal;
166         rmiObj->sendReturnObj(retObj, "string");
167 }
168
169
170 /*int64_t TestClass_Skeleton::sumArray(vector<int> newA) {
171
172         return tc->sumArray(newA);
173 }*/
174
175
176 /*int64_t TestClass_Skeleton::____sumArray() {
177
178 }*/
179
180
181 int TestClass_Skeleton::setAndGetA(int newA) {
182
183         return tc->setAndGetA(newA);
184 }
185
186
187 void TestClass_Skeleton::___setAndGetA() {
188
189         string paramCls[] = { "int" };
190         int numParam = 1;
191         int param1 = 0;
192         void* paramObj[] = { &param1 };
193         rmiObj->getMethodParams(paramCls, numParam, paramObj);
194         int retVal = setAndGetA(param1);
195         void* retObj = &retVal;
196         rmiObj->sendReturnObj(retObj, "int");
197 }
198
199
200 int TestClass_Skeleton::setACAndGetA(string newC, int newA) {
201
202         return tc->setACAndGetA(newC, newA);
203 }
204
205
206 void TestClass_Skeleton::___setACAndGetA() {
207
208         string paramCls[] = { "string", "int" };
209         int numParam = 2;
210         string param1 = "";
211         int param2 = 0;
212         void* paramObj[] = { &param1, &param2 };
213         rmiObj->getMethodParams(paramCls, numParam, paramObj);
214         int retVal = setACAndGetA(param1, param2);
215         void* retObj = &retVal;
216         rmiObj->sendReturnObj(retObj, "int");
217 }
218
219
220 void TestClass_Skeleton::registerCallback(CallBackInterface* _cb) {
221
222         tc->registerCallback(_cb);
223 }
224
225
226 void TestClass_Skeleton::___registerCallback() {
227
228 }
229
230
231 void TestClass_Skeleton::registerCallback(vector<CallBackInterface*> _cb) {
232
233         tc->registerCallback(_cb);
234 }
235
236
237 void TestClass_Skeleton::___regCB() {
238
239         string paramCls[] = { "int", "string", "int" };
240         int numParam = 3;
241         int param1 = 0;
242         string param2 = "";
243         int param3 = 0;
244         void* paramObj[] = { &param1, &param2, &param3 };
245         rmiObj->getMethodParams(paramCls, numParam, paramObj);
246         // Instantiate IoTRMICall object
247         bool bResult = false;
248         cout << "Port: " << param1 << endl;
249         cout << "Address: " << param2 << endl;
250
251         rmiCall = new IoTRMICall(param1, param2.c_str(), param3, &bResult);
252 }
253
254
255 void TestClass_Skeleton::____registerCallback() {
256
257         string paramCls[] = { "int" };
258         int numParam = 1;
259         int numStubs = 0;
260         void* paramObj[] = { &numStubs };
261         rmiObj->getMethodParams(paramCls, numParam, paramObj);
262         vector<CallBackInterface*> stub;
263         for (int objId = 0; objId < numStubs; objId++) {
264                 CallBackInterface* cb = new CallBack_CBStub(rmiCall, objIdCnt);
265                 stub.push_back(cb);
266                 vecCBObj.push_back(cb);
267                 objIdCnt++;
268         }
269         registerCallback(stub);
270 }
271
272
273 int TestClass_Skeleton::callBack() {
274
275         tc->callBack();
276 }
277
278
279 void TestClass_Skeleton::___callBack() {
280
281         int retVal = callBack();
282         void* retObj = &retVal;
283         rmiObj->sendReturnObj(retObj, "int");
284 }
285
286
287 vector<data> TestClass_Skeleton::handleStruct(vector<data> vecData) {
288
289         return tc->handleStruct(vecData);
290 }
291
292
293 int TestClass_Skeleton::___structSize() {
294
295         string paramCls[] = { "int" };
296         int numParam = 1;
297         int param1 = 0;
298         void* paramObj[] = { &param1 };
299         rmiObj->getMethodParams(paramCls, numParam, paramObj);
300         return param1;
301 }
302
303
304 void TestClass_Skeleton::___handleStruct(int structsize1) {
305
306         string paramCls[3*structsize1];
307         void* paramObj[3*structsize1];
308         int numParam = 3*structsize1;
309         // define array of everything
310         string param1[structsize1];
311         float param2[structsize1];
312         int param3[structsize1];
313         int pos = 0;
314         for(int i=0; i < structsize1; i++) {
315                 paramCls[pos] = "string";
316                 paramObj[pos++] = &param1[i];
317                 paramCls[pos] = "float";
318                 paramObj[pos++] = &param2[i];
319                 paramCls[pos] = "int";
320                 paramObj[pos++] = &param3[i];
321         }
322         rmiObj->getMethodParams(paramCls, numParam, paramObj);
323         vector<data> dat(structsize1);
324         pos = 0;
325         for (int i=0; i < structsize1; i++) {
326                 dat[i].name = param1[i];
327                 dat[i].value = param2[i];
328                 dat[i].year = param3[i];
329         }
330         // This is a return value of type vector of struct
331         // If no return value, then just "handleStruct(dat)"
332         vector<data> retData = handleStruct(dat);
333         // Send the length first!
334         int retLength = retData.size();
335         void* retObj = &retLength;
336         rmiObj->sendReturnObj(retObj, "int");
337         // Send the actual bytes - struct of 3 members
338         int numRetObj = 3*retLength;
339         string retCls[numRetObj];
340         void* retObj2[numRetObj];
341         pos = 0;
342         for(int i = 0; i < retLength; i++) {
343                 retCls[pos] = "string";
344                 retObj2[pos] = &retData[i].name; pos++;
345                 retCls[pos] = "float";
346                 retObj2[pos] = &retData[i].value; pos++;
347                 retCls[pos] = "int";
348                 retObj2[pos] = &retData[i].year; pos++;
349         }
350         rmiObj->sendReturnObj(retObj2, retCls, numRetObj);
351 }
352
353
354 vector<EnumC> TestClass_Skeleton::handleEnum(vector<EnumC> vecEn) {
355
356         return tc->handleEnum(vecEn);
357 }
358
359
360 int TestClass_Skeleton::___enumSize() {
361
362         string paramCls[] = { "int" };
363         int numParam = 1;
364         int param1 = 0;
365         void* paramObj[] = { &param1 };
366         rmiObj->getMethodParams(paramCls, numParam, paramObj);
367         return param1;
368 }
369
370
371 void TestClass_Skeleton::___handleEnum() {
372
373         int numParam = 1;
374         string paramCls[] = { "int[]" };
375         vector<int> paramInt;
376         void* paramObj[] = { &paramInt };
377         // Receive the array of integers
378         rmiObj->getMethodParams(paramCls, numParam, paramObj);
379         int enumSize1 = paramInt.size();
380         vector<EnumC> en(enumSize1);
381         for (int i=0; i < enumSize1; i++) {
382                 en[i] = (EnumC) paramInt[i];
383         }
384         //handleEnum(en);
385         // if void, just "handleEnum(en)"
386         // this is when we have return value vector<EnumC>
387         vector<EnumC> retEnum = handleEnum(en);
388         // Get length first
389         int enumSize2 = retEnum.size();
390         // Now send the array of integers
391         vector<int> retEnumInt(enumSize2);
392         for (int i=0; i < enumSize2; i++) {
393                 retEnumInt[i] = (int) retEnum[i];
394         }
395         void* retObj = &retEnumInt;
396         rmiObj->sendReturnObj(retObj, "int[]");
397 }
398
399
400 void TestClass_Skeleton::___waitRequestInvokeMethod() {
401
402         int structsize1 = 0;
403
404         // Loop continuously waiting for incoming bytes
405         while (true) {
406
407                 rmiObj->getMethodBytes();
408                 int _objectId = rmiObj->getObjectId();
409                 int methodId = rmiObj->getMethodId();
410                 if (_objectId == object0Id) {
411                 // Multiplex based on object Id
412                         // Complain if the method is not allowed
413                         if (set0Allowed.find(methodId) == set0Allowed.end()) {
414                                 cerr << "TestClass_Skeleton: This object is not allowed to access method " << methodId << endl;
415                                 //exit(1);
416                                 throw exception();
417                         }
418                 // If we have more than 1 object Id...
419                 //else if (_objectId == object1Id) {
420
421                 } else {
422                         cerr << "TestClass_Skeleton: Unrecognizable object Id: " << _objectId << endl;
423                         throw exception();
424                         //exit(1);
425                 }
426                 
427                 switch (methodId) {
428                         case 0: ___setA(); break;
429                         case 1: ___setB(); break;
430                         case 2: ___setC(); break;
431                         case 3: ___sumArray(); break;
432                 /*  case 3: ____sumArray(); break;*/
433                         case 4: ___setAndGetA(); break;
434                         case 5: ___setACAndGetA(); break;
435                         case 6: ___callBack(); break; 
436                         case 7: ___registerCallback(); break;
437                         case 8: ____registerCallback(); break;
438                         case 9: ___regCB(); break;
439                         // Handle struct
440                         case 10: ___handleStruct(structsize1); break;
441                         case 11: structsize1 = ___structSize(); break;
442                         case 12: ___handleEnum(); break;
443                         default:
444                                 string error = "Method Id not recognized!";
445                                 throw error;
446                 }
447         }
448 }
449
450
451 #endif
452