34e47092ae8f52c353a7f8db340903f9aadc0cd6
[iot2.git] / iotjava / iotrmi / C++ / basics / TestClassInterface_Skeleton.cpp
1 #include <iostream>
2 #include <string>
3 #include "TestClassInterface_Skeleton.hpp"
4 #include "TestClass.hpp"
5
6 #include "CallBackInterfaceWithCallBack_Stub.cpp"
7
8 using namespace std;
9
10 TestClassInterface_Skeleton::TestClassInterface_Skeleton(TestClassInterface *_mainObj, int _portSend, int _portRecv) {
11         bool _bResult = false;
12         mainObj = _mainObj;
13         rmiComm = new IoTRMICommServer(_portSend, _portRecv, &_bResult);
14         IoTRMIUtil::mapSkel->insert(make_pair(_mainObj, this));
15         IoTRMIUtil::mapSkelId->insert(make_pair(_mainObj, objectId));
16         rmiComm->registerSkeleton(objectId, &methodReceived);
17         thread th1 (&TestClassInterface_Skeleton::___waitRequestInvokeMethod, this, this);
18 //      th1.detach();
19         th1.join();
20 }
21
22 TestClassInterface_Skeleton::TestClassInterface_Skeleton(TestClassInterface *_mainObj, IoTRMIComm *_rmiComm, int _objectId) {
23         bool _bResult = false;
24         mainObj = _mainObj;
25         rmiComm = _rmiComm;
26         objectId = _objectId;
27         rmiComm->registerSkeleton(objectId, &methodReceived);
28 }
29
30 TestClassInterface_Skeleton::~TestClassInterface_Skeleton() {
31         if (rmiComm != NULL) {
32                 delete rmiComm;
33                 rmiComm = NULL;
34         }
35         for(CallBackInterfaceWithCallBack* cb : vecCallbackObj) {
36                 delete cb;
37                 cb = NULL;
38         }
39 }
40
41 bool TestClassInterface_Skeleton::didInitWaitInvoke() {
42
43         return didAlreadyInitWaitInvoke;
44 }
45
46 short TestClassInterface_Skeleton::getShort(short in) {
47         return mainObj->getShort(in);
48 }
49
50 void TestClassInterface_Skeleton::registerCallback(CallBackInterfaceWithCallBack* _cb) {
51         mainObj->registerCallback(_cb);
52 }
53
54 int TestClassInterface_Skeleton::callBack() {
55         return mainObj->callBack();
56 }
57
58 void TestClassInterface_Skeleton::___getShort(TestClassInterface_Skeleton* skel) {
59         char* localMethodBytes = new char[methodLen];
60         memcpy(localMethodBytes, skel->methodBytes, methodLen);
61         //cout << "Bytes inside getShort: " << endl;
62         //IoTRMIUtil::printBytes(localMethodBytes, methodLen, false);
63         didGetMethodBytes.exchange(true);
64         string paramCls[] = { "short" };
65         int numParam = 1;
66         short in;
67         void* paramObj[] = { &in };
68         skel->rmiComm->getMethodParams(paramCls, numParam, paramObj, localMethodBytes);
69         short retVal = getShort(in);
70         cout << "Getting return value getShort(): " << retVal << endl;
71         void* retObj = &retVal;
72         skel->rmiComm->sendReturnObj(retObj, "short", localMethodBytes);
73         cout << "Sent return value for getShort()" << endl;
74         delete[] localMethodBytes;
75 }
76
77 void TestClassInterface_Skeleton::___registerCallback(TestClassInterface_Skeleton* skel) {
78         char* localMethodBytes = new char[methodLen];
79         memcpy(localMethodBytes, skel->methodBytes, methodLen);
80         didGetMethodBytes.exchange(true);
81         string paramCls[] = { "int" };
82         int numParam = 1;
83         int numStubs0 = 0;
84         void* paramObj[] = { &numStubs0 };
85         skel->rmiComm->getMethodParams(paramCls, numParam, paramObj, localMethodBytes);
86         // Choosing the right stub
87         int objIdRecv = numStubs0;
88         CallBackInterfaceWithCallBack* stub0 = NULL;
89         auto it = IoTRMIUtil::mapStub->find(objIdRecv);
90         if (it == IoTRMIUtil::mapStub->end()) { // Not in the map, so new object
91                 stub0 = new CallBackInterfaceWithCallBack_Stub(rmiComm, objIdRecv);
92                 IoTRMIUtil::mapStub->insert(make_pair(objIdRecv, stub0));
93                 cout << "Create new stub for Callback! ID=" << objIdRecv << endl;
94                 rmiComm->setObjectIdCounter(objIdRecv);
95                 rmiComm->decrementObjectIdCounter();
96         } else {
97                 stub0 = (CallBackInterfaceWithCallBack_Stub*) it->second;
98                 cout << "Stub exists for Callback! ID=" << objIdRecv << endl;
99         }
100         skel->vecCallbackObj.push_back(stub0);
101         skel->registerCallback(stub0);
102         delete[] localMethodBytes;
103 }
104
105 void TestClassInterface_Skeleton::___callBack(TestClassInterface_Skeleton* skel) {
106         char* localMethodBytes = new char[methodLen];
107         memcpy(localMethodBytes, skel->methodBytes, methodLen);
108         didGetMethodBytes.exchange(true);
109         string paramCls[] = {  };
110         int numParam = 0;
111         void* paramObj[] = {  };
112         skel->rmiComm->getMethodParams(paramCls, numParam, paramObj, localMethodBytes);
113         int retVal = callBack();
114         void* retObj = &retVal;
115         skel->rmiComm->sendReturnObj(retObj, "int", localMethodBytes);
116         delete[] localMethodBytes;
117 }
118
119 void TestClassInterface_Skeleton::___waitRequestInvokeMethod(TestClassInterface_Skeleton* skel) {
120         cout << "Running loop!" << endl;
121         //didAlreadyInitWaitInvoke.exchange(true);
122         skel->didAlreadyInitWaitInvoke = true;
123         while (true) {
124                 if (!methodReceived)
125                         continue;
126                 skel->methodBytes = skel->rmiComm->getMethodBytes();
127                 skel->methodLen = skel->rmiComm->getMethodLength();
128                 cout << endl;
129                 // TODO: Get method length as well!!!
130                 //methodReceived.exchange(false);
131                 methodReceived = false;
132                 int _objectId = skel->rmiComm->getObjectId(skel->methodBytes);
133                 int methodId = skel->rmiComm->getMethodId(skel->methodBytes);
134                 if (_objectId == objectId) {
135                         if (skel->set0Allowed.find(methodId) == skel->set0Allowed.end()) {
136                                 cerr << "Object with object Id: " << _objectId << "  is not allowed to access method: " << methodId << endl;
137                                 return;
138                         }
139                 }
140                 else
141                         continue;
142                 switch (methodId) {
143                         case 0: { thread th0 (&TestClassInterface_Skeleton::___getShort, std::ref(skel), skel); th0.detach(); break; }
144                                         //___getShort(skel); break;
145                         case 1: { thread th1 (&TestClassInterface_Skeleton::___registerCallback, std::ref(skel), skel); th1.detach(); break; }
146                                         //___registerCallback(skel); break;
147                         case 2: { thread th2 (&TestClassInterface_Skeleton::___callBack, std::ref(skel), skel); th2.detach(); break; }
148                                         //___callBack(skel); break;
149                         default: 
150                         cerr << "Method Id " << methodId << " not recognized!" << endl;
151                         return;
152                 }
153                 cout << "Out of switch statement!" << endl;
154         }
155 }
156
157
158
159 int main(int argc, char *argv[])
160 {
161         // First argument is port number
162         /*int port = atoi(argv[1]);
163         int argv2 = atoi(argv[2]);
164         float argv3 = atof(argv[3]);
165         string argv4 = string(argv[4]);
166
167         cout << port << endl;
168         cout << argv2 << endl;
169         cout << argv3 << endl;
170         cout << argv4 << endl;*/
171
172         int portSend = 5000;
173         int portRecv = 6000;
174         //TestClassInterface *tc = new TestClass(argv2, argv3, argv4);
175         TestClassInterface *tc = new TestClass(123, 2.345, "test");
176         TestClassInterface_Skeleton *tcSkel = new TestClassInterface_Skeleton(tc, portSend, portRecv);
177
178         //delete tc;
179         //delete tcSkel;
180         return 0;
181 }