From: rtrimana Date: Thu, 27 Oct 2016 21:59:45 +0000 (-0700) Subject: Refactoring type size handler; using Java types as standard X-Git-Url: http://plrg.eecs.uci.edu/git/?p=iot2.git;a=commitdiff_plain;h=87ac04592d1d46c7474cad5b046fdf1ecef3dcd1 Refactoring type size handler; using Java types as standard --- diff --git a/iotjava/iotrmi/C++/IoTRMICall.hpp b/iotjava/iotrmi/C++/IoTRMICall.hpp index eb767dd..11797f9 100644 --- a/iotjava/iotrmi/C++/IoTRMICall.hpp +++ b/iotjava/iotrmi/C++/IoTRMICall.hpp @@ -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*) 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*) 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); diff --git a/iotjava/iotrmi/C++/IoTRMIObject.hpp b/iotjava/iotrmi/C++/IoTRMIObject.hpp index a512dd2..5ffb81e 100644 --- a/iotjava/iotrmi/C++/IoTRMIObject.hpp +++ b/iotjava/iotrmi/C++/IoTRMIObject.hpp @@ -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*) retObj); - } else { - string error = "IoTRMICall: Unrecognizable type: " + type; - throw error; - } + retLen = rmiUtil->getVarTypeSize(type, retObj); } // Need object bytes variable char retObjBytes[retLen]; diff --git a/iotjava/iotrmi/C++/IoTRMITypes.hpp b/iotjava/iotrmi/C++/IoTRMITypes.hpp index 2c32f47..629543d 100644 --- a/iotjava/iotrmi/C++/IoTRMITypes.hpp +++ b/iotjava/iotrmi/C++/IoTRMITypes.hpp @@ -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 }; diff --git a/iotjava/iotrmi/C++/IoTRMIUtil.hpp b/iotjava/iotrmi/C++/IoTRMIUtil.hpp index 50e43bb..b80b115 100644 --- a/iotjava/iotrmi/C++/IoTRMIUtil.hpp +++ b/iotjava/iotrmi/C++/IoTRMIUtil.hpp @@ -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 arrString); static int getByteStringLength(vector arrString); @@ -97,8 +98,7 @@ class IoTRMIUtil { private: map mapPrimitives; - map mapPrimitiveSizesJava; - map mapPrimitiveSizesCplus; + map mapPrimitiveSizes; map mapNonPrimitives; }; @@ -111,10 +111,8 @@ IoTRMIUtil::IoTRMIUtil() { IoTRMITypes::primitivesJava + sizeof(IoTRMITypes::primitivesJava)/sizeof(string)); std::vector primCplus (IoTRMITypes::primitivesCplus, IoTRMITypes::primitivesCplus + sizeof(IoTRMITypes::primitivesCplus)/sizeof(string)); - std::vector primJavaSizes (IoTRMITypes::primitivesJavaSizes, - IoTRMITypes::primitivesJavaSizes + sizeof(IoTRMITypes::primitivesJavaSizes)/sizeof(int)); - std::vector primCplusSizes (IoTRMITypes::primitivesCplusSizes, - IoTRMITypes::primitivesCplusSizes + sizeof(IoTRMITypes::primitivesCplusSizes)/sizeof(int)); + std::vector primSizes (IoTRMITypes::primitivesSizes, + IoTRMITypes::primitivesSizes + sizeof(IoTRMITypes::primitivesSizes)/sizeof(int)); std::vector nonPrimJava (IoTRMITypes::nonPrimitivesJava, IoTRMITypes::nonPrimitivesJava + sizeof(IoTRMITypes::nonPrimitivesJava)/sizeof(string)); std::vector 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*) paramObj); + } else if (type.compare("byte[]") == 0) { + int dataSize = getTypeSize("byte"); + paramLen = (*(vector*) paramObj).size() * dataSize; + } else if (type.compare("short[]") == 0) { + int dataSize = getTypeSize("short"); + paramLen = (*(vector*) paramObj).size() * dataSize; + } else if (type.compare("int[]") == 0) { + int dataSize = getTypeSize("int"); + paramLen = (*(vector*) paramObj).size() * dataSize; + } else if (type.compare("long[]") == 0) { + int dataSize = getTypeSize("long"); + paramLen = (*(vector*) paramObj).size() * dataSize; + } else if (type.compare("float[]") == 0) { + int dataSize = getTypeSize("float"); + paramLen = (*(vector*) paramObj).size() * dataSize; + } else if (type.compare("double[]") == 0) { + int dataSize = getTypeSize("double"); + paramLen = (*(vector*) paramObj).size() * dataSize; + } else if (type.compare("bool[]") == 0) { + int dataSize = getTypeSize("bool"); + paramLen = (*(vector*) paramObj).size() * dataSize; + } else if (type.compare("char[]") == 0) { + int dataSize = getTypeSize("char"); + paramLen = (*(vector*) paramObj).size() * dataSize; + } else { + string error = "IoTRMICall: Unrecognizable type: " + type; + throw error; + } + + return paramLen; +} + + int IoTRMIUtil::getArrStringLength(vector arrString) { int len = 0; diff --git a/iotjava/iotrmi/C++/sample/TestClass.cpp b/iotjava/iotrmi/C++/sample/TestClass.cpp index b70d237..d9229ab 100644 --- a/iotjava/iotrmi/C++/sample/TestClass.cpp +++ b/iotjava/iotrmi/C++/sample/TestClass.cpp @@ -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 input; + /*vector input; input.push_back("123"); input.push_back("456"); - input.push_back("987"); + input.push_back("987");*/ + vector input; + input.push_back(123); + input.push_back(456); + input.push_back(987); cout << "Return value: " << tc->sumArray(input) << endl; diff --git a/iotjava/iotrmi/C++/sample/TestClass.hpp b/iotjava/iotrmi/C++/sample/TestClass.hpp index 975481a..891e44e 100644 --- a/iotjava/iotrmi/C++/sample/TestClass.hpp +++ b/iotjava/iotrmi/C++/sample/TestClass.hpp @@ -12,7 +12,8 @@ class TestClass : public TestClassInterface { void setA(int _int); void setB(float _float); void setC(string _string); - string sumArray(vector newA); + //string sumArray(vector newA); + int64_t sumArray(vector 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 newA) { +/*string TestClass::sumArray(vector newA) { string sum = ""; int len = newA.size(); @@ -71,6 +72,17 @@ string TestClass::sumArray(vector newA) { sum = sum + newA[c]; } return sum; +}*/ + + +int64_t TestClass::sumArray(vector newA) { + + int64_t sum = 0; + int len = newA.size(); + for(int c = 0; c < len; c++) { + sum = sum + newA[c]; + } + return sum; } diff --git a/iotjava/iotrmi/C++/sample/TestClassInterface.hpp b/iotjava/iotrmi/C++/sample/TestClassInterface.hpp index 0f05a93..5ad53a8 100644 --- a/iotjava/iotrmi/C++/sample/TestClassInterface.hpp +++ b/iotjava/iotrmi/C++/sample/TestClassInterface.hpp @@ -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 newA) = 0; + //virtual string sumArray(vector newA) = 0; + 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); diff --git a/iotjava/iotrmi/C++/sample/TestClass_Skeleton.hpp b/iotjava/iotrmi/C++/sample/TestClass_Skeleton.hpp index 95afe9b..0527378 100644 --- a/iotjava/iotrmi/C++/sample/TestClass_Skeleton.hpp +++ b/iotjava/iotrmi/C++/sample/TestClass_Skeleton.hpp @@ -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[] = { ¶m1 }; 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 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 param1; + void* paramObj[] = { ¶m1 }; + 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; diff --git a/iotjava/iotrmi/C++/sample/TestClass_Stub.cpp b/iotjava/iotrmi/C++/sample/TestClass_Stub.cpp index 77cea16..7870578 100644 --- a/iotjava/iotrmi/C++/sample/TestClass_Stub.cpp +++ b/iotjava/iotrmi/C++/sample/TestClass_Stub.cpp @@ -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 input; + /*vector input; input.push_back("123"); input.push_back("456"); - input.push_back("987"); + input.push_back("987");*/ + vector input; + input.push_back(123); + input.push_back(456); + input.push_back(987); cout << "Return value: " << tcStub->sumArray(input) << endl; diff --git a/iotjava/iotrmi/C++/sample/TestClass_Stub.hpp b/iotjava/iotrmi/C++/sample/TestClass_Stub.hpp index 057643d..1d73cbe 100644 --- a/iotjava/iotrmi/C++/sample/TestClass_Stub.hpp +++ b/iotjava/iotrmi/C++/sample/TestClass_Stub.hpp @@ -13,7 +13,8 @@ class TestClass_Stub : public TestClassInterface { void setA(int _int); void setB(float _float); void setC(string _string); - string sumArray(vector newA); + //string sumArray(vector newA); + int64_t sumArray(vector 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 newA) { +/*string TestClass_Stub::sumArray(vector newA) { int numParam = 1; string sign = "sumArray(string[])"; @@ -100,9 +101,24 @@ string TestClass_Stub::sumArray(vector newA) { void* retObj = &retVal; rmiCall->remoteCall(sign, retType, paramCls, paramObj, numParam, retObj); return retVal; +}*/ + + +int64_t TestClass_Stub::sumArray(vector 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;