Making classes final to make inheritance impossible
[iot2.git] / iotjava / iotrmi / Java / IoTRMIUtil.java
index 91496ed6f38eeec902a24a3120de7061618a58f5..cdeb608aa8a7d2a614648db68442ee2363c78647 100644 (file)
@@ -25,7 +25,7 @@ import java.util.Set;
  * @version     1.0
  * @since       2016-10-04
  */
-public class IoTRMIUtil {
+public final class IoTRMIUtil {
 
        /**
         * Class Properties
@@ -37,8 +37,13 @@ public class IoTRMIUtil {
        /**
         * Class Constants
         */
-       public final static int METHOD_ID_LEN = 4;      // 4 bytes = 32 bits
-       public final static int PARAM_LEN = 4;          // 4 bytes = 32 bits (4-byte field that stores the length of the param)
+       public final static int OBJECT_ID_LEN = 4;              // 4 bytes = 32 bits
+       public final static int METHOD_ID_LEN = 4;              // 4 bytes = 32 bits
+       public final static int PACKET_TYPE_LEN = 4;    // 4 bytes = 32 bits
+       public final static int PARAM_LEN = 4;                  // 4 bytes = 32 bits (4-byte field that stores the length of the param)
+       public final static int RETURN_LEN = 4;                 // 4 bytes = 32 bits (4-byte field that stores the length of the return object)
+       public final static int RET_VAL_TYPE = -1;              // Packet type of return value
+       public final static int METHOD_TYPE = 1;                // Packet type of method
 
        public final static int SHT_LEN = 2;
        public final static int INT_LEN = 4;
@@ -49,6 +54,14 @@ public class IoTRMIUtil {
        public final static int BYT_LEN = 1;
        public final static int BOL_LEN = 1;
 
+       /**
+        * Public static data structure to keep track of multiple skeletons and stubs
+        */
+       public static Map<Integer,Object> mapStub = new HashMap<Integer,Object>();              // Map object to its stub ID
+       public static Map<Object,Object> mapSkel = new HashMap<Object,Object>();                // Map object to its skeleton
+       public static Map<Object,Integer> mapSkelId = new HashMap<Object,Integer>();    // Map object to its skeleton ID
+
+
        /**
         * Constructors
         */
@@ -65,6 +78,20 @@ public class IoTRMIUtil {
                                IoTRMITypes.nonPrimitivesJava, IoTRMITypes.nonPrimitivesCplus);
        }
 
+       
+       /*public static void initRMICall(int port, String address, int rev) throws IOException {
+               rmiCall = new IoTRMICall(port, address, rev);
+       }
+       
+       public static void initRMICall(int localPort, int port, String address, int rev) throws IOException {
+               rmiCall = new IoTRMICall(localPort, port, address, rev);
+       }
+       
+       public static void initRMIObject(int port) throws IOException, ClassNotFoundException, 
+                       InstantiationException, IllegalAccessException {
+               rmiObj = new IoTRMIObject(port);
+       }*/
+       
 
        /**
         * getHashCodeBytes() gets hash value (in bytes) from method name
@@ -161,7 +188,7 @@ public class IoTRMIUtil {
        /**
         * getParamObject() converts byte array of certain object type into Object
         */
-       public static Object getParamObject(Class<?> type, Class<?> genTypeKey, Class<?> genTypeVal, byte[] paramBytes) {
+       public static Object getParamObject(Class<?> type, Class<?> genTypeVal, byte[] paramBytes) {
                
                Object retObj = null;
                if (type == byte.class ||
@@ -193,17 +220,9 @@ public class IoTRMIUtil {
                // Array
                } else if (type.isArray()) {
                        retObj = getParamObjectArray(type, paramBytes);
-               // Set
-               // e.g. Set<String> - type = Set.class, genTypeVal = String.class
-               } else if (type == Set.class) {
-                       retObj = getParamSetObject(genTypeVal, paramBytes);
                // List
                } else if (type == List.class) {
                        retObj = getParamListObject(genTypeVal, paramBytes);
-               // Map
-               // e.g. Map<String,Integer> - type = Map.class, genTypeKey = String.class, genTypeVal = Integer.class
-               } else if (type == Map.class) {
-                       retObj = getParamMapObject(genTypeKey, genTypeVal, paramBytes);
                } else
                        throw new Error("IoTRMIUtil: Unrecognizable type: " + type.getName());
                
@@ -282,7 +301,7 @@ public class IoTRMIUtil {
                
                byte[] retObjBytes = null;
                if (obj instanceof Byte) {
-                       retObjBytes = (byte[]) obj;
+                       retObjBytes = new byte[] { (byte) obj };
                } else if (obj instanceof Short) {
                        retObjBytes = shortToByteArray((short) obj);
                } else if (obj instanceof Integer) {
@@ -302,15 +321,9 @@ public class IoTRMIUtil {
                // Arrays
                } else if (obj.getClass().isArray()) {
                        retObjBytes = getArrayObjectBytes(obj);
-               // Set and its implementations
-               } 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
                        throw new Error("IoTRMIUtil: Unrecognizable object: " + obj.getClass());
 
@@ -365,40 +378,6 @@ public class IoTRMIUtil {
        }
 
 
-       // Collection data structures
-       public static byte[] setToByteArray(Set<?> set) {
-
-               // Find out the class of the type
-               Iterator<?> it = set.iterator();
-               Object[] arrObj = null;
-               Object obj = it.next();
-
-               if (obj instanceof Byte) {
-                       arrObj = set.toArray(new Byte[set.size()]);
-               } else if (obj instanceof Short) {
-                       arrObj = set.toArray(new Short[set.size()]);
-               } else if (obj instanceof Integer) {
-                       arrObj = set.toArray(new Integer[set.size()]);
-               } else if (obj instanceof Long) {
-                       arrObj = set.toArray(new Long[set.size()]);
-               } else if (obj instanceof Float) {
-                       arrObj = set.toArray(new Float[set.size()]);
-               } else if (obj instanceof Double) {
-                       arrObj = set.toArray(new Double[set.size()]);
-               } else if (obj instanceof Character) {
-                       arrObj = set.toArray(new Character[set.size()]);
-               } else if (obj instanceof Boolean) {
-                       arrObj = set.toArray(new Boolean[set.size()]);
-               } else if (obj instanceof String) {
-                       arrObj = set.toArray(new String[set.size()]);
-               } else
-                       throw new Error("IoTRMIUtil: Unrecognizable object: " + obj.getClass());
-
-               byte[] arrObjBytes = getArrayObjectBytes(arrObj);
-               return arrObjBytes;
-       }
-
-
        public static byte[] listToByteArray(List<?> list) {
 
                // Find out the class of the type
@@ -432,132 +411,6 @@ public class IoTRMIUtil {
        }
 
 
-       // Convert keySet of a Map
-       public static byte[] mapKeyToByteArray(Map<?,?> map) {
-
-               // Map<K,V>
-               // Find out the class of the type for K
-               Iterator<?> it = map.keySet().iterator();
-               Object[] arrObj = null;
-               Object obj = it.next();
-
-               if (obj instanceof Byte) {
-                       arrObj = map.keySet().toArray(new Byte[map.size()]);
-               } else if (obj instanceof Short) {
-                       arrObj = map.keySet().toArray(new Short[map.size()]);
-               } else if (obj instanceof Integer) {
-                       arrObj = map.keySet().toArray(new Integer[map.size()]);
-               } else if (obj instanceof Long) {
-                       arrObj = map.keySet().toArray(new Long[map.size()]);
-               } else if (obj instanceof Float) {
-                       arrObj = map.keySet().toArray(new Float[map.size()]);
-               } else if (obj instanceof Double) {
-                       arrObj = map.keySet().toArray(new Double[map.size()]);
-               } else if (obj instanceof Character) {
-                       arrObj = map.keySet().toArray(new Character[map.size()]);
-               } else if (obj instanceof Boolean) {
-                       arrObj = map.keySet().toArray(new Boolean[map.size()]);
-               } else if (obj instanceof String) {
-                       arrObj = map.keySet().toArray(new String[map.size()]);
-               } else
-                       throw new Error("IoTRMIUtil: Unrecognizable object: " + obj.getClass());
-               byte[] arrObjBytes = getArrayObjectBytes(arrObj);
-
-               return arrObjBytes;
-       }
-
-
-       // Convert entrySet of a Map
-       public static byte[] mapEntryToByteArray(Map<?,?> map) {
-
-               // Map<K,V>
-               // Find out the class of the type for V
-               Iterator<?> it = map.values().iterator();
-               Object[] arrObj = null;
-               Object obj = it.next();
-
-               if (obj instanceof Byte) {
-                       arrObj = map.values().toArray(new Byte[map.size()]);
-               } else if (obj instanceof Short) {
-                       arrObj = map.values().toArray(new Short[map.size()]);
-               } else if (obj instanceof Integer) {
-                       arrObj = map.values().toArray(new Integer[map.size()]);
-               } else if (obj instanceof Long) {
-                       arrObj = map.values().toArray(new Long[map.size()]);
-               } else if (obj instanceof Float) {
-                       arrObj = map.values().toArray(new Float[map.size()]);
-               } else if (obj instanceof Double) {
-                       arrObj = map.values().toArray(new Double[map.size()]);
-               } else if (obj instanceof Character) {
-                       arrObj = map.values().toArray(new Character[map.size()]);
-               } else if (obj instanceof Boolean) {
-                       arrObj = map.values().toArray(new Boolean[map.size()]);
-               } else if (obj instanceof String) {
-                       arrObj = map.values().toArray(new String[map.size()]);
-               } else
-                       throw new Error("IoTRMIUtil: Unrecognizable object: " + obj.getClass());
-
-               byte[] arrObjBytes = getArrayObjectBytes(arrObj);
-               return arrObjBytes;
-       }
-
-
-       // Merge keySet and entrySet of a Map into one long byte array
-       public static byte[] mapToByteArray(Map<?,?> map) {
-
-               // Put map size in the packet
-               byte[] numEntries = intToByteArray(map.size());
-               byte[] keySetBytes = mapKeyToByteArray(map);
-               byte[] entrySetBytes = mapEntryToByteArray(map);
-               byte[] mapBytes = new byte[INT_LEN + keySetBytes.length + entrySetBytes.length];
-               // Copy the bytes
-               System.arraycopy(numEntries, 0, mapBytes, 0, INT_LEN);
-               System.arraycopy(keySetBytes, 0, mapBytes, INT_LEN, keySetBytes.length);
-               System.arraycopy(entrySetBytes, 0, mapBytes, (INT_LEN + keySetBytes.length), entrySetBytes.length);
-
-               return mapBytes;
-       }
-
-
-       // Get a Set object from bytes
-       public static Object getParamSetObject(Class<?> genericType, byte[] paramBytes) {
-
-               Set<Object> retSet = new HashSet<Object>();
-               Object retObj = null;
-               if (genericType == Byte.class) {
-                       Byte[] retArr = byteArrayToByteArray(paramBytes);
-                       Collections.addAll(retSet, retArr);
-               } else if (genericType == Short.class) {
-                       Short[] retArr = byteArrayToShortArray(paramBytes);
-                       Collections.addAll(retSet, retArr);
-               } else if (genericType == Integer.class) {
-                       Integer[] retArr = byteArrayToIntegerArray(paramBytes);
-                       Collections.addAll(retSet, retArr);
-               } else if (genericType == Long.class) {
-                       Long[] retArr = byteArrayToLongArray(paramBytes);
-                       Collections.addAll(retSet, retArr);
-               } else if (genericType == Float.class) {
-                       Float[] retArr = byteArrayToFloatArray(paramBytes);
-                       Collections.addAll(retSet, retArr);
-               } else if (genericType == Double.class) {
-                       Double[] retArr = byteArrayToDoubleArray(paramBytes);
-                       Collections.addAll(retSet, retArr);
-               } else if (genericType == Boolean.class) {
-                       Boolean[] retArr = byteArrayToBooleanArray(paramBytes);
-                       Collections.addAll(retSet, retArr);
-               } else if (genericType == Character.class) {
-                       Character[] retArr = byteArrayToCharacterArray(paramBytes);
-                       Collections.addAll(retSet, retArr);
-               } else if (genericType == String.class) {
-                       String[] retArr = byteArrayToStringArray(paramBytes);
-                       Collections.addAll(retSet, retArr);
-               } else
-                       throw new Error("IoTRMIUtil: Unrecognizable object: " + genericType.getSimpleName());
-
-               return retSet;
-       }
-
-
        // Get a List object from bytes
        public static Object getParamListObject(Class<?> genericType, byte[] paramBytes) {
 
@@ -597,32 +450,6 @@ public class IoTRMIUtil {
        }
 
 
-       // Get a Key array for Map object from bytes
-       public static Object getParamMapObject(Class<?> genTypeKey, Class<?> genTypeVal, byte[] paramBytes) {
-
-               // The complete set of bytes always consists of all keys followed by all values - <K,V> pairs
-               // Calculate number of elements
-               byte[] numElBytes = new byte[INT_LEN];
-               System.arraycopy(paramBytes, 0, numElBytes, 0, INT_LEN);
-               int numEl = byteArrayToInt(numElBytes);
-               int keyLen = numEl * getTypeSize(genTypeKey);
-               int valLen = numEl * getTypeSize(genTypeVal);
-               byte[] prmKeyBytes = new byte[keyLen];
-               byte[] prmValBytes = new byte[valLen];
-               // Copy bytes
-               System.arraycopy(paramBytes, INT_LEN, prmKeyBytes, 0, keyLen);
-               System.arraycopy(paramBytes, (INT_LEN + keyLen), prmValBytes, 0, valLen);
-               // Get array of keys
-               Object[] retObjKey = (Object[]) getParamObjectArray(genTypeKey, prmKeyBytes);
-               Object[] retObjVal = (Object[]) getParamObjectArray(genTypeVal, prmValBytes);
-               // Put everything back to a Map
-               Map<Object,Object> retMap = new HashMap<Object,Object>();
-               IoTRMITypes.arraysToMap(retMap, retObjKey, retObjVal);
-
-               return retMap;
-       }
-
-
        /**
         * Converters to byte array
         */
@@ -1299,23 +1126,4 @@ public class IoTRMIUtil {
         }
         return obj;
     }
-
-
-       public static void main(String[] args) {
-
-               //boolean data = false;
-               //char data = 'c';
-               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));
-               
-               String str = "methodA(int,string,float,double,double)";
-               int hash = str.hashCode();
-               System.out.println("Hash value: " + hash);
-       }
 }