Refactoring type size handler; using Java types as standard
authorrtrimana <rtrimana@uci.edu>
Thu, 27 Oct 2016 21:59:45 +0000 (14:59 -0700)
committerrtrimana <rtrimana@uci.edu>
Thu, 27 Oct 2016 21:59:45 +0000 (14:59 -0700)
iotjava/iotrmi/C++/IoTRMICall.hpp
iotjava/iotrmi/C++/IoTRMIObject.hpp
iotjava/iotrmi/C++/IoTRMITypes.hpp
iotjava/iotrmi/C++/IoTRMIUtil.hpp
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

index eb767dd5a84f8c4f2fed0037df3582c9c4b1a08c..11797f92de09d661189db1814a51739a650e6c4f 100644 (file)
@@ -90,6 +90,7 @@ void* IoTRMICall::remoteCall(string methodSign, string retType, string paramCls[
                int retLen = 0;
                char* retObjBytes = NULL;
                retObjBytes = rmiClient->receiveBytes(retObjBytes, &retLen);
                int retLen = 0;
                char* retObjBytes = NULL;
                retObjBytes = rmiClient->receiveBytes(retObjBytes, &retLen);
+               IoTRMIUtil::printBytes(retObjBytes, retLen, false);
                retObj = IoTRMIUtil::getParamObject(retObj, retType.c_str(), retObjBytes, retLen);
        }
        
                retObj = IoTRMIUtil::getParamObject(retObj, retType.c_str(), retObjBytes, retLen);
        }
        
@@ -107,15 +108,7 @@ int IoTRMICall::methodLength(string paramCls[], void* paramObj[], int numParam)
                // Find the parameter length
                int paramLen = rmiUtil->getTypeSize(paramCls[i]);
                if (paramLen == -1) { // Store the length of the field - indefinite length
                // Find the parameter length
                int paramLen = rmiUtil->getTypeSize(paramCls[i]);
                if (paramLen == -1) { // Store the length of the field - indefinite length
-                       if (paramCls[i].compare("string") == 0) {
-                               // Get the length of the string through void* casting to string*
-                               paramLen = (*(string*)paramObj[i]).length();
-                       } else if (paramCls[i].compare("string[]") == 0) {
-                               paramLen = IoTRMIUtil::getByteStringLength(*(vector<string>*) paramObj[i]);
-                       } else {
-                               string error = "IoTRMICall: Unrecognizable type: " + paramCls[i];
-                               throw error;
-                       }
+                       paramLen = rmiUtil->getVarTypeSize(paramCls[i], paramObj[i]);
                        // Some space for param length, i.e. 32 bits for integer                
                        methodLen = methodLen + IoTRMIUtil::PARAM_LEN;
                }
                        // Some space for param length, i.e. 32 bits for integer                
                        methodLen = methodLen + IoTRMIUtil::PARAM_LEN;
                }
