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 class IoTRMICall {
32 private IoTRMIUtil rmiUtil;
33 private IoTSocketClient rmiClient;
34 //private List<String> listMethodId; // Map from method ID to signature
40 public IoTRMICall(int _port, String _address, int _rev) throws IOException {
42 rmiUtil = new IoTRMIUtil();
43 rmiClient = new IoTSocketClient(_port, _address, _rev);
48 * remoteCall() calls a method remotely by passing in parameters and getting a return Object
50 public synchronized Object remoteCall(int objectId, int methodId, Class<?> retType,
51 Class<?> retGenTypeVal, Class<?>[] paramCls, Object[] paramObj) {
54 byte[] methodBytes = methodToBytes(objectId, methodId, paramCls, paramObj);
56 rmiClient.sendBytes(methodBytes);
57 } catch (IOException ex) {
59 throw new Error("IoTRMICall: Error when sending bytes - rmiClient.sendBytes()");
61 // Receive return value and return it to caller
63 if (retType != void.class) {
64 byte[] retObjBytes = null;
66 retObjBytes = rmiClient.receiveBytes(retObjBytes);
67 } catch (IOException ex) {
69 throw new Error("IoTRMICall: Error when receiving bytes - rmiClient.receiveBytes()");
71 retObj = IoTRMIUtil.getParamObject(retType, retGenTypeVal, retObjBytes);
78 * methodToBytes() returns byte representation of a method
80 public byte[] methodToBytes(int objectId, int methId, Class<?>[] paramCls, Object[] paramObj) {
82 // Initialized to the length of method ID
83 int methodLen = IoTRMIUtil.OBJECT_ID_LEN;
84 byte[] objId = IoTRMIUtil.intToByteArray(objectId);
85 // Get method ID in bytes
86 byte[] methodId = IoTRMIUtil.intToByteArray(methId);
87 // Get byte arrays and calculate method bytes length
88 int numbParam = paramObj.length;
89 methodLen = methodLen + IoTRMIUtil.METHOD_ID_LEN;
90 byte[][] objBytesArr = new byte[numbParam][];
91 for (int i = 0; i < numbParam; i++) {
92 // Get byte arrays for the objects
93 objBytesArr[i] = IoTRMIUtil.getObjectBytes(paramObj[i]);
94 String clsName = paramCls[i].getSimpleName();
95 int paramLen = rmiUtil.getTypeSize(clsName);
96 if (paramLen == -1) { // indefinite length - store the length first
97 methodLen = methodLen + IoTRMIUtil.PARAM_LEN;
99 methodLen = methodLen + objBytesArr[i].length;
101 // Construct method in byte array
102 byte[] method = new byte[methodLen];
104 System.arraycopy(objId, 0, method, 0, IoTRMIUtil.METHOD_ID_LEN);
105 pos = pos + IoTRMIUtil.OBJECT_ID_LEN;
106 System.arraycopy(methodId, 0, method, pos, IoTRMIUtil.METHOD_ID_LEN);
107 pos = pos + IoTRMIUtil.METHOD_ID_LEN;
108 // Second iteration for copying bytes
109 for (int i = 0; i < numbParam; i++) {
111 String clsName = paramCls[i].getSimpleName();
112 int paramLen = rmiUtil.getTypeSize(clsName);
113 if (paramLen == -1) { // indefinite length
114 paramLen = objBytesArr[i].length;
115 byte[] paramLenBytes = IoTRMIUtil.intToByteArray(paramLen);
116 System.arraycopy(paramLenBytes, 0, method, pos, IoTRMIUtil.PARAM_LEN);
117 pos = pos + IoTRMIUtil.PARAM_LEN;
119 System.arraycopy(objBytesArr[i], 0, method, pos, paramLen);
120 pos = pos + paramLen;
128 * remoteCall() calls a method remotely by passing in parameters and getting a return Object
130 public synchronized Object[] getStructObjects(Class<?>[] retType, Class<?>[] retGenTypeVal) {
132 // Receive return value and return it to caller
133 Object[] retObj = null;
134 byte[] retObjBytes = null;
136 retObjBytes = rmiClient.receiveBytes(retObjBytes);
137 } catch (IOException ex) {
138 ex.printStackTrace();
139 throw new Error("IoTRMICall: Error when receiving bytes - rmiClient.receiveBytes()");
141 retObj = getReturnObjects(retObjBytes, retType, retGenTypeVal);
147 public Object[] getReturnObjects(byte[] retBytes, Class<?>[] arrCls, Class<?>[] arrGenValCls) {
149 // Byte scanning position
151 Object[] retObj = new Object[arrCls.length];
152 for (int i=0; i < arrCls.length; i++) {
154 String retType = arrCls[i].getSimpleName();
155 int retSize = rmiUtil.getTypeSize(retType);
156 // Get the 32-bit field in the byte array to get the actual
157 // length (this is a param with indefinite length)
159 byte[] bytRetLen = new byte[IoTRMIUtil.RETURN_LEN];
160 System.arraycopy(retBytes, pos, bytRetLen, 0, IoTRMIUtil.RETURN_LEN);
161 pos = pos + IoTRMIUtil.RETURN_LEN;
162 retSize = IoTRMIUtil.byteArrayToInt(bytRetLen);
164 byte[] retObjBytes = new byte[retSize];
165 System.arraycopy(retBytes, pos, retObjBytes, 0, retSize);
167 retObj[i] = IoTRMIUtil.getParamObject(arrCls[i], arrGenValCls[i], retObjBytes);