63457fe69fefa006d51499c91da0c911c1d8b35f
[iot2.git] / iotjava / iotrmi / C++ / sample / TestClass_Stub.hpp
1 #ifndef _TESTCLASS_STUB_HPP__
2 #define _TESTCLASS_STUB_HPP__
3
4 #include <iostream>
5 #include <thread>
6 #include "../IoTRMICall.hpp"
7 #include "../IoTRMIObject.hpp"
8 #include "TestClassInterface.hpp"
9 #include "CallBack_CBSkeleton.hpp"
10 #include "StructC.hpp"
11
12 using namespace std;
13
14 class TestClass_Stub : public TestClassInterface {
15         public:
16                 TestClass_Stub();
17                 TestClass_Stub(int _port, const char* _address, int _rev, bool* _bResult, vector<int> _ports);
18                 ~TestClass_Stub();
19
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                 void                            handleStruct(vector<data> vecData);
31                 void                            handleEnum(vector<EnumC> vecEn);
32                 void                            ____init_CallBack();    // thread
33                 void                            ____registerCallBack(); // tell the other side that we are ready
34
35                 const static int        size = 12;
36                 const static string methodSignatures[size];
37
38         private:                
39                 int                                                     intA;
40                 float                                           floatB;
41                 string                                          stringC;
42                 //CallBackInterface                     cb;
43                 IoTRMICall                                      *rmiCall;
44                 IoTRMIObject                            *rmiObj;
45                 string                                          address;
46                 vector<int>                                     ports;
47                 vector<CallBackInterface*>      vecCBObj;
48
49                 int             objectId = 0;   // Default value is 0
50                 static int      objIdCnt;
51 };
52
53
54 int TestClass_Stub::objIdCnt = 0;
55
56
57 const string TestClass_Stub::methodSignatures[TestClass_Stub::size] = {
58         "voidsetA(int)", // 0
59         "voidsetB(float)", // 1
60         "voidsetC(string)", // 2
61         "sumArray(string[])", // 3
62         //"sumArray(int[])",
63         "intsetAndGetA(int)", // 4
64         "intsetACAndGetA(string,int)", // 5
65         "intcallBack()", // 6
66         "voidregisterCallBack(CallBackInterface)", // 7
67         "voidregisterCallBack(CallBackInterface[])", // 8
68         "registercallback", // 9
69         "handleStruct(StructJ[])", // 10
70         "structsize" // 11
71 };
72
73
74 TestClass_Stub::TestClass_Stub() {
75
76         address = "";
77         rmiCall = NULL;
78 }
79
80
81 TestClass_Stub::TestClass_Stub(int _port, const char* _address, int _rev, bool* _bResult, vector<int> _ports) {
82
83         address = _address;
84         rmiCall = new IoTRMICall(_port, _address, _rev, _bResult);
85         ports = _ports;
86         // Start thread
87         thread th1 (&TestClass_Stub::____init_CallBack, this);
88         th1.detach();
89         //th1.join();
90         ____registerCallBack();
91 }
92
93
94 TestClass_Stub::~TestClass_Stub() {
95
96         if (rmiCall != NULL) {
97                 delete rmiCall;
98                 rmiCall = NULL;
99         }
100         if (rmiObj != NULL) {
101                 delete rmiObj;
102                 rmiObj = NULL;
103         }
104         for(CallBackInterface* cb : vecCBObj) {
105                 delete cb;
106                 cb = NULL;
107         }
108 }
109
110
111 // Callback handler thread
112 void TestClass_Stub::____init_CallBack() {
113
114         bool bResult = false;
115         rmiObj = new IoTRMIObject(ports[0], &bResult);
116         while (true) {
117                 char* method = rmiObj->getMethodBytes();
118                 int objId = IoTRMIObject::getObjectId(method);
119                 if (objId < vecCBObj.size()) {  // Check if still within range
120                         CallBack_CBSkeleton* skel = 
121                                 dynamic_cast<CallBack_CBSkeleton*> (vecCBObj.at(objId));
122                         skel->invokeMethod(rmiObj);
123                 } else {
124                         string error = "TestClass_Stub: Illegal object Id: " + to_string(objId);
125                         throw error;
126                 }
127         }
128 }
129
130
131 // Notify that callback thread is ready
132 void TestClass_Stub::____registerCallBack() {
133
134         int numParam = 3;
135         int methodId = 9;
136         string retType = "void";
137         string paramCls[] = { "int", "string", "int" };
138         int rev = 0;
139         void* paramObj[] = { &ports[0], &address, &rev };
140         void* retObj = NULL;
141         rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj);
142 }
143
144
145 void TestClass_Stub::setA(int _int) {
146
147         int numParam = 1;
148         int methodId = 0;
149         string retType = "void";
150         string paramCls[] = { "int" };
151         void* paramObj[] = { &_int };
152         void* retObj = NULL;
153         rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj);
154 }
155
156
157 void TestClass_Stub::setB(float _float) {
158
159         int numParam = 1;
160         int methodId = 1;
161         string retType = "void";
162         string paramCls[] = { "float" };
163         void* paramObj[] = { &_float };
164         void* retObj = NULL;
165         rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj);
166 }
167
168
169 void TestClass_Stub::setC(string _string) {
170
171         int numParam = 1;
172         int methodId = 2;
173         string retType = "void";
174         string paramCls[] = { "string" };
175         void* paramObj[] = { &_string };
176         void* retObj = NULL;
177         rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj);
178 }
179
180
181 string TestClass_Stub::sumArray(vector<string> newA) {
182
183         int numParam = 1;
184         int methodId = 3;
185         string retType = "string";
186         string paramCls[] = { "string[]" };
187         void* paramObj[] = { &newA };
188         string retVal = "";
189         void* retObj = &retVal;
190         rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj);
191         return retVal;
192 }
193
194
195 /*int64_t TestClass_Stub::sumArray(vector<int> newA) {
196
197         int numParam = 1;
198         string sign = "sumArray(int[])";
199         string retType = "long";
200         string paramCls[] = { "int[]" };
201         void* paramObj[] = { &newA };
202         int64_t retVal = 0;
203         void* retObj = &retVal;
204         rmiCall->remoteCall(objectId, sign, retType, paramCls, paramObj, numParam, retObj);
205         return retVal;
206 }*/
207
208
209
210 int TestClass_Stub::setAndGetA(int newA) {
211
212         int numParam = 1;
213         int methodId = 4;
214         string retType = "int";
215         string paramCls[] = { "int" };
216         void* paramObj[] = { &newA };
217         int retVal = 0;
218         void* retObj = &retVal;
219         rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj);
220         return retVal;
221 }
222
223
224 int TestClass_Stub::setACAndGetA(string newC, int newA) {
225
226         int numParam = 2;
227         int methodId = 5;
228         string retType = "int";
229         string paramCls[] = { "string", "int" };
230         void* paramObj[] = { &newC, &newA };
231         int retVal = 0;
232         void* retObj = &retVal;
233         rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj);
234         return retVal;
235 }
236
237
238 void TestClass_Stub::registerCallback(CallBackInterface* _cb) {
239
240         //Should implement the callback here
241 }
242
243
244 void TestClass_Stub::registerCallback(vector<CallBackInterface*> _cb) {
245
246         for (CallBackInterface* cb: _cb) {
247                 CallBack_CBSkeleton* skel = new CallBack_CBSkeleton(cb, objIdCnt++);
248                 vecCBObj.push_back(skel);
249         }
250
251         int numParam = 1;
252         int methodId = 8;
253         string retType = "void";
254         string paramCls[] = { "int" };
255         int param1 = _cb.size();
256         void* paramObj[] = { &param1 };
257         void* retObj = NULL;
258         rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj);
259 }
260
261
262 int TestClass_Stub::callBack() {
263
264         int numParam = 0;
265         int methodId = 6;
266         string retType = "int";
267         string paramCls[] = { };
268         void* paramObj[] = { };
269         int retVal = 0;
270         void* retObj = &retVal;
271         rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj);
272         return retVal;
273 }
274
275
276 void TestClass_Stub::handleStruct(vector<data> vecData) {
277
278         int numParam = 1;
279         int methodId = 11;
280         string retType = "void";
281         string paramCls[] = { "int" };
282         int structsize = vecData.size();
283         void* paramObj[] = { &structsize };
284         void* retObj = NULL;
285         rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj);
286
287         int numParam2 = 3*vecData.size();
288         int methodId2 = 10;
289         string retType2 = "void";
290         string paramCls2[numParam2];
291         void* paramObj2[numParam2];
292         int pos = 0;
293         for(int i = 0; i < vecData.size(); i++) {
294                 paramCls2[pos] = "string";
295                 paramObj2[pos] = &vecData[i].name; pos++;
296                 paramCls2[pos] = "float";
297                 paramObj2[pos] = &vecData[i].value; pos++;
298                 paramCls2[pos] = "int";
299                 paramObj2[pos] = &vecData[i].year; pos++;
300         }
301         void* retObj2 = NULL;
302         rmiCall->remoteCall(objectId, methodId2, retType2, paramCls2, paramObj2, numParam2, retObj2);
303 }
304
305
306 void TestClass_Stub::handleEnum(vector<EnumC> vecEn) {
307
308         int numParam = 1;
309         int methodId = 13;
310         string retType = "void";
311         string paramCls[] = { "int" };
312         int enumsize = vecEn.size();
313         void* paramObj[] = { &enumsize };
314         void* retObj = NULL;
315         rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj);
316
317         int numParam2 = vecEn.size();
318         int methodId2 = 12;
319         string retType2 = "void";
320         string paramCls2[numParam2];
321         // Need to define this container for integer version of enum
322         int paramEnum[numParam2];
323         void* paramObj2[numParam2];
324         for(int i = 0; i < vecEn.size(); i++) {
325                 paramCls2[i] = "int";
326                 paramEnum[i] = (int) vecEn[i]; // cast enum to integer
327                 paramObj2[i] = &paramEnum[i];
328         }
329         void* retObj2 = NULL;
330         rmiCall->remoteCall(objectId, methodId2, retType2, paramCls2, paramObj2, numParam2, retObj2);
331 }
332
333
334
335 #endif