X-Git-Url: http://plrg.eecs.uci.edu/git/?p=iot2.git;a=blobdiff_plain;f=iotjava%2Fiotrmi%2FC%2B%2B%2FIoTRMIUtil.hpp;h=2f18adc5952f4d44ab9dbc0bc3477cb576f22525;hp=f42241edd94c79a65c6a8ae701b701d359e26d1a;hb=903f2886824222a912502db3fdf432e4351141e6;hpb=85fe913ad1c6f10b89ad73476315ed8e2453e080 diff --git a/iotjava/iotrmi/C++/IoTRMIUtil.hpp b/iotjava/iotrmi/C++/IoTRMIUtil.hpp index f42241e..2f18adc 100644 --- a/iotjava/iotrmi/C++/IoTRMIUtil.hpp +++ b/iotjava/iotrmi/C++/IoTRMIUtil.hpp @@ -16,6 +16,7 @@ #include #include #include +#include #include #include @@ -26,7 +27,7 @@ using namespace std; -class IoTRMIUtil { +class IoTRMIUtil final { public: IoTRMIUtil(); @@ -42,6 +43,7 @@ class IoTRMIUtil { static int getByteStringLength(vector arrString); // Primitives to byte array + static char* byteToByteArray(char c, char* bytes); static char* shortToByteArray(short i, char* bytes); static char* intToByteArray(int i, char* bytes); static char* longToByteArray(int64_t i, char* bytes); @@ -52,6 +54,7 @@ class IoTRMIUtil { static char* stringToByteArray(string c, char* bytes); // Byte array to primitives + static char* byteArrayToByte(char* result, char* bytes); static short* byteArrayToShort(short* result, char* bytes); static int* byteArrayToInt(int* result, char* bytes); static int64_t* byteArrayToLong(int64_t* result, char* bytes); @@ -67,6 +70,7 @@ class IoTRMIUtil { static char* getObjectBytes(char* retObjBytes, void* obj, const char* type); // Arrays to bytes + static char* arrByteToByteArray(vector arrByte, char* bytes); static char* arrShortToByteArray(vector arrShort, char* bytes); static char* arrIntToByteArray(vector arrInt, char* bytes); static char* arrLongToByteArray(vector arrInt, char* bytes); @@ -77,6 +81,7 @@ class IoTRMIUtil { static char* arrStringToByteArray(vector arrString, char* bytes); // Bytes to array + static vector* byteArrayToByteArray(vector* result, char* bytes, int len); static vector* byteArrayToShortArray(vector* result, char* bytes, int len); static vector* byteArrayToIntArray(vector* result, char* bytes, int len); static vector* byteArrayToLongArray(vector* result, char* bytes, int len); @@ -93,10 +98,19 @@ class IoTRMIUtil { // 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 PACKET_TYPE_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 RETURN_LEN = 4; // 4 bytes = 32 bits (4-byte field that stores the length of the return object) const static int CHAR_LEN = 2; // 2 bytes (we follow Java convention) + const static int BYTE_LEN = 1; // 1 byte const static int BOOL_LEN = 1; // 1 byte + const static int METHOD_TYPE = 1; // Packet type of method + const static int RET_VAL_TYPE = -1; // Packet type of return value + + // Static containers + static map* mapStub; // Map object to its stub ID + static map* mapSkel; // Map object to its skeleton + static map* mapSkelId; // Map object to its skeleton ID private: map mapPrimitives; @@ -104,6 +118,10 @@ class IoTRMIUtil { map mapNonPrimitives; }; +map* IoTRMIUtil::mapStub = new map(); +map* IoTRMIUtil::mapSkel = new map(); +map* IoTRMIUtil::mapSkelId = new map(); + // Constructor IoTRMIUtil::IoTRMIUtil() { @@ -185,38 +203,56 @@ int IoTRMIUtil::getTypeSize(string type) { int IoTRMIUtil::getVarTypeSize(string type, void* paramObj) { int paramLen = 0; - if (type.compare("string") == 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) { + } else if ( (type.compare("String*") == 0) || + (type.compare("string*") == 0) || + (type.compare("vector") == 0)) { paramLen = IoTRMIUtil::getByteStringLength(*(vector*) paramObj); - } else if (type.compare("byte[]") == 0) { + } else if ( (type.compare("byte*") == 0) || + (type.compare("Byte*") == 0) || + (type.compare("vector") == 0)) { int dataSize = getTypeSize("byte"); paramLen = (*(vector*) paramObj).size() * dataSize; - } else if (type.compare("short[]") == 0) { + } else if ( (type.compare("short*") == 0) || + (type.compare("Short*") == 0) || + (type.compare("vector") == 0)) { int dataSize = getTypeSize("short"); paramLen = (*(vector*) paramObj).size() * dataSize; - } else if (type.compare("int[]") == 0) { + } else if ( (type.compare("int*") == 0) || + (type.compare("Integer*") == 0) || + (type.compare("vector") == 0)) { int dataSize = getTypeSize("int"); paramLen = (*(vector*) paramObj).size() * dataSize; - } else if (type.compare("long[]") == 0) { + } else if ( (type.compare("long*") == 0) || + (type.compare("Long*") == 0) || + (type.compare("vector") == 0)) { int dataSize = getTypeSize("long"); paramLen = (*(vector*) paramObj).size() * dataSize; - } else if (type.compare("float[]") == 0) { + } else if ( (type.compare("float*") == 0) || + (type.compare("Float*") == 0) || + (type.compare("vector") == 0)) { int dataSize = getTypeSize("float"); paramLen = (*(vector*) paramObj).size() * dataSize; - } else if (type.compare("double[]") == 0) { + } else if ( (type.compare("double*") == 0) || + (type.compare("Double*") == 0) || + (type.compare("vector") == 0)) { int dataSize = getTypeSize("double"); paramLen = (*(vector*) paramObj).size() * dataSize; - } else if (type.compare("bool[]") == 0) { - int dataSize = getTypeSize("bool"); + } else if ( (type.compare("boolean*") == 0) || + (type.compare("Boolean*") == 0) || + (type.compare("vector") == 0)) { + int dataSize = getTypeSize("boolean"); paramLen = (*(vector*) paramObj).size() * dataSize; - } else if (type.compare("char[]") == 0) { + } else if ( (type.compare("char*") == 0) || + (type.compare("Character*") == 0) || + (type.compare("vector") == 0)) { int dataSize = getTypeSize("char"); paramLen = (*(vector*) paramObj).size() * dataSize; } else { - string error = "IoTRMICall: Unrecognizable type: " + type; - throw error; + cerr << "IoTRMIUtil: Unrecognizable type: " << type << endl; + exit(-1); } return paramLen; @@ -249,7 +285,7 @@ void* IoTRMIUtil::getParamObject(void* retObj, const char* type, char* paramByte if (strcmp(type, "b") == 0 || strcmp(type, "byte") == 0) { - retObj = (void*) ¶mBytes[0]; + retObj = (void*) byteArrayToByte((char*) retObj, paramBytes); } else if ( strcmp(type, "s") == 0 || strcmp(type, "short") == 0) { retObj = (void*) byteArrayToShort((short*) retObj, paramBytes); @@ -266,50 +302,71 @@ void* IoTRMIUtil::getParamObject(void* retObj, const char* type, char* paramByte strcmp(type, "double") == 0) { retObj = (void*) byteArrayToDouble((double*) retObj, paramBytes); } else if ( strcmp(type, "b") == 0 || - strcmp(type, "bool") == 0) { + strcmp(type, "boolean") == 0) { retObj = (void*) byteArrayToBoolean((bool*) retObj, paramBytes); } else if ( strcmp(type, "c") == 0 || strcmp(type, "char") == 0) { retObj = (void*) byteArrayToChar((char*) retObj, paramBytes); } else if ( strcmp(type, "Ss") == 0 || - strcmp(type, "string") == 0) { + strcmp(type, "String") == 0) { retObj = (void*) byteArrayToString((string*) retObj, paramBytes, len); - } else if ( string(type).find("[]") != string::npos) { + } else if ( string(type).find("*") != string::npos) { // This is an array type, i.e. vector retObj = getArrayParamObject(retObj, type, paramBytes, len); + } else if ( (string(type).find("<") != string::npos) && + (string(type).find(">") != string::npos)) { + // This is a vector/list type + retObj = getArrayParamObject(retObj, type, paramBytes, len); } else { - string error = "IoTRMIUtil: Unrecognizable type: " + string(type); - throw error; + cerr << "IoTRMIUtil: Unrecognizable type: " << type << endl; + exit(-1); } return retObj; } -// Get array of objects from byte array - overload getParamObject function +// Get array of objects from byte array void* IoTRMIUtil::getArrayParamObject(void* retObj, const char* type, char* paramBytes, int len) { - if (strcmp(type, "byte[]") == 0) { - retObj = (vector*) paramBytes; - } else if (strcmp(type, "short[]") == 0) { + if ((strcmp(type, "byte*") == 0) || + (strcmp(type, "Byte*") == 0) || + (strcmp(type, "vector") == 0)) { + retObj = byteArrayToByteArray((vector*) retObj, paramBytes, len); + } else if ( (strcmp(type, "short*") == 0) || + (strcmp(type, "Short*") == 0) || + (strcmp(type, "vector") == 0)) { retObj = byteArrayToShortArray((vector*) retObj, paramBytes, len); - } else if (strcmp(type, "int[]") == 0) { + } else if ( (strcmp(type, "int*") == 0) || + (strcmp(type, "Integer*") == 0) || + (strcmp(type, "vector") == 0)) { retObj = byteArrayToIntArray((vector*) retObj, paramBytes, len); - } else if (strcmp(type, "long[]") == 0) { + } else if ( (strcmp(type, "long*") == 0) || + (strcmp(type, "Long*") == 0) || + (strcmp(type, "vector") == 0)) { retObj = byteArrayToLongArray((vector*) retObj, paramBytes, len); - } else if (strcmp(type, "float[]") == 0) { + } else if ( (strcmp(type, "float*") == 0) || + (strcmp(type, "Float*") == 0) || + (strcmp(type, "vector") == 0)) { retObj = byteArrayToFloatArray((vector*) retObj, paramBytes, len); - } else if (strcmp(type, "double[]") == 0) { + } else if ( (strcmp(type, "double*") == 0) || + (strcmp(type, "Double*") == 0) || + (strcmp(type, "vector") == 0)) { retObj = byteArrayToDoubleArray((vector*) retObj, paramBytes, len); - } else if (strcmp(type, "bool[]") == 0) { + } else if ( (strcmp(type, "boolean*") == 0) || + (strcmp(type, "Boolean*") == 0) || + (strcmp(type, "vector") == 0)) { retObj = byteArrayToBooleanArray((vector*) retObj, paramBytes, len); - } else if (strcmp(type, "char[]") == 0) { + } else if ( (strcmp(type, "char*") == 0) || + (strcmp(type, "Character*") == 0) || + (strcmp(type, "vector") == 0)) { retObj = byteArrayToCharArray((vector*) retObj, paramBytes, len); - } else if (strcmp(type, "string[]") == 0) { + } else if ( (strcmp(type, "String*") == 0) || + (strcmp(type, "vector") == 0)) { retObj = byteArrayToStringArray((vector*) retObj, paramBytes, len); } else { - string error = "IoTRMIUtil: Unrecognizable type: " + string(type); - throw error; + cerr << "IoTRMIUtil: Unrecognizable type: " << type << endl; + exit(-1); } return retObj; @@ -321,7 +378,7 @@ char* IoTRMIUtil::getObjectBytes(char* retObjBytes, void* obj, const char* type) if (strcmp(type, "b") == 0 || strcmp(type, "byte") == 0) { - retObjBytes = (char*) obj; + retObjBytes = byteToByteArray(*((char*) obj), retObjBytes); } else if ( strcmp(type, "s") == 0 || strcmp(type, "short") == 0) { retObjBytes = shortToByteArray(*((short*) obj), retObjBytes); @@ -338,20 +395,24 @@ char* IoTRMIUtil::getObjectBytes(char* retObjBytes, void* obj, const char* type) strcmp(type, "double") == 0) { retObjBytes = doubleToByteArray(*((double*) obj), retObjBytes); } else if ( strcmp(type, "b") == 0 || - strcmp(type, "bool") == 0) { + strcmp(type, "boolean") == 0) { retObjBytes = booleanToByteArray(*((bool*) obj), retObjBytes); } else if ( strcmp(type, "c") == 0 || strcmp(type, "char") == 0) { retObjBytes = charToByteArray(*((char*) obj), retObjBytes); } else if ( strcmp(type, "Ss") == 0 || - strcmp(type, "string") == 0) { + strcmp(type, "String") == 0) { retObjBytes = stringToByteArray(*((string*) obj), retObjBytes); - } else if ( string(type).find("[]") != string::npos) { + } else if ( string(type).find("*") != string::npos) { // This is an array type, i.e. vector retObjBytes = getArrayObjectBytes(retObjBytes, obj, type); + } else if ( (string(type).find("<") != string::npos) && + (string(type).find(">") != string::npos)) { + // This is a vector/list type + retObjBytes = getArrayObjectBytes(retObjBytes, obj, type); } else { - string error = "IoTRMIUtil: Unrecognizable type: " + string(type); - throw error; + cerr << "IoTRMIUtil: Unrecognizable type: " << type << endl; + exit(-1); } return retObjBytes; @@ -361,27 +422,44 @@ char* IoTRMIUtil::getObjectBytes(char* retObjBytes, void* obj, const char* type) // Getting byte array for arrays of primitives char* IoTRMIUtil::getArrayObjectBytes(char* retObjBytes, void* obj, const char* type) { - if (strcmp(type, "byte[]") == 0) { - retObjBytes = (char*) obj; - } else if (strcmp(type, "short[]") == 0) { + if ((strcmp(type, "byte*") == 0) || + (strcmp(type, "Byte*") == 0) || + (strcmp(type, "vector") == 0)) { + retObjBytes = arrByteToByteArray(*((vector*) obj), retObjBytes); + } else if ( (strcmp(type, "short*") == 0) || + (strcmp(type, "Short*") == 0) || + (strcmp(type, "vector") == 0)) { retObjBytes = arrShortToByteArray(*((vector*) obj), retObjBytes); - } else if (strcmp(type, "int[]") == 0) { + } else if ( (strcmp(type, "int*") == 0) || + (strcmp(type, "Integer*") == 0) || + (strcmp(type, "vector") == 0)) { retObjBytes = arrIntToByteArray(*((vector*) obj), retObjBytes); - } else if (strcmp(type, "long[]") == 0) { + } else if ( (strcmp(type, "long*") == 0) || + (strcmp(type, "Long*") == 0) || + (strcmp(type, "vector") == 0)) { retObjBytes = arrLongToByteArray(*((vector*) obj), retObjBytes); - } else if (strcmp(type, "float[]") == 0) { + } else if ( (strcmp(type, "float*") == 0) || + (strcmp(type, "Float*") == 0) || + (strcmp(type, "vector") == 0)) { retObjBytes = arrFloatToByteArray(*((vector*) obj), retObjBytes); - } else if (strcmp(type, "double[]") == 0) { + } else if ( (strcmp(type, "double*") == 0) || + (strcmp(type, "Double*") == 0) || + (strcmp(type, "vector") == 0)) { retObjBytes = arrDoubleToByteArray(*((vector*) obj), retObjBytes); - } else if (strcmp(type, "bool[]") == 0) { + } else if ( (strcmp(type, "boolean*") == 0) || + (strcmp(type, "Boolean*") == 0) || + (strcmp(type, "vector") == 0)) { retObjBytes = arrBooleanToByteArray(*((vector*) obj), retObjBytes); - } else if (strcmp(type, "char[]") == 0) { + } else if ( (strcmp(type, "char*") == 0) || + (strcmp(type, "Character*") == 0) || + (strcmp(type, "vector") == 0)) { retObjBytes = arrCharToByteArray(*((vector*) obj), retObjBytes); - } else if (strcmp(type, "string[]") == 0) { + } else if ( (strcmp(type, "String*") == 0) || + (strcmp(type, "vector") == 0)) { retObjBytes = arrStringToByteArray(*((vector*) obj), retObjBytes); } else { - string error = "IoTRMIUtil: Unrecognizable type: " + string(type); - throw error; + cerr << "IoTRMIUtil: Unrecognizable type: " << type << endl; + exit(-1); } return retObjBytes; @@ -391,6 +469,20 @@ char* IoTRMIUtil::getArrayObjectBytes(char* retObjBytes, void* obj, const char* // Conversions // Array handlers - we use vector data type and not traditional arrays // Array to bytes +char* IoTRMIUtil::arrByteToByteArray(vector arrByte, char* bytes) { + + int pos = 0; + for (char chr : arrByte) { + char tmpBytes[BYTE_LEN]; + byteToByteArray(chr, tmpBytes); + memcpy(bytes + pos, tmpBytes, BYTE_LEN); + pos = pos + BYTE_LEN; + } + + return bytes; +} + + char* IoTRMIUtil::arrShortToByteArray(vector arrShort, char* bytes) { int pos = 0; @@ -516,6 +608,24 @@ char* IoTRMIUtil::arrStringToByteArray(vector arrString, char* bytes) { // Bytes to array +vector* IoTRMIUtil::byteArrayToByteArray(vector* result, char* bytes, int len) { + + // Single element bytes + char elmt[BYTE_LEN]; + // Prepare vector + int arrLen = len/BYTE_LEN; + for(int i = 0; i < arrLen; i++) { + int offset = i * BYTE_LEN; + memcpy(elmt, bytes + offset, BYTE_LEN); + char res; + byteArrayToByte(&res, elmt); + result->push_back(res); + } + + return result; +} + + vector* IoTRMIUtil::byteArrayToShortArray(vector* result, char* bytes, int len) { // Single element bytes @@ -679,6 +789,15 @@ vector* IoTRMIUtil::byteArrayToStringArray(vector* result, char* // Conversions // Primitives to byte array +char* IoTRMIUtil::byteToByteArray(char c, char* bytes) { + + // Just copy the char into char* + bytes[0] = c; + + return bytes; +} + + char* IoTRMIUtil::shortToByteArray(short s, char* bytes) { short sInvert = htobe16(s); @@ -813,6 +932,13 @@ double* IoTRMIUtil::byteArrayToDouble(double* result, char* bytes) { } +char* IoTRMIUtil::byteArrayToByte(char* result, char* bytes) { + + *result = bytes[0]; + return result; +} + + char* IoTRMIUtil::byteArrayToChar(char* result, char* bytes) { *result = bytes[1];