1 #ifndef _TESTCLASS_STUB_HPP__
2 #define _TESTCLASS_STUB_HPP__
6 #include "../IoTRMICall.hpp"
7 #include "../IoTRMIObject.hpp"
8 #include "TestClassInterface.hpp"
9 #include "CallBack_CBSkeleton.hpp"
10 #include "StructC.hpp"
14 class TestClass_Stub : public TestClassInterface {
17 TestClass_Stub(int _port, const char* _address, int _rev, bool* _bResult, vector<int> _ports);
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);
30 void handleStruct(vector<data> vecData);
31 vector<EnumC> handleEnum(vector<EnumC> vecEn);
32 void ____init_CallBack(); // thread
33 void ____registerCallBack(); // tell the other side that we are ready
35 const static int size = 12;
36 const static string methodSignatures[size];
42 //CallBackInterface cb;
47 vector<CallBackInterface*> vecCBObj;
49 int objectId = 0; // Default value is 0
54 int TestClass_Stub::objIdCnt = 0;
57 const string TestClass_Stub::methodSignatures[TestClass_Stub::size] = {
59 "voidsetB(float)", // 1
60 "voidsetC(string)", // 2
61 "sumArray(string[])", // 3
63 "intsetAndGetA(int)", // 4
64 "intsetACAndGetA(string,int)", // 5
66 "voidregisterCallBack(CallBackInterface)", // 7
67 "voidregisterCallBack(CallBackInterface[])", // 8
68 "registercallback", // 9
69 "handleStruct(StructJ[])", // 10
74 TestClass_Stub::TestClass_Stub() {
81 TestClass_Stub::TestClass_Stub(int _port, const char* _address, int _rev, bool* _bResult, vector<int> _ports) {
84 rmiCall = new IoTRMICall(_port, _address, _rev, _bResult);
87 thread th1 (&TestClass_Stub::____init_CallBack, this);
90 ____registerCallBack();
94 TestClass_Stub::~TestClass_Stub() {
96 if (rmiCall != NULL) {
100 if (rmiObj != NULL) {
104 for(CallBackInterface* cb : vecCBObj) {
111 // Callback handler thread
112 void TestClass_Stub::____init_CallBack() {
114 bool bResult = false;
115 rmiObj = new IoTRMIObject(ports[0], &bResult);
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);
124 string error = "TestClass_Stub: Illegal object Id: " + to_string(objId);
131 // Notify that callback thread is ready
132 void TestClass_Stub::____registerCallBack() {
136 string retType = "void";
137 string paramCls[] = { "int", "string", "int" };
139 void* paramObj[] = { &ports[0], &address, &rev };
141 rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj);
145 void TestClass_Stub::setA(int _int) {
149 string retType = "void";
150 string paramCls[] = { "int" };
151 void* paramObj[] = { &_int };
153 rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj);
157 void TestClass_Stub::setB(float _float) {
161 string retType = "void";
162 string paramCls[] = { "float" };
163 void* paramObj[] = { &_float };
165 rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj);
169 void TestClass_Stub::setC(string _string) {
173 string retType = "void";
174 string paramCls[] = { "string" };
175 void* paramObj[] = { &_string };
177 rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj);
181 string TestClass_Stub::sumArray(vector<string> newA) {
185 string retType = "string";
186 string paramCls[] = { "string[]" };
187 void* paramObj[] = { &newA };
189 void* retObj = &retVal;
190 rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj);
195 /*int64_t TestClass_Stub::sumArray(vector<int> newA) {
198 string sign = "sumArray(int[])";
199 string retType = "long";
200 string paramCls[] = { "int[]" };
201 void* paramObj[] = { &newA };
203 void* retObj = &retVal;
204 rmiCall->remoteCall(objectId, sign, retType, paramCls, paramObj, numParam, retObj);
210 int TestClass_Stub::setAndGetA(int newA) {
214 string retType = "int";
215 string paramCls[] = { "int" };
216 void* paramObj[] = { &newA };
218 void* retObj = &retVal;
219 rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj);
224 int TestClass_Stub::setACAndGetA(string newC, int newA) {
228 string retType = "int";
229 string paramCls[] = { "string", "int" };
230 void* paramObj[] = { &newC, &newA };
232 void* retObj = &retVal;
233 rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj);
238 void TestClass_Stub::registerCallback(CallBackInterface* _cb) {
240 //Should implement the callback here
244 void TestClass_Stub::registerCallback(vector<CallBackInterface*> _cb) {
246 for (CallBackInterface* cb: _cb) {
247 CallBack_CBSkeleton* skel = new CallBack_CBSkeleton(cb, objIdCnt++);
248 vecCBObj.push_back(skel);
253 string retType = "void";
254 string paramCls[] = { "int" };
255 int param1 = _cb.size();
256 void* paramObj[] = { ¶m1 };
258 rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj);
262 int TestClass_Stub::callBack() {
266 string retType = "int";
267 string paramCls[] = { };
268 void* paramObj[] = { };
270 void* retObj = &retVal;
271 rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj);
276 void TestClass_Stub::handleStruct(vector<data> vecData) {
280 string retType = "void";
281 string paramCls[] = { "int" };
282 int structsize = vecData.size();
283 void* paramObj[] = { &structsize };
285 rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj);
287 int numParam2 = 3*vecData.size();
289 string retType2 = "void";
290 string paramCls2[numParam2];
291 void* paramObj2[numParam2];
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++;
301 void* retObj2 = NULL;
302 rmiCall->remoteCall(objectId, methodId2, retType2, paramCls2, paramObj2, numParam2, retObj2);
306 vector<EnumC> TestClass_Stub::handleEnum(vector<EnumC> vecEn) {
310 string retType = "void";
311 string paramCls[] = { "int" };
312 int enumsize = vecEn.size();
313 void* paramObj[] = { &enumsize };
315 rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj);
317 int numParam2 = vecEn.size();
319 string retType2 = "int[]";
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 < numParam2; i++) {
325 paramCls2[i] = "int";
326 paramEnum[i] = (int) vecEn[i]; // cast enum to integer
327 paramObj2[i] = ¶mEnum[i];
329 vector<int> retEnumInt(numParam2);
330 // if no return value just
331 // void* retObj2 = NULL;
332 // This is with return value:
333 void* retObj2 = &retEnumInt;
334 rmiCall->remoteCall(objectId, methodId2, retType2, paramCls2, paramObj2, numParam2, retObj2);
335 int enumsize1 = retEnumInt.size();
336 vector<EnumC> retVal(enumsize1);
337 for (int i=0; i < enumsize1; i++) {
338 retVal[i] = (EnumC) retEnumInt[i];