Making classes final to make inheritance impossible
[iot2.git] / iotjava / iotrmi / Java / IoTRMICall.java
index 8bd86c9207eb90a544dc6a193db0641c796e6e4a..b3f9108667b9edb16d1417fc65c0d99453dd77c3 100644 (file)
@@ -23,7 +23,7 @@ import java.util.Set;
  * @version     1.0
  * @since       2016-10-04
  */
-public class IoTRMICall {
+public final class IoTRMICall {
 
 
        /**
@@ -31,28 +31,26 @@ public class IoTRMICall {
         */
        private IoTRMIUtil rmiUtil;
        private IoTSocketClient rmiClient;
-       private List<String> listMethodId;      // Map from method ID to signature
 
 
        /**
         * Constructors
         */
-       public IoTRMICall(int _port, String _address, int _rev, String[] _methodSign) throws IOException {
+       public IoTRMICall(int _port, String _address, int _rev) throws IOException {
 
                rmiUtil = new IoTRMIUtil();
                rmiClient = new IoTSocketClient(_port, _address, _rev);
-               listMethodId = Arrays.asList(_methodSign); // Initialize the method ID map
        }
 
 
        /**
         * remoteCall() calls a method remotely by passing in parameters and getting a return Object
         */
-       public Object remoteCall(int objectId, String methodSign, Class<?> retType, Class<?> retGenTypeKey
+       public synchronized Object remoteCall(int objectId, int methodId, Class<?> retType
                        Class<?> retGenTypeVal, Class<?>[] paramCls, Object[] paramObj) {
 
                // Send method info
-               byte[] methodBytes = methodToBytes(objectId, methodSign, paramCls, paramObj);
+               byte[] methodBytes = methodToBytes(objectId, methodId, paramCls, paramObj);
                try {
                        rmiClient.sendBytes(methodBytes);
                } catch (IOException ex) {
@@ -69,7 +67,7 @@ public class IoTRMICall {
                                ex.printStackTrace();
                                throw new Error("IoTRMICall: Error when receiving bytes - rmiClient.receiveBytes()");
                        }
-                       retObj = IoTRMIUtil.getParamObject(retType, retGenTypeKey, retGenTypeVal, retObjBytes);
+                       retObj = IoTRMIUtil.getParamObject(retType, retGenTypeVal, retObjBytes);
                }
                return retObj;
        }
@@ -78,13 +76,12 @@ public class IoTRMICall {
        /**
         * methodToBytes() returns byte representation of a method
         */
-       public byte[] methodToBytes(int objectId, String methodSign, Class<?>[] paramCls, Object[] paramObj) {
+       public byte[] methodToBytes(int objectId, int methId, Class<?>[] paramCls, Object[] paramObj) {
 
                // Initialized to the length of method ID
                int methodLen = IoTRMIUtil.OBJECT_ID_LEN;
                byte[] objId = IoTRMIUtil.intToByteArray(objectId);
                // Get method ID in bytes
-               int methId = listMethodId.indexOf(methodSign);
                byte[] methodId = IoTRMIUtil.intToByteArray(methId);
                // Get byte arrays and calculate method bytes length
                int numbParam = paramObj.length;
@@ -126,66 +123,49 @@ public class IoTRMICall {
        }
 
 
-       public static void main(String[] args) throws Exception {
-
-               String[] test = { "123", "456", "789" };
-               byte[] b = IoTRMIUtil.getObjectBytes(test);
-
-               Boolean[] test2 = new Boolean[] { true, false, false };
-               byte[] b2 = IoTRMIUtil.getObjectBytes(test2);
-
-               System.out.println(Arrays.toString(b));
-               System.out.println(Arrays.toString(b2));
-
-               String[] c = (String[]) IoTRMIUtil.getParamObjectArray(String[].class, b);
-               System.out.println(Arrays.toString(c));
-
-               Boolean[] c2 = (Boolean[]) IoTRMIUtil.getParamObjectArray(Boolean[].class, b2);
-               System.out.println(Arrays.toString(c2));
-
-               // Set
-               /*Set<String> set = new HashSet<String>();
-               set.add("1234");
-               set.add("5678");
-
-               byte[] objBytes = IoTRMIUtil.getObjectBytes(set);
-               System.out.println(Arrays.toString(objBytes));
-               Object obj = IoTRMIUtil.getParamObject(Set.class, null, String.class, objBytes);
-
-               @SuppressWarnings("unchecked")
-               Set<String> setStr = (Set<String>) obj;
-               System.out.println("Set: " + setStr.toString());*/
-
-               // List
-               /*List<Long> list = new ArrayList<Long>();
-               list.add(12345678l);
-               list.add(23455432l);
-               list.add(34566543l);
-
-               byte[] objBytes = IoTRMIUtil.getObjectBytes(list);
-               System.out.println(Arrays.toString(objBytes));
-               Object obj = IoTRMIUtil.getParamObject(List.class, null, Long.class, objBytes);
+       /**
+        * remoteCall() calls a method remotely by passing in parameters and getting a return Object
+        */
+       public synchronized Object[] getStructObjects(Class<?>[] retType, Class<?>[] retGenTypeVal) {
 
-               @SuppressWarnings("unchecked")
-               List<Long> listStr = (List<Long>) obj;
-               System.out.println("List: " + listStr.toString());*/
+               // Receive return value and return it to caller
+               Object[] retObj = null;
+               byte[] retObjBytes = null;
+               try {
+                       retObjBytes = rmiClient.receiveBytes(retObjBytes);
+               } catch (IOException ex) {
+                       ex.printStackTrace();
+                       throw new Error("IoTRMICall: Error when receiving bytes - rmiClient.receiveBytes()");
+               }
+               retObj = getReturnObjects(retObjBytes, retType, retGenTypeVal);
 
-               // Map
-               Map<Long,Integer> map = new HashMap<Long,Integer>();
-               map.put(12345678l, 1234);
-               map.put(23455432l, 5678);
-               map.put(34566543l, 4321);
+               return retObj;
+       }
 
-               byte[] objBytes = IoTRMIUtil.getObjectBytes(map);
-               System.out.println(Arrays.toString(objBytes));
-               Object obj = IoTRMIUtil.getParamObject(Map.class, Long.class, Integer.class, objBytes);
 
-               map = (Map<Long,Integer>) obj;
-               System.out.println("Received map: " + map.toString());
+       public Object[] getReturnObjects(byte[] retBytes, Class<?>[] arrCls, Class<?>[] arrGenValCls) {
 
-               //@SuppressWarnings("unchecked")
-               //List<Long> listStr = (List<Long>) obj;
-               //System.out.println("List: " + listStr.toString());
+               // Byte scanning position
+               int pos = 0;
+               Object[] retObj = new Object[arrCls.length];
+               for (int i=0; i < arrCls.length; i++) {
+
+                       String retType = arrCls[i].getSimpleName();
+                       int retSize = rmiUtil.getTypeSize(retType);
+                       // Get the 32-bit field in the byte array to get the actual
+                       //              length (this is a param with indefinite length)
+                       if (retSize == -1) {
+                               byte[] bytRetLen = new byte[IoTRMIUtil.RETURN_LEN];
+                               System.arraycopy(retBytes, pos, bytRetLen, 0, IoTRMIUtil.RETURN_LEN);
+                               pos = pos + IoTRMIUtil.RETURN_LEN;
+                               retSize = IoTRMIUtil.byteArrayToInt(bytRetLen);
+                       }
+                       byte[] retObjBytes = new byte[retSize];
+                       System.arraycopy(retBytes, pos, retObjBytes, 0, retSize);
+                       pos = pos + retSize;
+                       retObj[i] = IoTRMIUtil.getParamObject(arrCls[i], arrGenValCls[i], retObjBytes);
+               }
 
+               return retObj;
        }
 }