Fixing bugs on char translation; testing for arrays
authorrtrimana <rtrimana@uci.edu>
Mon, 28 Nov 2016 22:50:32 +0000 (14:50 -0800)
committerrtrimana <rtrimana@uci.edu>
Mon, 28 Nov 2016 22:50:32 +0000 (14:50 -0800)
config/iotpolicy/testclasspolicy.pol
iotjava/iotpolicy/IoTCompiler.java
iotjava/iotrmi/C++/IoTRMIUtil.hpp
iotjava/iotrmi/C++/basics/TestClass.hpp
iotjava/iotrmi/C++/basics/TestClass_Stub.cpp
iotjava/iotrmi/Java/basics/TestClass.java
iotjava/iotrmi/Java/basics/TestClass_Stub.java

index 24be14c..e2b70cc 100644 (file)
@@ -7,6 +7,15 @@ public interface TestClassInterface {
        public double getDouble(double in);
        public boolean getBoolean(boolean in);
        public char getChar(char in);
+
+       public byte[] getByteArray(byte in[]);
+       public short[] getShortArray(short in[]);
+       public long[] getLongArray(long in[]);
+       public float[] getFloatArray(float in[]);
+       public double[] getDoubleArray(double in[]);
+       public boolean[] getBooleanArray(boolean in[]);
+       public char[] getCharArray(char in[]);
+
        public int getA();
        public void setA(int _int);
        public void setB(float _float);
@@ -24,6 +33,13 @@ public interface TestClassInterface {
                method = "getDouble(double in)";
                method = "getBoolean(boolean in)";
                method = "getChar(char in)";
+               method = "getByteArray(byte in[])";
+               method = "getShortArray(short in[])";
+               method = "getLongArray(long in[])";
+               method = "getFloatArray(float in[])";
+               method = "getDoubleArray(double in[])";
+               method = "getBooleanArray(boolean in[])";
+               method = "getCharArray(char in[])";
                method = "getA()";
                method = "setA(int _int)";
                method = "setB(float _float)";
index ee3d287..651a4b8 100644 (file)
@@ -327,7 +327,9 @@ public class IoTCompiler {
                        InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(intface);
                        List<String> methods = intDecl.getMethods();
                        Set<String> importClasses = getImportClasses(methods, intDecl);
-                       printImportStatements(importClasses);
+                       List<String> stdImportClasses = getStandardJavaIntfaceImportClasses();
+                       List<String> allImportClasses = getAllLibClasses(stdImportClasses, importClasses);
+                       printImportStatements(allImportClasses);
                        // Write interface header
                        println("");
                        println("public interface " + intface + " {");
@@ -359,13 +361,16 @@ public class IoTCompiler {
                                DeclarationHandler decHandler = mapIntDeclHand.get(intface);
                                InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(intface);
                                // Pass in set of methods and get import classes
-                               Set<String> importClasses = getImportClasses(intMeth.getValue(), intDecl);
-                               printImportStatements(importClasses);
+                               List<String> methods = intDecl.getMethods();
+                               Set<String> importClasses = getImportClasses(methods, intDecl);
+                               List<String> stdImportClasses = getStandardJavaIntfaceImportClasses();
+                               List<String> allImportClasses = getAllLibClasses(stdImportClasses, importClasses);
+                               printImportStatements(allImportClasses);
                                // Write interface header
                                println("");
                                println("public interface " + newIntface + " {\n");
                                // Write methods
-                               writeMethodJavaInterface(intMeth.getValue(), intDecl);
+                               writeMethodJavaInterface(methods, intDecl);
                                println("}");
                                pw.close();
                                System.out.println("IoTCompiler: Generated interface " + newIntface + ".java...");
@@ -2494,6 +2499,9 @@ public class IoTCompiler {
 
                        List<String> methParams = intDecl.getMethodParams(method);
                        List<String> methPrmTypes = intDecl.getMethodParamTypes(method);
+
+                       //System.out.println("\n\nMethod return type: " + checkAndGetCplusType(intDecl.getMethodType(method)) + "\n\n");
+
                        print(checkAndGetCplusType(intDecl.getMethodType(method)) + " " +
                                intDecl.getMethodId(method) + "(");
                        boolean isCallbackMethod = false;
@@ -2561,8 +2569,8 @@ public class IoTCompiler {
                println("int methodId = " + intDecl.getMethodNumId(method) + ";");
                String retType = intDecl.getMethodType(method);
                //String retTypeC = checkAndGetCplusType(retType);
-               //println("string retType = \"" + checkAndGetCplusArrayType(retTypeC) + "\";");
-               println("string retType = \"" + retType + "\";");
+               //println("string retType = \"" + checkAndGetCplusArrayType(getStructType(getEnumType(retTypeC))) + "\";");
+               println("string retType = \"" + checkAndGetCplusRetClsType(getStructType(getEnumType(retType))) + "\";");
                // Generate array of parameter types
                print("string paramCls[] = { ");
                for (int i = 0; i < methParams.size(); i++) {
@@ -2570,10 +2578,10 @@ public class IoTCompiler {
                        if (checkCallbackType(paramType, callbackType)) { // Check if this has callback object
                                print("\"int\"");
                        } else { // Generate normal classes if it's not a callback object
-                               //String paramTypeC = checkAndGetCplusType(methPrmTypes.get(i));
-                               //String prmType = checkAndGetCplusArrayType(paramTypeC, methParams.get(i));
-                               String paramTypeC = checkAndGetArray(methPrmTypes.get(i), methParams.get(i));
-                               String prmType = getSimpleType(getEnumType(paramTypeC));
+                               //String paramTypeC = checkAndGetArray(methPrmTypes.get(i), methParams.get(i));
+                               //String prmType = getSimpleType(getEnumType(paramTypeC));
+                               String paramTypeC = checkAndGetCplusArgClsType(methPrmTypes.get(i), methParams.get(i));
+                               String prmType = getEnumType(paramTypeC);
                                print("\"" + prmType + "\"");
                        }
                        if (i != methParams.size() - 1) // Check if this is the last element
@@ -2896,7 +2904,7 @@ public class IoTCompiler {
                String retType = intDecl.getMethodType(method);
                //String retTypeC = checkAndGetCplusType(retType);
                //println("string retType = \"" + checkAndGetCplusArrayType(getStructType(getEnumType(retTypeC))) + "\";");
-               println("string retType = \"" + retType + "\";");
+               println("string retType = \"" + checkAndGetCplusRetClsType(getStructType(getEnumType(retType))) + "\";");
                // Generate array of parameter types
                if (isStructPresent(methParams, methPrmTypes)) {
                        writeStructParamClassCplusStub(methParams, methPrmTypes);
@@ -2904,11 +2912,10 @@ public class IoTCompiler {
                        println("int numParam = " + methParams.size() + ";");
                        print("string paramCls[] = { ");
                        for (int i = 0; i < methParams.size(); i++) {
-                               //String paramTypeC = checkAndGetCplusType(methPrmTypes.get(i));
-                               //String paramType = checkAndGetCplusArrayType(paramTypeC, methParams.get(i));
-                               //print("\"" + getEnumType(paramType) + "\"");
-                               String paramTypeC = checkAndGetArray(methPrmTypes.get(i), methParams.get(i));
-                               String prmType = getSimpleType(getEnumType(paramTypeC));
+                               //String paramTypeC = checkAndGetArray(methPrmTypes.get(i), methParams.get(i));
+                               //String prmType = getSimpleType(getEnumType(paramTypeC));
+                               String paramTypeC = checkAndGetCplusArgClsType(methPrmTypes.get(i), methParams.get(i));
+                               String prmType = getEnumType(paramTypeC);
                                print("\"" + prmType + "\"");
                                // Check if this is the last element (don't print a comma)
                                if (i != methParams.size() - 1) {
@@ -2941,10 +2948,12 @@ public class IoTCompiler {
                                if (getParamCategory(retType) == ParamCategory.ENUM) {
                                        checkAndWriteEnumRetTypeCplusStub(retType);
                                } else {
-                                       if (getParamCategory(retType) == ParamCategory.NONPRIMITIVES)
+                                       //if (getParamCategory(retType) == ParamCategory.NONPRIMITIVES)
+                                       if (isArrayOrList(retType,retType))
                                                println(checkAndGetCplusType(retType) + " retVal;");
-                                       else
+                                       else {
                                                println(checkAndGetCplusType(retType) + " retVal = " + generateCplusInitializer(retType) + ";");
+                                       }
                                        println("void* retObj = &retVal;");
                                        println("rmiCall->remoteCall(objectId, methodId, retType, paramCls, paramObj, numParam, retObj);");
                                        println("return retVal;");
@@ -3603,8 +3612,7 @@ public class IoTCompiler {
                        if (isStructClass(getSimpleArrayType(getSimpleType(retType)))) // Struct type
                                println("rmiObj->sendReturnObj(retObj, retCls, numRetObj);");
                        else
-                               //println("rmiObj->sendReturnObj(retObj, \"" + getEnumType(checkAndGetCplusArrayType(retTypeC)) + "\");");
-                               println("rmiObj->sendReturnObj(retObj, \"" + getEnumType(retType) + "\");");
+                               println("rmiObj->sendReturnObj(retObj, \"" + checkAndGetCplusRetClsType(getEnumType(retType)) + "\");");
                }
        }
 
@@ -3626,11 +3634,10 @@ public class IoTCompiler {
                                callbackType = paramType;
                                print("\"int\"");
                        } else {        // Generate normal classes if it's not a callback object
-                               //String paramTypeC = checkAndGetCplusType(methPrmTypes.get(i));
-                               //String prmType = checkAndGetCplusArrayType(paramTypeC, methParams.get(i));
-                               //print("\"" + getEnumType(prmType) + "\"");
-                               String paramTypeC = checkAndGetArray(methPrmTypes.get(i), methParams.get(i));
-                               String prmType = getSimpleType(getEnumType(paramTypeC));
+                               //String paramTypeC = checkAndGetArray(methPrmTypes.get(i), methParams.get(i));
+                               //String prmType = getSimpleType(getEnumType(paramTypeC));
+                               String paramTypeC = checkAndGetCplusArgClsType(methPrmTypes.get(i), methParams.get(i));
+                               String prmType = getEnumType(paramTypeC);
                                print("\"" + prmType + "\"");
                        }
                        if (i != methParams.size() - 1) {
@@ -3650,7 +3657,6 @@ public class IoTCompiler {
                                        println("vector<int> paramEnumInt" + i + ";");
                                } else {
                                        String methParamComplete = checkAndGetCplusArray(methPrmType, methParams.get(i));
-                                       //println(methParamComplete + " = " + generateCplusInitializer(methPrmType) + ";");
                     println(methParamComplete + ";");
                                }
                        }
@@ -4570,6 +4576,18 @@ public class IoTCompiler {
        }
 
 
+       // Generate a set of standard classes for import statements
+       private List<String> getStandardJavaIntfaceImportClasses() {
+
+               List<String> importClasses = new ArrayList<String>();
+               // Add the standard list first
+               importClasses.add("java.util.List");
+               importClasses.add("java.util.ArrayList");
+
+               return importClasses;
+       }
+
+
        // Generate a set of standard classes for import statements
        private List<String> getStandardJavaImportClasses() {
 
@@ -4909,7 +4927,7 @@ public class IoTCompiler {
                        } else
                                return getNonPrimitiveCplusClass(paramType);
                } else if(paramType.contains("[]")) {   // Array type (used for return type only)
-                       String cArray = "vector<" + getSimpleArrayType(paramType) + ">";
+                       String cArray = "vector<" + convertType(getSimpleArrayType(paramType)) + ">";
                        return cArray;
                } else if(getParamCategory(paramType) == ParamCategory.USERDEFINED) {
                        return paramType + "*";
@@ -4977,6 +4995,47 @@ public class IoTCompiler {
        }
 
 
+       // Return the class type for class resolution (for return value)
+       // - Check and return C++ array class, e.g. int A[] into int*
+       // - Check and return C++ vector class, e.g. List<Integer> A into vector<int>
+       private String checkAndGetCplusRetClsType(String paramType) {
+
+               String paramTypeRet = null;
+               // Check for array declaration
+               if (paramType.contains("[]")) {
+                       String type = paramType.split("\\[\\]")[0];
+                       paramTypeRet = getSimpleArrayType(type) + "*";
+               } else if (paramType.contains("<") && paramType.contains(">")) {
+                       // Just return it as is if it's not an array
+                       String type = paramType.split("<")[1].split(">")[0];
+                       paramTypeRet = "vector<" + getGenericType(type) + ">";
+               } else
+                       paramTypeRet = paramType;
+
+               return paramTypeRet;
+       }
+
+
+       // Return the class type for class resolution (for method arguments)
+       // - Check and return C++ array class, e.g. int A[] into int*
+       // - Check and return C++ vector class, e.g. List<Integer> A into vector<int>
+       private String checkAndGetCplusArgClsType(String paramType, String param) {
+
+               String paramTypeRet = null;
+               // Check for array declaration
+               if (param.contains("[]")) {
+                       paramTypeRet = getSimpleArrayType(paramType) + "*";
+               } else if (paramType.contains("<") && paramType.contains(">")) {
+                       // Just return it as is if it's not an array
+                       String type = paramType.split("<")[1].split(">")[0];
+                       paramTypeRet = "vector<" + getGenericType(type) + ">";
+               } else
+                       paramTypeRet = paramType;
+
+               return paramTypeRet;
+       }
+
+
        // Detect array declaration, e.g. int A[],
        //              then generate type "int[]"
        private String checkAndGetArray(String paramType, String param) {
index 6b33b61..5da411c 100644 (file)
@@ -42,6 +42,7 @@ class IoTRMIUtil {
                static int              getByteStringLength(vector<string> 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 +53,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 +69,7 @@ class IoTRMIUtil {
                static char*    getObjectBytes(char* retObjBytes, void* obj, const char* type);
 
                // Arrays to bytes
+               static char*    arrByteToByteArray(vector<char> arrByte, char* bytes);
                static char*    arrShortToByteArray(vector<short> arrShort, char* bytes);
                static char*    arrIntToByteArray(vector<int> arrInt, char* bytes);
                static char*    arrLongToByteArray(vector<int64_t> arrInt, char* bytes);
@@ -77,6 +80,7 @@ class IoTRMIUtil {
                static char*    arrStringToByteArray(vector<string> arrString, char* bytes);
 
                // Bytes to array
+               static vector<char>*    byteArrayToByteArray(vector<char>* result, char* bytes, int len);
                static vector<short>*   byteArrayToShortArray(vector<short>* result, char* bytes, int len);
                static vector<int>*     byteArrayToIntArray(vector<int>* result, char* bytes, int len);
                static vector<int64_t>* byteArrayToLongArray(vector<int64_t>* result, char* bytes, int len);
@@ -96,6 +100,7 @@ class IoTRMIUtil {
                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
                
        private:
@@ -188,34 +193,52 @@ int IoTRMIUtil::getVarTypeSize(string type, void* paramObj) {
        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<String>") == 0)) {
                paramLen = IoTRMIUtil::getByteStringLength(*(vector<string>*) paramObj);
-       } else if (type.compare("byte[]") == 0) {
+       } else if ( (type.compare("byte*") == 0) ||
+                               (type.compare("Byte*") == 0) ||
+                               (type.compare("vector<Byte>") == 0)) {
                int dataSize = getTypeSize("byte");
                paramLen = (*(vector<char>*) paramObj).size() * dataSize;
-       } else if (type.compare("short[]") == 0) {
+       } else if ( (type.compare("short*") == 0) ||
+                               (type.compare("Short*") == 0) ||
+                               (type.compare("vector<Short>") == 0)) {
                int dataSize = getTypeSize("short");
                paramLen = (*(vector<short>*) paramObj).size() * dataSize;
-       } else if (type.compare("int[]") == 0) {
+       } else if ( (type.compare("int*") == 0) ||
+                               (type.compare("Integer*") == 0) ||
+                               (type.compare("vector<Integer>") == 0)) {
                int dataSize = getTypeSize("int");
                paramLen = (*(vector<int>*) paramObj).size() * dataSize;
-       } else if (type.compare("long[]") == 0) {
+       } else if ( (type.compare("long*") == 0) ||
+                               (type.compare("Long*") == 0) ||
+                               (type.compare("vector<Long>") == 0)) {
                int dataSize = getTypeSize("long");
                paramLen = (*(vector<int64_t>*) paramObj).size() * dataSize;
-       } else if (type.compare("float[]") == 0) {
+       } else if ( (type.compare("float*") == 0) ||
+                               (type.compare("Float*") == 0) ||
+                               (type.compare("vector<Float>") == 0)) {
                int dataSize = getTypeSize("float");
                paramLen = (*(vector<float>*) paramObj).size() * dataSize;
-       } else if (type.compare("double[]") == 0) {
+       } else if ( (type.compare("double*") == 0) ||
+                               (type.compare("Double*") == 0) ||
+                               (type.compare("vector<Double>") == 0)) {
                int dataSize = getTypeSize("double");
                paramLen = (*(vector<double>*) paramObj).size() * dataSize;
-       } else if (type.compare("bool[]") == 0) {
+       } else if ( (type.compare("boolean*") == 0) ||
+                               (type.compare("Boolean*") == 0) ||
+                               (type.compare("vector<Boolean>") == 0)) {
                int dataSize = getTypeSize("boolean");
                paramLen = (*(vector<bool>*) paramObj).size() * dataSize;
-       } else if (type.compare("char[]") == 0) {
+       } else if ( (type.compare("char*") == 0) ||
+                               (type.compare("Character*") == 0) ||
+                               (type.compare("vector<Character>") == 0)) {
                int dataSize = getTypeSize("char");
                paramLen = (*(vector<char>*) paramObj).size() * dataSize;
        } else {
-               cerr << "IoTRMIUtil: Unrecognizable type: " << type;
+               cerr << "IoTRMIUtil: Unrecognizable type: " << type << endl;
                exit(-1);
        }
 
@@ -249,7 +272,7 @@ void* IoTRMIUtil::getParamObject(void* retObj, const char* type, char* paramByte
 
        if (strcmp(type, "b") == 0 ||
                strcmp(type, "byte") == 0) {
-               retObj = (void*) &paramBytes[0];
+               retObj = (void*) byteArrayToByte((char*) retObj, paramBytes);
        } else if ( strcmp(type, "s") == 0 ||
                                strcmp(type, "short") == 0) {
                retObj = (void*) byteArrayToShort((short*) retObj, paramBytes);
@@ -274,11 +297,11 @@ void* IoTRMIUtil::getParamObject(void* retObj, const char* type, char* paramByte
        } else if ( strcmp(type, "Ss") == 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 {
-               cerr << "IoTRMIUtil: Unrecognizable type: " << type;
+               cerr << "IoTRMIUtil: Unrecognizable type: " << type << endl;
                exit(-1);
        }
 
@@ -286,29 +309,46 @@ void* IoTRMIUtil::getParamObject(void* retObj, const char* type, char* paramByte
 }
 
 
-// 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<char>*) paramBytes;
-       } else if (strcmp(type, "short[]") == 0) {
+       if ((strcmp(type, "byte*") == 0) ||
+               (strcmp(type, "Byte*") == 0) ||
+               (strcmp(type, "vector<Byte>") == 0)) {
+               retObj = byteArrayToByteArray((vector<char>*) retObj, paramBytes, len);
+       } else if ( (strcmp(type, "short*") == 0) ||
+                               (strcmp(type, "Short*") == 0) ||
+                               (strcmp(type, "vector<Short>") == 0)) {
                retObj = byteArrayToShortArray((vector<short>*) retObj, paramBytes, len);
-       } else if (strcmp(type, "int[]") == 0) {
+       } else if ( (strcmp(type, "int*") == 0) ||
+                               (strcmp(type, "Integer*") == 0) ||
+                               (strcmp(type, "vector<Integer>") == 0)) {
                retObj = byteArrayToIntArray((vector<int>*) retObj, paramBytes, len);
-       } else if (strcmp(type, "long[]") == 0) {
+       } else if ( (strcmp(type, "long*") == 0) ||
+                               (strcmp(type, "Long*") == 0) ||
+                               (strcmp(type, "vector<Long>") == 0)) {
                retObj = byteArrayToLongArray((vector<int64_t>*) retObj, paramBytes, len);
-       } else if (strcmp(type, "float[]") == 0) {
+       } else if ( (strcmp(type, "float*") == 0) ||
+                               (strcmp(type, "Float*") == 0) ||
+                               (strcmp(type, "vector<Float>") == 0)) {
                retObj = byteArrayToFloatArray((vector<float>*) retObj, paramBytes, len);
-       } else if (strcmp(type, "double[]") == 0) {
+       } else if ( (strcmp(type, "double*") == 0) ||
+                               (strcmp(type, "Double*") == 0) ||
+                               (strcmp(type, "vector<Double>") == 0)) {
                retObj = byteArrayToDoubleArray((vector<double>*) retObj, paramBytes, len);
-       } else if (strcmp(type, "bool[]") == 0) {
+       } else if ( (strcmp(type, "boolean*") == 0) ||
+                               (strcmp(type, "Boolean*") == 0) ||
+                               (strcmp(type, "vector<Boolean>") == 0)) {
                retObj = byteArrayToBooleanArray((vector<bool>*) retObj, paramBytes, len);
-       } else if (strcmp(type, "char[]") == 0) {
+       } else if ( (strcmp(type, "char*") == 0)      ||
+                               (strcmp(type, "Character*") == 0) ||
+                               (strcmp(type, "vector<Character>") == 0)) {
                retObj = byteArrayToCharArray((vector<char>*) retObj, paramBytes, len);
-       } else if (strcmp(type, "String[]") == 0) {
+       } else if ( (strcmp(type, "String*") == 0) ||
+                               (strcmp(type, "vector<String>") == 0)) {
                retObj = byteArrayToStringArray((vector<string>*) retObj, paramBytes, len);
        } else {
-               cerr << "IoTRMIUtil: Unrecognizable type: " << type;
+               cerr << "IoTRMIUtil: Unrecognizable type: " << type << endl;
                exit(-1);       
        }
 
@@ -321,7 +361,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);
@@ -344,14 +384,13 @@ char* IoTRMIUtil::getObjectBytes(char* retObjBytes, void* obj, const char* type)
                                strcmp(type, "char") == 0) {
                retObjBytes = charToByteArray(*((char*) obj), retObjBytes);
        } else if ( strcmp(type, "Ss") == 0 ||
-                               strcmp(type, "String") == 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 {
-               cerr << "IoTRMIUtil: Unrecognizable type: " << type;
+               cerr << "IoTRMIUtil: Unrecognizable type: " << type << endl;
                exit(-1);
        }
 
@@ -362,26 +401,43 @@ 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<Byte>") == 0)) {
+               retObjBytes = arrByteToByteArray(*((vector<char>*) obj), retObjBytes);
+       } else if ( (strcmp(type, "short*") == 0) ||
+                               (strcmp(type, "Short*") == 0) ||
+                               (strcmp(type, "vector<Short>") == 0)) {
                retObjBytes = arrShortToByteArray(*((vector<short>*) obj), retObjBytes);
-       } else if (strcmp(type, "int[]") == 0) {
+       } else if ( (strcmp(type, "int*") == 0) ||
+                               (strcmp(type, "Integer*") == 0) ||
+                               (strcmp(type, "vector<Integer>") == 0)) {
                retObjBytes = arrIntToByteArray(*((vector<int>*) obj), retObjBytes);
-       } else if (strcmp(type, "long[]") == 0) {
+       } else if ( (strcmp(type, "long*") == 0) ||
+                               (strcmp(type, "Long*") == 0) ||
+                               (strcmp(type, "vector<Long>") == 0)) {
                retObjBytes = arrLongToByteArray(*((vector<int64_t>*) obj), retObjBytes);
-       } else if (strcmp(type, "float[]") == 0) {
+       } else if ( (strcmp(type, "float*") == 0) ||
+                               (strcmp(type, "Float*") == 0) ||
+                               (strcmp(type, "vector<Float>") == 0)) {
                retObjBytes = arrFloatToByteArray(*((vector<float>*) obj), retObjBytes);
-       } else if (strcmp(type, "double[]") == 0) {
+       } else if ( (strcmp(type, "double*") == 0) ||
+                               (strcmp(type, "Double*") == 0) ||
+                               (strcmp(type, "vector<Double>") == 0)) {
                retObjBytes = arrDoubleToByteArray(*((vector<double>*) obj), retObjBytes);
-       } else if (strcmp(type, "bool[]") == 0) {
+       } else if ( (strcmp(type, "boolean*") == 0) ||
+                               (strcmp(type, "Boolean*") == 0) ||
+                               (strcmp(type, "vector<Boolean>") == 0)) {
                retObjBytes = arrBooleanToByteArray(*((vector<bool>*) obj), retObjBytes);
-       } else if (strcmp(type, "char[]") == 0) {
+       } else if ( (strcmp(type, "char*") == 0) ||
+                               (strcmp(type, "Character*") == 0) ||
+                               (strcmp(type, "vector<Character>") == 0)) {
                retObjBytes = arrCharToByteArray(*((vector<char>*) obj), retObjBytes);
-       } else if (strcmp(type, "String[]") == 0) {
+       } else if ( (strcmp(type, "String*") == 0) ||
+                               (strcmp(type, "vector<String>") == 0)) {
                retObjBytes = arrStringToByteArray(*((vector<string>*) obj), retObjBytes);
        } else {
-               cerr << "IoTRMIUtil: Unrecognizable type: " << type;
+               cerr << "IoTRMIUtil: Unrecognizable type: " << type << endl;
                exit(-1);
        }
 
@@ -392,6 +448,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<char> 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<short> arrShort, char* bytes) {
 
        int pos = 0;
@@ -517,6 +587,24 @@ char* IoTRMIUtil::arrStringToByteArray(vector<string> arrString, char* bytes) {
 
 
 // Bytes to array
+vector<char>* IoTRMIUtil::byteArrayToByteArray(vector<char>* 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<short>* IoTRMIUtil::byteArrayToShortArray(vector<short>* result, char* bytes, int len) {
 
        // Single element bytes
@@ -680,6 +768,15 @@ vector<string>*    IoTRMIUtil::byteArrayToStringArray(vector<string>* 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);
@@ -814,6 +911,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];
index d39960b..24fe1ac 100644 (file)
@@ -20,6 +20,15 @@ class TestClass : public TestClassInterface {
                double                          getDouble(double in);
                bool                            getBoolean(bool in);
                char                            getChar(char in);
+
+               vector<char>            getByteArray(vector<char> in);
+               vector<short>           getShortArray(vector<short> in);
+               vector<int64_t>         getLongArray(vector<int64_t> in);
+               vector<float>           getFloatArray(vector<float> in);
+               vector<double>          getDoubleArray(vector<double> in);
+               vector<bool>            getBooleanArray(vector<bool> in);
+               vector<char>            getCharArray(vector<char> in);
+
                int                                     getA();
                void                            setA(int _int);
                void                            setB(float _float);
@@ -53,6 +62,7 @@ TestClass::TestClass(int _int, float _float, string _string) {
 }
 
 
+// Single variables
 char TestClass::getByte(char in) {
 
        return in;
@@ -95,6 +105,49 @@ char TestClass::getChar(char in) {
 }
 
 
+// Arrays
+vector<char> TestClass::getByteArray(vector<char> in) {
+
+       return in;
+}
+
+
+vector<short> TestClass::getShortArray(vector<short> in) {
+
+       return in;
+}
+
+
+vector<int64_t> TestClass::getLongArray(vector<int64_t> in) {
+
+       return in;
+}
+
+
+vector<float> TestClass::getFloatArray(vector<float> in) {
+
+       return in;
+}
+
+
+vector<double> TestClass::getDoubleArray(vector<double> in) {
+
+       return in;
+}
+
+
+vector<bool> TestClass::getBooleanArray(vector<bool> in) {
+
+       return in;
+}
+
+
+vector<char> TestClass::getCharArray(vector<char> in) {
+
+       return in;
+}
+
+
 int TestClass::getA() {
 
        return intA;
index c4a692d..1d8ad91 100644 (file)
@@ -16,14 +16,44 @@ int main(int argc, char *argv[])
        ports.push_back(12345);
 
        TestClassComplete *tcStub = new TestClassComplete_Stub(port, address, rev, &bResult, ports);
+       cout << "==== SINGLE ====" << endl;
        cout << "Return value: " << tcStub->getByte(68) << endl;
        cout << "Return value: " << tcStub->getShort(1234) << endl;
-       //cout << "Return value: " << tcStub->getLong(12345678) << endl;
+       cout << "Return value: " << tcStub->getLong(12345678) << endl;
        cout << "Return value: " << tcStub->getFloat(12.345) << endl;
        cout << "Return value: " << tcStub->getDouble(12345.678) << endl;
        cout << "Return value: " << tcStub->getBoolean(true) << endl;
        cout << "Return value: " << tcStub->getChar('c') << endl;
-
+       cout << "==== ARRAY ====" << endl;
+       vector<char> in1;
+       in1.push_back(68);
+       in1.push_back(69);
+       cout << "Return value: " << tcStub->getByteArray(in1)[0] << ", " << tcStub->getByteArray(in1)[1] << endl;
+       vector<short> in2;
+       in2.push_back(1234);
+       in2.push_back(1235);
+       cout << "Return value: " << tcStub->getShortArray(in2)[0] << ", "  << tcStub->getShortArray(in2)[1] << endl;
+       vector<int64_t> in3;
+       in3.push_back(12345678);
+       in3.push_back(12356782);
+       cout << "Return value: " << tcStub->getLongArray(in3)[0] << ", "  << tcStub->getLongArray(in3)[1] << endl;
+       vector<float> in4;
+       in4.push_back(12.345);
+       in4.push_back(12.346);
+       cout << "Return value: " << tcStub->getFloatArray(in4)[0] << ", "  << tcStub->getFloatArray(in4)[1] << endl;
+       vector<double> in5;
+       in5.push_back(12345.678);
+       in5.push_back(12345.543);
+       cout << "Return value: " << tcStub->getDoubleArray(in5)[0] << ", "  << tcStub->getDoubleArray(in5)[1] << endl;
+       vector<bool> in6;
+       in6.push_back(true);
+       in6.push_back(false);
+       cout << "Return value: " << tcStub->getBooleanArray(in6)[0] << ", "  << tcStub->getBooleanArray(in6)[1] << endl;
+       vector<char> in7;
+       in7.push_back('c');
+       in7.push_back('e');
+       cout << "Return value: " << tcStub->getCharArray(in7)[0] << ", "  << tcStub->getCharArray(in7)[1] << endl;
+       cout << "==== OTHERS ====" << endl;
        cout << "Return value: " << tcStub->getA() << endl;
        cout << "Return value: " << tcStub->setAndGetA(123) << endl;
        cout << "Return value: " << tcStub->setACAndGetA("string", 123) << endl;
index 19b99de..4f55242 100644 (file)
@@ -30,6 +30,7 @@ public class TestClass implements TestClassInterface {
        }
 
 
+       // Single variables
        public byte getByte(byte in) {
 
                return in;
@@ -72,6 +73,50 @@ public class TestClass implements TestClassInterface {
        }
 
 
+       // Arrays
+       public byte[] getByteArray(byte[] in) {
+
+               return in;
+       }
+
+
+       public short[] getShortArray(short[] in) {
+
+               return in;
+       }
+
+
+       public long[] getLongArray(long[] in) {
+
+               return in;
+       }
+
+
+       public float[] getFloatArray(float[] in) {
+
+               return in;
+       }
+
+
+       public double[] getDoubleArray(double[] in) {
+
+               return in;
+       }
+
+
+       public boolean[] getBooleanArray(boolean[] in) {
+
+               return in;
+       }
+
+
+       public char[] getCharArray(char[] in) {
+
+               return in;
+       }
+
+
+       // Other functions
        public int getA() {
 
                return intA;
index d94a09d..01b1bdd 100644 (file)
@@ -17,6 +17,7 @@ public class TestClass_Stub {
                System.out.println("Allocated ports: " + Arrays.toString(ports));
 
                TestClassComplete_Stub tcstub = new TestClassComplete_Stub(port, address, rev, ports);
+               System.out.println("==== SINGLE ====");
                System.out.println("Return value: " + tcstub.getByte((byte)68));
                System.out.println("Return value: " + tcstub.getShort((short)1234));
                System.out.println("Return value: " + tcstub.getLong(12345678l));
@@ -25,6 +26,23 @@ public class TestClass_Stub {
                System.out.println("Return value: " + tcstub.getBoolean(true));
                System.out.println("Return value: " + tcstub.getChar('c'));
 
+               System.out.println("==== ARRAY ====");
+               byte[] in1 = { 68, 69 };
+               System.out.println("Return value: " + Arrays.toString(tcstub.getByteArray(in1)));
+               short[] in2 = { (short)1234, (short)1235 };
+               System.out.println("Return value: " + Arrays.toString(tcstub.getShortArray(in2)));
+               long[] in3 = { 12345678l, 12356782l };
+               System.out.println("Return value: " + Arrays.toString(tcstub.getLongArray(in3)));
+               float[] in4 = { 12.345f, 12.346f };
+               System.out.println("Return value: " + Arrays.toString(tcstub.getFloatArray(in4)));
+               double[] in5 = { 12345.678, 12345.543 };
+               System.out.println("Return value: " + Arrays.toString(tcstub.getDoubleArray(in5)));
+               boolean[] in6 = { true, false };
+               System.out.println("Return value: " + Arrays.toString(tcstub.getBooleanArray(in6)));
+               char[] in7 = { 'c', 'e' };
+               System.out.println("Return value: " + Arrays.toString(tcstub.getCharArray(in7)));
+
+               System.out.println("==== OTHERS ====");
                System.out.println("Return value: " + tcstub.getA());
                System.out.println("Return value: " + tcstub.setAndGetA(123));
                System.out.println("Return value: " + tcstub.setACAndGetA("string", 123));