dd66cc668d97caf2f8b3ceb8c376e16921f93e02
[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 "../IoTRMIObject.hpp"
6 #include "../IoTRMICall.hpp"
7 #include "CallBack_CBStub.hpp"
8 #include "TestClassInterface.hpp"
9
10 using namespace std;
11
12 class TestClass_Skeleton : public TestClassInterface {
13         public:
14                 TestClass_Skeleton(TestClassInterface* _tc, int _port);
15                 ~TestClass_Skeleton();
16
17                 void                    ___waitRequestInvokeMethod();
18                 void                    setA(int _int);
19                 void                    setB(float _float);
20                 void                    setC(string _string);
21                 string                  sumArray(vector<string> newA);
22                 //int64_t               sumArray(vector<int> newA);
23                 int                             setAndGetA(int newA);
24                 int                             setACAndGetA(string newC, int newA);
25                 void                    registerCallback(CallBackInterface* _cb);
26                 void                    registerCallback(vector<CallBackInterface*> _cb);
27                 int                             callBack();
28                 void                    handleStruct(vector<data> vecData);
29                 
30                 void                    ___setA();
31                 void                    ___setB();
32                 void                    ___setC();
33                 void                    ___sumArray();
34                 //int64_t               ____sumArray();
35                 void                    ___setAndGetA();
36                 void                    ___setACAndGetA();
37                 void                    ___registerCallback();
38                 void                    ____registerCallback();
39                 // For array of callbacks
40                 void                    ___regCB();
41                 void                    ___callBack();
42                 // For array of structs
43                 int                             ___structSize();
44                 void                    ___handleStruct(int structsize1);
45
46                 const static int        size = 12;
47                 const static string methodSignatures[size];
48
49         private:                
50                 TestClassInterface                      *tc;
51                 IoTRMIObject                            *rmiObj;
52                 IoTRMICall                                      *rmiCall;
53                 static int                                      objIdCnt;
54                 vector<CallBackInterface*>      vecCBObj;
55                 //CallBackInterface cbstub;
56 };
57
58
59 const string TestClass_Skeleton::methodSignatures[TestClass_Skeleton::size] = {
60         "voidsetA(int)",
61         "voidsetB(float)",
62         "voidsetC(string)",
63         "sumArray(string[])",
64         //"sumArray(int[])",
65         "intsetAndGetA(int)",
66         "intsetACAndGetA(string,int)",
67         "intcallBack()",
68         "voidregisterCallBack(CallBackInterface)",
69         "voidregisterCallBack(CallBackInterface[])",
70         "registercallback",
71         "handleStruct(StructJ[])",
72         "structsize"
73 };
74
75
76 int TestClass_Skeleton::objIdCnt = 0;
77
78
79 TestClass_Skeleton::TestClass_Skeleton(TestClassInterface* _tc, int _port) {
80
81         bool _bResult = false;
82         tc = _tc;
83         rmiObj = new IoTRMIObject(_port, &_bResult, methodSignatures, size);
84         ___waitRequestInvokeMethod();
85 }
86
87
88 TestClass_Skeleton::~TestClass_Skeleton() {
89
90         if (rmiObj != NULL) {
91                 delete rmiObj;
92                 rmiObj = NULL;
93         }
94         if (rmiCall != NULL) {
95                 delete rmiCall;
96                 rmiCall = NULL;
97         }
98         for(CallBackInterface* cb : vecCBObj) {
99                 delete cb;
100                 cb = NULL;
101         }
102 }
103
104
105 void TestClass_Skeleton::setA(int _int) {
106
107         tc->setA(_int);
108 }
109
110
111 void TestClass_Skeleton::___setA() {
112
113         string paramCls[] = { "int" };
114         int numParam = 1;
115         int param1 = 0;
116         void* paramObj[] = { &param1 };
117         rmiObj->getMethodParams(paramCls, numParam, paramObj);
118         setA(param1);
119 }
120
121
122 void TestClass_Skeleton::setB(float _float) {
123
124         tc->setB(_float);
125 }
126
127
128 void TestClass_Skeleton::___setB() {
129
130         string paramCls[] = { "float" };
131         int numParam = 1;
132         float param1 = 0.0;
133         void* paramObj[] = { &param1 };
134         rmiObj->getMethodParams(paramCls, numParam, paramObj);
135         setB(param1);
136 }
137
138
139 void TestClass_Skeleton::setC(string _string) {
140
141         tc->setC(_string);
142 }
143
144
145 void TestClass_Skeleton::___setC() {
146
147         string paramCls[] = { "string" };
148         int numParam = 1;
149         string param1 = "";
150         void* paramObj[] = { &param1 };
151         rmiObj->getMethodParams(paramCls, numParam, paramObj);
152         setC(param1);
153 }
154
155
156 string TestClass_Skeleton::sumArray(vector<string> newA) {
157
158         return tc->sumArray(newA);
159 }
160
161
162 void TestClass_Skeleton::___sumArray() {
163
164         string paramCls[] = { "string[]" };
165         int numParam = 1;
166         vector<string> param1;
167         void* paramObj[] = { &param1 };
168         rmiObj->getMethodParams(paramCls, numParam, paramObj);
169         string retVal = sumArray(param1);
170         void* retObj = &retVal;
171         rmiObj->sendReturnObj(retObj, "string");
172 }
173
174
175 /*int64_t TestClass_Skeleton::sumArray(vector<int> newA) {
176
177         return tc->sumArray(newA);
178 }*/
179
180
181 /*int64_t TestClass_Skeleton::____sumArray() {
182
183 }*/
184
185
186 int TestClass_Skeleton::setAndGetA(int newA) {
187
188         return tc->setAndGetA(newA);
189 }
190
191
192 void TestClass_Skeleton::___setAndGetA() {
193
194         string paramCls[] = { "int" };
195         int numParam = 1;
196         int param1 = 0;
197         void* paramObj[] = { &param1 };
198         rmiObj->getMethodParams(paramCls, numParam, paramObj);
199         int retVal = setAndGetA(param1);
200         void* retObj = &retVal;
201         rmiObj->sendReturnObj(retObj, "int");
202 }
203
204
205 int TestClass_Skeleton::setACAndGetA(string newC, int newA) {
206
207         return tc->setACAndGetA(newC, newA);
208 }
209
210
211 void TestClass_Skeleton::___setACAndGetA() {
212
213         string paramCls[] = { "string", "int" };
214         int numParam = 2;
215         string param1 = "";
216         int param2 = 0;
217         void* paramObj[] = { &param1, &param2 };
218         rmiObj->getMethodParams(paramCls, numParam, paramObj);
219         int retVal = setACAndGetA(param1, param2);
220         void* retObj = &retVal;
221         rmiObj->sendReturnObj(retObj, "int");
222 }
223
224
225 void TestClass_Skeleton::registerCallback(CallBackInterface* _cb) {
226
227         tc->registerCallback(_cb);
228 }
229
230
231 void TestClass_Skeleton::___registerCallback() {
232
233 }
234
235
236 void TestClass_Skeleton::registerCallback(vector<CallBackInterface*> _cb) {
237
238         tc->registerCallback(_cb);
239 }
240
241
242 void TestClass_Skeleton::___regCB() {
243
244         string paramCls[] = { "int", "string", "int" };
245         int numParam = 3;
246         int param1 = 0;
247         string param2 = "";
248         int param3 = 0;
249         void* paramObj[] = { &param1, &param2, &param3 };
250         rmiObj->getMethodParams(paramCls, numParam, paramObj);
251         // Instantiate IoTRMICall object
252         bool bResult = false;
253         rmiCall = new IoTRMICall(param1, param2.c_str(), param3, &bResult, 
254                 CallBack_CBStub::methodSignatures, CallBack_CBStub::size);
255 }
256
257
258 void TestClass_Skeleton::____registerCallback() {
259
260         string paramCls[] = { "int" };
261         int numParam = 1;
262         int numStubs = 0;
263         void* paramObj[] = { &numStubs };
264         rmiObj->getMethodParams(paramCls, numParam, paramObj);
265         vector<CallBackInterface*> stub;
266         for (int objId = 0; objId < numStubs; objId++) {
267                 CallBackInterface* cb = new CallBack_CBStub(rmiCall, objIdCnt);
268                 stub.push_back(cb);
269                 vecCBObj.push_back(cb);
270                 objIdCnt++;
271         }
272         registerCallback(stub);
273 }
274
275
276 int TestClass_Skeleton::callBack() {
277
278         tc->callBack();
279 }
280
281
282 void TestClass_Skeleton::___callBack() {
283
284         int retVal = callBack();
285         void* retObj = &retVal;
286         rmiObj->sendReturnObj(retObj, "int");
287 }
288
289
290 int TestClass_Skeleton::___structSize() {
291
292         string paramCls[] = { "int" };
293         int numParam = 1;
294         int param1 = 0;
295         void* paramObj[] = { &param1 };
296         rmiObj->getMethodParams(paramCls, numParam, paramObj);
297         return param1;
298 }
299
300
301 void TestClass_Skeleton::handleStruct(vector<data> vecData) {
302
303         tc->handleStruct(vecData);
304 }
305
306
307 void TestClass_Skeleton::___handleStruct(int structsize1) {
308
309         string paramCls[3*structsize1];
310         void* paramObj[3*structsize1];
311         int numParam = 3*structsize1;
312         // define array of everything
313         string param1[structsize1];
314         float param2[structsize1];
315         int param3[structsize1];
316         int pos = 0;
317         for(int i=0; i < structsize1; i++) {
318                 paramCls[pos] = "string";
319                 paramObj[pos++] = &param1[i];
320                 paramCls[pos] = "float";
321                 paramObj[pos++] = &param2[i];
322                 paramCls[pos] = "int";
323                 paramObj[pos++] = &param3[i];
324         }
325         rmiObj->getMethodParams(paramCls, numParam, paramObj);
326         vector<data> dat(structsize1);
327         pos = 0;
328         for (int i=0; i < structsize1; i++) {
329                 dat[i].name = param1[i];
330                 dat[i].value = param2[i];
331                 dat[i].year = param3[i];
332         }
333         handleStruct(dat);
334 }
335
336
337 void TestClass_Skeleton::___waitRequestInvokeMethod() {
338
339         int structsize1 = 0;
340         // Loop continuously waiting for incoming bytes
341         while (true) {
342
343                 rmiObj->getMethodBytes();
344                 string methodSign = rmiObj->getSignature();
345                 cout << "Method sign: " << methodSign << endl;
346                 
347                 if (methodSign.compare("voidsetA(int)") == 0) {
348                         ___setA();
349                 } else if (methodSign.compare("voidsetB(float)") == 0) {
350                         ___setB();
351                 } else if (methodSign.compare("voidsetC(string)") == 0) {
352                         ___setC();
353                 } else if (methodSign.compare("sumArray(string[])") == 0) {
354                         ___sumArray();
355                 /*} else if (methodSign.compare("sumArray(int[])") == 0) {
356                         ____sumArray();*/
357                 } else if (methodSign.compare("intsetAndGetA(int)") == 0) {
358                         ___setAndGetA();
359                 } else if (methodSign.compare("intsetACAndGetA(string,int)") == 0) {
360                         ___setACAndGetA();
361                 } else if (methodSign.compare("voidregisterCallBack(CallBackInterface)") == 0) {
362                         //
363                 } else if (methodSign.compare("voidregisterCallBack(CallBackInterface[])") == 0) {
364                         ____registerCallback();
365                 } else if (methodSign.compare("registercallback") == 0) {
366                         ___regCB();
367                 } else if (methodSign.compare("intcallBack()") == 0) {
368                         ___callBack();
369                 // Handle struct
370                 } else if (methodSign.compare("structsize") == 0) {
371                         structsize1 = ___structSize();
372                 } else if (methodSign.compare("handleStruct(StructJ[])") == 0) {
373                         ___handleStruct(structsize1);
374                 } else {
375                         string error = "Signature unrecognized: " + string(methodSign);
376                         throw error;
377                 }
378         }
379 }
380
381
382 #endif
383