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, String[] _methodSign) throws
41 ClassNotFoundException, InstantiationException,
42 IllegalAccessException, IOException {
44 rmiUtil = new IoTRMIUtil();
45 listMethodId2Sign = Arrays.asList(_methodSign); // Initialize the method ID list
47 rmiServer = new IoTSocketServer(_port);
53 * sendReturnObj() sends back return Object to client
55 public void sendReturnObj(Object retObj) throws IOException {
57 // Send back return value
58 byte[] retObjBytes = IoTRMIUtil.getObjectBytes(retObj);
59 rmiServer.sendBytes(retObjBytes);
64 * getMethodBytes() waits for method transmission in bytes
66 public void getMethodBytes() throws IOException {
68 // Receive method info
69 methodBytes = rmiServer.receiveBytes(methodBytes);
74 * getSignature() gets method signature from bytes
76 public String getSignature() {
78 // Get method Id bytes
79 byte[] methodIdBytes = new byte[IoTRMIUtil.METHOD_ID_LEN];
80 System.arraycopy(methodBytes, 0, methodIdBytes, 0, IoTRMIUtil.METHOD_ID_LEN);
82 int methodId = IoTRMIUtil.byteArrayToInt(methodIdBytes);
83 // Get method signature from the list
84 return listMethodId2Sign.get(methodId);
89 * getMethodParams() gets method params based on byte array received
91 * Basically this is the format of a method in bytes:
92 * 1) 32-bit value of method ID (hash code)
93 * 2) m parameters with n-bit value each (m x n-bit)
94 * For the parameters that don't have definite length,
95 * we need to extract the length from a preceding 32-bit
96 * field in front of it.
98 * For primitive objects:
99 * | 32-bit method ID | m-bit actual data (fixed length) |
101 * For string, arrays, and non-primitive objects:
102 * | 32-bit method ID | 32-bit length | n-bit actual data | ...
105 public Object[] getMethodParams(Class<?>[] arrCls, Class<?>[] arrGenKeyCls, Class<?>[] arrGenValCls) {
107 // Byte scanning position
108 int pos = IoTRMIUtil.METHOD_ID_LEN;
109 Object[] paramObj = new Object[arrCls.length];
110 for (int i=0; i < arrCls.length; i++) {
112 String paramType = arrCls[i].getSimpleName();
113 int paramSize = rmiUtil.getTypeSize(paramType);
114 // Get the 32-bit field in the byte array to get the actual
115 // length (this is a param with indefinite length)
116 if (paramSize == -1) {
117 byte[] bytPrmLen = new byte[IoTRMIUtil.PARAM_LEN];
118 System.arraycopy(methodBytes, pos, bytPrmLen, 0, IoTRMIUtil.PARAM_LEN);
119 pos = pos + IoTRMIUtil.PARAM_LEN;
120 paramSize = IoTRMIUtil.byteArrayToInt(bytPrmLen);
122 byte[] paramBytes = new byte[paramSize];
123 System.arraycopy(methodBytes, pos, paramBytes, 0, paramSize);
124 pos = pos + paramSize;
125 paramObj[i] = IoTRMIUtil.getParamObject(arrCls[i], arrGenKeyCls[i],
126 arrGenValCls[i], paramBytes);