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 void ____init_CallBack(); // thread
32 void ____registerCallBack(); // tell the other side that we are ready
34 const static int size = 12;
35 const static string methodSignatures[size];
41 //CallBackInterface cb;
46 vector<CallBackInterface*> vecCBObj;
48 int objectId = 0; // Default value is 0
53 int TestClass_Stub::objIdCnt = 0;
56 const string TestClass_Stub::methodSignatures[TestClass_Stub::size] = {
63 "intsetACAndGetA(string,int)",
65 "voidregisterCallBack(CallBackInterface)",
66 "voidregisterCallBack(CallBackInterface[])",
68 "handleStruct(StructJ[])",
73 TestClass_Stub::TestClass_Stub() {
80 TestClass_Stub::TestClass_Stub(int _port, const char* _address, int _rev, bool* _bResult, vector<int> _ports) {
83 rmiCall = new IoTRMICall(_port, _address, _rev, _bResult, methodSignatures, size);
86 thread th1 (&TestClass_Stub::____init_CallBack, this);
89 ____registerCallBack();
93 TestClass_Stub::~TestClass_Stub() {
95 if (rmiCall != NULL) {
103 for(CallBackInterface* cb : vecCBObj) {
110 // Callback handler thread
111 void TestClass_Stub::____init_CallBack() {
113 bool bResult = false;
114 rmiObj = new IoTRMIObject(ports[0], &bResult, CallBack_CBSkeleton::methodSignatures, CallBack_CBSkeleton::size);
116 char* method = rmiObj->getMethodBytes();
117 int objId = IoTRMIObject::getObjectId(method);
118 if (objId < vecCBObj.size()) { // Check if still within range
119 CallBack_CBSkeleton* skel =
120 dynamic_cast<CallBack_CBSkeleton*> (vecCBObj.at(objId));
121 skel->invokeMethod(rmiObj);
123 string error = "TestClass_Stub: Illegal object Id: " + to_string(objId);
130 // Notify that callback thread is ready
131 void TestClass_Stub::____registerCallBack() {
134 string sign = "registercallback";
135 string retType = "void";
136 string paramCls[] = { "int", "string", "int" };
138 void* paramObj[] = { &ports[0], &address, &rev };
140 rmiCall->remoteCall(objectId, sign, retType, paramCls, paramObj, numParam, retObj);
144 void TestClass_Stub::setA(int _int) {
147 string sign = "voidsetA(int)";
148 string retType = "void";
149 string paramCls[] = { "int" };
150 void* paramObj[] = { &_int };
152 rmiCall->remoteCall(objectId, sign, retType, paramCls, paramObj, numParam, retObj);
156 void TestClass_Stub::setB(float _float) {
159 string sign = "voidsetB(float)";
160 string retType = "void";
161 string paramCls[] = { "float" };
162 void* paramObj[] = { &_float };
164 rmiCall->remoteCall(objectId, sign, retType, paramCls, paramObj, numParam, retObj);
168 void TestClass_Stub::setC(string _string) {
171 string sign = "voidsetC(string)";
172 string retType = "void";
173 string paramCls[] = { "string" };
174 void* paramObj[] = { &_string };
176 rmiCall->remoteCall(objectId, sign, retType, paramCls, paramObj, numParam, retObj);
180 string TestClass_Stub::sumArray(vector<string> newA) {
183 string sign = "sumArray(string[])";
184 string retType = "string";
185 string paramCls[] = { "string[]" };
186 void* paramObj[] = { &newA };
188 void* retObj = &retVal;
189 rmiCall->remoteCall(objectId, sign, retType, paramCls, paramObj, numParam, retObj);
194 /*int64_t TestClass_Stub::sumArray(vector<int> newA) {
197 string sign = "sumArray(int[])";
198 string retType = "long";
199 string paramCls[] = { "int[]" };
200 void* paramObj[] = { &newA };
202 void* retObj = &retVal;
203 rmiCall->remoteCall(objectId, sign, retType, paramCls, paramObj, numParam, retObj);
209 int TestClass_Stub::setAndGetA(int newA) {
212 string sign = "intsetAndGetA(int)";
213 string retType = "int";
214 string paramCls[] = { "int" };
215 void* paramObj[] = { &newA };
217 void* retObj = &retVal;
218 rmiCall->remoteCall(objectId, sign, retType, paramCls, paramObj, numParam, retObj);
223 int TestClass_Stub::setACAndGetA(string newC, int newA) {
226 string sign = "intsetACAndGetA(string,int)";
227 string retType = "int";
228 string paramCls[] = { "string", "int" };
229 void* paramObj[] = { &newC, &newA };
231 void* retObj = &retVal;
232 rmiCall->remoteCall(objectId, sign, retType, paramCls, paramObj, numParam, retObj);
237 void TestClass_Stub::registerCallback(CallBackInterface* _cb) {
239 //Should implement the callback here
243 void TestClass_Stub::registerCallback(vector<CallBackInterface*> _cb) {
245 for (CallBackInterface* cb: _cb) {
246 CallBack_CBSkeleton* skel = new CallBack_CBSkeleton(cb, objIdCnt++);
247 vecCBObj.push_back(skel);
251 string sign = "voidregisterCallBack(CallBackInterface[])";
252 string retType = "void";
253 string paramCls[] = { "int" };
254 int param1 = _cb.size();
255 void* paramObj[] = { ¶m1 };
257 rmiCall->remoteCall(objectId, sign, retType, paramCls, paramObj, numParam, retObj);
261 int TestClass_Stub::callBack() {
264 string sign = "intcallBack()";
265 string retType = "int";
266 string paramCls[] = { };
267 void* paramObj[] = { };
269 void* retObj = &retVal;
270 rmiCall->remoteCall(objectId, sign, retType, paramCls, paramObj, numParam, retObj);
275 void TestClass_Stub::handleStruct(vector<data> vecData) {
278 string sign = "structsize";
279 string retType = "void";
280 string paramCls[] = { "int" };
281 int structsize = vecData.size();
282 void* paramObj[] = { &structsize };
284 rmiCall->remoteCall(objectId, sign, retType, paramCls, paramObj, numParam, retObj);
286 int numParam2 = 3*vecData.size();
287 string sign2 = "handleStruct(StructJ[])";
288 string retType2 = "void";
289 string paramCls2[numParam2];
290 void* paramObj2[numParam2];
292 for(int i = 0; i < vecData.size(); i++) {
293 paramCls2[pos] = "string";
294 paramObj2[pos] = &vecData[i].name; pos++;
295 paramCls2[pos] = "float";
296 paramObj2[pos] = &vecData[i].value; pos++;
297 paramCls2[pos] = "int";
298 paramObj2[pos] = &vecData[i].year; pos++;
300 void* retObj2 = NULL;
301 cout << "In handle struct 3!" << endl;
302 rmiCall->remoteCall(objectId, sign2, retType2, paramCls2, paramObj2, numParam2, retObj2);