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 System.out.println("Return object bytes: " + Arrays.toString(retObjBytes));
72 retObj = IoTRMIUtil.getParamObject(retType, retGenTypeVal, retObjBytes);
79 * methodToBytes() returns byte representation of a method
81 public byte[] methodToBytes(int objectId, int methId, Class<?>[] paramCls, Object[] paramObj) {
83 // Initialized to the length of method ID
84 int methodLen = IoTRMIUtil.OBJECT_ID_LEN;
85 byte[] objId = IoTRMIUtil.intToByteArray(objectId);
86 // Get method ID in bytes
87 byte[] methodId = IoTRMIUtil.intToByteArray(methId);
88 // Get byte arrays and calculate method bytes length
89 int numbParam = paramObj.length;
90 methodLen = methodLen + IoTRMIUtil.METHOD_ID_LEN;
91 byte[][] objBytesArr = new byte[numbParam][];
92 for (int i = 0; i < numbParam; i++) {
93 // Get byte arrays for the objects
94 objBytesArr[i] = IoTRMIUtil.getObjectBytes(paramObj[i]);
95 String clsName = paramCls[i].getSimpleName();
96 int paramLen = rmiUtil.getTypeSize(clsName);
97 if (paramLen == -1) { // indefinite length - store the length first
98 methodLen = methodLen + IoTRMIUtil.PARAM_LEN;
100 methodLen = methodLen + objBytesArr[i].length;
102 // Construct method in byte array
103 byte[] method = new byte[methodLen];
105 System.arraycopy(objId, 0, method, 0, IoTRMIUtil.METHOD_ID_LEN);
106 pos = pos + IoTRMIUtil.OBJECT_ID_LEN;
107 System.arraycopy(methodId, 0, method, pos, IoTRMIUtil.METHOD_ID_LEN);
108 pos = pos + IoTRMIUtil.METHOD_ID_LEN;
109 // Second iteration for copying bytes
110 for (int i = 0; i < numbParam; i++) {
112 String clsName = paramCls[i].getSimpleName();
113 int paramLen = rmiUtil.getTypeSize(clsName);
114 if (paramLen == -1) { // indefinite length
115 paramLen = objBytesArr[i].length;
116 byte[] paramLenBytes = IoTRMIUtil.intToByteArray(paramLen);
117 System.arraycopy(paramLenBytes, 0, method, pos, IoTRMIUtil.PARAM_LEN);
118 pos = pos + IoTRMIUtil.PARAM_LEN;
120 System.arraycopy(objBytesArr[i], 0, method, pos, paramLen);
121 pos = pos + paramLen;
128 public static void main(String[] args) throws Exception {
130 String[] test = { "123", "456", "789" };
131 byte[] b = IoTRMIUtil.getObjectBytes(test);
133 Boolean[] test2 = new Boolean[] { true, false, false };
134 byte[] b2 = IoTRMIUtil.getObjectBytes(test2);
136 System.out.println(Arrays.toString(b));
137 System.out.println(Arrays.toString(b2));
139 String[] c = (String[]) IoTRMIUtil.getParamObjectArray(String[].class, b);
140 System.out.println(Arrays.toString(c));
142 Boolean[] c2 = (Boolean[]) IoTRMIUtil.getParamObjectArray(Boolean[].class, b2);
143 System.out.println(Arrays.toString(c2));
146 /*List<Long> list = new ArrayList<Long>();
151 byte[] objBytes = IoTRMIUtil.getObjectBytes(list);
152 System.out.println(Arrays.toString(objBytes));
153 Object obj = IoTRMIUtil.getParamObject(List.class, null, Long.class, objBytes);
155 @SuppressWarnings("unchecked")
156 List<Long> listStr = (List<Long>) obj;
157 System.out.println("List: " + listStr.toString());*/
159 //@SuppressWarnings("unchecked")
160 //List<Long> listStr = (List<Long>) obj;
161 //System.out.println("List: " + listStr.toString());