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, String[] _methodSign) throws IOException {
42 rmiUtil = new IoTRMIUtil();
43 rmiClient = new IoTSocketClient(_port, _address, _rev);
44 listMethodId = Arrays.asList(_methodSign); // Initialize the method ID map
49 * remoteCall() calls a method remotely by passing in parameters and getting a return Object
51 public Object remoteCall(String methodSign, Class<?> retType, Class<?> retGenTypeKey,
52 Class<?> retGenTypeVal, Class<?>[] paramCls, Object[] paramObj) {
55 byte[] methodBytes = methodToBytes(methodSign, paramCls, paramObj);
57 rmiClient.sendBytes(methodBytes);
58 } catch (IOException ex) {
60 throw new Error("IoTRMICall: Error when sending bytes - rmiClient.sendBytes()");
62 // Receive return value and return it to caller
64 if (retType != void.class) {
65 byte[] retObjBytes = null;
67 retObjBytes = rmiClient.receiveBytes(retObjBytes);
68 } catch (IOException ex) {
70 throw new Error("IoTRMICall: Error when receiving bytes - rmiClient.receiveBytes()");
72 retObj = IoTRMIUtil.getParamObject(retType, retGenTypeKey, retGenTypeVal, retObjBytes);
79 * methodToBytes() returns byte representation of a method
81 public byte[] methodToBytes(String methodSign, Class<?>[] paramCls, Object[] paramObj) {
83 // Get method ID in bytes
84 int methId = listMethodId.indexOf(methodSign);
85 byte[] methodId = IoTRMIUtil.intToByteArray(methId);
87 // Get byte arrays and calculate method bytes length
88 int numbParam = paramObj.length;
89 int methodLen = IoTRMIUtil.METHOD_ID_LEN; // Initialized to the length of method ID
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;
102 // Construct method in byte array
103 byte[] method = new byte[methodLen];
105 System.arraycopy(methodId, 0, method, 0, methodId.length);
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;
126 public static void main(String[] args) throws Exception {
128 String[] test = { "123", "456", "789" };
129 byte[] b = IoTRMIUtil.getObjectBytes(test);
131 Boolean[] test2 = new Boolean[] { true, false, false };
132 byte[] b2 = IoTRMIUtil.getObjectBytes(test2);
134 System.out.println(Arrays.toString(b));
135 System.out.println(Arrays.toString(b2));
137 String[] c = (String[]) IoTRMIUtil.getParamObjectArray(String[].class, b);
138 System.out.println(Arrays.toString(c));
140 Boolean[] c2 = (Boolean[]) IoTRMIUtil.getParamObjectArray(Boolean[].class, b2);
141 System.out.println(Arrays.toString(c2));
144 /*Set<String> set = new HashSet<String>();
148 byte[] objBytes = IoTRMIUtil.getObjectBytes(set);
149 System.out.println(Arrays.toString(objBytes));
150 Object obj = IoTRMIUtil.getParamObject(Set.class, null, String.class, objBytes);
152 @SuppressWarnings("unchecked")
153 Set<String> setStr = (Set<String>) obj;
154 System.out.println("Set: " + setStr.toString());*/
157 /*List<Long> list = new ArrayList<Long>();
162 byte[] objBytes = IoTRMIUtil.getObjectBytes(list);
163 System.out.println(Arrays.toString(objBytes));
164 Object obj = IoTRMIUtil.getParamObject(List.class, null, Long.class, objBytes);
166 @SuppressWarnings("unchecked")
167 List<Long> listStr = (List<Long>) obj;
168 System.out.println("List: " + listStr.toString());*/
171 Map<Long,Integer> map = new HashMap<Long,Integer>();
172 map.put(12345678l, 1234);
173 map.put(23455432l, 5678);
174 map.put(34566543l, 4321);
176 byte[] objBytes = IoTRMIUtil.getObjectBytes(map);
177 System.out.println(Arrays.toString(objBytes));
178 Object obj = IoTRMIUtil.getParamObject(Map.class, Long.class, Integer.class, objBytes);
180 map = (Map<Long,Integer>) obj;
181 System.out.println("Received map: " + map.toString());
183 //@SuppressWarnings("unchecked")
184 //List<Long> listStr = (List<Long>) obj;
185 //System.out.println("List: " + listStr.toString());