@@ -141,15 +134,7 @@ char* IoTRMICall::methodToBytes(string methodSign, string paramCls[],
                // Find the parameter length
                int paramLen = rmiUtil->getTypeSize(paramCls[i]);
                if (paramLen == -1) { // Store the length of the field - indefinite length
                // Find the parameter length
                int paramLen = rmiUtil->getTypeSize(paramCls[i]);
                if (paramLen == -1) { // Store the length of the field - indefinite length
-                       if (paramCls[i].compare("string") == 0) {
-                               // Get the length of the string through void* casting to string*
-                               paramLen = (*(string*)paramObj[i]).length();
-                       } else if (paramCls[i].compare("string[]") == 0) {
-                               paramLen = IoTRMIUtil::getByteStringLength(*(vector<string>*) paramObj[i]);
-                       } else {
-                               string error = "IoTRMICall: Unrecognizable type: " + paramCls[i];
-                               throw error;
-                       }
+                       paramLen = rmiUtil->getVarTypeSize(paramCls[i], paramObj[i]);
                        // Write the parameter length
                        char prmLenBytes[IoTRMIUtil::METHOD_ID_LEN];
                        IoTRMIUtil::intToByteArray(paramLen, prmLenBytes);
                        // Write the parameter length
                        char prmLenBytes[IoTRMIUtil::METHOD_ID_LEN];
                        IoTRMIUtil::intToByteArray(paramLen, prmLenBytes);
index a512dd2574942362c4ad8708b8e33dce39219795..5ffb81eeed8d767ca4fec64137ff03a4760e7a36 100644 (file)
@@ -89,15 +89,7 @@ void IoTRMIObject::sendReturnObj(void* retObj, string type) {
        // Find the length of return object in bytes
        int retLen = rmiUtil->getTypeSize(type);
        if (retLen == -1) {
        // Find the length of return object in bytes
        int retLen = rmiUtil->getTypeSize(type);
        if (retLen == -1) {
-               if (type.compare("string") == 0) {
-                       // Get the length of the string through void* casting to string*
-                       retLen = (*(string*)retObj).length();
-               } else if (type.compare("string[]") == 0) {
-                       retLen = IoTRMIUtil::getByteStringLength(*(vector<string>*) retObj);
-               } else {
-                       string error = "IoTRMICall: Unrecognizable type: " + type;
-                       throw error;
-               }
+               retLen = rmiUtil->getVarTypeSize(type, retObj);
        }
        // Need object bytes variable
        char retObjBytes[retLen];
        }
        // Need object bytes variable
        char retObjBytes[retLen];
index 2c32f47a8d25f7ffd384924b99b292c999ed9663..629543d89f73dae8c23d676ce8a3061e94938a4e 100644 (file)
@@ -36,15 +36,9 @@ class IoTRMITypes {
 
 
                /**
 
 
                /**
-                * Primitive sizes in Java - Long is 8 bytes and char is 2 bytes
+                * Primitive sizes in Java/C++
                 */
                 */
-               const static int primitivesJavaSizes[NUM_PRIMITIVES];
-
-
-               /**
-                * Primitive sizes in Cplus - Long is 4 bytes and char is 1 byte
-                */
-               const static int primitivesCplusSizes[NUM_PRIMITIVES];
+               const static int primitivesSizes[NUM_PRIMITIVES];
 
 
                /**
 
 
                /**
@@ -117,13 +111,7 @@ const string IoTRMITypes::primitivesCplus[IoTRMITypes::NUM_PRIMITIVES] = {
 };
 
 
 };
 
 
-const int IoTRMITypes::primitivesJavaSizes[IoTRMITypes::NUM_PRIMITIVES] = {
-
-       1, 1, 2, 2, 4, 4, 8, 8, 4, 4, 8, 8, 1, 1, 2, 2, -1, -1, 0
-};
-
-
-const int IoTRMITypes::primitivesCplusSizes[IoTRMITypes::NUM_PRIMITIVES] = {
+const int IoTRMITypes::primitivesSizes[IoTRMITypes::NUM_PRIMITIVES] = {
 
        1, 1, 2, 2, 4, 4, 8, 8, 4, 4, 8, 8, 1, 1, 2, 2, -1, -1, 0
 };
 
        1, 1, 2, 2, 4, 4, 8, 8, 4, 4, 8, 8, 1, 1, 2, 2, -1, -1, 0
 };
index 50e43bb2baf416521d2baac23bc3ec41908ad1bd..b80b1156b6a9e353c8146ae6ba65058956847dfd 100644 (file)
@@ -37,6 +37,7 @@ class IoTRMIUtil {
                static int              hashCode(string str);
                static char*    getHashCodeBytes(string methodSign, char* bytes);
                int                     getTypeSize(string type);
                static int              hashCode(string str);
                static char*    getHashCodeBytes(string methodSign, char* bytes);
                int                     getTypeSize(string type);
+               int                             getVarTypeSize(string type, void* paramObj);
                static int              getArrStringLength(vector<string> arrString);
                static int              getByteStringLength(vector<string> arrString);
                
                static int              getArrStringLength(vector<string> arrString);
                static int              getByteStringLength(vector<string> arrString);
                
@@ -97,8 +98,7 @@ class IoTRMIUtil {
                
        private:
                map<string,string>      mapPrimitives;
                
        private:
                map<string,string>      mapPrimitives;
-               map<string,int>         mapPrimitiveSizesJava;
-               map<string,int>         mapPrimitiveSizesCplus;
+               map<string,int>         mapPrimitiveSizes;
                map<string,string>      mapNonPrimitives;
 };
 
                map<string,string>      mapNonPrimitives;
 };
 
@@ -111,10 +111,8 @@ IoTRMIUtil::IoTRMIUtil() {
                IoTRMITypes::primitivesJava + sizeof(IoTRMITypes::primitivesJava)/sizeof(string));
        std::vector<string> primCplus (IoTRMITypes::primitivesCplus, 
                IoTRMITypes::primitivesCplus + sizeof(IoTRMITypes::primitivesCplus)/sizeof(string));
                IoTRMITypes::primitivesJava + sizeof(IoTRMITypes::primitivesJava)/sizeof(string));
        std::vector<string> primCplus (IoTRMITypes::primitivesCplus, 
                IoTRMITypes::primitivesCplus + sizeof(IoTRMITypes::primitivesCplus)/sizeof(string));
-       std::vector<int> primJavaSizes (IoTRMITypes::primitivesJavaSizes, 
-               IoTRMITypes::primitivesJavaSizes + sizeof(IoTRMITypes::primitivesJavaSizes)/sizeof(int));
-       std::vector<int> primCplusSizes (IoTRMITypes::primitivesCplusSizes, 
-               IoTRMITypes::primitivesCplusSizes + sizeof(IoTRMITypes::primitivesCplusSizes)/sizeof(int));
+       std::vector<int> primSizes (IoTRMITypes::primitivesSizes, 
+               IoTRMITypes::primitivesSizes + sizeof(IoTRMITypes::primitivesSizes)/sizeof(int));
        std::vector<string> nonPrimJava (IoTRMITypes::nonPrimitivesJava, 
                IoTRMITypes::nonPrimitivesJava + sizeof(IoTRMITypes::nonPrimitivesJava)/sizeof(string));
        std::vector<string> nonPrimCplus (IoTRMITypes::nonPrimitivesCplus, 
        std::vector<string> nonPrimJava (IoTRMITypes::nonPrimitivesJava, 
                IoTRMITypes::nonPrimitivesJava + sizeof(IoTRMITypes::nonPrimitivesJava)/sizeof(string));
        std::vector<string> nonPrimCplus (IoTRMITypes::nonPrimitivesCplus, 
@@ -123,8 +121,7 @@ IoTRMIUtil::IoTRMIUtil() {
 
        // Write into maps
        IoTRMITypes::arraysToMap(mapPrimitives, primJava, primCplus);
 
        // Write into maps
        IoTRMITypes::arraysToMap(mapPrimitives, primJava, primCplus);
-       IoTRMITypes::arraysToMap(mapPrimitiveSizesJava, primJava, primJavaSizes); 
-       IoTRMITypes::arraysToMap(mapPrimitiveSizesCplus, primJava, primCplusSizes); 
+       IoTRMITypes::arraysToMap(mapPrimitiveSizes, primJava, primSizes); 
        IoTRMITypes::arraysToMap(mapNonPrimitives, nonPrimJava, nonPrimCplus);
 }
 
        IoTRMITypes::arraysToMap(mapNonPrimitives, nonPrimJava, nonPrimCplus);
 }
 
@@ -175,13 +172,55 @@ char* IoTRMIUtil::getHashCodeBytes(string methodSign, char* bytes) {
 int IoTRMIUtil::getTypeSize(string type) {
 
        // Handle the types and find the sizes
 int IoTRMIUtil::getTypeSize(string type) {
 
        // Handle the types and find the sizes
-       if (mapPrimitiveSizesCplus.find(type) != mapPrimitiveSizesCplus.end())
-               return mapPrimitiveSizesCplus.find(type)->second;
+       if (mapPrimitiveSizes.find(type) != mapPrimitiveSizes.end())
+               return mapPrimitiveSizes.find(type)->second;
        else
                return -1; // Size is unknown
 }
 
 
        else
                return -1; // Size is unknown
 }
 
 
+// Get variable type size, e.g. strings, arrays, etc.
+int IoTRMIUtil::getVarTypeSize(string type, void* paramObj) {
+
+       int paramLen = 0;
+       if (type.compare("string") == 0) {
+               // Get the length of the string through void* casting to string*
+               paramLen = (*(string*)paramObj).length();
+       } else if (type.compare("string[]") == 0) {
+               paramLen = IoTRMIUtil::getByteStringLength(*(vector<string>*) paramObj);
+       } else if (type.compare("byte[]") == 0) {
+               int dataSize = getTypeSize("byte");
+               paramLen = (*(vector<char>*) paramObj).size() * dataSize;
+       } else if (type.compare("short[]") == 0) {
+               int dataSize = getTypeSize("short");
+               paramLen = (*(vector<short>*) paramObj).size() * dataSize;
+       } else if (type.compare("int[]") == 0) {
+               int dataSize = getTypeSize("int");
+               paramLen = (*(vector<int>*) paramObj).size() * dataSize;
+       } else if (type.compare("long[]") == 0) {
+               int dataSize = getTypeSize("long");
+               paramLen = (*(vector<int64_t>*) paramObj).size() * dataSize;
+       } else if (type.compare("float[]") == 0) {
+               int dataSize = getTypeSize("float");
+               paramLen = (*(vector<float>*) paramObj).size() * dataSize;
+       } else if (type.compare("double[]") == 0) {
+               int dataSize = getTypeSize("double");
+               paramLen = (*(vector<double>*) paramObj).size() * dataSize;
+       } else if (type.compare("bool[]") == 0) {
+               int dataSize = getTypeSize("bool");
+               paramLen = (*(vector<bool>*) paramObj).size() * dataSize;
+       } else if (type.compare("char[]") == 0) {
+               int dataSize = getTypeSize("char");
+               paramLen = (*(vector<char>*) paramObj).size() * dataSize;
+       } else {
+               string error = "IoTRMICall: Unrecognizable type: " + type;
+               throw error;
+       }
+
+       return paramLen;
+}
+
+
 int IoTRMIUtil::getArrStringLength(vector<string> arrString) {
 
        int len = 0;
 int IoTRMIUtil::getArrStringLength(vector<string> arrString) {
 
        int len = 0;
index b70d237d058b655b4a1b6a05cd64885d13454fa8..d9229ab079485286c0681f2ff0d63866b2a7c853 100644 (file)
@@ -10,10 +10,14 @@ int main(int argc, char *argv[])
        TestClassInterface *tc = new TestClass();
        cout << "Return value: " << tc->setAndGetA(123) << endl;
        cout << "Return value: " << tc->setACAndGetA("string", 123) << endl;
        TestClassInterface *tc = new TestClass();
        cout << "Return value: " << tc->setAndGetA(123) << endl;
        cout << "Return value: " << tc->setACAndGetA("string", 123) << endl;
-       vector<string> input;
+       /*vector<string> input;
        input.push_back("123");
        input.push_back("456");
        input.push_back("123");
        input.push_back("456");
-       input.push_back("987");
+       input.push_back("987");*/
+       vector<int> input;
+       input.push_back(123);
+       input.push_back(456);
+       input.push_back(987);
 
        cout << "Return value: " << tc->sumArray(input) << endl;
 
 
        cout << "Return value: " << tc->sumArray(input) << endl;
 
index 975481ab94298bb256bc57109801971f33a59489..891e44e75d987b9f05883401953d74fe17a7e547 100644 (file)
@@ -12,7 +12,8 @@ class TestClass : public TestClassInterface {
                void                            setA(int _int);
                void                            setB(float _float);
                void                            setC(string _string);
                void                            setA(int _int);
                void                            setB(float _float);
                void                            setC(string _string);
-               string                          sumArray(vector<string> newA);
+               //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                                     setAndGetA(int newA);
                int                                     setACAndGetA(string newC, int newA);
                //void                          registerCallback(CallBackInterface _cb);
@@ -63,7 +64,7 @@ void TestClass::setC(string _string) {
 }
 
 
 }
 
 
-string TestClass::sumArray(vector<string> newA) {
+/*string TestClass::sumArray(vector<string> newA) {
 
        string sum = "";
        int len = newA.size();
 
        string sum = "";
        int len = newA.size();
@@ -71,6 +72,17 @@ string TestClass::sumArray(vector<string> newA) {
                sum = sum + newA[c];
        }
        return sum;
                sum = sum + newA[c];
        }
        return sum;
+}*/
+
+
+int64_t TestClass::sumArray(vector<int> newA) {
+
+       int64_t sum = 0;
+       int len = newA.size();
+       for(int c = 0; c < len; c++) {
+               sum = sum + newA[c];
+       }
+       return sum;
 }
 
 
 }
 
 
index 0f05a937fcc9b52e77bc59f0c0b7fe550cd14850..5ad53a87f49fc11b2c14625d875f27b167188887 100644 (file)
@@ -8,7 +8,8 @@ class TestClassInterface {
                virtual void    setA(int _int) = 0;
                virtual void    setB(float _float) = 0;
                virtual void    setC(string _string) = 0;
                virtual void    setA(int _int) = 0;
                virtual void    setB(float _float) = 0;
                virtual void    setC(string _string) = 0;
-               virtual string  sumArray(vector<string> newA) = 0;
+               //virtual string        sumArray(vector<string> newA) = 0;
+               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             setAndGetA(int newA) = 0;
                virtual int             setACAndGetA(string newC, int newA) = 0;
                //virtual void  registerCallback(CallBackInterface _cb);
index 95afe9b7e35b4c01614a7bfdbdd51c5c981e6c7a..052737841dc16a0427f54635a04ef1391b9595d9 100644 (file)
@@ -22,7 +22,8 @@ class TestClass_Skeleton {
                        "voidsetA(int)",
                        "voidsetB(float)",
                        "voidsetC(string)",
                        "voidsetA(int)",
                        "voidsetB(float)",
                        "voidsetC(string)",
-                       "sumArray(string[])",
+                       //"sumArray(string[])",
+                       "sumArray(int[])",
                        "intsetAndGetA(int)",
                        "intsetACAndGetA(string,int)",
                        "intcallBack()",
                        "intsetAndGetA(int)",
                        "intsetACAndGetA(string,int)",
                        "intcallBack()",
@@ -78,7 +79,7 @@ void TestClass_Skeleton::waitRequestInvokeMethod() {
                        void* paramObj[] = { &param1 };
                        rmiObj->getMethodParams(paramCls, numParam, paramObj);
                        tc->setC(param1);
                        void* paramObj[] = { &param1 };
                        rmiObj->getMethodParams(paramCls, numParam, paramObj);
                        tc->setC(param1);
-               } else if (methodSign.compare("sumArray(string[])") == 0) {
+               /*} else if (methodSign.compare("sumArray(string[])") == 0) {
                        string paramCls[] = { "string[]" };
                        int numParam = 1;
                        vector<string> param1;
                        string paramCls[] = { "string[]" };
                        int numParam = 1;
                        vector<string> param1;
@@ -86,7 +87,16 @@ void TestClass_Skeleton::waitRequestInvokeMethod() {
                        rmiObj->getMethodParams(paramCls, numParam, paramObj);
                        string retVal = tc->sumArray(param1);
                        void* retObj = &retVal;
                        rmiObj->getMethodParams(paramCls, numParam, paramObj);
                        string retVal = tc->sumArray(param1);
                        void* retObj = &retVal;
-                       rmiObj->sendReturnObj(retObj, "string");
+                       rmiObj->sendReturnObj(retObj, "string");*/
+               } else if (methodSign.compare("sumArray(int[])") == 0) {
+                       string paramCls[] = { "int[]" };
+                       int numParam = 1;
+                       vector<int> param1;
+                       void* paramObj[] = { &param1 };
+                       rmiObj->getMethodParams(paramCls, numParam, paramObj);
+                       int64_t retVal = tc->sumArray(param1);          
+                       void* retObj = &retVal;
+                       rmiObj->sendReturnObj(retObj, "long");
                } else if (methodSign.compare("intsetAndGetA(int)") == 0) {
                        string paramCls[] = { "int" };
                        int numParam = 1;
                } else if (methodSign.compare("intsetAndGetA(int)") == 0) {
                        string paramCls[] = { "int" };
                        int numParam = 1;
index 77cea1650243ed5571f802c413c56eea68776ede..7870578175dabcc133043b2927313570c2bb3277 100644 (file)
@@ -15,10 +15,14 @@ int main(int argc, char *argv[])
        TestClassInterface *tcStub = new TestClass_Stub(port, address, rev, &bResult);
        cout << "Return value: " << tcStub->setAndGetA(123) << endl;
        cout << "Return value: " << tcStub->setACAndGetA("string", 123) << endl;
        TestClassInterface *tcStub = new TestClass_Stub(port, address, rev, &bResult);
        cout << "Return value: " << tcStub->setAndGetA(123) << endl;
        cout << "Return value: " << tcStub->setACAndGetA("string", 123) << endl;
-       vector<string> input;
+       /*vector<string> input;
        input.push_back("123");
        input.push_back("456");
        input.push_back("123");
        input.push_back("456");
-       input.push_back("987");
+       input.push_back("987");*/
+       vector<int> input;
+       input.push_back(123);
+       input.push_back(456);
+       input.push_back(987);
 
        cout << "Return value: " << tcStub->sumArray(input) << endl;
 
 
        cout << "Return value: " << tcStub->sumArray(input) << endl;
 
index 057643d70a63e5502ee7fa2236e3426c98fdaacd..1d73cbe2a43d43cd231d2c00f116b067a5ba3b54 100644 (file)
@@ -13,7 +13,8 @@ class TestClass_Stub : public TestClassInterface {
                void                            setA(int _int);
                void                            setB(float _float);
                void                            setC(string _string);
                void                            setA(int _int);
                void                            setB(float _float);
                void                            setC(string _string);
-               string                          sumArray(vector<string> newA);
+               //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                                     setAndGetA(int newA);
                int                                     setACAndGetA(string newC, int newA);
                //void                          registerCallback(CallBackInterface _cb);
@@ -89,7 +90,7 @@ void TestClass_Stub::setC(string _string) {
 }
 
 
 }
 
 
-string TestClass_Stub::sumArray(vector<string> newA) {
+/*string TestClass_Stub::sumArray(vector<string> newA) {
 
        int numParam = 1;
        string sign = "sumArray(string[])";
 
        int numParam = 1;
        string sign = "sumArray(string[])";
@@ -100,9 +101,24 @@ string TestClass_Stub::sumArray(vector<string> newA) {
        void* retObj = &retVal;
        rmiCall->remoteCall(sign, retType, paramCls, paramObj, numParam, retObj);
        return retVal;
        void* retObj = &retVal;
        rmiCall->remoteCall(sign, retType, paramCls, paramObj, numParam, retObj);
        return retVal;
+}*/
+
+
+int64_t TestClass_Stub::sumArray(vector<int> newA) {
+
+       int numParam = 1;
+       string sign = "sumArray(int[])";
+       string retType = "long";
+       string paramCls[] = { "int[]" };
+       void* paramObj[] = { &newA };
+       int64_t retVal = 0;
+       void* retObj = &retVal;
+       rmiCall->remoteCall(sign, retType, paramCls, paramObj, numParam, retObj);
+       return retVal;
 }
 
 
 }
 
 
+
 int TestClass_Stub::setAndGetA(int newA) {
 
        int numParam = 1;
 int TestClass_Stub::setAndGetA(int newA) {
 
        int numParam = 1;