Cleaning up code for runtime, installer, RMI, compiler for the Java side
[iot2.git] / iotjava / iotrmi / Java / IoTRMIObject.java
1 package iotrmi.Java;
2
3 import java.io.IOException;
4 import java.nio.ByteBuffer;
5 import java.util.Arrays;
6 import java.util.ArrayList;
7 import java.util.HashMap;
8 import java.util.List;
9 import java.util.Map;
10 import java.util.Set;
11 import java.lang.reflect.*;
12
13 import java.util.concurrent.locks.Lock;
14 import java.util.concurrent.locks.ReentrantLock;
15
16
17 /** Class IoTRMIObject is a class that stores info of an object.
18  *  <p>
19  *  It stores object ID, methods, method ID, method's signature 
20  *  and parameters.
21  *  This class also receive calls from different objects as they
22  *  ask to execute certain methods remotely. This will have the 
23  *  execution result (return value) sent back to 
24  *
25  * @author      Rahmadi Trimananda <rtrimana @ uci.edu>
26  * @version     1.0
27  * @since       2016-10-03
28  */
29 public final class IoTRMIObject {
30
31         /**
32          * Class Properties
33          */
34         private List<String> listMethodId2Sign; // List of method signature (we use list index as method Id)
35         private IoTRMIUtil rmiUtil;
36         private IoTSocketServer rmiServer;
37         private byte[] methodBytes;
38         private Lock lock = new ReentrantLock();
39
40
41         /**
42          * Constructors
43          */
44         public IoTRMIObject(int _port) throws  
45                 ClassNotFoundException, InstantiationException, 
46                         IllegalAccessException, IOException {
47
48                 rmiUtil = new IoTRMIUtil();
49                 methodBytes = null;
50                 rmiServer = new IoTSocketServer(_port);
51                 rmiServer.connect();
52         }
53
54
55         /**
56          * getMethodBytes() waits for method transmission in bytes
57          */
58         public byte[] getMethodBytes() throws IOException {
59
60                 // Receive method info
61                 //System.out.println("Method RMIObj before: " + Arrays.toString(methodBytes));
62                 methodBytes = rmiServer.receiveBytes(methodBytes);
63                 //System.out.println("Method RMIObj after: " + Arrays.toString(methodBytes));
64                 return methodBytes;
65         }
66
67
68         /**
69          * getObjectId() gets object Id from bytes
70          */
71         public int getObjectId() {
72
73                 // Get object Id bytes
74                 byte[] objectIdBytes = new byte[IoTRMIUtil.OBJECT_ID_LEN];
75                 System.arraycopy(methodBytes, 0, objectIdBytes, 0, IoTRMIUtil.OBJECT_ID_LEN);
76                 // Get object Id
77                 int objectId = IoTRMIUtil.byteArrayToInt(objectIdBytes);
78                 return objectId;
79         }
80
81
82         /**
83          * static version of getObjectId()
84          */
85         public static int getObjectId(byte[] methodBytes) {
86
87                 // Get object Id bytes
88                 byte[] objectIdBytes = new byte[IoTRMIUtil.OBJECT_ID_LEN];
89                 System.arraycopy(methodBytes, 0, objectIdBytes, 0, IoTRMIUtil.OBJECT_ID_LEN);
90                 // Get object Id
91                 int objectId = IoTRMIUtil.byteArrayToInt(objectIdBytes);
92                 return objectId;
93         }
94
95
96         /**
97          * getMethodId() gets method Id from bytes
98          */
99         public int getMethodId() {
100
101                 // Get method Id bytes
102                 byte[] methodIdBytes = new byte[IoTRMIUtil.METHOD_ID_LEN];
103                 // Method Id is positioned after object Id in the byte array
104                 System.arraycopy(methodBytes, IoTRMIUtil.OBJECT_ID_LEN, methodIdBytes, 0, IoTRMIUtil.METHOD_ID_LEN);
105                 // Get method Id
106                 int methodId = IoTRMIUtil.byteArrayToInt(methodIdBytes);
107                 return methodId;
108         }
109
110
111         /**
112          * static version of getMethodId()
113          */
114         public static int getMethodId(byte[] methodBytes) {
115
116                 // Get method Id bytes
117                 byte[] methodIdBytes = new byte[IoTRMIUtil.METHOD_ID_LEN];
118                 // Method Id is positioned after object Id in the byte array
119                 System.arraycopy(methodBytes, IoTRMIUtil.OBJECT_ID_LEN, methodIdBytes, 0, IoTRMIUtil.METHOD_ID_LEN);
120                 // Get method Id
121                 int methodId = IoTRMIUtil.byteArrayToInt(methodIdBytes);
122                 return methodId;
123         }
124
125
126         /**
127          * getMethodParams() gets method params based on byte array received
128          * <p>
129          * Basically this is the format of a method in bytes:
130          * 1) 32-bit value of object ID
131          * 2) 32-bit value of method ID
132          * 3) m parameters with n-bit value each (m x n-bit)
133          * For the parameters that don't have definite length,
134          * we need to extract the length from a preceding 32-bit
135          * field in front of it.
136          *
137          * For primitive objects:
138          * | 32-bit object ID | 32-bit method ID | m-bit actual data (fixed length)  | ...
139          * 
140          * For string, arrays, and non-primitive objects:
141          * | 32-bit object ID | 32-bit method ID | 32-bit length | n-bit actual data | ...
142          * 
143          */
144         public Object[] getMethodParams(Class<?>[] arrCls, Class<?>[] arrGenValCls) {
145
146                 // Byte scanning position
147                 int pos = IoTRMIUtil.OBJECT_ID_LEN + IoTRMIUtil.METHOD_ID_LEN;
148                 Object[] paramObj = new Object[arrCls.length];
149                 for (int i=0; i < arrCls.length; i++) {
150
151                         String paramType = arrCls[i].getSimpleName();
152                         int paramSize = rmiUtil.getTypeSize(paramType);
153                         // Get the 32-bit field in the byte array to get the actual
154                         //              length (this is a param with indefinite length)
155                         if (paramSize == -1) {
156                                 byte[] bytPrmLen = new byte[IoTRMIUtil.PARAM_LEN];
157                                 System.arraycopy(methodBytes, pos, bytPrmLen, 0, IoTRMIUtil.PARAM_LEN);
158                                 pos = pos + IoTRMIUtil.PARAM_LEN;
159                                 paramSize = IoTRMIUtil.byteArrayToInt(bytPrmLen);
160                         }
161                         byte[] paramBytes = new byte[paramSize];
162                         System.arraycopy(methodBytes, pos, paramBytes, 0, paramSize);
163                         pos = pos + paramSize;
164                         paramObj[i] = IoTRMIUtil.getParamObject(arrCls[i], arrGenValCls[i], paramBytes);
165                 }
166
167                 return paramObj;
168         }
169
170
171         /**
172          * sendReturnObj() sends back return Object to client
173          */
174         public void sendReturnObj(Object retObj) throws IOException {
175
176                 // Send back return value
177                 byte[] retObjBytes = IoTRMIUtil.getObjectBytes(retObj);
178                 rmiServer.sendBytes(retObjBytes);
179         }
180
181
182         /**
183          * sendReturnObj() overloaded to send multiple return objects for structs
184          */
185         public void sendReturnObj(Class<?>[] retCls, Object[] retObj) throws IOException {
186
187                 // Send back return value
188                 byte[] retObjBytes = returnToBytes(retCls, retObj);
189                 rmiServer.sendBytes(retObjBytes);
190         }
191
192
193         /**
194          * returnToBytes() takes array of objects and generates bytes
195          */
196         public byte[] returnToBytes(Class<?>[] retCls, Object[] retObj) {
197
198                 // Get byte arrays and calculate method bytes length
199                 int numbRet = retObj.length;
200                 int retLen = 0;
201                 byte[][] objBytesArr = new byte[numbRet][];
202                 for (int i = 0; i < numbRet; i++) {
203                         // Get byte arrays for the objects
204                         objBytesArr[i] = IoTRMIUtil.getObjectBytes(retObj[i]);
205                         String clsName = retCls[i].getSimpleName();
206                         int retObjLen = rmiUtil.getTypeSize(clsName);
207                         if (retObjLen == -1) {          // indefinite length - store the length first
208                                 retLen = retLen + IoTRMIUtil.RETURN_LEN;
209                         }
210                         retLen = retLen + objBytesArr[i].length;
211                 }
212                 // Construct return in byte array
213                 byte[] retBytes = new byte[retLen];
214                 int pos = 0;
215                 // Iteration for copying bytes
216                 for (int i = 0; i < numbRet; i++) {
217
218                         String clsName = retCls[i].getSimpleName();
219                         int retObjLen = rmiUtil.getTypeSize(clsName);
220                         if (retObjLen == -1) {          // indefinite length
221                                 retObjLen = objBytesArr[i].length;
222                                 byte[] retLenBytes = IoTRMIUtil.intToByteArray(retObjLen);
223                                 System.arraycopy(retLenBytes, 0, retBytes, pos, IoTRMIUtil.RETURN_LEN);
224                                 pos = pos + IoTRMIUtil.RETURN_LEN;
225                         }               
226                         System.arraycopy(objBytesArr[i], 0, retBytes, pos, retObjLen);
227                         pos = pos + retObjLen;
228                 }
229
230                 return retBytes;
231         }
232 }