X-Git-Url: http://plrg.eecs.uci.edu/git/?p=iot2.git;a=blobdiff_plain;f=iotjava%2Fiotrmi%2FC%2B%2B%2Fbasics%2FTestClassComplete_Stub.cpp;h=96827a2450efe4808d6df2f8449a9e424348a60e;hp=4003d89d07df99f9ae6e3bfd3768d39fcaec0123;hb=302042de817d92a659f4e3015ec56cd4e382ee44;hpb=4481cce5a5a9a852a9633287ed166ae0209c7d2e;ds=sidebyside diff --git a/iotjava/iotrmi/C++/basics/TestClassComplete_Stub.cpp b/iotjava/iotrmi/C++/basics/TestClassComplete_Stub.cpp index 4003d89..96827a2 100644 --- a/iotjava/iotrmi/C++/basics/TestClassComplete_Stub.cpp +++ b/iotjava/iotrmi/C++/basics/TestClassComplete_Stub.cpp @@ -8,34 +8,24 @@ using namespace std; -TestClassComplete_Stub::TestClassComplete_Stub(int _port, const char* _skeletonAddress, string _callbackAddress, int _rev, bool* _bResult, vector _ports) { - callbackAddress = _callbackAddress; - ports = _ports; - rmiCall = new IoTRMICall(_port, _skeletonAddress, _rev, _bResult); - set0Allowed.insert(-9998); - //thread th1 (&TestClassComplete_Stub::___initCallBack, this); - //th1.detach(); - ___regCB(); +TestClassComplete_Stub::TestClassComplete_Stub(int _portSend, int _portRecv, const char* _skeletonAddress, int _rev, bool* _bResult) { + rmiComm = new IoTRMICommClient(_portSend, _portRecv, _skeletonAddress, _rev, _bResult); + rmiComm->registerStub(objectId, 0, &retValueReceived0); + rmiComm->registerStub(objectId, 2, &retValueReceived2); + IoTRMIUtil::mapStub->insert(make_pair(objectId, this)); } -TestClassComplete_Stub::TestClassComplete_Stub(IoTRMICall* _rmiCall, string _callbackAddress, int _objIdCnt, vector _ports) { - callbackAddress = _callbackAddress; - rmiCall = _rmiCall; - objIdCnt = _objIdCnt; - set0Allowed.insert(-9998); - //thread th1 (&TestClassComplete_Stub::___initCallBack, this); - //th1.detach(); - ___regCB(); +TestClassComplete_Stub::TestClassComplete_Stub(IoTRMIComm* _rmiComm, int _objectId) { + rmiComm = _rmiComm; + objectId = _objectId; + rmiComm->registerStub(objectId, 0, &retValueReceived0); + rmiComm->registerStub(objectId, 2, &retValueReceived2); } TestClassComplete_Stub::~TestClassComplete_Stub() { - if (rmiCall != NULL) { - delete rmiCall; - rmiCall = NULL; - } - if (rmiObj != NULL) { - delete rmiObj; - rmiObj = NULL; + if (rmiComm != NULL) { + delete rmiComm; + rmiComm = NULL; } for(CallBackInterface* cb : vecCallbackObj) { delete cb; @@ -43,32 +33,42 @@ TestClassComplete_Stub::~TestClassComplete_Stub() { } } +mutex mtxMethodExec1; // TODO: We probably need to correlate this always with class name, e.g. methodExecCallBackInterfaceWithCallBack void TestClassComplete_Stub::registerCallback(CallBackInterface* _cb) { - //CallBackInterface_CallbackSkeleton* skel0 = new CallBackInterface_CallbackSkeleton(_cb, callbackAddress, objIdCnt++); - CallBackInterface_Skeleton* skel0 = new CallBackInterface_Skeleton(_cb, callbackAddress, objIdCnt++); - vecCallbackObj.push_back(skel0); - int ___paramCB0 = 1; + lock_guard guard(mtxMethodExec1); + int objIdSent = 0; + auto it = IoTRMIUtil::mapSkel->find(_cb); + if (it == IoTRMIUtil::mapSkel->end()) { // Not in the map, so new object + objIdSent = rmiComm->getObjectIdCounter(); + rmiComm->decrementObjectIdCounter(); + CallBackInterface_Skeleton* skel0 = new CallBackInterface_Skeleton(_cb, rmiComm, objIdSent); + vecCallbackObj.push_back(skel0); + IoTRMIUtil::mapSkel->insert(make_pair(_cb, skel0)); + IoTRMIUtil::mapSkelId->insert(make_pair(_cb, objIdSent)); + cout << "Create new skeleton for TestClass! ID=" << objIdSent << endl; + thread th0 (&CallBackInterface_Skeleton::___waitRequestInvokeMethod, std::ref(skel0), std::ref(skel0)); + th0.detach(); + while(!skel0->didInitWaitInvoke()); + } else { + auto itId = IoTRMIUtil::mapSkelId->find(_cb); + objIdSent = itId->second; + cout << "Skeleton exists for TestClass! ID=" << objIdSent << endl; + } + + int ___paramCB0 = objIdSent; int methodId = 1; string retType = "void"; int numParam = 1; string paramCls[] = { "int" }; void* paramObj[] = { &___paramCB0 }; void* retObj = NULL; - rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj); -} - -void TestClassComplete_Stub::___regCB() { - int numParam = 3; - int methodId = -9999; - string retType = "void"; - string paramCls[] = { "int*", "String", "int" }; - int rev = 0; - void* paramObj[] = { &ports, &callbackAddress, &rev }; - void* retObj = NULL; - rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj); + rmiComm->remoteCall(objectId, methodId, paramCls, paramObj, numParam); } -short TestClassComplete_Stub::getShort(short in) { +mutex mtxMethodExec0; // TODO: We probably need to correlate this always with class name, e.g. methodExecCallBackInterfaceWithCallBack +short TestClassComplete_Stub::getShort(short in) { + lock_guard guard(mtxMethodExec0); + cout << "getShort() is called!!!" << endl << endl; int methodId = 0; string retType = "short"; int numParam = 1; @@ -76,11 +76,23 @@ short TestClassComplete_Stub::getShort(short in) { void* paramObj[] = { &in }; short retVal = 0; void* retObj = &retVal; - rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj); + cout << "Calling remote call!" << endl; + rmiComm->remoteCall(objectId, methodId, paramCls, paramObj, numParam); + cout << "Finished calling remote call!" << endl; + // Waiting for return value + while(!retValueReceived0); + rmiComm->getReturnValue(retType, retObj); + //retValueReceived0.exchange(false); + retValueReceived0 = false; + didGetReturnBytes.exchange(true); + cout << "Getting return value for getShort(): " << retVal << endl; + return retVal; } -int TestClassComplete_Stub::callBack() { +mutex mtxMethodExec2; // TODO: We probably need to correlate this always with class name, e.g. methodExecCallBackInterfaceWithCallBack +int TestClassComplete_Stub::callBack() { + lock_guard guard(mtxMethodExec2); int methodId = 2; string retType = "int"; int numParam = 0; @@ -88,7 +100,16 @@ int TestClassComplete_Stub::callBack() { void* paramObj[] = { }; int retVal = 0; void* retObj = &retVal; - rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj); + rmiComm->remoteCall(objectId, methodId, paramCls, paramObj, numParam); + // Waiting for return value + while(!retValueReceived2); + rmiComm->getReturnValue(retType, retObj); + //retValueReceived2.exchange(false); + retValueReceived2 = false; + didGetReturnBytes.exchange(true); + + cout << "Getting return value for callback(): " << retVal << endl; + return retVal; } @@ -96,27 +117,46 @@ int TestClassComplete_Stub::callBack() { int main(int argc, char *argv[]) { - int port = 5010; + int portSend = 5000; + int portRecv = 6000; const char* address = "localhost"; //const char* address = "192.168.2.191"; // RPi2 - //const char* skeletonAddress = "128.195.136.170"; // dc-9.calit2.uci.edu - const char* skeletonAddress = "128.195.204.132"; + const char* skeletonAddress = "128.195.136.163"; // dc-2.calit2.uci.edu + //const char* skeletonAddress = "128.195.204.132"; const char* callbackAddress = "128.195.204.132"; // dw-2.eecs.uci.edu (this machine) //const char* skeletonAddress = "192.168.2.108"; // RPi1 //const char* callbackAddress = "192.168.2.191"; // RPi2 int rev = 0; bool bResult = false; - vector ports; - ports.push_back(12345); - ports.push_back(22346); + //vector ports; + //ports.push_back(12345); + //ports.push_back(22346); //ports.push_back(32344); //ports.push_back(43212); - TestClassComplete *tcStub = new TestClassComplete_Stub(port, skeletonAddress, callbackAddress, rev, &bResult, ports); + TestClassComplete *tcStub = new TestClassComplete_Stub(portSend, portRecv, skeletonAddress, rev, &bResult); + vector in; + in.push_back(68); + in.push_back(68); + vector result = tcStub->getByteArray(in); + cout << "Test print: " << in[0] << endl; +/* +// cout << "Getting return value from getShort(): " << tcStub->getShort(1234) << endl; + //cout << "Getting return value from getShort(): " << tcStub->getShort(4321) << endl; + //cout << "Getting return value from getShort(): " << tcStub->getShort(5678) << endl; cout << "==== CALLBACK ====" << endl; CallBackInterface *cbSingle = new CallBack(2354); tcStub->registerCallback(cbSingle); + //tcStub->registerCallback(cbSingle); + CallBackInterface *cbSingle1 = new CallBack(2646); + tcStub->registerCallback(cbSingle1); + CallBackInterface *cbSingle2 = new CallBack(2000); + tcStub->registerCallback(cbSingle2); cout << "Return value from callback: " << tcStub->callBack() << endl; + //cout << "Return value from callback: " << tcStub->callBack() << endl; + + // TODO: we need this while loop at the end to keep the threads running + while(true);*/ return 0; }