Fixed compiler for Java code generation (not heavily tested yet, but fixes include...
[iot2.git] / iotjava / iotrmi / C++ / basics / TestClassComplete_Stub.cpp
1 #include <iostream>
2 #include <string>
3 #include "TestClassComplete_Stub.hpp"
4 #include "CallBack.hpp"
5
6 #include "CallBackInterface_Skeleton.cpp"
7
8 using namespace std;
9
10
11 TestClassComplete_Stub::TestClassComplete_Stub(int _portSend, int _portRecv, const char* _skeletonAddress, int _rev, bool* _bResult) {
12         rmiComm = new IoTRMICommClient(_portSend, _portRecv, _skeletonAddress, _rev, _bResult);
13         rmiComm->registerStub(objectId, 0, &retValueReceived0);
14         rmiComm->registerStub(objectId, 2, &retValueReceived2);
15         IoTRMIUtil::mapStub->insert(make_pair(objectId, this));
16 }
17
18 TestClassComplete_Stub::TestClassComplete_Stub(IoTRMIComm* _rmiComm, int _objectId) {
19         rmiComm = _rmiComm;
20         objectId = _objectId;
21         rmiComm->registerStub(objectId, 0, &retValueReceived0);
22         rmiComm->registerStub(objectId, 2, &retValueReceived2);
23 }
24
25 TestClassComplete_Stub::~TestClassComplete_Stub() {
26         if (rmiComm != NULL) {
27                 delete rmiComm;
28                 rmiComm = NULL;
29         }
30         for(CallBackInterface* cb : vecCallbackObj) {
31                 delete cb;
32                 cb = NULL;
33         }
34 }
35
36 mutex mtxMethodExec1;   // TODO: We probably need to correlate this always with class name, e.g. methodExecCallBackInterfaceWithCallBack
37 void TestClassComplete_Stub::registerCallback(CallBackInterface* _cb) { 
38         lock_guard<mutex> guard(mtxMethodExec1);
39         int objIdSent = 0;
40         auto it = IoTRMIUtil::mapSkel->find(_cb);
41         if (it == IoTRMIUtil::mapSkel->end()) { // Not in the map, so new object
42                 objIdSent = rmiComm->getObjectIdCounter();
43                 rmiComm->decrementObjectIdCounter();
44                 CallBackInterface_Skeleton* skel0 = new CallBackInterface_Skeleton(_cb, rmiComm, objIdSent);
45                 vecCallbackObj.push_back(skel0);
46                 IoTRMIUtil::mapSkel->insert(make_pair(_cb, skel0));
47                 IoTRMIUtil::mapSkelId->insert(make_pair(_cb, objIdSent));
48                 cout << "Create new skeleton for TestClass! ID=" << objIdSent << endl;
49                 thread th0 (&CallBackInterface_Skeleton::___waitRequestInvokeMethod, std::ref(skel0), std::ref(skel0));  
50                 th0.detach();
51                 while(!skel0->didInitWaitInvoke());
52         } else {
53                 auto itId = IoTRMIUtil::mapSkelId->find(_cb);
54                 objIdSent = itId->second;
55                 cout << "Skeleton exists for TestClass! ID=" << objIdSent << endl;
56         }
57
58         int ___paramCB0 = objIdSent;
59         int methodId = 1;
60         string retType = "void";
61         int numParam = 1;
62         string paramCls[] = { "int" };
63         void* paramObj[] = { &___paramCB0 };
64         void* retObj = NULL;
65         rmiComm->remoteCall(objectId, methodId, paramCls, paramObj, numParam);
66 }
67
68 mutex mtxMethodExec0;   // TODO: We probably need to correlate this always with class name, e.g. methodExecCallBackInterfaceWithCallBack
69 short TestClassComplete_Stub::getShort(short in) {
70         lock_guard<mutex> guard(mtxMethodExec0);
71         cout << "getShort() is called!!!" << endl << endl;
72         int methodId = 0;
73         string retType = "short";
74         int numParam = 1;
75         string paramCls[] = { "short" };
76         void* paramObj[] = { &in };
77         short retVal = 0;
78         void* retObj = &retVal;
79         cout << "Calling remote call!" << endl;
80         rmiComm->remoteCall(objectId, methodId, paramCls, paramObj, numParam);
81         cout << "Finished calling remote call!" << endl;
82         // Waiting for return value
83         while(!retValueReceived0);
84         rmiComm->getReturnValue(retType, retObj);
85         //retValueReceived0.exchange(false);
86         retValueReceived0 = false;
87         didGetReturnBytes.exchange(true);
88         cout << "Getting return value for getShort(): " << retVal << endl;
89
90         return retVal;
91 }
92
93 mutex mtxMethodExec2;   // TODO: We probably need to correlate this always with class name, e.g. methodExecCallBackInterfaceWithCallBack
94 int TestClassComplete_Stub::callBack() {
95         lock_guard<mutex> guard(mtxMethodExec2);
96         int methodId = 2;
97         string retType = "int";
98         int numParam = 0;
99         string paramCls[] = {  };
100         void* paramObj[] = {  };
101         int retVal = 0;
102         void* retObj = &retVal;
103         rmiComm->remoteCall(objectId, methodId, paramCls, paramObj, numParam);
104         // Waiting for return value
105         while(!retValueReceived2);
106         rmiComm->getReturnValue(retType, retObj);
107         //retValueReceived2.exchange(false);
108         retValueReceived2 = false;
109         didGetReturnBytes.exchange(true);
110
111         cout << "Getting return value for callback(): " << retVal << endl;
112
113         return retVal;
114 }
115
116
117 int main(int argc, char *argv[])
118 {
119
120         int portSend = 5000;
121         int portRecv = 6000;
122         const char* address = "localhost";
123         //const char* address = "192.168.2.191";        // RPi2
124         //const char* skeletonAddress = "128.195.136.170";      // dc-9.calit2.uci.edu
125         const char* skeletonAddress = "128.195.204.132";
126         const char* callbackAddress = "128.195.204.132";        // dw-2.eecs.uci.edu (this machine)
127         //const char* skeletonAddress = "192.168.2.108";        // RPi1
128         //const char* callbackAddress = "192.168.2.191";        // RPi2
129         int rev = 0;
130         bool bResult = false;
131         //vector<int> ports;
132         //ports.push_back(12345);
133         //ports.push_back(22346);
134         //ports.push_back(32344);
135         //ports.push_back(43212);
136
137         TestClassComplete *tcStub = new TestClassComplete_Stub(portSend, portRecv, skeletonAddress, rev, &bResult);
138 //      cout << "Getting return value from getShort(): " << tcStub->getShort(1234) << endl;
139         //cout << "Getting return value from getShort(): " << tcStub->getShort(4321) << endl;
140         //cout << "Getting return value from getShort(): " << tcStub->getShort(5678) << endl;
141         cout << "==== CALLBACK ====" << endl;
142         CallBackInterface *cbSingle = new CallBack(2354);
143         tcStub->registerCallback(cbSingle);
144         //tcStub->registerCallback(cbSingle);
145         CallBackInterface *cbSingle1 = new CallBack(2646);
146         tcStub->registerCallback(cbSingle1);
147         CallBackInterface *cbSingle2 = new CallBack(2000);
148         tcStub->registerCallback(cbSingle2);
149         cout << "Return value from callback: " << tcStub->callBack() << endl;
150         //cout << "Return value from callback: " << tcStub->callBack() << endl;
151
152         // TODO: we need this while loop at the end to keep the threads running
153         while(true);
154
155         return 0;
156 }