Making classes final to make inheritance impossible
[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 final class IoTRMICall {
27
28
29         /**
30          * Class Properties
31          */
32         private IoTRMIUtil rmiUtil;
33         private IoTSocketClient rmiClient;
34
35
36         /**
37          * Constructors
38          */
39         public IoTRMICall(int _port, String _address, int _rev) throws IOException {
40
41                 rmiUtil = new IoTRMIUtil();
42                 rmiClient = new IoTSocketClient(_port, _address, _rev);
43         }
44
45
46         /**
47          * remoteCall() calls a method remotely by passing in parameters and getting a return Object
48          */
49         public synchronized Object remoteCall(int objectId, int methodId, Class<?> retType, 
50                         Class<?> retGenTypeVal, Class<?>[] paramCls, Object[] paramObj) {
51
52                 // Send method info
53                 byte[] methodBytes = methodToBytes(objectId, methodId, paramCls, paramObj);
54                 try {
55                         rmiClient.sendBytes(methodBytes);
56                 } catch (IOException ex) {
57                         ex.printStackTrace();
58                         throw new Error("IoTRMICall: Error when sending bytes - rmiClient.sendBytes()");
59                 }
60                 // Receive return value and return it to caller
61                 Object retObj = null;
62                 if (retType != void.class) {
63                         byte[] retObjBytes = null;
64                         try {
65                                 retObjBytes = rmiClient.receiveBytes(retObjBytes);
66                         } catch (IOException ex) {
67                                 ex.printStackTrace();
68                                 throw new Error("IoTRMICall: Error when receiving bytes - rmiClient.receiveBytes()");
69                         }
70                         retObj = IoTRMIUtil.getParamObject(retType, retGenTypeVal, retObjBytes);
71                 }
72                 return retObj;
73         }
74
75
76         /**
77          * methodToBytes() returns byte representation of a method
78          */
79         public byte[] methodToBytes(int objectId, int methId, Class<?>[] paramCls, Object[] paramObj) {
80
81                 // Initialized to the length of method ID
82                 int methodLen = IoTRMIUtil.OBJECT_ID_LEN;
83                 byte[] objId = IoTRMIUtil.intToByteArray(objectId);
84                 // Get method ID in bytes
85                 byte[] methodId = IoTRMIUtil.intToByteArray(methId);
86                 // Get byte arrays and calculate method bytes length
87                 int numbParam = paramObj.length;
88                 methodLen = methodLen + IoTRMIUtil.METHOD_ID_LEN;
89                 byte[][] objBytesArr = new byte[numbParam][];
90                 for (int i = 0; i < numbParam; i++) {
91                         // Get byte arrays for the objects
92                         objBytesArr[i] = IoTRMIUtil.getObjectBytes(paramObj[i]);
93                         String clsName = paramCls[i].getSimpleName();
94                         int paramLen = rmiUtil.getTypeSize(clsName);
95                         if (paramLen == -1) {           // indefinite length - store the length first
96                                 methodLen = methodLen + IoTRMIUtil.PARAM_LEN;
97                         }
98                         methodLen = methodLen + objBytesArr[i].length;
99                 }
100                 // Construct method in byte array
101                 byte[] method = new byte[methodLen];
102                 int pos = 0;
103                 System.arraycopy(objId, 0, method, 0, IoTRMIUtil.METHOD_ID_LEN);
104                 pos = pos + IoTRMIUtil.OBJECT_ID_LEN;
105                 System.arraycopy(methodId, 0, method, pos, IoTRMIUtil.METHOD_ID_LEN);
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         /**
127          * remoteCall() calls a method remotely by passing in parameters and getting a return Object
128          */
129         public synchronized Object[] getStructObjects(Class<?>[] retType, Class<?>[] retGenTypeVal) {
130
131                 // Receive return value and return it to caller
132                 Object[] retObj = null;
133                 byte[] retObjBytes = null;
134                 try {
135                         retObjBytes = rmiClient.receiveBytes(retObjBytes);
136                 } catch (IOException ex) {
137                         ex.printStackTrace();
138                         throw new Error("IoTRMICall: Error when receiving bytes - rmiClient.receiveBytes()");
139                 }
140                 retObj = getReturnObjects(retObjBytes, retType, retGenTypeVal);
141
142                 return retObj;
143         }
144
145
146         public Object[] getReturnObjects(byte[] retBytes, Class<?>[] arrCls, Class<?>[] arrGenValCls) {
147
148                 // Byte scanning position
149                 int pos = 0;
150                 Object[] retObj = new Object[arrCls.length];
151                 for (int i=0; i < arrCls.length; i++) {
152
153                         String retType = arrCls[i].getSimpleName();
154                         int retSize = rmiUtil.getTypeSize(retType);
155                         // Get the 32-bit field in the byte array to get the actual
156                         //              length (this is a param with indefinite length)
157                         if (retSize == -1) {
158                                 byte[] bytRetLen = new byte[IoTRMIUtil.RETURN_LEN];
159                                 System.arraycopy(retBytes, pos, bytRetLen, 0, IoTRMIUtil.RETURN_LEN);
160                                 pos = pos + IoTRMIUtil.RETURN_LEN;
161                                 retSize = IoTRMIUtil.byteArrayToInt(bytRetLen);
162                         }
163                         byte[] retObjBytes = new byte[retSize];
164                         System.arraycopy(retBytes, pos, retObjBytes, 0, retSize);
165                         pos = pos + retSize;
166                         retObj[i] = IoTRMIUtil.getParamObject(arrCls[i], arrGenValCls[i], retObjBytes);
167                 }
168
169                 return retObj;
170         }
171 }