From ef69794d7b791f13acb42fa1449c24b1a26bf356 Mon Sep 17 00:00:00 2001 From: rtrimana Date: Fri, 28 Oct 2016 16:48:15 -0700 Subject: [PATCH] Using sorted integer as method Id instead of hash values --- iotjava/Makefile | 10 +-- iotjava/iotrmi/C++/IoTRMICall.hpp | 28 ++++++-- iotjava/iotrmi/C++/IoTRMIObject.hpp | 7 +- iotjava/iotrmi/C++/sample/TestClass_Stub.hpp | 16 ++++- iotjava/iotrmi/Java/IoTRMICall.java | 9 ++- iotjava/iotrmi/Java/IoTRMIObject.java | 22 ++---- iotjava/iotrmi/Java/IoTRMIUtil.java | 71 +++++++++++++------ iotjava/iotrmi/Java/sample/CallBack_Stub.java | 8 ++- .../iotrmi/Java/sample/TestClass_Stub.java | 14 +++- 9 files changed, 126 insertions(+), 59 deletions(-) diff --git a/iotjava/Makefile b/iotjava/Makefile index bd36e8b..9b858f7 100644 --- a/iotjava/Makefile +++ b/iotjava/Makefile @@ -36,12 +36,12 @@ rmi: mkdir -p $(BIN_DIR)/iotrmi/C++ #$(G++) iotrmi/C++/IoTSocketServer.cpp -o $(BIN_DIR)/iotrmi/C++/IoTSocketServer.out #$(G++) iotrmi/C++/IoTSocketClient.cpp -o $(BIN_DIR)/iotrmi/C++/IoTSocketClient.out - #$(G++) iotrmi/C++/IoTRMICall.cpp -o $(BIN_DIR)/iotrmi/C++/IoTRMICall.out --std=c++11 - #$(G++) iotrmi/C++/IoTRMIObject.cpp -o $(BIN_DIR)/iotrmi/C++/IoTRMIObject.out --std=c++11 + $(G++) iotrmi/C++/IoTRMICall.cpp -o $(BIN_DIR)/iotrmi/C++/IoTRMICall.out --std=c++11 + $(G++) iotrmi/C++/IoTRMIObject.cpp -o $(BIN_DIR)/iotrmi/C++/IoTRMIObject.out --std=c++11 mkdir -p $(BIN_DIR)/iotrmi/C++/sample - #$(G++) iotrmi/C++/sample/TestClass.cpp -o $(BIN_DIR)/iotrmi/C++/sample/TestClass.out --std=c++11 - #$(G++) iotrmi/C++/sample/TestClass_Stub.cpp -o $(BIN_DIR)/iotrmi/C++/sample/TestClass_Stub.out --std=c++11 - #$(G++) iotrmi/C++/sample/TestClass_Skeleton.cpp -o $(BIN_DIR)/iotrmi/C++/sample/TestClass_Skeleton.out --std=c++11 + $(G++) iotrmi/C++/sample/TestClass.cpp -o $(BIN_DIR)/iotrmi/C++/sample/TestClass.out --std=c++11 + $(G++) iotrmi/C++/sample/TestClass_Stub.cpp -o $(BIN_DIR)/iotrmi/C++/sample/TestClass_Stub.out --std=c++11 + $(G++) iotrmi/C++/sample/TestClass_Skeleton.cpp -o $(BIN_DIR)/iotrmi/C++/sample/TestClass_Skeleton.out --std=c++11 PHONY += run-rmiserver run-rmiserver: diff --git a/iotjava/iotrmi/C++/IoTRMICall.hpp b/iotjava/iotrmi/C++/IoTRMICall.hpp index 11797f9..68ead38 100644 --- a/iotjava/iotrmi/C++/IoTRMICall.hpp +++ b/iotjava/iotrmi/C++/IoTRMICall.hpp @@ -22,7 +22,7 @@ using namespace std; class IoTRMICall { public: - IoTRMICall(int _port, const char* _address, int _rev, bool* _bResult); + IoTRMICall(int _port, const char* _address, int _rev, bool* _bResult, string _methodSign[], int _size); ~IoTRMICall(); // Public methods int methodLength(string paramCls[], void* paramObj[], int numParam); @@ -32,14 +32,19 @@ class IoTRMICall { void* paramObj[], int numParam, void* retObj); private: - IoTRMIUtil *rmiUtil; - IoTSocketClient *rmiClient; + map mapSign2MethodId; + IoTRMIUtil *rmiUtil; + IoTSocketClient *rmiClient; + + // Private methods + void getMethodIds(string methodSign[], int size); }; // Constructor -IoTRMICall::IoTRMICall(int _port, const char* _address, int _rev, bool* _bResult) { +IoTRMICall::IoTRMICall(int _port, const char* _address, int _rev, bool* _bResult, string _methodSign[], int _size) { + getMethodIds(_methodSign, _size); rmiUtil = new IoTRMIUtil(); if (rmiUtil == NULL) { perror("IoTRMICall: IoTRMIUtil isn't initialized!"); @@ -126,7 +131,8 @@ char* IoTRMICall::methodToBytes(string methodSign, string paramCls[], // Get method ID in bytes char methodId[IoTRMIUtil::METHOD_ID_LEN]; - IoTRMIUtil::getHashCodeBytes(methodSign, methodId); + int methId = mapSign2MethodId.find(methodSign)->second; + IoTRMIUtil::intToByteArray(methId, methodId); memcpy(method, methodId, IoTRMIUtil::METHOD_ID_LEN); int pos = IoTRMIUtil::METHOD_ID_LEN; // Get byte arrays and calculate method bytes length @@ -152,6 +158,18 @@ char* IoTRMICall::methodToBytes(string methodSign, string paramCls[], } +// ************* +// Helpers +// ************* +void IoTRMICall::getMethodIds(string methodSign[], int size) { + + for(int i = 0; i < size; i++) { + mapSign2MethodId[methodSign[i]] = i; + } +} + + + #endif diff --git a/iotjava/iotrmi/C++/IoTRMIObject.hpp b/iotjava/iotrmi/C++/IoTRMIObject.hpp index 5ffb81e..af0541b 100644 --- a/iotjava/iotrmi/C++/IoTRMIObject.hpp +++ b/iotjava/iotrmi/C++/IoTRMIObject.hpp @@ -31,7 +31,7 @@ class IoTRMIObject { void** getMethodParams(string paramCls[], int numParam, void* paramObj[]); private: - map mapHash2Sign; + map mapMethodId2Sign; IoTRMIUtil *rmiUtil; IoTSocketServer *rmiServer; char* methodBytes; @@ -117,7 +117,7 @@ string IoTRMIObject::getSignature() { int methodId = 0; IoTRMIUtil::byteArrayToInt(&methodId, methodIdBytes); - return mapHash2Sign.find(methodId)->second; + return mapMethodId2Sign.find(methodId)->second; } @@ -161,8 +161,7 @@ void** IoTRMIObject::getMethodParams(string paramCls[], int numParam, void* para void IoTRMIObject::getMethodIds(string methodSign[], int size) { for(int i = 0; i < size; i++) { - int methodId = IoTRMIUtil::hashCode(methodSign[i]); - mapHash2Sign[methodId] = methodSign[i]; + mapMethodId2Sign[i] = methodSign[i]; } } diff --git a/iotjava/iotrmi/C++/sample/TestClass_Stub.hpp b/iotjava/iotrmi/C++/sample/TestClass_Stub.hpp index 97d3db5..3611bbf 100644 --- a/iotjava/iotrmi/C++/sample/TestClass_Stub.hpp +++ b/iotjava/iotrmi/C++/sample/TestClass_Stub.hpp @@ -28,6 +28,20 @@ class TestClass_Stub : public TestClassInterface { IoTRMICall *rmiCall; string address; //vector ports; + + const static int size = 8; + string methodSignatures[size] = { + + "voidsetA(int)", + "voidsetB(float)", + "voidsetC(string)", + "sumArray(string[])", + //"sumArray(int[])", + "intsetAndGetA(int)", + "intsetACAndGetA(string,int)", + "intcallBack()", + "voidregisterCallBack(CallBackInterface)" + }; }; @@ -41,7 +55,7 @@ TestClass_Stub::TestClass_Stub() { TestClass_Stub::TestClass_Stub(int _port, const char* _address, int _rev, bool* _bResult) { address = _address; - rmiCall = new IoTRMICall(_port, _address, _rev, _bResult); + rmiCall = new IoTRMICall(_port, _address, _rev, _bResult, methodSignatures, size); } diff --git a/iotjava/iotrmi/Java/IoTRMICall.java b/iotjava/iotrmi/Java/IoTRMICall.java index 2f21c72..433c659 100644 --- a/iotjava/iotrmi/Java/IoTRMICall.java +++ b/iotjava/iotrmi/Java/IoTRMICall.java @@ -31,15 +31,17 @@ public class IoTRMICall { */ private IoTRMIUtil rmiUtil; private IoTSocketClient rmiClient; + private List listMethodId; // Map from method ID to signature /** * Constructors */ - public IoTRMICall(int _port, String _address, int _rev) throws IOException { + public IoTRMICall(int _port, String _address, int _rev, String[] _methodSign) throws IOException { rmiUtil = new IoTRMIUtil(); rmiClient = new IoTSocketClient(_port, _address, _rev); + listMethodId = Arrays.asList(_methodSign); // Initialize the method ID map } @@ -79,7 +81,8 @@ public class IoTRMICall { public byte[] methodToBytes(String methodSign, Class[] paramCls, Object[] paramObj) { // Get method ID in bytes - byte[] methodId = IoTRMIUtil.getHashCodeBytes(methodSign); + int methId = listMethodId.indexOf(methodSign); + byte[] methodId = IoTRMIUtil.intToByteArray(methId); // Get byte arrays and calculate method bytes length int numbParam = paramObj.length; @@ -119,7 +122,7 @@ public class IoTRMICall { return method; } - + public static void main(String[] args) throws Exception { String[] test = { "123", "456", "789" }; diff --git a/iotjava/iotrmi/Java/IoTRMIObject.java b/iotjava/iotrmi/Java/IoTRMIObject.java index bb093a9..3ecd6bf 100644 --- a/iotjava/iotrmi/Java/IoTRMIObject.java +++ b/iotjava/iotrmi/Java/IoTRMIObject.java @@ -28,7 +28,7 @@ public class IoTRMIObject { /** * Class Properties */ - private Map mapHash2Sign; // Map from hashcode(method ID) to signature + private List listMethodId2Sign; // List of method signature (we use list index as method Id) private IoTRMIUtil rmiUtil; private IoTSocketServer rmiServer; private byte[] methodBytes; @@ -42,9 +42,8 @@ public class IoTRMIObject { IllegalAccessException, IOException { rmiUtil = new IoTRMIUtil(); - mapHash2Sign = new HashMap(); + listMethodId2Sign = Arrays.asList(_methodSign); // Initialize the method ID list methodBytes = null; - getMethodIds(_methodSign); // Initialize the method ID map rmiServer = new IoTSocketServer(_port); rmiServer.connect(); } @@ -81,8 +80,8 @@ public class IoTRMIObject { System.arraycopy(methodBytes, 0, methodIdBytes, 0, IoTRMIUtil.METHOD_ID_LEN); // Get method Id int methodId = IoTRMIUtil.byteArrayToInt(methodIdBytes); - // Get method signature from the Map - return mapHash2Sign.get(methodId); + // Get method signature from the list + return listMethodId2Sign.get(methodId); } @@ -129,17 +128,4 @@ public class IoTRMIObject { return paramObj; } - - - /** - * getMethodIds() gets methods identifiers (hash code) and store them in the Map - */ - private void getMethodIds(String[] methodSign) { - - for (String sign : methodSign) { - byte[] hashCode = IoTRMIUtil.getHashCodeBytes(sign); - int methodId = IoTRMIUtil.byteArrayToInt(hashCode); - mapHash2Sign.put(methodId, sign); - } - } } diff --git a/iotjava/iotrmi/Java/IoTRMIUtil.java b/iotjava/iotrmi/Java/IoTRMIUtil.java index 91496ed..a382b0c 100644 --- a/iotjava/iotrmi/Java/IoTRMIUtil.java +++ b/iotjava/iotrmi/Java/IoTRMIUtil.java @@ -195,15 +195,15 @@ public class IoTRMIUtil { retObj = getParamObjectArray(type, paramBytes); // Set // e.g. Set - type = Set.class, genTypeVal = String.class - } else if (type == Set.class) { - retObj = getParamSetObject(genTypeVal, paramBytes); + /*} else if (type == Set.class) { + retObj = getParamSetObject(genTypeVal, paramBytes);*/ // List } else if (type == List.class) { retObj = getParamListObject(genTypeVal, paramBytes); // Map // e.g. Map - type = Map.class, genTypeKey = String.class, genTypeVal = Integer.class - } else if (type == Map.class) { - retObj = getParamMapObject(genTypeKey, genTypeVal, paramBytes); + /*} else if (type == Map.class) { + retObj = getParamMapObject(genTypeKey, genTypeVal, paramBytes);*/ } else throw new Error("IoTRMIUtil: Unrecognizable type: " + type.getName()); @@ -268,6 +268,13 @@ public class IoTRMIUtil { } else if ( (type == String[].class) || (type == String.class)) { retObj = (Object) byteArrayToStringArray(paramBytes); + } else if (type.isArray()) { + // This is an array but it's more than 1 dimension, e.g. 2-dimensional, + // 3-dimensional, etc. + // for loop to check inner array perhaps using object + // then call this function recursively + // combine the result altogether + } else throw new Error("IoTRMIUtil: Unrecognizable type: " + type.getName()); @@ -303,14 +310,14 @@ public class IoTRMIUtil { } else if (obj.getClass().isArray()) { retObjBytes = getArrayObjectBytes(obj); // Set and its implementations - } else if (obj instanceof Set) { - retObjBytes = setToByteArray((Set) obj); + /*} else if (obj instanceof Set) { + retObjBytes = setToByteArray((Set) obj);*/ // List and its implementations } else if (obj instanceof List) { retObjBytes = listToByteArray((List) obj); // Map and its implementations - } else if (obj instanceof Map) { - retObjBytes = mapToByteArray((Map) obj); + /*} else if (obj instanceof Map) { + retObjBytes = mapToByteArray((Map) obj);*/ } else throw new Error("IoTRMIUtil: Unrecognizable object: " + obj.getClass()); @@ -366,7 +373,7 @@ public class IoTRMIUtil { // Collection data structures - public static byte[] setToByteArray(Set set) { + /*public static byte[] setToByteArray(Set set) { // Find out the class of the type Iterator it = set.iterator(); @@ -396,7 +403,7 @@ public class IoTRMIUtil { byte[] arrObjBytes = getArrayObjectBytes(arrObj); return arrObjBytes; - } + }*/ public static byte[] listToByteArray(List list) { @@ -433,7 +440,7 @@ public class IoTRMIUtil { // Convert keySet of a Map - public static byte[] mapKeyToByteArray(Map map) { + /*public static byte[] mapKeyToByteArray(Map map) { // Map // Find out the class of the type for K @@ -555,7 +562,7 @@ public class IoTRMIUtil { throw new Error("IoTRMIUtil: Unrecognizable object: " + genericType.getSimpleName()); return retSet; - } + }*/ // Get a List object from bytes @@ -598,7 +605,7 @@ public class IoTRMIUtil { // Get a Key array for Map object from bytes - public static Object getParamMapObject(Class genTypeKey, Class genTypeVal, byte[] paramBytes) { + /*public static Object getParamMapObject(Class genTypeKey, Class genTypeVal, byte[] paramBytes) { // The complete set of bytes always consists of all keys followed by all values - pairs // Calculate number of elements @@ -620,7 +627,7 @@ public class IoTRMIUtil { IoTRMITypes.arraysToMap(retMap, retObjKey, retObjVal); return retMap; - } + }*/ /** @@ -1305,17 +1312,39 @@ public class IoTRMIUtil { //boolean data = false; //char data = 'c'; - float data = 1234.123f; +// float data = 1234.123f; //double data = 12.51231234; //long data = 1234l; //short data = 1234; //int data = 12345678; - byte[] result = floatToByteArray(data); - System.out.println("Result: " + Arrays.toString(result)); - System.out.println("Converted back: " + byteArrayToFloat(result)); +// byte[] result = floatToByteArray(data); +// System.out.println("Result: " + Arrays.toString(result)); +// System.out.println("Converted back: " + byteArrayToFloat(result)); + + //String str = "methodA(int,string,float,double,double)"; + //int hash = str.hashCode(); + //System.out.println("Hash value: " + hash); + + int[][] multi = new int[][] { + { 1, 2, 3 }, + { 6, 5, 4}, + { 11, 17, 13} + }; + + for (int[] inner : multi ) { + System.out.println("New row!"); + for (Object i : inner) { + System.out.println("Element i: " + i); + } + System.out.println("Change row!\n"); + } + + int[] int1 = { 1, 2, 3 }; + int[] int2 = { 6, 5, 4 }; + int[] result = new int[int1.length + int2.length]; + System.arraycopy(int1, 0, result, 0, int1.length); + System.arraycopy(int2, 0, result, int1.length, int2.length); - String str = "methodA(int,string,float,double,double)"; - int hash = str.hashCode(); - System.out.println("Hash value: " + hash); + System.out.println("Combined array: " + Arrays.toString(result)); } } diff --git a/iotjava/iotrmi/Java/sample/CallBack_Stub.java b/iotjava/iotrmi/Java/sample/CallBack_Stub.java index 3ec19d5..0ea9728 100644 --- a/iotjava/iotrmi/Java/sample/CallBack_Stub.java +++ b/iotjava/iotrmi/Java/sample/CallBack_Stub.java @@ -10,12 +10,18 @@ public class CallBack_Stub implements CallBackInterface { */ private IoTRMICall rmiCall; + private String[] methodSignatures = { + + "intprintInt()", + "voidsetInt(int)" + }; + /** * Constructors */ public CallBack_Stub(int _port, String _address, int _rev) throws IOException { - rmiCall = new IoTRMICall(_port, _address, _rev); + rmiCall = new IoTRMICall(_port, _address, _rev, methodSignatures); } diff --git a/iotjava/iotrmi/Java/sample/TestClass_Stub.java b/iotjava/iotrmi/Java/sample/TestClass_Stub.java index 31d20a2..1eb77d4 100644 --- a/iotjava/iotrmi/Java/sample/TestClass_Stub.java +++ b/iotjava/iotrmi/Java/sample/TestClass_Stub.java @@ -20,6 +20,18 @@ public class TestClass_Stub implements TestClassInterface { */ private final static int NUM_CB_OBJ = 1; + private String[] methodSignatures = { + + "voidsetA(int)", + "voidsetB(float)", + "voidsetC(string)", + "sumArray(string[])", + "intsetAndGetA(int)", + "intsetACAndGetA(string,int)", + "intcallBack()", + "voidregisterCallBack(CallBackInterface)" + }; + /** * Constructors */ @@ -27,7 +39,7 @@ public class TestClass_Stub implements TestClassInterface { address = _address; ports = _ports; - rmiCall = new IoTRMICall(_port, _address, _rev); + rmiCall = new IoTRMICall(_port, _address, _rev, methodSignatures); } -- 2.34.1