3 #include "TestClassComplete_Stub.hpp"
4 #include "CallBack.hpp"
6 #include "CallBackInterface_Skeleton.cpp"
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));
18 TestClassComplete_Stub::TestClassComplete_Stub(IoTRMIComm* _rmiComm, int _objectId) {
21 rmiComm->registerStub(objectId, 0, &retValueReceived0);
22 rmiComm->registerStub(objectId, 2, &retValueReceived2);
25 TestClassComplete_Stub::~TestClassComplete_Stub() {
26 if (rmiComm != NULL) {
30 for(CallBackInterface* cb : vecCallbackObj) {
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);
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));
51 while(!skel0->didInitWaitInvoke());
53 auto itId = IoTRMIUtil::mapSkelId->find(_cb);
54 objIdSent = itId->second;
55 cout << "Skeleton exists for TestClass! ID=" << objIdSent << endl;
58 int ___paramCB0 = objIdSent;
60 string retType = "void";
62 string paramCls[] = { "int" };
63 void* paramObj[] = { &___paramCB0 };
65 rmiComm->remoteCall(objectId, methodId, paramCls, paramObj, numParam);
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;
73 string retType = "short";
75 string paramCls[] = { "short" };
76 void* paramObj[] = { &in };
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;
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);
97 string retType = "int";
99 string paramCls[] = { };
100 void* paramObj[] = { };
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);
111 cout << "Getting return value for callback(): " << retVal << endl;
117 int main(int argc, char *argv[])
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
130 bool bResult = false;
132 //ports.push_back(12345);
133 //ports.push_back(22346);
134 //ports.push_back(32344);
135 //ports.push_back(43212);
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;
152 // TODO: we need this while loop at the end to keep the threads running