Adding object ID and tons of minor adjustments for callback support
authorrtrimana <rtrimana@uci.edu>
Thu, 3 Nov 2016 18:28:18 +0000 (11:28 -0700)
committerrtrimana <rtrimana@uci.edu>
Thu, 3 Nov 2016 18:28:18 +0000 (11:28 -0700)
20 files changed:
iotjava/Makefile
iotjava/iotrmi/C++/IoTRMICall.hpp
iotjava/iotrmi/C++/IoTRMIObject.hpp
iotjava/iotrmi/C++/IoTRMIUtil.hpp
iotjava/iotrmi/C++/sample/CallBack.hpp [new file with mode: 0644]
iotjava/iotrmi/C++/sample/CallBackInterface.hpp [new file with mode: 0644]
iotjava/iotrmi/C++/sample/CallBack_CBSkeleton.hpp [new file with mode: 0644]
iotjava/iotrmi/C++/sample/CallBack_CBStub.hpp [new file with mode: 0644]
iotjava/iotrmi/C++/sample/CallBack_Skeleton.cpp [new file with mode: 0644]
iotjava/iotrmi/C++/sample/CallBack_Skeleton.hpp [new file with mode: 0644]
iotjava/iotrmi/C++/sample/CallBack_Stub.cpp [new file with mode: 0644]
iotjava/iotrmi/C++/sample/CallBack_Stub.hpp [new file with mode: 0644]
iotjava/iotrmi/C++/sample/TestClass.cpp
iotjava/iotrmi/C++/sample/TestClass.hpp
iotjava/iotrmi/C++/sample/TestClassInterface.hpp
iotjava/iotrmi/C++/sample/TestClass_Skeleton.hpp
iotjava/iotrmi/C++/sample/TestClass_Stub.cpp
iotjava/iotrmi/C++/sample/TestClass_Stub.hpp
iotjava/iotrmi/Java/sample/TestClass_CBStub.java
iotjava/iotrmi/Java/sample/TestClass_Stub.java

index d9814d86da9b24ebe2f59c0be6d92715522cc22b..3d7b7f0b3dcd8050cb78c3e7c11da20c24bfb7da 100644 (file)
@@ -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:
index 68ead38715090c098d2fe9becadd75c852cdf992..f498758dc572989f709946e85409d5a36e80603a 100644 (file)
@@ -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;
index af0541befdd53420d6c661a5f484dbc9954316cd..366c6ef1f3231a8053f1d7bcaf68c8e6cd33e3ce 100644 (file)
@@ -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];
index b80b1156b6a9e353c8146ae6ba65058956847dfd..e93cc8513cb20dbe2f7e37571e9220d65ee05a8a 100644 (file)
@@ -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 (file)
index 0000000..a4b9db4
--- /dev/null
@@ -0,0 +1,39 @@
+#include <iostream>
+#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 (file)
index 0000000..9864d6f
--- /dev/null
@@ -0,0 +1,10 @@
+#include <iostream>
+
+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 (file)
index 0000000..b2f56a2
--- /dev/null
@@ -0,0 +1,89 @@
+#include <iostream>
+#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[] = { &param1 };
+                       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 (file)
index 0000000..07b5b3c
--- /dev/null
@@ -0,0 +1,81 @@
+#include <iostream>
+#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 (file)
index 0000000..0d8ea86
--- /dev/null
@@ -0,0 +1,18 @@
+#include <iostream>
+#include <string>
+#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 (file)
index 0000000..36477ac
--- /dev/null
@@ -0,0 +1,94 @@
+#include <iostream>
+#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[] = { &param1 };
+                       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 (file)
index 0000000..04fd684
--- /dev/null
@@ -0,0 +1,23 @@
+#include <iostream>
+#include <string>
+#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 (file)
index 0000000..b1fa580
--- /dev/null
@@ -0,0 +1,83 @@
+#include <iostream>
+#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);
+}
+
+
index cbaeebab86a317eee9b88734615e3a9efe71ff30..1981601d1e5092af78038b1f9a8bf107eef6176b 100644 (file)
@@ -20,8 +20,22 @@ int main(int argc, char *argv[])
        input.push_back(987);*/
 
        cout << "Return value: " << tc->sumArray(input) << endl;
