Initial version that handles multiple callback objects through 1 socket
[iot2.git] / iotjava / iotrmi / Java / IoTRMIObject.java
index 3ecd6bfa8d858eaf3984c838969ca89af286efda..043c28a8cb1b0359e6ebcf8d3a117c047650e345 100644 (file)
@@ -50,23 +50,51 @@ public class IoTRMIObject {
 
 
        /**
-        * sendReturnObj() sends back return Object to client
+        * getMethodBytes() waits for method transmission in bytes
         */
-       public void sendReturnObj(Object retObj) throws IOException {
+       public byte[] getMethodBytes() throws IOException {
 
-               // Send back return value
-               byte[] retObjBytes = IoTRMIUtil.getObjectBytes(retObj);
-               rmiServer.sendBytes(retObjBytes);
+               // Receive method info
+               methodBytes = rmiServer.receiveBytes(methodBytes);
+               return methodBytes;
        }
 
 
        /**
-        * getMethodBytes() waits for method transmission in bytes
+        * getObjectId() gets object Id from bytes
         */
-       public void getMethodBytes() throws IOException {
+       public int getObjectId() {
+
+               // Get object Id bytes
+               byte[] objectIdBytes = new byte[IoTRMIUtil.OBJECT_ID_LEN];
+               System.arraycopy(methodBytes, 0, objectIdBytes, 0, IoTRMIUtil.OBJECT_ID_LEN);
+               // Get object Id
+               int objectId = IoTRMIUtil.byteArrayToInt(objectIdBytes);
+               return objectId;
+       }
 
-               // Receive method info
-               methodBytes = rmiServer.receiveBytes(methodBytes);
+
+       /**
+        * static version of getObjectId()
+        */
+       public static int getObjectId(byte[] methodBytes) {
+
+               // Get object Id bytes
+               byte[] objectIdBytes = new byte[IoTRMIUtil.OBJECT_ID_LEN];
+               System.arraycopy(methodBytes, 0, objectIdBytes, 0, IoTRMIUtil.OBJECT_ID_LEN);
+               // Get object Id
+               int objectId = IoTRMIUtil.byteArrayToInt(objectIdBytes);
+               return objectId;
+       }
+
+
+       /**
+        * setMethodBytes() sets bytes for method
+        */
+       public void setMethodBytes(byte[] _methodBytes) throws IOException {
+
+               // Set method bytes
+               methodBytes = _methodBytes;
        }
 
 
@@ -77,7 +105,8 @@ public class IoTRMIObject {
 
                // Get method Id bytes
                byte[] methodIdBytes = new byte[IoTRMIUtil.METHOD_ID_LEN];
-               System.arraycopy(methodBytes, 0, methodIdBytes, 0, 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);
                // Get method signature from the list
@@ -89,23 +118,24 @@ public class IoTRMIObject {
         * getMethodParams() gets method params based on byte array received
         * <p>
         * Basically this is the format of a method in bytes:
-        * 1) 32-bit value of method ID (hash code)
-        * 2) m parameters with n-bit value each (m x n-bit)
+        * 1) 32-bit value of object ID
+        * 2) 32-bit value of method ID
+        * 3) m parameters with n-bit value each (m x n-bit)
         * For the parameters that don't have definite length,
         * we need to extract the length from a preceding 32-bit
         * field in front of it.
         *
         * For primitive objects:
-        * | 32-bit method ID | m-bit actual data (fixed length)  |
+        * | 32-bit object ID | 32-bit method ID | m-bit actual data (fixed length)  | ...
         * 
         * For string, arrays, and non-primitive objects:
-        * | 32-bit method ID | 32-bit length | n-bit actual data | ...
+        * | 32-bit object ID | 32-bit method ID | 32-bit length | n-bit actual data | ...
         * 
         */
        public Object[] getMethodParams(Class<?>[] arrCls, Class<?>[] arrGenKeyCls, Class<?>[] arrGenValCls) {
 
                // Byte scanning position
-               int pos = IoTRMIUtil.METHOD_ID_LEN;
+               int pos = IoTRMIUtil.OBJECT_ID_LEN + IoTRMIUtil.METHOD_ID_LEN;
                Object[] paramObj = new Object[arrCls.length];
                for (int i=0; i < arrCls.length; i++) {
 
@@ -128,4 +158,15 @@ public class IoTRMIObject {
 
                return paramObj;
        }
+
+
+       /**
+        * sendReturnObj() sends back return Object to client
+        */
+       public void sendReturnObj(Object retObj) throws IOException {
+
+               // Send back return value
+               byte[] retObjBytes = IoTRMIUtil.getObjectBytes(retObj);
+               rmiServer.sendBytes(retObjBytes);
+       }
 }