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;
10 import java.lang.reflect.Method;
12 import java.util.HashSet;
16 /** Class IoTRMICall is a class that serves method calls on stub.
18 * A stub will use an object of this class to send the method
19 * information, e.g. object identifier, method identifier, and
22 * @author Rahmadi Trimananda <rtrimana @ uci.edu>
26 public final class IoTRMICall {
32 private IoTRMIUtil rmiUtil;
33 private IoTSocketClient rmiClient;
39 public IoTRMICall(int _port, String _address, int _rev) throws IOException {
41 rmiUtil = new IoTRMIUtil();
42 rmiClient = new IoTSocketClient(_port, _address, _rev);
47 * remoteCall() calls a method remotely by passing in parameters and getting a return Object
49 public synchronized Object remoteCall(int objectId, int methodId, Class<?> retType,
50 Class<?> retGenTypeVal, Class<?>[] paramCls, Object[] paramObj) {
53 byte[] methodBytes = methodToBytes(objectId, methodId, paramCls, paramObj);
55 rmiClient.sendBytes(methodBytes);
56 } catch (IOException ex) {
58 throw new Error("IoTRMICall: Error when sending bytes - rmiClient.sendBytes()");
60 // Receive return value and return it to caller
62 if (retType != void.class) {
63 byte[] retObjBytes = null;
65 retObjBytes = rmiClient.receiveBytes(retObjBytes);
66 } catch (IOException ex) {
68 throw new Error("IoTRMICall: Error when receiving bytes - rmiClient.receiveBytes()");
70 retObj = IoTRMIUtil.getParamObject(retType, retGenTypeVal, retObjBytes);
77 * methodToBytes() returns byte representation of a method
79 public byte[] methodToBytes(int objectId, int methId, Class<?>[] paramCls, Object[] paramObj) {
81 // Initialized to the length of method ID
82 int methodLen = IoTRMIUtil.OBJECT_ID_LEN;
83 byte[] objId = IoTRMIUtil.intToByteArray(objectId);
84 // Get method ID in bytes
85 byte[] methodId = IoTRMIUtil.intToByteArray(methId);
86 // Get byte arrays and calculate method bytes length
87 int numbParam = paramObj.length;
88 methodLen = methodLen + IoTRMIUtil.METHOD_ID_LEN;
89 byte[][] objBytesArr = new byte[numbParam][];
90 for (int i = 0; i < numbParam; i++) {
91 // Get byte arrays for the objects
92 objBytesArr[i] = IoTRMIUtil.getObjectBytes(paramObj[i]);
93 String clsName = paramCls[i].getSimpleName();
94 int paramLen = rmiUtil.getTypeSize(clsName);
95 if (paramLen == -1) { // indefinite length - store the length first
96 methodLen = methodLen + IoTRMIUtil.PARAM_LEN;
98 methodLen = methodLen + objBytesArr[i].length;
100 // Construct method in byte array
101 byte[] method = new byte[methodLen];
103 System.arraycopy(objId, 0, method, 0, IoTRMIUtil.METHOD_ID_LEN);
104 pos = pos + IoTRMIUtil.OBJECT_ID_LEN;
105 System.arraycopy(methodId, 0, method, pos, IoTRMIUtil.METHOD_ID_LEN);
106 pos = pos + IoTRMIUtil.METHOD_ID_LEN;
107 // Second iteration for copying bytes
108 for (int i = 0; i < numbParam; i++) {
110 String clsName = paramCls[i].getSimpleName();
111 int paramLen = rmiUtil.getTypeSize(clsName);
112 if (paramLen == -1) { // indefinite length
113 paramLen = objBytesArr[i].length;
114 byte[] paramLenBytes = IoTRMIUtil.intToByteArray(paramLen);
115 System.arraycopy(paramLenBytes, 0, method, pos, IoTRMIUtil.PARAM_LEN);
116 pos = pos + IoTRMIUtil.PARAM_LEN;
118 System.arraycopy(objBytesArr[i], 0, method, pos, paramLen);
119 pos = pos + paramLen;
127 * remoteCall() calls a method remotely by passing in parameters and getting a return Object
129 public synchronized Object[] getStructObjects(Class<?>[] retType, Class<?>[] retGenTypeVal) {
131 // Receive return value and return it to caller
132 Object[] retObj = null;
133 byte[] retObjBytes = null;
135 retObjBytes = rmiClient.receiveBytes(retObjBytes);
136 } catch (IOException ex) {
137 ex.printStackTrace();
138 throw new Error("IoTRMICall: Error when receiving bytes - rmiClient.receiveBytes()");
140 retObj = getReturnObjects(retObjBytes, retType, retGenTypeVal);
146 public Object[] getReturnObjects(byte[] retBytes, Class<?>[] arrCls, Class<?>[] arrGenValCls) {
148 // Byte scanning position
150 Object[] retObj = new Object[arrCls.length];
151 for (int i=0; i < arrCls.length; i++) {
153 String retType = arrCls[i].getSimpleName();
154 int retSize = rmiUtil.getTypeSize(retType);
155 // Get the 32-bit field in the byte array to get the actual
156 // length (this is a param with indefinite length)
158 byte[] bytRetLen = new byte[IoTRMIUtil.RETURN_LEN];
159 System.arraycopy(retBytes, pos, bytRetLen, 0, IoTRMIUtil.RETURN_LEN);
160 pos = pos + IoTRMIUtil.RETURN_LEN;
161 retSize = IoTRMIUtil.byteArrayToInt(bytRetLen);
163 byte[] retObjBytes = new byte[retSize];
164 System.arraycopy(retBytes, pos, retObjBytes, 0, retSize);
166 retObj[i] = IoTRMIUtil.getParamObject(arrCls[i], arrGenValCls[i], retObjBytes);