From 4481cce5a5a9a852a9633287ed166ae0209c7d2e Mon Sep 17 00:00:00 2001 From: rtrimana Date: Fri, 27 Jan 2017 16:11:35 -0800 Subject: [PATCH] Preparing C++ stubs and skeletons for circular compilation (it couldn't compile earlier) --- iotjava/Makefile | 22 +-- iotjava/iotrmi/C++/basics/CallBack.hpp | 2 - iotjava/iotrmi/C++/basics/TestClass.hpp | 2 +- .../C++/basics/TestClassCallbacks_Stub.cpp | 92 ++++++++++++- .../C++/basics/TestClassComplete_Stub.cpp | 122 +++++++++++++++++ .../basics/TestClassInterface_Skeleton.cpp | 129 +++++++++++++++++- 6 files changed, 353 insertions(+), 16 deletions(-) create mode 100644 iotjava/iotrmi/C++/basics/TestClassComplete_Stub.cpp diff --git a/iotjava/Makefile b/iotjava/Makefile index 01795d0..2afe366 100644 --- a/iotjava/Makefile +++ b/iotjava/Makefile @@ -115,15 +115,21 @@ compile: cd $(BIN_DIR)/iotpolicy/output_files/Java; $(JAVAC) -cp .:..:../../../$(BIN_DIR) TestClass_Skeleton.java # cd $(BIN_DIR)/iotpolicy/output_files/Java; $(JAVAC) -cp .:..:../../../$(BIN_DIR) TestClassAdvanced_Stub.java cd $(BIN_DIR)/iotpolicy/output_files/Java; $(JAVAC) -cp .:..:../../../$(BIN_DIR) TestClassCallbacks_Stub.java - cd $(BIN_DIR)/iotpolicy/output_files/Java; $(JAVAC) -cp .:..:../../../$(BIN_DIR) TestClassInterface*.java - cd $(BIN_DIR)/iotpolicy/output_files/Java; $(JAVAC) -cp .:..:../../../$(BIN_DIR) CallBackInterface*.java -# cd $(BIN_DIR)/iotpolicy/output_files/Java; $(JAVAC) -cp .:..:../../../$(BIN_DIR) TestClassComplete*.java + cd $(BIN_DIR)/iotpolicy/output_files/Java; $(JAVAC) -cp .:..:../../../$(BIN_DIR) TestClassInterface_Skeleton.java + cd $(BIN_DIR)/iotpolicy/output_files/Java; $(JAVAC) -cp .:..:../../../$(BIN_DIR) TestClassComplete_Stub.java + cd $(BIN_DIR)/iotpolicy/output_files/Java; $(JAVAC) -cp .:..:../../../$(BIN_DIR) CallBackInterfaceWithCallBack_Stub.java + cd $(BIN_DIR)/iotpolicy/output_files/Java; $(JAVAC) -cp .:..:../../../$(BIN_DIR) CallBackInterface_Skeleton.java # cd $(BIN_DIR)/iotpolicy/output_files/Java; $(JAVAC) -cp .:..:../../../$(BIN_DIR) CallBackInterface_CallbackSkeleton.java # cd $(BIN_DIR)/iotpolicy/output_files/Java; $(JAVAC) -cp .:..:../../../$(BIN_DIR) CallBackInterfaceWithCallBack_CallbackStub.java -# cd $(BIN_DIR)/iotpolicy/output_files/Cplus; $(G++) ./TestClass_Skeleton.cpp -o ./TestClass_Skeleton.out --std=c++11 -pthread -pg -I../../../../iotjava/iotrmi/C++/ + +# cd $(BIN_DIR)/iotpolicy/output_files/Cplus; $(G++) ./TestClassInterface_Skeleton.cpp -o ./TestClassInterface_Skeleton.out --std=c++11 -pthread -pg -I../../../../iotjava/iotrmi/C++/ +# cd $(BIN_DIR)/iotpolicy/output_files/Cplus; $(G++) ./TestClassComplete_Stub.cpp -o ./TestClassComplete_Stub.out --std=c++11 -pthread -pg -I../../../../iotjava/iotrmi/C++/ +# cp ./iotrmi/C++/ConcurrentLinkedListQueue.cpp $(BIN_DIR)/iotpolicy/output_files/Cplus +# cd $(BIN_DIR)/iotpolicy/output_files/Cplus; $(G++) ./ConcurrentLinkedListQueue.cpp -o ./ConcurrentLinkedListQueue.out --std=c++11 -pthread -pg -I../../../../iotjava/iotrmi/C++/ + #cd $(BIN_DIR)/iotpolicy/output_files/Cplus; $(G++) ./TestClassAdvanced_Stub.cpp -o ./TestClassAdvanced_Stub.out --std=c++11 -pthread -pg -I../../../../iotjava/iotrmi/C++/ -# cd $(BIN_DIR)/iotpolicy/output_files/Cplus; $(G++) ./TestClassCallbacks_Stub.cpp -o ./TestClassCallbacks_Stub.out --std=c++11 -pthread -pg -I../../../../iotjava/iotrmi/C++/ -# cd $(BIN_DIR)/iotpolicy/output_files/Cplus; $(G++) ./TestClass.hpp -o ./TestClass.out --std=c++11 -pthread -pg -I../../../../iotjava/iotrmi/C++/ + #cd $(BIN_DIR)/iotpolicy/output_files/Cplus; $(G++) ./TestClass.hpp -o ./TestClass.out --std=c++11 -pthread -pg -I../../../../iotjava/iotrmi/C++/ + #cd $(BIN_DIR)/iotpolicy/output_files/Cplus; $(G++) ./TestClassInterface_Skeleton.cpp -o ./TestClassInterface_Skeleton.out --std=c++11 -pthread -pg -I../../../../iotjava/iotrmi/C++/ #cd $(BIN_DIR)/iotpolicy/output_files/Cplus; $(ARM_G++) ./TestClassInterface_Skeleton.cpp -o ./TestClassInterface_Skeleton.out --std=c++11 -pthread -pg -I../../../../iotjava/iotrmi/C++/ #cd $(BIN_DIR)/iotpolicy/output_files/Cplus; $(ARM_G++) ./TestClassCallbacks_Stub.cpp -o ./TestClassCallbacks_Stub.out --std=c++11 -pthread -pg -I../../../../iotjava/iotrmi/C++/ #cd $(BIN_DIR)/iotpolicy/output_files/Cplus; $(ARM_G++) ./TestClassComplete_Stub.hpp --std=c++11 -pthread -pg -I../../../../iotjava/iotrmi/C++/ @@ -165,8 +171,8 @@ PHONY += rmi rmi: mkdir -p $(BIN_DIR) $(JAVAC) -cp .:../$(BIN_DIR) -d $(BIN_DIR) iotrmi/Java/*.java - $(JAVAC) -cp .:../$(BIN_DIR) -d $(BIN_DIR) iotrmi/Java/sample/*.java - mkdir -p $(BIN_DIR)/iotrmi/C++/sample + #$(JAVAC) -cp .:../$(BIN_DIR) -d $(BIN_DIR) iotrmi/Java/sample/*.java + #mkdir -p $(BIN_DIR)/iotrmi/C++/sample #$(G++) iotrmi/C++/sample/TestClass_Stub.cpp -o $(BIN_DIR)/iotrmi/C++/sample/TestClass_Stub.out --std=c++11 -pthread -pg #$(G++) iotrmi/C++/sample/TestClass_Skeleton.cpp -o $(BIN_DIR)/iotrmi/C++/sample/TestClass_Skeleton.out --std=c++11 -pthread -pg diff --git a/iotjava/iotrmi/C++/basics/CallBack.hpp b/iotjava/iotrmi/C++/basics/CallBack.hpp index a6e94c3..3aa71ae 100644 --- a/iotjava/iotrmi/C++/basics/CallBack.hpp +++ b/iotjava/iotrmi/C++/basics/CallBack.hpp @@ -2,8 +2,6 @@ #define _CALLBACK_HPP__ #include -#include "CallBackInterface.hpp" -#include "TestClassComplete.hpp" using namespace std; diff --git a/iotjava/iotrmi/C++/basics/TestClass.hpp b/iotjava/iotrmi/C++/basics/TestClass.hpp index cc6c854..e4918b5 100644 --- a/iotjava/iotrmi/C++/basics/TestClass.hpp +++ b/iotjava/iotrmi/C++/basics/TestClass.hpp @@ -146,7 +146,7 @@ int TestClass::callBack() { int sum = 0; for (CallBackInterfaceWithCallBack* cb : cbvec) { - cb->needCallback(this); + //cb->needCallback(this); sum = sum + cb->printInt(); } diff --git a/iotjava/iotrmi/C++/basics/TestClassCallbacks_Stub.cpp b/iotjava/iotrmi/C++/basics/TestClassCallbacks_Stub.cpp index f8fa236..4003d89 100644 --- a/iotjava/iotrmi/C++/basics/TestClassCallbacks_Stub.cpp +++ b/iotjava/iotrmi/C++/basics/TestClassCallbacks_Stub.cpp @@ -3,8 +3,96 @@ #include "TestClassComplete_Stub.hpp" #include "CallBack.hpp" +#include "CallBackInterface_Skeleton.cpp" + 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(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() { + if (rmiCall != NULL) { + delete rmiCall; + rmiCall = NULL; + } + if (rmiObj != NULL) { + delete rmiObj; + rmiObj = NULL; + } + for(CallBackInterface* cb : vecCallbackObj) { + delete cb; + cb = NULL; + } +} + +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; + 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); +} + +short TestClassComplete_Stub::getShort(short in) { + int methodId = 0; + string retType = "short"; + int numParam = 1; + string paramCls[] = { "short" }; + void* paramObj[] = { &in }; + short retVal = 0; + void* retObj = &retVal; + rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj); + return retVal; +} + +int TestClassComplete_Stub::callBack() { + int methodId = 2; + string retType = "int"; + int numParam = 0; + string paramCls[] = { }; + void* paramObj[] = { }; + int retVal = 0; + void* retObj = &retVal; + rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj); + return retVal; +} + + int main(int argc, char *argv[]) { @@ -21,8 +109,8 @@ int main(int argc, char *argv[]) vector ports; ports.push_back(12345); ports.push_back(22346); - ports.push_back(32344); - ports.push_back(43212); + //ports.push_back(32344); + //ports.push_back(43212); TestClassComplete *tcStub = new TestClassComplete_Stub(port, skeletonAddress, callbackAddress, rev, &bResult, ports); cout << "==== CALLBACK ====" << endl; diff --git a/iotjava/iotrmi/C++/basics/TestClassComplete_Stub.cpp b/iotjava/iotrmi/C++/basics/TestClassComplete_Stub.cpp new file mode 100644 index 0000000..4003d89 --- /dev/null +++ b/iotjava/iotrmi/C++/basics/TestClassComplete_Stub.cpp @@ -0,0 +1,122 @@ +#include +#include +#include "TestClassComplete_Stub.hpp" +#include "CallBack.hpp" + +#include "CallBackInterface_Skeleton.cpp" + +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(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() { + if (rmiCall != NULL) { + delete rmiCall; + rmiCall = NULL; + } + if (rmiObj != NULL) { + delete rmiObj; + rmiObj = NULL; + } + for(CallBackInterface* cb : vecCallbackObj) { + delete cb; + cb = NULL; + } +} + +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; + 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); +} + +short TestClassComplete_Stub::getShort(short in) { + int methodId = 0; + string retType = "short"; + int numParam = 1; + string paramCls[] = { "short" }; + void* paramObj[] = { &in }; + short retVal = 0; + void* retObj = &retVal; + rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj); + return retVal; +} + +int TestClassComplete_Stub::callBack() { + int methodId = 2; + string retType = "int"; + int numParam = 0; + string paramCls[] = { }; + void* paramObj[] = { }; + int retVal = 0; + void* retObj = &retVal; + rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj); + return retVal; +} + + +int main(int argc, char *argv[]) +{ + + int port = 5010; + 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* 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); + //ports.push_back(32344); + //ports.push_back(43212); + + TestClassComplete *tcStub = new TestClassComplete_Stub(port, skeletonAddress, callbackAddress, rev, &bResult, ports); + cout << "==== CALLBACK ====" << endl; + CallBackInterface *cbSingle = new CallBack(2354); + tcStub->registerCallback(cbSingle); + cout << "Return value from callback: " << tcStub->callBack() << endl; + + return 0; +} diff --git a/iotjava/iotrmi/C++/basics/TestClassInterface_Skeleton.cpp b/iotjava/iotrmi/C++/basics/TestClassInterface_Skeleton.cpp index 60ad5fd..0ad7586 100644 --- a/iotjava/iotrmi/C++/basics/TestClassInterface_Skeleton.cpp +++ b/iotjava/iotrmi/C++/basics/TestClassInterface_Skeleton.cpp @@ -3,12 +3,133 @@ #include "TestClassInterface_Skeleton.hpp" #include "TestClass.hpp" +#include "CallBackInterfaceWithCallBack_Stub.cpp" + using namespace std; +TestClassInterface_Skeleton::TestClassInterface_Skeleton(TestClassInterface *_mainObj, string _callbackAddress, int _port) { + bool _bResult = false; + mainObj = _mainObj; + callbackAddress = _callbackAddress; + rmiObj = new IoTRMIObject(_port, &_bResult); + set0Allowed.insert(-9999); + ___waitRequestInvokeMethod(); +} + +TestClassInterface_Skeleton::TestClassInterface_Skeleton(TestClassInterface *_mainObj, int _objIdCnt, string _callbackAddress) { + bool _bResult = false; + mainObj = _mainObj; + objIdCnt = _objIdCnt; + callbackAddress = _callbackAddress; + set0Allowed.insert(-9999); + ___waitRequestInvokeMethod(); +} + +TestClassInterface_Skeleton::~TestClassInterface_Skeleton() { + if (rmiObj != NULL) { + delete rmiObj; + rmiObj = NULL; + } + if (rmiCall != NULL) { + delete rmiCall; + rmiCall = NULL; + } + for(CallBackInterfaceWithCallBack* cb : vecCallbackObj) { + delete cb; + cb = NULL; + } +} + +short TestClassInterface_Skeleton::getShort(short in) { + return mainObj->getShort(in); +} + +void TestClassInterface_Skeleton::registerCallback(CallBackInterfaceWithCallBack* _cb) { + mainObj->registerCallback(_cb); +} + +void TestClassInterface_Skeleton::___regCB() { + int numParam = 3; + vector param1; + string param2 = ""; + int param3 = 0; + string paramCls[] = { "int*", "String", "int" }; + void* paramObj[] = { ¶m1, ¶m2, ¶m3 }; + rmiObj->getMethodParams(paramCls, numParam, paramObj); + bool bResult = false; + rmiCall = new IoTRMICall(param1[1], param2.c_str(), param3, &bResult); +} + +int TestClassInterface_Skeleton::callBack() { + return mainObj->callBack(); +} + +void TestClassInterface_Skeleton::___getShort() { + string paramCls[] = { "short" }; + int numParam = 1; + short in; + void* paramObj[] = { &in }; + rmiObj->getMethodParams(paramCls, numParam, paramObj); + short retVal = getShort(in); + void* retObj = &retVal; + rmiObj->sendReturnObj(retObj, "short"); +} + +void TestClassInterface_Skeleton::___registerCallback() { + string paramCls[] = { "int" }; + int numParam = 1; + int numStubs0 = 0; + void* paramObj[] = { &numStubs0 }; + rmiObj->getMethodParams(paramCls, numParam, paramObj); + //CallBackInterfaceWithCallBack* stub0 = new CallBackInterfaceWithCallBack_CallbackStub(rmiCall, callbackAddress, objIdCnt, ports); + CallBackInterfaceWithCallBack* stub0 = new CallBackInterfaceWithCallBack_Stub(rmiCall, callbackAddress, objIdCnt, ports); + vecCallbackObj.push_back(stub0); + objIdCnt++; + registerCallback(stub0); +} + +void TestClassInterface_Skeleton::___callBack() { + string paramCls[] = { }; + int numParam = 0; + void* paramObj[] = { }; + rmiObj->getMethodParams(paramCls, numParam, paramObj); + int retVal = callBack(); + void* retObj = &retVal; + rmiObj->sendReturnObj(retObj, "int"); +} + +void TestClassInterface_Skeleton::___waitRequestInvokeMethod() { + while (true) { + rmiObj->getMethodBytes(); + int _objectId = rmiObj->getObjectId(); + int methodId = rmiObj->getMethodId(); + if (_objectId == object0Id) { + if (set0Allowed.find(methodId) == set0Allowed.end()) { + cerr << "Object with object Id: " << _objectId << " is not allowed to access method: " << methodId << endl; + return; + } + } + else { + cerr << "Object Id: " << _objectId << " not recognized!" << endl; + return; + } + switch (methodId) { + case 0: ___getShort(); break; + case 1: ___registerCallback(); break; + case 2: ___callBack(); break; + case -9999: ___regCB(); break; + default: + cerr << "Method Id " << methodId << " not recognized!" << endl; + throw exception(); + } + } +} + + int main(int argc, char *argv[]) { // First argument is port number - int port = atoi(argv[1]); + /*int port = atoi(argv[1]); int argv2 = atoi(argv[2]); float argv3 = atof(argv[3]); string argv4 = string(argv[4]); @@ -16,10 +137,12 @@ int main(int argc, char *argv[]) cout << port << endl; cout << argv2 << endl; cout << argv3 << endl; - cout << argv4 << endl; + cout << argv4 << endl;*/ + int port = 5010; //TestClassInterface *tc = new TestClass(argv2, argv3, argv4); - //TestClassInterface_Skeleton *tcSkel = new TestClassInterface_Skeleton(tc, port); + TestClassInterface *tc = new TestClass(123, 2.345, "test"); + TestClassInterface_Skeleton *tcSkel = new TestClassInterface_Skeleton(tc, "localhost", port); //delete tc; //delete tcSkel; -- 2.34.1