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;
11 import java.lang.reflect.*;
14 /** Class IoTRMIObject is a class that stores info of an object.
16 * It stores object ID, methods, method ID, method's signature
18 * This class also receive calls from different objects as they
19 * ask to execute certain methods remotely. This will have the
20 * execution result (return value) sent back to
22 * @author Rahmadi Trimananda <rtrimana @ uci.edu>
26 public class IoTRMIObject {
31 private List<String> listMethodId2Sign; // List of method signature (we use list index as method Id)
32 private IoTRMIUtil rmiUtil;
33 private IoTSocketServer rmiServer;
34 private byte[] methodBytes;
40 public IoTRMIObject(int _port) throws
41 ClassNotFoundException, InstantiationException,
42 IllegalAccessException, IOException {
44 rmiUtil = new IoTRMIUtil();
46 rmiServer = new IoTSocketServer(_port);
52 * getMethodBytes() waits for method transmission in bytes
54 public byte[] getMethodBytes() throws IOException {
56 // Receive method info
57 methodBytes = rmiServer.receiveBytes(methodBytes);
63 * getObjectId() gets object Id from bytes
65 public int getObjectId() {
67 // Get object Id bytes
68 byte[] objectIdBytes = new byte[IoTRMIUtil.OBJECT_ID_LEN];
69 System.arraycopy(methodBytes, 0, objectIdBytes, 0, IoTRMIUtil.OBJECT_ID_LEN);
71 int objectId = IoTRMIUtil.byteArrayToInt(objectIdBytes);
77 * static version of getObjectId()
79 public static int getObjectId(byte[] methodBytes) {
81 // Get object Id bytes
82 byte[] objectIdBytes = new byte[IoTRMIUtil.OBJECT_ID_LEN];
83 System.arraycopy(methodBytes, 0, objectIdBytes, 0, IoTRMIUtil.OBJECT_ID_LEN);
85 int objectId = IoTRMIUtil.byteArrayToInt(objectIdBytes);
91 * setMethodBytes() sets bytes for method
93 /*public void setMethodBytes(byte[] _methodBytes) throws IOException {
96 methodBytes = _methodBytes;
101 * getMethodId() gets method Id from bytes
103 public int getMethodId() {
105 // Get method Id bytes
106 byte[] methodIdBytes = new byte[IoTRMIUtil.METHOD_ID_LEN];
107 // Method Id is positioned after object Id in the byte array
108 System.arraycopy(methodBytes, IoTRMIUtil.OBJECT_ID_LEN, methodIdBytes, 0, IoTRMIUtil.METHOD_ID_LEN);
110 int methodId = IoTRMIUtil.byteArrayToInt(methodIdBytes);
117 * static version of getMethodId()
119 public static int getMethodId(byte[] methodBytes) {
121 // Get method Id bytes
122 byte[] methodIdBytes = new byte[IoTRMIUtil.METHOD_ID_LEN];
123 // Method Id is positioned after object Id in the byte array
124 System.arraycopy(methodBytes, IoTRMIUtil.OBJECT_ID_LEN, methodIdBytes, 0, IoTRMIUtil.METHOD_ID_LEN);
126 int methodId = IoTRMIUtil.byteArrayToInt(methodIdBytes);
133 * getMethodParams() gets method params based on byte array received
135 * Basically this is the format of a method in bytes:
136 * 1) 32-bit value of object ID
137 * 2) 32-bit value of method ID
138 * 3) m parameters with n-bit value each (m x n-bit)
139 * For the parameters that don't have definite length,
140 * we need to extract the length from a preceding 32-bit
141 * field in front of it.
143 * For primitive objects:
144 * | 32-bit object ID | 32-bit method ID | m-bit actual data (fixed length) | ...
146 * For string, arrays, and non-primitive objects:
147 * | 32-bit object ID | 32-bit method ID | 32-bit length | n-bit actual data | ...
150 public Object[] getMethodParams(Class<?>[] arrCls, Class<?>[] arrGenValCls) {
152 // Byte scanning position
153 int pos = IoTRMIUtil.OBJECT_ID_LEN + IoTRMIUtil.METHOD_ID_LEN;
154 Object[] paramObj = new Object[arrCls.length];
155 for (int i=0; i < arrCls.length; i++) {
157 String paramType = arrCls[i].getSimpleName();
158 int paramSize = rmiUtil.getTypeSize(paramType);
159 // Get the 32-bit field in the byte array to get the actual
160 // length (this is a param with indefinite length)
161 if (paramSize == -1) {
162 byte[] bytPrmLen = new byte[IoTRMIUtil.PARAM_LEN];
163 System.arraycopy(methodBytes, pos, bytPrmLen, 0, IoTRMIUtil.PARAM_LEN);
164 pos = pos + IoTRMIUtil.PARAM_LEN;
165 paramSize = IoTRMIUtil.byteArrayToInt(bytPrmLen);
167 byte[] paramBytes = new byte[paramSize];
168 System.arraycopy(methodBytes, pos, paramBytes, 0, paramSize);
169 pos = pos + paramSize;
170 paramObj[i] = IoTRMIUtil.getParamObject(arrCls[i], arrGenValCls[i], paramBytes);
178 * sendReturnObj() sends back return Object to client
180 public void sendReturnObj(Object retObj) throws IOException {
182 // Send back return value
183 byte[] retObjBytes = IoTRMIUtil.getObjectBytes(retObj);
184 rmiServer.sendBytes(retObjBytes);
189 * sendReturnObj() overloaded to send multiple return objects for structs
191 public void sendReturnObj(Class<?>[] retCls, Object[] retObj) throws IOException {
193 // Send back return value
194 byte[] retObjBytes = returnToBytes(retCls, retObj);
195 rmiServer.sendBytes(retObjBytes);
200 * returnToBytes() takes array of objects and generates bytes
202 public byte[] returnToBytes(Class<?>[] retCls, Object[] retObj) {
204 // Get byte arrays and calculate method bytes length
205 int numbRet = retObj.length;
207 byte[][] objBytesArr = new byte[numbRet][];
208 for (int i = 0; i < numbRet; i++) {
209 // Get byte arrays for the objects
210 objBytesArr[i] = IoTRMIUtil.getObjectBytes(retObj[i]);
211 String clsName = retCls[i].getSimpleName();
212 int retObjLen = rmiUtil.getTypeSize(clsName);
213 if (retObjLen == -1) { // indefinite length - store the length first
214 retLen = retLen + IoTRMIUtil.RETURN_LEN;
216 retLen = retLen + objBytesArr[i].length;
218 // Construct return in byte array
219 byte[] retBytes = new byte[retLen];
221 // Iteration for copying bytes
222 for (int i = 0; i < numbRet; i++) {
224 String clsName = retCls[i].getSimpleName();
225 int retObjLen = rmiUtil.getTypeSize(clsName);
226 if (retObjLen == -1) { // indefinite length
227 retObjLen = objBytesArr[i].length;
228 byte[] retLenBytes = IoTRMIUtil.intToByteArray(retObjLen);
229 System.arraycopy(retLenBytes, 0, retBytes, pos, IoTRMIUtil.RETURN_LEN);
230 pos = pos + IoTRMIUtil.RETURN_LEN;
232 System.arraycopy(objBytesArr[i], 0, retBytes, pos, retObjLen);
233 pos = pos + retObjLen;