Using sorted integer as method Id instead of hash values
[iot2.git] / iotjava / iotrmi / Java / IoTRMICall.java
1 package iotrmi.Java;
2
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;
8 import java.util.List;
9 import java.util.Map;
10 import java.lang.reflect.Method;
11
12 import java.util.HashSet;
13 import java.util.Set;
14
15
16 /** Class IoTRMICall is a class that serves method calls on stub.
17  *  <p>
18  *  A stub will use an object of this class to send the method
19  *  information, e.g. object identifier, method identifier, and
20  *  parameters.
21  *
22  * @author      Rahmadi Trimananda <rtrimana @ uci.edu>
23  * @version     1.0
24  * @since       2016-10-04
25  */
26 public class IoTRMICall {
27
28
29         /**
30          * Class Properties
31          */
32         private IoTRMIUtil rmiUtil;
33         private IoTSocketClient rmiClient;
34         private List<String> listMethodId;      // Map from method ID to signature
35
36
37         /**
38          * Constructors
39          */
40         public IoTRMICall(int _port, String _address, int _rev, String[] _methodSign) throws IOException {
41
42                 rmiUtil = new IoTRMIUtil();
43                 rmiClient = new IoTSocketClient(_port, _address, _rev);
44                 listMethodId = Arrays.asList(_methodSign); // Initialize the method ID map
45         }
46
47
48         /**
49          * remoteCall() calls a method remotely by passing in parameters and getting a return Object
50          */
51         public Object remoteCall(String methodSign, Class<?> retType, Class<?> retGenTypeKey, 
52                         Class<?> retGenTypeVal, Class<?>[] paramCls, Object[] paramObj) {
53
54                 // Send method info
55                 byte[] methodBytes = methodToBytes(methodSign, paramCls, paramObj);
56                 try {
57                         rmiClient.sendBytes(methodBytes);
58                 } catch (IOException ex) {
59                         ex.printStackTrace();
60                         throw new Error("IoTRMICall: Error when sending bytes - rmiClient.sendBytes()");
61                 }
62                 // Receive return value and return it to caller
63                 Object retObj = null;
64                 if (retType != void.class) {
65                         byte[] retObjBytes = null;
66                         try {
67                                 retObjBytes = rmiClient.receiveBytes(retObjBytes);
68                         } catch (IOException ex) {
69                                 ex.printStackTrace();
70                                 throw new Error("IoTRMICall: Error when receiving bytes - rmiClient.receiveBytes()");
71                         }
72                         retObj = IoTRMIUtil.getParamObject(retType, retGenTypeKey, retGenTypeVal, retObjBytes);
73                 }
74                 return retObj;
75         }
76
77
78         /**
79          * methodToBytes() returns byte representation of a method
80          */
81         public byte[] methodToBytes(String methodSign, Class<?>[] paramCls, Object[] paramObj) {
82
83                 // Get method ID in bytes
84                 int methId = listMethodId.indexOf(methodSign);
85                 byte[] methodId = IoTRMIUtil.intToByteArray(methId);
86
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;
98                         }
99                         methodLen = methodLen + objBytesArr[i].length;
100                 }
101
102                 // Construct method in byte array
103                 byte[] method = new byte[methodLen];
104                 int pos = 0;
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++) {
109
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;
117                         }               
118                         System.arraycopy(objBytesArr[i], 0, method, pos, paramLen);
119                         pos = pos + paramLen;
120                 }
121
122                 return method;
123         }
124
125
126         public static void main(String[] args) throws Exception {
127
128                 String[] test = { "123", "456", "789" };
129                 byte[] b = IoTRMIUtil.getObjectBytes(test);
130
131                 Boolean[] test2 = new Boolean[] { true, false, false };
132                 byte[] b2 = IoTRMIUtil.getObjectBytes(test2);
133
134                 System.out.println(Arrays.toString(b));
135                 System.out.println(Arrays.toString(b2));
136
137                 String[] c = (String[]) IoTRMIUtil.getParamObjectArray(String[].class, b);
138                 System.out.println(Arrays.toString(c));
139
140                 Boolean[] c2 = (Boolean[]) IoTRMIUtil.getParamObjectArray(Boolean[].class, b2);
141                 System.out.println(Arrays.toString(c2));
142
143                 // Set
144                 /*Set<String> set = new HashSet<String>();
145                 set.add("1234");
146                 set.add("5678");
147
148                 byte[] objBytes = IoTRMIUtil.getObjectBytes(set);
149                 System.out.println(Arrays.toString(objBytes));
150                 Object obj = IoTRMIUtil.getParamObject(Set.class, null, String.class, objBytes);
151
152                 @SuppressWarnings("unchecked")
153                 Set<String> setStr = (Set<String>) obj;
154                 System.out.println("Set: " + setStr.toString());*/
155
156                 // List
157                 /*List<Long> list = new ArrayList<Long>();
158                 list.add(12345678l);
159                 list.add(23455432l);
160                 list.add(34566543l);
161
162                 byte[] objBytes = IoTRMIUtil.getObjectBytes(list);
163                 System.out.println(Arrays.toString(objBytes));
164                 Object obj = IoTRMIUtil.getParamObject(List.class, null, Long.class, objBytes);
165
166                 @SuppressWarnings("unchecked")
167                 List<Long> listStr = (List<Long>) obj;
168                 System.out.println("List: " + listStr.toString());*/
169
170                 // Map
171                 Map<Long,Integer> map = new HashMap<Long,Integer>();
172                 map.put(12345678l, 1234);
173                 map.put(23455432l, 5678);
174                 map.put(34566543l, 4321);
175
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);
179
180                 map = (Map<Long,Integer>) obj;
181                 System.out.println("Received map: " + map.toString());
182
183                 //@SuppressWarnings("unchecked")
184                 //List<Long> listStr = (List<Long>) obj;
185                 //System.out.println("List: " + listStr.toString());
186
187         }
188 }