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 eb767dd..11797f9 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);
+               IoTRMIUtil::printBytes(retObjBytes, retLen, false);
                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
-                       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;
                }
@@ -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
-                       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);
index a512dd2..5ffb81e 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) {
-               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];
index 2c32f47..629543d 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
 };
index 50e43bb..b80b115 100644 (file)
@@ -37,6 +37,7 @@ class IoTRMIUtil {
                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);
                
@@ -97,8 +98,7 @@ class IoTRMIUtil {
                
        private:
                map<string,string>      mapPrimitives;
-               map<string,int>         mapPrimitiveSizesJava;
-               map<string,int>         mapPrimitiveSizesCplus;
+               map<string,int>         mapPrimitiveSizes;
                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));
-       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, 
@@ -123,8 +121,7 @@ IoTRMIUtil::IoTRMIUtil() {
 
        // 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);
 }
 
@@ -175,13 +172,55 @@ char* IoTRMIUtil::getHashCodeBytes(string methodSign, char* bytes) {
 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
 }
 
 
+// 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;
index b70d237..d9229ab 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;
-       vector<string> input;
+       /*vector<string> input;
        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;
 
index 975481a..891e44e 100644 (file)
@@ -12,7 +12,8 @@ class TestClass : public TestClassInterface {
                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);
@@ -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();
@@ -71,6 +72,17 @@ string TestClass::sumArray(vector<string> newA) {
                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 0f05a93..5ad53a8 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 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);
index 95afe9b..0527378 100644 (file)
@@ -22,7 +22,8 @@ class TestClass_Skeleton {
                        "voidsetA(int)",
                        "voidsetB(float)",
                        "voidsetC(string)",
-                       "sumArray(string[])",
+                       //"sumArray(string[])",
+                       "sumArray(int[])",
                        "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);
-               } else if (methodSign.compare("sumArray(string[])") == 0) {
+               /*} else if (methodSign.compare("sumArray(string[])") == 0) {
                        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->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;
index 77cea16..7870578 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;
-       vector<string> input;
+       /*vector<string> input;
        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;
 
index 057643d..1d73cbe 100644 (file)
@@ -13,7 +13,8 @@ class TestClass_Stub : public TestClassInterface {
                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);
@@ -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[])";
@@ -100,9 +101,24 @@ string TestClass_Stub::sumArray(vector<string> newA) {
        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;