Using methodId directly without method signature; placing sendReturnObj in individual...
[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) throws IOException {
41
42                 rmiUtil = new IoTRMIUtil();
43                 rmiClient = new IoTSocketClient(_port, _address, _rev);
44         }
45
46
47         /**
48          * remoteCall() calls a method remotely by passing in parameters and getting a return Object
49          */
50         public synchronized Object remoteCall(int objectId, int methodId, Class<?> retType, Class<?> retGenTypeKey, 
51                         Class<?> retGenTypeVal, Class<?>[] paramCls, Object[] paramObj) {
52
53                 // Send method info
54                 byte[] methodBytes = methodToBytes(objectId, methodId, paramCls, paramObj);
55                 try {
56                         rmiClient.sendBytes(methodBytes);
57                 } catch (IOException ex) {
58                         ex.printStackTrace();
59                         throw new Error("IoTRMICall: Error when sending bytes - rmiClient.sendBytes()");
60                 }
61                 // Receive return value and return it to caller
62                 Object retObj = null;
63                 if (retType != void.class) {
64                         byte[] retObjBytes = null;
65                         try {
66                                 retObjBytes = rmiClient.receiveBytes(retObjBytes);
67                         } catch (IOException ex) {
68                                 ex.printStackTrace();
69                                 throw new Error("IoTRMICall: Error when receiving bytes - rmiClient.receiveBytes()");
70                         }
71                         System.out.println("Return object bytes: " + Arrays.toString(retObjBytes));
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(int objectId, int methId, Class<?>[] paramCls, Object[] paramObj) {
82
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;
99                         }
100                         methodLen = methodLen + objBytesArr[i].length;
101                 }
102                 // Construct method in byte array
103                 byte[] method = new byte[methodLen];
104                 int pos = 0;
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++) {
111
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;
119                         }               
120                         System.arraycopy(objBytesArr[i], 0, method, pos, paramLen);
121                         pos = pos + paramLen;
122                 }
123
124                 return method;
125         }
126
127
128         public static void main(String[] args) throws Exception {
129
130                 String[] test = { "123", "456", "789" };
131                 byte[] b = IoTRMIUtil.getObjectBytes(test);
132
133                 Boolean[] test2 = new Boolean[] { true, false, false };
134                 byte[] b2 = IoTRMIUtil.getObjectBytes(test2);
135
136                 System.out.println(Arrays.toString(b));
137                 System.out.println(Arrays.toString(b2));
138
139                 String[] c = (String[]) IoTRMIUtil.getParamObjectArray(String[].class, b);
140                 System.out.println(Arrays.toString(c));
141
142                 Boolean[] c2 = (Boolean[]) IoTRMIUtil.getParamObjectArray(Boolean[].class, b2);
143                 System.out.println(Arrays.toString(c2));
144
145                 // Set
146                 /*Set<String> set = new HashSet<String>();
147                 set.add("1234");
148                 set.add("5678");
149
150                 byte[] objBytes = IoTRMIUtil.getObjectBytes(set);
151                 System.out.println(Arrays.toString(objBytes));
152                 Object obj = IoTRMIUtil.getParamObject(Set.class, null, String.class, objBytes);
153
154                 @SuppressWarnings("unchecked")
155                 Set<String> setStr = (Set<String>) obj;
156                 System.out.println("Set: " + setStr.toString());*/
157
158                 // List
159                 /*List<Long> list = new ArrayList<Long>();
160                 list.add(12345678l);
161                 list.add(23455432l);
162                 list.add(34566543l);
163
164                 byte[] objBytes = IoTRMIUtil.getObjectBytes(list);
165                 System.out.println(Arrays.toString(objBytes));
166                 Object obj = IoTRMIUtil.getParamObject(List.class, null, Long.class, objBytes);
167
168                 @SuppressWarnings("unchecked")
169                 List<Long> listStr = (List<Long>) obj;
170                 System.out.println("List: " + listStr.toString());*/
171
172                 // Map
173                 Map<Long,Integer> map = new HashMap<Long,Integer>();
174                 map.put(12345678l, 1234);
175                 map.put(23455432l, 5678);
176                 map.put(34566543l, 4321);
177
178                 byte[] objBytes = IoTRMIUtil.getObjectBytes(map);
179                 System.out.println(Arrays.toString(objBytes));
180                 Object obj = IoTRMIUtil.getParamObject(Map.class, Long.class, Integer.class, objBytes);
181
182                 map = (Map<Long,Integer>) obj;
183                 System.out.println("Received map: " + map.toString());
184
185                 //@SuppressWarnings("unchecked")
186                 //List<Long> listStr = (List<Long>) obj;
187                 //System.out.println("List: " + listStr.toString());
188
189         }
190 }