-
        delete tc;
 
+       vector<CallBackInterface*> 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;
 }
index 272f1c8c319a6c425e52c9823419aa858923e67f..efa4f2ae6b8561c61ef90175786be7965b75683f 100644 (file)
@@ -16,14 +16,16 @@ class TestClass : public TestClassInterface {
                //int64_t                               sumArray(vector<int> newA);
                int                                     setAndGetA(int newA);
                int                                     setACAndGetA(string newC, int newA);
-               //void                          registerCallback(CallBackInterface _cb);
-               //int                           callBack();
+               void                            registerCallback(CallBackInterface* _cb);
+               void                            registerCallback(vector<CallBackInterface*> _cb);
+               int                                     callBack();
 
        private:                
-               int                                     intA;
-               float                           floatB;
-               string                          stringC;
-               //CallBackInterface cb;
+               int                                                     intA;
+               float                                           floatB;
+               string                                          stringC;
+               CallBackInterface                       *cb;
+               vector<CallBackInterface*>      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<CallBackInterface*> _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();
+       }
+}
 
index 9dc3e001c52ce199c7787b9324c6d95662661670..0dd0845062fd39b79de1561d86c71d6b7fec7dc3 100644 (file)
@@ -1,5 +1,7 @@
 #include <iostream>
 #include <vector>
+#include "CallBack_CBSkeleton.hpp"
+//#include "CallBack.hpp"
 
 using namespace std;
 
@@ -12,7 +14,8 @@ class TestClassInterface {
                //virtual int64_t       sumArray(vector<int> 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<CallBackInterface*> _cb) = 0;
+               virtual int             callBack() = 0;
 };
 
index d5be54b0cfb9ff882279597b1e0edfca3611f7f3..8786e0eb37488542028cc28b6c1f2bd98ec6931a 100644 (file)
@@ -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<string> newA);
-               //int64_t       sumArray(vector<int> 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<string> newA);
+               //int64_t               sumArray(vector<int> newA);
+               int                             setAndGetA(int newA);
+               int                             setACAndGetA(string newC, int newA);
+               void                    registerCallback(CallBackInterface* _cb);
+               void                    registerCallback(vector<CallBackInterface*> _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<CallBackInterface*> _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();
+}
+
 
index cfb7aa759dcc8aafd80faad7a18e45127e782a1a..12f47b345254fea1697882124c74694b3e5ef8a8 100644 (file)
@@ -11,8 +11,11 @@ int main(int argc, char *argv[])
        const char* address = "localhost";
        int rev = 0;
        bool bResult = false;
+       vector<int> 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<string> input;
index 3611bbffd49211c420813ec2c7403069d33c8325..1749a59f7c5dfb37087818ecc39e7ec80b65d1fe 100644 (file)
@@ -1,5 +1,6 @@
 #include <iostream>
 #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<int> _ports);
                ~TestClass_Stub();
 
                void                            setA(int _int);
@@ -17,31 +18,40 @@ class TestClass_Stub : public TestClassInterface {
                //int64_t                               sumArray(vector<int> newA);
                int                                     setAndGetA(int newA);
                int                                     setACAndGetA(string newC, int newA);
-               //void                          registerCallback(CallBackInterface _cb);
-               //int                           callBack();
+               void                            registerCallback(CallBackInterface* _cb);
+               void                            registerCallback(vector<CallBackInterface*>_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<int>                   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<int>                                     ports;
+               map<int,CallBackInterface*> 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<int> _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<string> 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<string> 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<CallBackInterface*> _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;
+}
 
index 80e5830549766db7c4e40838fcf5856fcbe03a59..65550c77fdddcd1975554b313be9b4ffd8529fa6 100644 (file)
@@ -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);
index d071d132f66a6d977cd51b5b48c77a9eac1f7c3a..90e2264b965d8443ca142e1f2955534559fd4884 100644 (file)
@@ -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 };