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
39 //CallBackInterface cb;
44 vector<CallBackInterface*> vecCBObj;
46 int objectId = 0; // Default value is 0
51 int TestClass_Stub::objIdCnt = 0;
54 TestClass_Stub::TestClass_Stub() {
61 TestClass_Stub::TestClass_Stub(int _port, const char* _address, int _rev, bool* _bResult, vector<int> _ports) {
64 rmiCall = new IoTRMICall(_port, _address, _rev, _bResult);
67 thread th1 (&TestClass_Stub::____init_CallBack, this);
70 ____registerCallBack();
74 TestClass_Stub::~TestClass_Stub() {
76 if (rmiCall != NULL) {
84 for(CallBackInterface* cb : vecCBObj) {
91 // Callback handler thread
92 void TestClass_Stub::____init_CallBack() {
95 rmiObj = new IoTRMIObject(ports[0], &bResult);
97 char* method = rmiObj->getMethodBytes();
98 int objId = IoTRMIObject::getObjectId(method);
99 if (objId < vecCBObj.size()) { // Check if still within range
100 CallBack_CBSkeleton* skel =
101 dynamic_cast<CallBack_CBSkeleton*> (vecCBObj.at(objId));
102 skel->invokeMethod(rmiObj);
104 string error = "TestClass_Stub: Illegal object Id: " + to_string(objId);
111 // Notify that callback thread is ready
112 void TestClass_Stub::____registerCallBack() {
116 string retType = "void";
117 string paramCls[] = { "int", "string", "int" };
119 void* paramObj[] = { &ports[0], &address, &rev };
121 rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj);
125 void TestClass_Stub::setA(int _int) {
129 string retType = "void";
130 string paramCls[] = { "int" };
131 void* paramObj[] = { &_int };
133 rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj);
137 void TestClass_Stub::setB(float _float) {
141 string retType = "void";
142 string paramCls[] = { "float" };
143 void* paramObj[] = { &_float };
145 rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj);
149 void TestClass_Stub::setC(string _string) {
153 string retType = "void";
154 string paramCls[] = { "string" };
155 void* paramObj[] = { &_string };
157 rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj);
161 string TestClass_Stub::sumArray(vector<string> newA) {
165 string retType = "string";
166 string paramCls[] = { "string[]" };
167 void* paramObj[] = { &newA };
169 void* retObj = &retVal;
170 rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj);
175 /*int64_t TestClass_Stub::sumArray(vector<int> newA) {
178 string sign = "sumArray(int[])";
179 string retType = "long";
180 string paramCls[] = { "int[]" };
181 void* paramObj[] = { &newA };
183 void* retObj = &retVal;
184 rmiCall->remoteCall(objectId, sign, retType, paramCls, paramObj, numParam, retObj);
190 int TestClass_Stub::setAndGetA(int newA) {
194 string retType = "int";
195 string paramCls[] = { "int" };
196 void* paramObj[] = { &newA };
198 void* retObj = &retVal;
199 rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj);
204 int TestClass_Stub::setACAndGetA(string newC, int newA) {
208 string retType = "int";
209 string paramCls[] = { "string", "int" };
210 void* paramObj[] = { &newC, &newA };
212 void* retObj = &retVal;
213 rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj);
218 void TestClass_Stub::registerCallback(CallBackInterface* _cb) {
220 //Should implement the callback here
224 void TestClass_Stub::registerCallback(vector<CallBackInterface*> _cb) {
226 for (CallBackInterface* cb: _cb) {
227 CallBack_CBSkeleton* skel = new CallBack_CBSkeleton(cb, objIdCnt++);
228 vecCBObj.push_back(skel);
233 string retType = "void";
234 string paramCls[] = { "int" };
235 int param1 = _cb.size();
236 void* paramObj[] = { ¶m1 };
238 rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj);
242 int TestClass_Stub::callBack() {
246 string retType = "int";
247 string paramCls[] = { };
248 void* paramObj[] = { };
250 void* retObj = &retVal;
251 rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj);
256 void TestClass_Stub::handleStruct(vector<data> vecData) {
260 string retType = "void";
261 string paramCls[] = { "int" };
262 int structsize = vecData.size();
263 void* paramObj[] = { &structsize };
265 rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj);
267 int numParam2 = 3*vecData.size();
269 string retType2 = "void";
270 string paramCls2[numParam2];
271 void* paramObj2[numParam2];
273 for(int i = 0; i < vecData.size(); i++) {
274 paramCls2[pos] = "string";
275 paramObj2[pos] = &vecData[i].name; pos++;
276 paramCls2[pos] = "float";
277 paramObj2[pos] = &vecData[i].value; pos++;
278 paramCls2[pos] = "int";
279 paramObj2[pos] = &vecData[i].year; pos++;
281 void* retObj2 = NULL;
282 rmiCall->remoteCall(objectId, methodId2, retType2, paramCls2, paramObj2, numParam2, retObj2);
286 vector<EnumC> TestClass_Stub::handleEnum(vector<EnumC> vecEn) {
289 int numEl = vecEn.size();
291 string retType = "int[]";
292 string paramCls[] = { "int[]" };
293 // Need to define this container for integer version of enum
294 vector<int> paramInt(numEl);
295 for(int i = 0; i < numEl; i++) {
296 paramInt[i] = (int) vecEn[i]; // cast enum to integer
298 void* paramObj[] = { ¶mInt };
299 // if no return value just
300 // void* retObj2 = NULL;
301 // This is with return value:
302 vector<int> retEnumInt;
303 void* retObj = &retEnumInt;
304 rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj);
305 int enumsize1 = retEnumInt.size();
306 vector<EnumC> retVal(enumsize1);
307 for (int i=0; i < enumsize1; i++) {
308 retVal[i] = (EnumC) retEnumInt[i];