Adding return value support for enumeration (both Java and C++
[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, 
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, 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                 // List
146                 /*List<Long> list = new ArrayList<Long>();
147                 list.add(12345678l);
148                 list.add(23455432l);
149                 list.add(34566543l);
150
151                 byte[] objBytes = IoTRMIUtil.getObjectBytes(list);
152                 System.out.println(Arrays.toString(objBytes));
153                 Object obj = IoTRMIUtil.getParamObject(List.class, null, Long.class, objBytes);
154
155                 @SuppressWarnings("unchecked")
156                 List<Long> listStr = (List<Long>) obj;
157                 System.out.println("List: " + listStr.toString());*/
158
159                 //@SuppressWarnings("unchecked")
160                 //List<Long> listStr = (List<Long>) obj;
161                 //System.out.println("List: " + listStr.toString());
162
163         }
164 }