Merge branch 'master' of ssh://plrg.eecs.uci.edu/home/git/iot2
[iot2.git] / iotjava / iotrmi / Java / IoTRMIObject.java
index 317ed1f5804837c300a26643a33185b74ada7747..c960aa7080d3b08b344bf7027e308d53a6d038e0 100644 (file)
@@ -10,6 +10,9 @@ import java.util.Map;
 import java.util.Set;
 import java.lang.reflect.*;
 
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+
 
 /** Class IoTRMIObject is a class that stores info of an object.
  *  <p>
@@ -23,7 +26,7 @@ import java.lang.reflect.*;
  * @version     1.0
  * @since       2016-10-03
  */
-public class IoTRMIObject {
+public final class IoTRMIObject {
 
        /**
         * Class Properties
@@ -32,6 +35,7 @@ public class IoTRMIObject {
        private IoTRMIUtil rmiUtil;
        private IoTSocketServer rmiServer;
        private byte[] methodBytes;
+       private Lock lock = new ReentrantLock();
 
 
        /**
@@ -54,8 +58,9 @@ public class IoTRMIObject {
        public byte[] getMethodBytes() throws IOException {
 
                // Receive method info
+               //System.out.println("Method RMIObj before: " + Arrays.toString(methodBytes));
                methodBytes = rmiServer.receiveBytes(methodBytes);
-               System.out.println("Method: " + Arrays.toString(methodBytes));
+               //System.out.println("Method RMIObj after: " + Arrays.toString(methodBytes));
                return methodBytes;
        }
 
@@ -89,19 +94,24 @@ public class IoTRMIObject {
 
 
        /**
-        * setMethodBytes() sets bytes for method
+        * getMethodId() gets method Id from bytes
         */
-       public void setMethodBytes(byte[] _methodBytes) throws IOException {
+       public int getMethodId() {
 
-               // Set method bytes
-               methodBytes = _methodBytes;
+               // Get method Id bytes
+               byte[] methodIdBytes = new byte[IoTRMIUtil.METHOD_ID_LEN];
+               // Method Id is positioned after object Id in the byte array
+               System.arraycopy(methodBytes, IoTRMIUtil.OBJECT_ID_LEN, methodIdBytes, 0, IoTRMIUtil.METHOD_ID_LEN);
+               // Get method Id
+               int methodId = IoTRMIUtil.byteArrayToInt(methodIdBytes);
+               return methodId;
        }
 
 
        /**
-        * getMethodId() gets method Id from bytes
+        * static version of getMethodId()
         */
-       public int getMethodId() {
+       public static int getMethodId(byte[] methodBytes) {
 
                // Get method Id bytes
                byte[] methodIdBytes = new byte[IoTRMIUtil.METHOD_ID_LEN];
@@ -109,7 +119,6 @@ public class IoTRMIObject {
                System.arraycopy(methodBytes, IoTRMIUtil.OBJECT_ID_LEN, methodIdBytes, 0, IoTRMIUtil.METHOD_ID_LEN);
                // Get method Id
                int methodId = IoTRMIUtil.byteArrayToInt(methodIdBytes);
-               // Get method Id
                return methodId;
        }
 
@@ -132,7 +141,7 @@ public class IoTRMIObject {
         * | 32-bit object ID | 32-bit method ID | 32-bit length | n-bit actual data | ...
         * 
         */
-       public Object[] getMethodParams(Class<?>[] arrCls, Class<?>[] arrGenKeyCls, Class<?>[] arrGenValCls) {
+       public Object[] getMethodParams(Class<?>[] arrCls, Class<?>[] arrGenValCls) {
 
                // Byte scanning position
                int pos = IoTRMIUtil.OBJECT_ID_LEN + IoTRMIUtil.METHOD_ID_LEN;
@@ -152,8 +161,7 @@ public class IoTRMIObject {
                        byte[] paramBytes = new byte[paramSize];
                        System.arraycopy(methodBytes, pos, paramBytes, 0, paramSize);
                        pos = pos + paramSize;
-                       paramObj[i] = IoTRMIUtil.getParamObject(arrCls[i], arrGenKeyCls[i], 
-                               arrGenValCls[i], paramBytes);
+                       paramObj[i] = IoTRMIUtil.getParamObject(arrCls[i], arrGenValCls[i], paramBytes);
                }
 
                return paramObj;
@@ -169,4 +177,56 @@ public class IoTRMIObject {
                byte[] retObjBytes = IoTRMIUtil.getObjectBytes(retObj);
                rmiServer.sendBytes(retObjBytes);
        }
+
+
+       /**
+        * sendReturnObj() overloaded to send multiple return objects for structs
+        */
+       public void sendReturnObj(Class<?>[] retCls, Object[] retObj) throws IOException {
+
+               // Send back return value
+               byte[] retObjBytes = returnToBytes(retCls, retObj);
+               rmiServer.sendBytes(retObjBytes);
+       }
+
+
+       /**
+        * returnToBytes() takes array of objects and generates bytes
+        */
+       public byte[] returnToBytes(Class<?>[] retCls, Object[] retObj) {
+
+               // Get byte arrays and calculate method bytes length
+               int numbRet = retObj.length;
+               int retLen = 0;
+               byte[][] objBytesArr = new byte[numbRet][];
+               for (int i = 0; i < numbRet; i++) {
+                       // Get byte arrays for the objects
+                       objBytesArr[i] = IoTRMIUtil.getObjectBytes(retObj[i]);
+                       String clsName = retCls[i].getSimpleName();
+                       int retObjLen = rmiUtil.getTypeSize(clsName);
+                       if (retObjLen == -1) {          // indefinite length - store the length first
+                               retLen = retLen + IoTRMIUtil.RETURN_LEN;
+                       }
+                       retLen = retLen + objBytesArr[i].length;
+               }
+               // Construct return in byte array
+               byte[] retBytes = new byte[retLen];
+               int pos = 0;
+               // Iteration for copying bytes
+               for (int i = 0; i < numbRet; i++) {
+
+                       String clsName = retCls[i].getSimpleName();
+                       int retObjLen = rmiUtil.getTypeSize(clsName);
+                       if (retObjLen == -1) {          // indefinite length
+                               retObjLen = objBytesArr[i].length;
+                               byte[] retLenBytes = IoTRMIUtil.intToByteArray(retObjLen);
+                               System.arraycopy(retLenBytes, 0, retBytes, pos, IoTRMIUtil.RETURN_LEN);
+                               pos = pos + IoTRMIUtil.RETURN_LEN;
+                       }               
+                       System.arraycopy(objBytesArr[i], 0, retBytes, pos, retObjLen);
+                       pos = pos + retObjLen;
+               }
+
+               return retBytes;
+       }
 }