3 #include "TestClassInterface_Skeleton.hpp"
4 #include "TestClass.hpp"
6 #include "CallBackInterfaceWithCallBack_Stub.cpp"
10 TestClassInterface_Skeleton::TestClassInterface_Skeleton(TestClassInterface *_mainObj, int _portSend, int _portRecv) {
11 bool _bResult = false;
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);
22 TestClassInterface_Skeleton::TestClassInterface_Skeleton(TestClassInterface *_mainObj, IoTRMIComm *_rmiComm, int _objectId) {
23 bool _bResult = false;
27 rmiComm->registerSkeleton(objectId, &methodReceived);
30 TestClassInterface_Skeleton::~TestClassInterface_Skeleton() {
31 if (rmiComm != NULL) {
35 for(CallBackInterfaceWithCallBack* cb : vecCallbackObj) {
41 bool TestClassInterface_Skeleton::didInitWaitInvoke() {
43 return didAlreadyInitWaitInvoke;
46 short TestClassInterface_Skeleton::getShort(short in) {
47 return mainObj->getShort(in);
50 void TestClassInterface_Skeleton::registerCallback(CallBackInterfaceWithCallBack* _cb) {
51 mainObj->registerCallback(_cb);
54 int TestClassInterface_Skeleton::callBack() {
55 return mainObj->callBack();
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" };
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;
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" };
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();
97 stub0 = (CallBackInterfaceWithCallBack_Stub*) it->second;
98 cout << "Stub exists for Callback! ID=" << objIdRecv << endl;
100 skel->vecCallbackObj.push_back(stub0);
101 skel->registerCallback(stub0);
102 delete[] localMethodBytes;
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[] = { };
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;
119 void TestClassInterface_Skeleton::___waitRequestInvokeMethod(TestClassInterface_Skeleton* skel) {
120 cout << "Running loop!" << endl;
121 //didAlreadyInitWaitInvoke.exchange(true);
122 skel->didAlreadyInitWaitInvoke = true;
126 skel->methodBytes = skel->rmiComm->getMethodBytes();
127 skel->methodLen = skel->rmiComm->getMethodLength();
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;
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;
150 cerr << "Method Id " << methodId << " not recognized!" << endl;
154 cout << "Out of switch statement!" << endl;
160 int main(int argc, char *argv[])
162 // First argument is port number
163 /*int port = atoi(argv[1]);
164 int argv2 = atoi(argv[2]);
165 float argv3 = atof(argv[3]);
166 string argv4 = string(argv[4]);
168 cout << port << endl;
169 cout << argv2 << endl;
170 cout << argv3 << endl;
171 cout << argv4 << endl;*/
175 //TestClassInterface *tc = new TestClass(argv2, argv3, argv4);
176 TestClassInterface *tc = new TestClass(123, 2.345, "test");
177 TestClassInterface_Skeleton *tcSkel = new TestClassInterface_Skeleton(tc, portSend, portRecv);