From: rtrimana Date: Thu, 3 Nov 2016 18:28:18 +0000 (-0700) Subject: Adding object ID and tons of minor adjustments for callback support X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=5b5142548aabbcea7859766d4a58dfa979ce4928;p=iot2.git Adding object ID and tons of minor adjustments for callback support --- diff --git a/iotjava/Makefile b/iotjava/Makefile index d9814d8..3d7b7f0 100644 --- a/iotjava/Makefile +++ b/iotjava/Makefile @@ -31,17 +31,26 @@ runtime: 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 +# $(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++ #$(G++) iotrmi/C++/IoTSocketServer.cpp -o $(BIN_DIR)/iotrmi/C++/IoTSocketServer.out #$(G++) iotrmi/C++/IoTSocketClient.cpp -o $(BIN_DIR)/iotrmi/C++/IoTSocketClient.out # $(G++) iotrmi/C++/IoTRMICall.cpp -o $(BIN_DIR)/iotrmi/C++/IoTRMICall.out --std=c++11 # $(G++) iotrmi/C++/IoTRMIObject.cpp -o $(BIN_DIR)/iotrmi/C++/IoTRMIObject.out --std=c++11 # mkdir -p $(BIN_DIR)/iotrmi/C++/sample -# $(G++) iotrmi/C++/sample/TestClass.cpp -o $(BIN_DIR)/iotrmi/C++/sample/TestClass.out --std=c++11 -# $(G++) iotrmi/C++/sample/TestClass_Stub.cpp -o $(BIN_DIR)/iotrmi/C++/sample/TestClass_Stub.out --std=c++11 -# $(G++) iotrmi/C++/sample/TestClass_Skeleton.cpp -o $(BIN_DIR)/iotrmi/C++/sample/TestClass_Skeleton.out --std=c++11 + $(G++) iotrmi/C++/sample/CallBackInterface.hpp -o $(BIN_DIR)/iotrmi/C++/sample/CallBackInterface.out --std=c++11 + $(G++) iotrmi/C++/sample/CallBack.hpp -o $(BIN_DIR)/iotrmi/C++/sample/CallBack.out --std=c++11 + $(G++) iotrmi/C++/sample/CallBack_CBStub.hpp -o $(BIN_DIR)/iotrmi/C++/sample/CallBack_CBStub.out --std=c++11 + $(G++) iotrmi/C++/sample/CallBack_CBSkeleton.hpp -o $(BIN_DIR)/iotrmi/C++/sample/CallBack_CBSkeleton.out --std=c++11 + $(G++) iotrmi/C++/sample/CallBack_Stub.cpp -o $(BIN_DIR)/iotrmi/C++/sample/CallBack_Stub.out --std=c++11 + $(G++) iotrmi/C++/sample/CallBack_Skeleton.cpp -o $(BIN_DIR)/iotrmi/C++/sample/CallBack_Skeleton.out --std=c++11 + $(G++) iotrmi/C++/sample/TestClass.cpp -o $(BIN_DIR)/iotrmi/C++/sample/TestClass.out --std=c++11 + $(G++) iotrmi/C++/sample/TestClass_Stub.cpp -o $(BIN_DIR)/iotrmi/C++/sample/TestClass_Stub.out --std=c++11 + $(G++) iotrmi/C++/sample/TestClass_Skeleton.cpp -o $(BIN_DIR)/iotrmi/C++/sample/TestClass_Skeleton.out --std=c++11 + #$(G++) iotrmi/C++/sample/Test.cpp -o ../bin/iotrmi/C++/sample/Test.out --std=c++11 -lpthread + $(G++) iotrmi/C++/sample/Test2.cpp -o ../bin/iotrmi/C++/sample/Test2.out --std=c++11 -pthread -pg + PHONY += run-rmiserver run-rmiserver: diff --git a/iotjava/iotrmi/C++/IoTRMICall.hpp b/iotjava/iotrmi/C++/IoTRMICall.hpp index 68ead38..f498758 100644 --- a/iotjava/iotrmi/C++/IoTRMICall.hpp +++ b/iotjava/iotrmi/C++/IoTRMICall.hpp @@ -22,13 +22,14 @@ using namespace std; class IoTRMICall { public: - IoTRMICall(int _port, const char* _address, int _rev, bool* _bResult, string _methodSign[], int _size); + IoTRMICall(int _port, const char* _address, int _rev, bool* _bResult, + const string _methodSign[], const int _size); ~IoTRMICall(); // Public methods int methodLength(string paramCls[], void* paramObj[], int numParam); - char* methodToBytes(string methodSign, string paramCls[], void* paramObj[], + char* methodToBytes(int objectId, string methodSign, string paramCls[], void* paramObj[], char* method, int numParam); - void* remoteCall(string methodSign, string retType, string paramCls[], + void* remoteCall(int objectId, string methodSign, string retType, string paramCls[], void* paramObj[], int numParam, void* retObj); private: @@ -37,12 +38,12 @@ class IoTRMICall { IoTSocketClient *rmiClient; // Private methods - void getMethodIds(string methodSign[], int size); + void getMethodIds(const string methodSign[], const int size); }; // Constructor -IoTRMICall::IoTRMICall(int _port, const char* _address, int _rev, bool* _bResult, string _methodSign[], int _size) { +IoTRMICall::IoTRMICall(int _port, const char* _address, int _rev, bool* _bResult, const string _methodSign[], const int _size) { getMethodIds(_methodSign, _size); rmiUtil = new IoTRMIUtil(); @@ -76,13 +77,13 @@ IoTRMICall::~IoTRMICall() { // Calls a method remotely by passing in parameters and getting a return object -void* IoTRMICall::remoteCall(string methodSign, string retType, string paramCls[], +void* IoTRMICall::remoteCall(int objectId, string methodSign, string retType, string paramCls[], void* paramObj[], int numParam, void* retObj) { // Send input parameters int len = methodLength(paramCls, paramObj, numParam); char method[len]; - methodToBytes(methodSign, paramCls, paramObj, method, numParam); + methodToBytes(objectId, methodSign, paramCls, paramObj, method, numParam); // Send bytes fflush(NULL); rmiClient->sendBytes(method, len); @@ -107,8 +108,8 @@ void* IoTRMICall::remoteCall(string methodSign, string retType, string paramCls[ int IoTRMICall::methodLength(string paramCls[], void* paramObj[], int numParam) { // Get byte arrays and calculate method bytes length - // Start from the method Id... - int methodLen = IoTRMIUtil::METHOD_ID_LEN; + // Start from the object Id + method Id... + int methodLen = IoTRMIUtil::OBJECT_ID_LEN + IoTRMIUtil::METHOD_ID_LEN; for (int i = 0; i < numParam; i++) { // Find the parameter length int paramLen = rmiUtil->getTypeSize(paramCls[i]); @@ -126,15 +127,20 @@ int IoTRMICall::methodLength(string paramCls[], void* paramObj[], int numParam) // Convert method and its parameters into bytes -char* IoTRMICall::methodToBytes(string methodSign, string paramCls[], +char* IoTRMICall::methodToBytes(int objectId, string methodSign, string paramCls[], void* paramObj[], char* method, int numParam) { - // Get method ID in bytes + // Get object Id in bytes + char objId[IoTRMIUtil::OBJECT_ID_LEN]; + IoTRMIUtil::intToByteArray(objectId, objId); + memcpy(method, objId, IoTRMIUtil::OBJECT_ID_LEN); + int pos = IoTRMIUtil::OBJECT_ID_LEN; + // Get method Id in bytes char methodId[IoTRMIUtil::METHOD_ID_LEN]; int methId = mapSign2MethodId.find(methodSign)->second; IoTRMIUtil::intToByteArray(methId, methodId); - memcpy(method, methodId, IoTRMIUtil::METHOD_ID_LEN); - int pos = IoTRMIUtil::METHOD_ID_LEN; + memcpy(method + pos, methodId, IoTRMIUtil::METHOD_ID_LEN); + pos = pos + IoTRMIUtil::METHOD_ID_LEN; // Get byte arrays and calculate method bytes length for (int i = 0; i < numParam; i++) { // Find the parameter length @@ -161,7 +167,7 @@ char* IoTRMICall::methodToBytes(string methodSign, string paramCls[], // ************* // Helpers // ************* -void IoTRMICall::getMethodIds(string methodSign[], int size) { +void IoTRMICall::getMethodIds(const string methodSign[], const int size) { for(int i = 0; i < size; i++) { mapSign2MethodId[methodSign[i]] = i; diff --git a/iotjava/iotrmi/C++/IoTRMIObject.hpp b/iotjava/iotrmi/C++/IoTRMIObject.hpp index af0541b..366c6ef 100644 --- a/iotjava/iotrmi/C++/IoTRMIObject.hpp +++ b/iotjava/iotrmi/C++/IoTRMIObject.hpp @@ -22,11 +22,15 @@ using namespace std; class IoTRMIObject { public: - IoTRMIObject(int _port, bool* _bResult, string _methodSign[], int _size); + IoTRMIObject(int _port, bool* _bResult, const string _methodSign[], const int _size); ~IoTRMIObject(); // Public methods void sendReturnObj(void* retObj, string type); - void getMethodBytes(); + char* getMethodBytes(); + int getMethodBytesLen(); + void setMethodBytes(char* _methodBytes); + int getObjectId(); + static int getObjectId(char* methodBytes); string getSignature(); void** getMethodParams(string paramCls[], int numParam, void* paramObj[]); @@ -38,12 +42,12 @@ class IoTRMIObject { int methodLen; // Private methods - void getMethodIds(string methodSign[], int size); + void getMethodIds(const string methodSign[], const int size); }; // Constructor -IoTRMIObject::IoTRMIObject(int _port, bool* _bResult, string _methodSign[], int _size) { +IoTRMIObject::IoTRMIObject(int _port, bool* _bResult, const string _methodSign[], const int _size) { rmiUtil = new IoTRMIUtil(); if (rmiUtil == NULL) { @@ -99,11 +103,53 @@ void IoTRMIObject::sendReturnObj(void* retObj, string type) { // Get method bytes from the socket -void IoTRMIObject::getMethodBytes() { +char* IoTRMIObject::getMethodBytes() { // Get method in bytes and update method length methodBytes = rmiServer->receiveBytes(methodBytes, &methodLen); fflush(NULL); + return methodBytes; +} + + +// Get method bytes length +int IoTRMIObject::getMethodBytesLen() { + + return methodLen; +} + + +// Get object Id from bytes +int IoTRMIObject::getObjectId() { + + char objectIdBytes[IoTRMIUtil::OBJECT_ID_LEN]; + memcpy(objectIdBytes, methodBytes, IoTRMIUtil::OBJECT_ID_LEN); + // Get method signature + int objectId = 0; + IoTRMIUtil::byteArrayToInt(&objectId, objectIdBytes); + + return objectId; +} + + +// Get object Id from bytes (static version) +int IoTRMIObject::getObjectId(char* methodBytes) { + + char objectIdBytes[IoTRMIUtil::OBJECT_ID_LEN]; + memcpy(objectIdBytes, methodBytes, IoTRMIUtil::OBJECT_ID_LEN); + // Get method signature + int objectId = 0; + IoTRMIUtil::byteArrayToInt(&objectId, objectIdBytes); + + return objectId; +} + + +// Set method bytes +void IoTRMIObject::setMethodBytes(char* _methodBytes) { + + // Set method bytes + methodBytes = _methodBytes; } @@ -112,8 +158,8 @@ string IoTRMIObject::getSignature() { // Get method Id char methodIdBytes[IoTRMIUtil::METHOD_ID_LEN]; - memcpy(methodIdBytes, methodBytes, IoTRMIUtil::METHOD_ID_LEN); - // Get method object + memcpy(methodIdBytes, methodBytes + IoTRMIUtil::OBJECT_ID_LEN, IoTRMIUtil::METHOD_ID_LEN); + // Get method signature int methodId = 0; IoTRMIUtil::byteArrayToInt(&methodId, methodIdBytes); @@ -131,7 +177,7 @@ string IoTRMIObject::getSignature() { void** IoTRMIObject::getMethodParams(string paramCls[], int numParam, void* paramObj[]) { // Byte scanning position - int pos = IoTRMIUtil::METHOD_ID_LEN; + int pos = IoTRMIUtil::OBJECT_ID_LEN + IoTRMIUtil::METHOD_ID_LEN; for (int i = 0; i < numParam; i++) { int paramLen = rmiUtil->getTypeSize(paramCls[i]); // Get the 32-bit field in the byte array to get the actual @@ -158,7 +204,7 @@ void** IoTRMIObject::getMethodParams(string paramCls[], int numParam, void* para // ************* // Helpers // ************* -void IoTRMIObject::getMethodIds(string methodSign[], int size) { +void IoTRMIObject::getMethodIds(const string methodSign[], const int size) { for(int i = 0; i < size; i++) { mapMethodId2Sign[i] = methodSign[i]; diff --git a/iotjava/iotrmi/C++/IoTRMIUtil.hpp b/iotjava/iotrmi/C++/IoTRMIUtil.hpp index b80b115..e93cc85 100644 --- a/iotjava/iotrmi/C++/IoTRMIUtil.hpp +++ b/iotjava/iotrmi/C++/IoTRMIUtil.hpp @@ -91,6 +91,7 @@ class IoTRMIUtil { static void* getArrayParamObject(void* retObj, const char* type, char* paramBytes, int len); // Constants + const static int OBJECT_ID_LEN = 4; // 4 bytes = 32 bits const static int METHOD_ID_LEN = 4; // 4 bytes = 32 bits const static int PARAM_LEN = 4; // 4 bytes = 32 bits (4-byte field that stores the length of the param) const static int CHAR_LEN = 2; // 2 bytes (we follow Java convention) diff --git a/iotjava/iotrmi/C++/sample/CallBack.hpp b/iotjava/iotrmi/C++/sample/CallBack.hpp new file mode 100644 index 0000000..a4b9db4 --- /dev/null +++ b/iotjava/iotrmi/C++/sample/CallBack.hpp @@ -0,0 +1,39 @@ +#include +#include "CallBackInterface.hpp" + +using namespace std; + +class CallBack : public CallBackInterface { + public: + //CallBack(); + CallBack(int _i); + //~CallBack(); + + int printInt(); + void setInt(int _i); + + private: + int intA; +}; + + +// Constructor +CallBack::CallBack(int _i) { + + intA = _i; +} + + +int CallBack::printInt() { + + cout << "Integer: " << intA << endl; + return intA; +} + + +void CallBack::setInt(int _i) { + + intA = _i; +} + + diff --git a/iotjava/iotrmi/C++/sample/CallBackInterface.hpp b/iotjava/iotrmi/C++/sample/CallBackInterface.hpp new file mode 100644 index 0000000..9864d6f --- /dev/null +++ b/iotjava/iotrmi/C++/sample/CallBackInterface.hpp @@ -0,0 +1,10 @@ +#include + +using namespace std; + +class CallBackInterface { + public: + virtual int printInt() = 0; + virtual void setInt(int _i) = 0; +}; + diff --git a/iotjava/iotrmi/C++/sample/CallBack_CBSkeleton.hpp b/iotjava/iotrmi/C++/sample/CallBack_CBSkeleton.hpp new file mode 100644 index 0000000..b2f56a2 --- /dev/null +++ b/iotjava/iotrmi/C++/sample/CallBack_CBSkeleton.hpp @@ -0,0 +1,89 @@ +#include +#include "../IoTRMIObject.hpp" +#include "CallBack.hpp" + +using namespace std; + +class CallBack_CBSkeleton : public CallBackInterface { + public: + CallBack_CBSkeleton(CallBackInterface* _cb, int _objectId); + ~CallBack_CBSkeleton(); + + void invokeMethod(IoTRMIObject* rmiObj); + int printInt(); + void setInt(int _i); + + const static int size = 2; + const static string methodSignatures[size]; + + private: + CallBackInterface *cb; + int objectId = 0; +}; + + +const string CallBack_CBSkeleton::methodSignatures[CallBack_CBSkeleton::size] = { + + "intprintInt()", + "voidsetInt(int)" +}; + + +// Constructor +CallBack_CBSkeleton::CallBack_CBSkeleton(CallBackInterface* _cb, int _objectId) { + + cb = _cb; + objectId = _objectId; + cout << "Creating CallBack_Skeleton and waiting!" << endl; +} + + +CallBack_CBSkeleton::~CallBack_CBSkeleton() { + +} + + +void CallBack_CBSkeleton::invokeMethod(IoTRMIObject* rmiObj) { + + // Loop continuously waiting for incoming bytes + while (true) { + + rmiObj->getMethodBytes(); + string methodSign = rmiObj->getSignature(); + cout << "Method sign: " << methodSign << endl; + + if (methodSign.compare("intprintInt()") == 0) { + string paramCls[] = { }; + int numParam = 0; + void* paramObj[] = { }; + rmiObj->getMethodParams(paramCls, numParam, paramObj); + int retVal = printInt(); + void* retObj = &retVal; + rmiObj->sendReturnObj(retObj, "int"); + } else if (methodSign.compare("voidsetInt(int)") == 0) { + string paramCls[] = { "int" }; + int numParam = 1; + int param1 = 1; + void* paramObj[] = { ¶m1 }; + rmiObj->getMethodParams(paramCls, numParam, paramObj); + setInt(param1); + } else { + string error = "Signature not recognized: " + string(methodSign); + throw error; + } + } +} + + +int CallBack_CBSkeleton::printInt() { + + return cb->printInt(); +} + + +void CallBack_CBSkeleton::setInt(int _i) { + + cb->setInt(_i); +} + + diff --git a/iotjava/iotrmi/C++/sample/CallBack_CBStub.hpp b/iotjava/iotrmi/C++/sample/CallBack_CBStub.hpp new file mode 100644 index 0000000..07b5b3c --- /dev/null +++ b/iotjava/iotrmi/C++/sample/CallBack_CBStub.hpp @@ -0,0 +1,81 @@ +#include +#include "CallBackInterface.hpp" +#include "../IoTRMICall.hpp" + +using namespace std; + +class CallBack_CBStub : public CallBackInterface { + public: + CallBack_CBStub(); + CallBack_CBStub(IoTRMICall* _rmiCall, int _objectId); + ~CallBack_CBStub(); + + int printInt(); + void setInt(int _i); + + const static int size = 2; + const static string methodSignatures[size]; + + private: + + IoTRMICall *rmiCall; + int objectId = 0; // Default value is 0 +}; + + +const string CallBack_CBStub::methodSignatures[CallBack_CBStub::size] = { + + "intprintInt()", + "voidsetInt(int)" +}; + + +// Constructor +CallBack_CBStub::CallBack_CBStub() { + + rmiCall = NULL; +} + + +CallBack_CBStub::CallBack_CBStub(IoTRMICall* _rmiCall, int _objectId) { + + objectId = _objectId; + rmiCall = _rmiCall; +} + + +CallBack_CBStub::~CallBack_CBStub() { + + if (rmiCall != NULL) { + delete rmiCall; + rmiCall = NULL; + } +} + + +int CallBack_CBStub::printInt() { + + int numParam = 0; + string sign = "intprintInt()"; + string retType = "int"; + string paramCls[] = { }; + void* paramObj[] = { }; + int retVal = 0; + void* retObj = &retVal; + rmiCall->remoteCall(objectId, sign, retType, paramCls, paramObj, numParam, retObj); + return retVal; +} + + +void CallBack_CBStub::setInt(int _i) { + + int numParam = 1; + string sign = "voidsetInt(int)"; + string retType = "void"; + string paramCls[] = { "int" }; + void* paramObj[] = { &_i }; + void* retObj = NULL; + rmiCall->remoteCall(objectId, sign, retType, paramCls, paramObj, numParam, retObj); +} + + diff --git a/iotjava/iotrmi/C++/sample/CallBack_Skeleton.cpp b/iotjava/iotrmi/C++/sample/CallBack_Skeleton.cpp new file mode 100644 index 0000000..0d8ea86 --- /dev/null +++ b/iotjava/iotrmi/C++/sample/CallBack_Skeleton.cpp @@ -0,0 +1,18 @@ +#include +#include +#include "CallBack_Skeleton.hpp" + +using namespace std; + +int main(int argc, char *argv[]) +{ + + int port = 5010; + CallBackInterface *cb = new CallBack(23); + CallBack_Skeleton *cbSkel = new CallBack_Skeleton(cb, port); + //cbSkel->waitRequestInvokeMethod(); + + delete cb; + delete cbSkel; + return 0; +} diff --git a/iotjava/iotrmi/C++/sample/CallBack_Skeleton.hpp b/iotjava/iotrmi/C++/sample/CallBack_Skeleton.hpp new file mode 100644 index 0000000..36477ac --- /dev/null +++ b/iotjava/iotrmi/C++/sample/CallBack_Skeleton.hpp @@ -0,0 +1,94 @@ +#include +#include "../IoTRMIObject.hpp" +#include "CallBack.hpp" + +using namespace std; + +class CallBack_Skeleton : public CallBackInterface { + public: + CallBack_Skeleton(CallBackInterface* _cb, int _port); + ~CallBack_Skeleton(); + + void waitRequestInvokeMethod(); + int printInt(); + void setInt(int _i); + + const static int size = 2; + const static string methodSignatures[size]; + + private: + CallBackInterface *cb; + IoTRMIObject *rmiObj; +}; + + +const string CallBack_Skeleton::methodSignatures[CallBack_Skeleton::size] = { + + "intprintInt()", + "voidsetInt(int)" +}; + + +// Constructor +CallBack_Skeleton::CallBack_Skeleton(CallBackInterface* _cb, int _port) { + + bool _bResult = false; + cb = _cb; + rmiObj = new IoTRMIObject(_port, &_bResult, methodSignatures, size); + waitRequestInvokeMethod(); +} + + +CallBack_Skeleton::~CallBack_Skeleton() { + + if (rmiObj != NULL) { + delete rmiObj; + rmiObj = NULL; + } +} + + +void CallBack_Skeleton::waitRequestInvokeMethod() { + + // Loop continuously waiting for incoming bytes + while (true) { + + rmiObj->getMethodBytes(); + string methodSign = rmiObj->getSignature(); + cout << "Method sign: " << methodSign << endl; + + if (methodSign.compare("intprintInt()") == 0) { + string paramCls[] = { }; + int numParam = 0; + void* paramObj[] = { }; + rmiObj->getMethodParams(paramCls, numParam, paramObj); + int retVal = printInt(); + void* retObj = &retVal; + rmiObj->sendReturnObj(retObj, "int"); + } else if (methodSign.compare("voidsetInt(int)") == 0) { + string paramCls[] = { "int" }; + int numParam = 1; + int param1 = 1; + void* paramObj[] = { ¶m1 }; + rmiObj->getMethodParams(paramCls, numParam, paramObj); + setInt(param1); + } else { + string error = "Signature not recognized: " + string(methodSign); + throw error; + } + } +} + + +int CallBack_Skeleton::printInt() { + + return cb->printInt(); +} + + +void CallBack_Skeleton::setInt(int _i) { + + cb->setInt(_i); +} + + diff --git a/iotjava/iotrmi/C++/sample/CallBack_Stub.cpp b/iotjava/iotrmi/C++/sample/CallBack_Stub.cpp new file mode 100644 index 0000000..04fd684 --- /dev/null +++ b/iotjava/iotrmi/C++/sample/CallBack_Stub.cpp @@ -0,0 +1,23 @@ +#include +#include +#include "CallBack_Stub.hpp" + +using namespace std; + +int main(int argc, char *argv[]) +{ + + int port = 5010; + const char* address = "localhost"; + int rev = 0; + bool bResult = false; + + CallBackInterface *cbStub = new CallBack_Stub(port, address, rev, &bResult); + cout << "Return value: " << cbStub->printInt() << endl; + cbStub->setInt(123); + cout << "Return value: " << cbStub->printInt() << endl; + + delete cbStub; + + return 0; +} diff --git a/iotjava/iotrmi/C++/sample/CallBack_Stub.hpp b/iotjava/iotrmi/C++/sample/CallBack_Stub.hpp new file mode 100644 index 0000000..b1fa580 --- /dev/null +++ b/iotjava/iotrmi/C++/sample/CallBack_Stub.hpp @@ -0,0 +1,83 @@ +#include +#include "CallBackInterface.hpp" +#include "../IoTRMICall.hpp" + +using namespace std; + +class CallBack_Stub : public CallBackInterface { + public: + CallBack_Stub(); + CallBack_Stub(int _port, const char* _address, int _rev, bool* _bResult); + ~CallBack_Stub(); + + int printInt(); + void setInt(int _i); + + const static int size = 2; + const static string methodSignatures[size]; + + private: + + IoTRMICall *rmiCall; + string address; + int objectId = 0; // Default value is 0 +}; + + +const string CallBack_Stub::methodSignatures[CallBack_Stub::size] = { + + "intprintInt()", + "voidsetInt(int)" +}; + + +// Constructor +CallBack_Stub::CallBack_Stub() { + + address = ""; + rmiCall = NULL; +} + + +CallBack_Stub::CallBack_Stub(int _port, const char* _address, int _rev, bool* _bResult) { + + address = _address; + rmiCall = new IoTRMICall(_port, _address, _rev, _bResult, methodSignatures, size); +} + + +CallBack_Stub::~CallBack_Stub() { + + if (rmiCall != NULL) { + delete rmiCall; + rmiCall = NULL; + } +} + + +int CallBack_Stub::printInt() { + + int numParam = 0; + string sign = "intprintInt()"; + string retType = "int"; + string paramCls[] = { }; + void* paramObj[] = { }; + int retVal = 0; + void* retObj = &retVal; + rmiCall->remoteCall(objectId, sign, retType, paramCls, paramObj, numParam, retObj); + return retVal; +} + + +void CallBack_Stub::setInt(int _i) { + + int numParam = 1; + string sign = "voidsetInt(int)"; + string retType = "void"; + string paramCls[] = { "int" }; + void* paramObj[] = { &_i }; + void* retObj = NULL; + rmiCall->remoteCall(objectId, sign, retType, paramCls, paramObj, numParam, retObj); +} + + diff --git a/iotjava/iotrmi/C++/sample/TestClass.cpp b/iotjava/iotrmi/C++/sample/TestClass.cpp index cbaeeba..1981601 100644 --- a/iotjava/iotrmi/C++/sample/TestClass.cpp +++ b/iotjava/iotrmi/C++/sample/TestClass.cpp @@ -20,8 +20,22 @@ int main(int argc, char *argv[]) input.push_back(987);*/ cout << "Return value: " << tc->sumArray(input) << endl; - delete tc; + vector test; + CallBackInterface *cb1 = new CallBack(12); + CallBackInterface *cb2 = new CallBack(22); + CallBackInterface *cb3 = new CallBack(32); + test.push_back(cb1); + test.push_back(cb2); + test.push_back(cb3); + for (CallBackInterface *cb : test) { + cout << "Test print: " << cb->printInt() << endl; + } + + delete cb1; + delete cb2; + delete cb3; + return 0; } diff --git a/iotjava/iotrmi/C++/sample/TestClass.hpp b/iotjava/iotrmi/C++/sample/TestClass.hpp index 272f1c8..efa4f2a 100644 --- a/iotjava/iotrmi/C++/sample/TestClass.hpp +++ b/iotjava/iotrmi/C++/sample/TestClass.hpp @@ -16,14 +16,16 @@ class TestClass : public TestClassInterface { //int64_t sumArray(vector newA); int setAndGetA(int newA); int setACAndGetA(string newC, int newA); - //void registerCallback(CallBackInterface _cb); - //int callBack(); + void registerCallback(CallBackInterface* _cb); + void registerCallback(vector _cb); + int callBack(); private: - int intA; - float floatB; - string stringC; - //CallBackInterface cb; + int intA; + float floatB; + string stringC; + CallBackInterface *cb; + vector cbvec; }; @@ -33,7 +35,8 @@ TestClass::TestClass() { intA = 1; floatB = 2; stringC = "345"; - //cb = NULL; + cb = NULL; + // cbvec doesn't need to be initialized again } @@ -42,7 +45,8 @@ TestClass::TestClass(int _int, float _float, string _string) { intA = _int; floatB = _float; stringC = _string; - //cb = NULL; + cb = NULL; + // cbvec doesn't need to be initialized again } @@ -101,14 +105,31 @@ int TestClass::setACAndGetA(string newC, int newA) { } -/*void TestClass::registerCallback(CallBackInterface _cb) { +void TestClass::registerCallback(CallBackInterface* _cb) { cb = _cb; } +void TestClass::registerCallback(vector _cb) { + + for (CallBackInterface* cb : _cb) { + cbvec.push_back(cb); + cout << "Registering callback object!" << endl; + } +} + + +//int TestClass::callBack() { +// return cb.printInt(); +//} + + int TestClass::callBack() { - return cb.printInt(); -}*/ + int sum = 0; + for (CallBackInterface* cb : cbvec) { + sum = sum + cb->printInt(); + } +} diff --git a/iotjava/iotrmi/C++/sample/TestClassInterface.hpp b/iotjava/iotrmi/C++/sample/TestClassInterface.hpp index 9dc3e00..0dd0845 100644 --- a/iotjava/iotrmi/C++/sample/TestClassInterface.hpp +++ b/iotjava/iotrmi/C++/sample/TestClassInterface.hpp @@ -1,5 +1,7 @@ #include #include +#include "CallBack_CBSkeleton.hpp" +//#include "CallBack.hpp" using namespace std; @@ -12,7 +14,8 @@ class TestClassInterface { //virtual int64_t sumArray(vector newA) = 0; virtual int setAndGetA(int newA) = 0; virtual int setACAndGetA(string newC, int newA) = 0; - //virtual void registerCallback(CallBackInterface _cb); - //virtual int callBack(); + virtual void registerCallback(CallBackInterface* _cb) = 0; + virtual void registerCallback(vector _cb) = 0; + virtual int callBack() = 0; }; diff --git a/iotjava/iotrmi/C++/sample/TestClass_Skeleton.hpp b/iotjava/iotrmi/C++/sample/TestClass_Skeleton.hpp index d5be54b..8786e0e 100644 --- a/iotjava/iotrmi/C++/sample/TestClass_Skeleton.hpp +++ b/iotjava/iotrmi/C++/sample/TestClass_Skeleton.hpp @@ -9,35 +9,39 @@ class TestClass_Skeleton : public TestClassInterface { TestClass_Skeleton(TestClassInterface* _tc, int _port); ~TestClass_Skeleton(); - void waitRequestInvokeMethod(); - void setA(int _int); - void setB(float _float); - void setC(string _string); - string sumArray(vector newA); - //int64_t sumArray(vector newA); - int setAndGetA(int newA); - int setACAndGetA(string newC, int newA); - //void registerCallback(CallBackInterface _cb); - //int callBack(); + void waitRequestInvokeMethod(); + void setA(int _int); + void setB(float _float); + void setC(string _string); + string sumArray(vector newA); + //int64_t sumArray(vector newA); + int setAndGetA(int newA); + int setACAndGetA(string newC, int newA); + void registerCallback(CallBackInterface* _cb); + void registerCallback(vector _cb); + int callBack(); + + const static int size = 9; + const static string methodSignatures[size]; private: TestClassInterface *tc; IoTRMIObject *rmiObj; //CallBackInterface cbstub; +}; + - const static int size = 8; - string methodSignatures[size] = { - - "voidsetA(int)", - "voidsetB(float)", - "voidsetC(string)", - "sumArray(string[])", - //"sumArray(int[])", - "intsetAndGetA(int)", - "intsetACAndGetA(string,int)", - "intcallBack()", - "voidregisterCallBack(CallBackInterface)" - }; +const string TestClass_Skeleton::methodSignatures[TestClass_Skeleton::size] = { + "voidsetA(int)", + "voidsetB(float)", + "voidsetC(string)", + "sumArray(string[])", + //"sumArray(int[])", + "intsetAndGetA(int)", + "intsetACAndGetA(string,int)", + "intcallBack()", + "voidregisterCallBack(CallBackInterface)", + "voidregisterCallBack(CallBackInterface[])" }; @@ -179,7 +183,13 @@ int TestClass_Skeleton::setACAndGetA(string newC, int newA) { } -/*void TestClass_Skeleton::registerCallback(CallBackInterface _cb) { +void TestClass_Skeleton::registerCallback(CallBackInterface* _cb) { + + tc->registerCallback(_cb); +} + + +void TestClass_Skeleton::registerCallback(vector _cb) { tc->registerCallback(_cb); } @@ -187,6 +197,7 @@ int TestClass_Skeleton::setACAndGetA(string newC, int newA) { int TestClass_Skeleton::callBack() { - return tc->callBack(); -}*/ + tc->callBack(); +} + diff --git a/iotjava/iotrmi/C++/sample/TestClass_Stub.cpp b/iotjava/iotrmi/C++/sample/TestClass_Stub.cpp index cfb7aa7..12f47b3 100644 --- a/iotjava/iotrmi/C++/sample/TestClass_Stub.cpp +++ b/iotjava/iotrmi/C++/sample/TestClass_Stub.cpp @@ -11,8 +11,11 @@ int main(int argc, char *argv[]) const char* address = "localhost"; int rev = 0; bool bResult = false; + vector ports; + ports.push_back(12345); + //ports.push_back(13234); - TestClassInterface *tcStub = new TestClass_Stub(port, address, rev, &bResult); + TestClassInterface *tcStub = new TestClass_Stub(port, address, rev, &bResult, ports); cout << "Return value: " << tcStub->setAndGetA(123) << endl; cout << "Return value: " << tcStub->setACAndGetA("string", 123) << endl; vector input; diff --git a/iotjava/iotrmi/C++/sample/TestClass_Stub.hpp b/iotjava/iotrmi/C++/sample/TestClass_Stub.hpp index 3611bbf..1749a59 100644 --- a/iotjava/iotrmi/C++/sample/TestClass_Stub.hpp +++ b/iotjava/iotrmi/C++/sample/TestClass_Stub.hpp @@ -1,5 +1,6 @@ #include #include "../IoTRMICall.hpp" +#include "../IoTRMIObject.hpp" #include "TestClassInterface.hpp" using namespace std; @@ -7,7 +8,7 @@ using namespace std; class TestClass_Stub : public TestClassInterface { public: TestClass_Stub(); - TestClass_Stub(int _port, const char* _address, int _rev, bool* _bResult); + TestClass_Stub(int _port, const char* _address, int _rev, bool* _bResult, vector _ports); ~TestClass_Stub(); void setA(int _int); @@ -17,31 +18,40 @@ class TestClass_Stub : public TestClassInterface { //int64_t sumArray(vector newA); int setAndGetA(int newA); int setACAndGetA(string newC, int newA); - //void registerCallback(CallBackInterface _cb); - //int callBack(); + void registerCallback(CallBackInterface* _cb); + void registerCallback(vector_cb); + int callBack(); + void init_CallBack(); + + const static int size = 9; + const static string methodSignatures[size]; private: - int intA; - float floatB; - string stringC; - //CallBackInterface cb; - IoTRMICall *rmiCall; - string address; - //vector ports; - - const static int size = 8; - string methodSignatures[size] = { - - "voidsetA(int)", - "voidsetB(float)", - "voidsetC(string)", - "sumArray(string[])", - //"sumArray(int[])", - "intsetAndGetA(int)", - "intsetACAndGetA(string,int)", - "intcallBack()", - "voidregisterCallBack(CallBackInterface)" - }; + int intA; + float floatB; + string stringC; + //CallBackInterface cb; + IoTRMICall *rmiCall; + IoTRMIObject *rmiObj; + string address; + vector ports; + map mapCBObj; + + int objectId = 0; // Default value is 0 +}; + + +const string TestClass_Stub::methodSignatures[TestClass_Stub::size] = { + "voidsetA(int)", + "voidsetB(float)", + "voidsetC(string)", + "sumArray(string[])", + //"sumArray(int[])", + "intsetAndGetA(int)", + "intsetACAndGetA(string,int)", + "intcallBack()", + "voidregisterCallBack(CallBackInterface)", + "voidregisterCallBack(CallBackInterface[])" }; @@ -52,10 +62,11 @@ TestClass_Stub::TestClass_Stub() { } -TestClass_Stub::TestClass_Stub(int _port, const char* _address, int _rev, bool* _bResult) { +TestClass_Stub::TestClass_Stub(int _port, const char* _address, int _rev, bool* _bResult, vector _ports) { address = _address; rmiCall = new IoTRMICall(_port, _address, _rev, _bResult, methodSignatures, size); + ports = _ports; } @@ -68,6 +79,18 @@ TestClass_Stub::~TestClass_Stub() { } +// Callback handler thread +void TestClass_Stub::init_CallBack() { + + bool bResult = false; + rmiObj = new IoTRMIObject(ports[0], &bResult, CallBack_CBSkeleton::methodSignatures, CallBack_CBSkeleton::size); + while (true) { + char* method = rmiObj->getMethodBytes(); + + } +} + + void TestClass_Stub::setA(int _int) { int numParam = 1; @@ -76,7 +99,7 @@ void TestClass_Stub::setA(int _int) { string paramCls[] = { "int" }; void* paramObj[] = { &_int }; void* retObj = NULL; - rmiCall->remoteCall(sign, retType, paramCls, paramObj, numParam, retObj); + rmiCall->remoteCall(objectId, sign, retType, paramCls, paramObj, numParam, retObj); } @@ -88,7 +111,7 @@ void TestClass_Stub::setB(float _float) { string paramCls[] = { "float" }; void* paramObj[] = { &_float }; void* retObj = NULL; - rmiCall->remoteCall(sign, retType, paramCls, paramObj, numParam, retObj); + rmiCall->remoteCall(objectId, sign, retType, paramCls, paramObj, numParam, retObj); } @@ -100,7 +123,7 @@ void TestClass_Stub::setC(string _string) { string paramCls[] = { "string" }; void* paramObj[] = { &_string }; void* retObj = NULL; - rmiCall->remoteCall(sign, retType, paramCls, paramObj, numParam, retObj); + rmiCall->remoteCall(objectId, sign, retType, paramCls, paramObj, numParam, retObj); } @@ -113,7 +136,7 @@ string TestClass_Stub::sumArray(vector newA) { void* paramObj[] = { &newA }; string retVal = ""; void* retObj = &retVal; - rmiCall->remoteCall(sign, retType, paramCls, paramObj, numParam, retObj); + rmiCall->remoteCall(objectId, sign, retType, paramCls, paramObj, numParam, retObj); return retVal; } @@ -127,7 +150,7 @@ string TestClass_Stub::sumArray(vector newA) { void* paramObj[] = { &newA }; int64_t retVal = 0; void* retObj = &retVal; - rmiCall->remoteCall(sign, retType, paramCls, paramObj, numParam, retObj); + rmiCall->remoteCall(objectId, sign, retType, paramCls, paramObj, numParam, retObj); return retVal; }*/ @@ -142,7 +165,7 @@ int TestClass_Stub::setAndGetA(int newA) { void* paramObj[] = { &newA }; int retVal = 0; void* retObj = &retVal; - rmiCall->remoteCall(sign, retType, paramCls, paramObj, numParam, retObj); + rmiCall->remoteCall(objectId, sign, retType, paramCls, paramObj, numParam, retObj); return retVal; } @@ -156,19 +179,33 @@ int TestClass_Stub::setACAndGetA(string newC, int newA) { void* paramObj[] = { &newC, &newA }; int retVal = 0; void* retObj = &retVal; - rmiCall->remoteCall(sign, retType, paramCls, paramObj, numParam, retObj); + rmiCall->remoteCall(objectId, sign, retType, paramCls, paramObj, numParam, retObj); return retVal; } -/*void TestClass_Stub::registerCallback(CallBackInterface _cb) { +void TestClass_Stub::registerCallback(CallBackInterface* _cb) { - cb = _cb; + //Should implement the callback here +} + + +void TestClass_Stub::registerCallback(vector _cb) { + + //Should implement the callback here for multiple callback objects } int TestClass_Stub::callBack() { - return cb.printInt(); -}*/ + int numParam = 0; + string sign = "intcallBack()"; + string retType = "int"; + string paramCls[] = { }; + void* paramObj[] = { }; + int retVal = 0; + void* retObj = &retVal; + rmiCall->remoteCall(objectId, sign, retType, paramCls, paramObj, numParam, retObj); + return retVal; +} diff --git a/iotjava/iotrmi/Java/sample/TestClass_CBStub.java b/iotjava/iotrmi/Java/sample/TestClass_CBStub.java index 80e5830..65550c7 100644 --- a/iotjava/iotrmi/Java/sample/TestClass_CBStub.java +++ b/iotjava/iotrmi/Java/sample/TestClass_CBStub.java @@ -84,7 +84,12 @@ public class TestClass_CBStub implements TestClassInterface { while (true) { byte[] method = rmiObj.getMethodBytes(); int objId = IoTRMIObject.getObjectId(method); - CallBack_CBSkeleton skel = (CallBack_CBSkeleton) listCBObj.get(objId); + //CallBack_CBSkeleton skel = (CallBack_CBSkeleton) listCBObj.get(objId); + CallBackInterface cb = listCBObj.get(objId); + CallBack_CBSkeleton skel = null; + if (cb instanceof CallBack_CBSkeleton) { // in case we have multiple callback classes + skel = (CallBack_CBSkeleton) cb; + } if (skel != null) { rmiObj.setMethodBytes(method); retObj = skel.invokeMethod(rmiObj); diff --git a/iotjava/iotrmi/Java/sample/TestClass_Stub.java b/iotjava/iotrmi/Java/sample/TestClass_Stub.java index d071d13..90e2264 100644 --- a/iotjava/iotrmi/Java/sample/TestClass_Stub.java +++ b/iotjava/iotrmi/Java/sample/TestClass_Stub.java @@ -99,7 +99,7 @@ public class TestClass_Stub implements TestClassInterface { }; thread.start(); - String sign = "registercallback"; + String sign = "registercallback"; // can be any string Class retType = void.class; // port, address, rev, and number of objects Class[] paramCls = new Class[] { int.class, String.class, int.class };