a08ce6282871d19f54a61fa19aa80db801b3e3b4
[iot2.git] / iotjava / iotrmi / Java / sample / TestClass_Stub.java
1 package iotrmi.Java.sample;
2
3 import java.io.IOException;
4 import iotrmi.Java.IoTRMICall;
5 import iotruntime.master.CommunicationHandler;
6
7 import java.util.Arrays;
8 import java.util.List;
9 import java.util.ArrayList;
10
11 import iotrmi.Java.IoTRMIObject;
12
13 public class TestClass_Stub implements TestClassInterface {
14
15         /**
16          * Class Properties
17          */
18         private IoTRMICall rmiCall;
19         private IoTRMIObject rmiObj;
20         private String address;
21         private int[] ports;
22
23         /**
24          * Class Constants
25          */
26         private int objectId = 0;       // Default value is 0
27         // This is permission system for callback objects
28         private static Integer[] object0Permission = { 0, 1 };  // object0 is the callback object
29         private List<Integer> set0Allowed;
30
31         /**
32          * Properties and constants for Callbacks!
33          */
34         private List<CallBackInterface> listCBObj;
35         private final static int NUM_CB_OBJ = 1;
36         private static int objIdCnt = 0; // Counter for callback object Ids
37
38
39         /**
40          * Constructors
41          */
42         public TestClass_Stub(int _port, String _address, int _rev, int[] _ports) throws Exception {
43
44                 address = _address;
45                 ports = _ports;
46                 rmiCall = new IoTRMICall(_port, _address, _rev);
47
48                 set0Allowed = Arrays.asList(object0Permission);
49
50                 // Only for callbacks!!!
51                 listCBObj = new ArrayList<CallBackInterface>();
52                 ___initCallBack();
53         }
54
55
56         /**
57          * Instantiation of callback objects
58          */
59         public static int numCallbackObjects() {
60
61                 return NUM_CB_OBJ;      // Generated by the IoTCompiler
62         }
63
64
65         // Initialize callback
66         public void ___initCallBack() {
67
68                 Thread thread = new Thread() {
69                         public void run() {
70                         try{
71                                         rmiObj = new IoTRMIObject(ports[0]);
72                                         while (true) {
73                                                 byte[] method = rmiObj.getMethodBytes();
74                                                 // Permission checking
75                                                 int methId = IoTRMIObject.getMethodId(method);
76                                                 if (!set0Allowed.contains(methId))
77                                                         throw new Error("CallBack_CBSkeleton: This object is not allowed to access method " + methId);
78
79                                                 int objId = IoTRMIObject.getObjectId(method);
80                                                 CallBack_CBSkeleton skel = (CallBack_CBSkeleton) listCBObj.get(objId);
81                                                 if (skel != null) {
82                                                         //rmiObj.setMethodBytes(method);
83                                                         skel.invokeMethod(rmiObj);
84                                                 }
85                                         }
86                                 } catch (Exception ex){
87                                         ex.printStackTrace();
88                                         throw new Error("Error instantiating class CallBack_Skeleton!");
89                         }
90                     }
91                 };
92                 thread.start();
93
94                 //String sign = "registercallback";     // can be any string
95                 int methodId = 9;
96                 Class<?> retType = void.class;
97                 // port, address, rev
98                 Class<?>[] paramCls = new Class<?>[] { int.class, String.class, int.class };
99                 Object[] paramObj = new Object[] { ports[0], address, 0 };
100                 rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
101         }
102
103
104         // Single callback handling
105         public void registerCallback(CallBackInterface _cb) {
106
107                 /*Thread thread = new Thread() {
108                         public void run() {
109                     try{
110                                         CallBack_Skeleton cbskel = new CallBack_Skeleton(_cb, ports[0]);
111                                 } catch (Exception ex){
112                                         ex.printStackTrace();
113                                         throw new Error("Error instantiating class CallBack_Skeleton!");
114                     }
115                 }
116             };
117                 thread.start();
118
119                 String sign = "voidregisterCallBack(CallBackInterface)";
120                 Class<?> retType = void.class;
121                 // port, address, and rev
122                 Class<?>[] paramCls = new Class<?>[] { int.class, String.class, int.class };
123                 Object[] paramObj = new Object[] { ports[0], address, 0 };
124                 rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);*/
125         }
126
127
128         public void registerCallback(CallBackInterface[] _cb) {
129
130                 try {
131                         //for (int objId = 0; objId < _cb.length; objId++) {
132                         for (CallBackInterface cb : _cb) {
133                                 CallBack_CBSkeleton skel = new CallBack_CBSkeleton(cb, objIdCnt++);
134                                 listCBObj.add(skel);
135                         }
136                 } catch (Exception ex){
137                         ex.printStackTrace();
138                         throw new Error("Class not found / instantiation / illegal access / IO error!");
139                 }
140
141                 //String sign = "voidregisterCallBack(CallBackInterface[])";
142                 int methodId = 8;
143                 Class<?> retType = void.class;
144                 // port, address, rev, and number of objects
145                 Class<?>[] paramCls = new Class<?>[] { int.class };
146                 Object[] paramObj = new Object[] { _cb.length };
147                 //rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
148                 rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
149         }
150
151
152         public void setA(int _int) {
153
154                 //String sign = "voidsetA(int)";
155                 int methodId = 0;
156                 Class<?> retType = void.class;
157                 Class<?>[] paramCls = new Class<?>[] { int.class };
158                 Object[] paramObj = new Object[] { _int };
159                 rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
160         }
161
162
163         public void setB(float _float) {
164
165                 int methodId = 1;
166                 Class<?> retType = void.class;
167                 Class<?>[] paramCls = new Class<?>[] { float.class };
168                 Object[] paramObj = new Object[] { _float };
169                 rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
170         }
171
172
173         public void setC(String _string) {
174
175                 int methodId = 2;
176                 Class<?> retType = void.class;
177                 Class<?>[] paramCls = new Class<?>[] { String.class };
178                 Object[] paramObj = new Object[] { _string };
179                 rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
180         }
181
182
183         // Getters
184         public String sumArray(String[] newA) {
185
186                 int methodId = 3;
187                 Class<?> retType = String.class;
188                 Class<?>[] paramCls = new Class<?>[] { String[].class };
189                 Object[] paramObj = new Object[] { newA };
190                 Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
191                 return (String)retObj;
192         }
193
194
195         public int setAndGetA(int newA) {
196                 int methodId = 4;
197                 Class<?> retType = int.class;
198                 Class<?>[] paramCls = new Class<?>[] { int.class };
199                 Object[] paramObj = new Object[] { newA };
200                 Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
201                 return (int)retObj;
202         }
203
204
205         public int setACAndGetA(String newC, int newA) {
206
207                 int methodId = 5;
208                 Class<?> retType = int.class;
209                 Class<?>[] paramCls = new Class<?>[] { String.class, int.class };
210                 Object[] paramObj = new Object[] { newC, newA };
211                 Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
212                 return (int)retObj;
213         }
214
215
216         public int callBack() {
217
218                 int methodId = 6;
219                 Class<?> retType = int.class;
220                 Class<?>[] paramCls = new Class<?>[] { };
221                 Object[] paramObj = new Object[] { };
222                 Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
223                 return (int)retObj;
224
225         }
226
227
228         public StructJ[] handleStruct(StructJ[] data) {
229
230                 int methodId = 11;
231                 Class<?> retType = void.class;
232                 Class<?>[] paramCls = new Class<?>[] { int.class };
233                 Object[] paramObj = new Object[] { data.length };
234                 rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
235
236                 int methodId2 = 10;
237                 Class<?> retType2 = int.class;  // return type is integer if it is a struct!!!
238                 // Calculate the size of the array
239                 Class<?>[] paramCls2 = new Class<?>[3*data.length];
240                 Object[] paramObj2 = new Object[3*data.length];
241                 // Handle with for loop
242                 int pos = 0;
243                 for(int i = 0; i < data.length; i++) {
244                         paramCls2[pos] = String.class;
245                         paramObj2[pos++] = data[i].name;
246                         paramCls2[pos] = float.class;
247                         paramObj2[pos++] = data[i].value;
248                         paramCls2[pos] = int.class;
249                         paramObj2[pos++] = data[i].year;
250                 }
251                 //System.out.println(Arrays.toString(paramObj2));
252                 Object retObj = rmiCall.remoteCall(objectId, methodId2, retType2, null, paramCls2, paramObj2);
253                 // RETURN STRUCT
254                 // Get the length of the struct first
255                 int structsize1 = (int) retObj;
256                 // Construct the struct
257                 Class<?>[] retCls = new Class<?>[3*structsize1];
258                 Class<?>[] retClsVal = new Class<?>[3*structsize1];
259                 pos = 0;
260                 for(int i=0; i < structsize1; i++) {
261                         retCls[pos] = String.class;
262                         retClsVal[pos++] = null;
263                         retCls[pos] = float.class;
264                         retClsVal[pos++] = null;
265                         retCls[pos] = int.class;
266                         retClsVal[pos++] = null;
267                 }
268                 Object[] retObj2 = rmiCall.getStructObjects(retCls, 
269                         retClsVal);
270                 StructJ[] dataRet = new StructJ[structsize1];
271                 for (int i=0; i < structsize1; i++) {
272                         dataRet[i] = new StructJ();
273                 }
274                 pos = 0;
275                 for(int i=0; i < structsize1; i++) {
276                         dataRet[i].name = (String) retObj2[pos++];
277                         dataRet[i].value = (float) retObj2[pos++];
278                         dataRet[i].year = (int) retObj2[pos++];
279                 }
280
281                 return dataRet;
282         }
283
284
285         public EnumJ[] handleEnum(EnumJ[] en) {
286
287                 int methodId = 12;
288                 Class<?> retType = int[].class;
289                 // Handle with for loop
290                 int paramInt[] = new int[en.length];
291                 for(int i = 0; i < en.length; i++) {
292                         paramInt[i] = en[i].ordinal();
293                 }
294
295                 Class<?>[] paramCls = new Class<?>[] { int[].class };
296                 Object[] paramObj = new Object[] { paramInt };
297
298                 // if no return value just
299                 // rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
300                 // This is with return value:
301                 Object retObj = 
302                         rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
303                 int[] retEnumInt = (int[]) retObj;
304                 int enumsize1 = retEnumInt.length;
305                 // Encoder/decoder
306                 EnumJ[] enumJ = EnumJ.values();
307                 EnumJ[] enRetVal = new EnumJ[enumsize1];
308                 for(int i = 0; i < enumsize1; i++) {
309                         enRetVal[i] = enumJ[retEnumInt[i]];
310                 }
311                 return enRetVal;
312         }
313
314
315         public static void main(String[] args) throws Exception {
316
317                 CommunicationHandler comHan = new CommunicationHandler(true);
318                 int numOfPorts = TestClass_Stub.numCallbackObjects();
319                 int[] ports = comHan.getCallbackPorts(numOfPorts);
320
321                 int port = 5010;
322                 String address = "localhost";
323                 int rev = 0;
324
325                 System.out.println("Allocated ports: " + Arrays.toString(ports));
326
327                 TestClass_Stub tcstub = new TestClass_Stub(port, address, rev, ports);
328                 System.out.println("Return value: " + tcstub.setAndGetA(123));
329                 System.out.println("Return value: " + tcstub.setACAndGetA("string", 123));
330                 System.out.println("Return value: " + tcstub.sumArray(new String[] { "123", "456", "987" }));
331
332                 /*CallBackInterface cb1 = new CallBack(23);
333                 CallBackInterface cb2 = new CallBack(33);
334                 CallBackInterface cb3 = new CallBack(43);
335                 CallBackInterface[] cb = { cb1, cb2, cb3 };
336                 tcstub.registerCallback(cb);
337                 System.out.println("Return value from callback: " + tcstub.callBack());
338                 CallBackInterface cb4 = new CallBack(10);
339                 CallBackInterface cb5 = new CallBack(11);
340                 CallBackInterface cb6 = new CallBack(12);
341                 CallBackInterface[] cbt = { cb4, cb5, cb6 };
342                 tcstub.registerCallback(cbt);
343                 System.out.println("Return value from callback: " + tcstub.callBack());*/
344
345                 StructJ[] data = new StructJ[2];
346                 for (int i=0; i<2; i++) {
347                         data[i] = new StructJ();
348                 }
349                 data[0].name = "Rahmadi";
350                 data[0].value = 0.123f;
351                 data[0].year = 2016;
352                 //data[1].name = "Trimananda";
353                 //data[1].value = 0.223f;
354                 //data[1].year = 2017;
355
356                 for (StructJ str : data) {
357                         System.out.println("Name: " + str.name);
358                         System.out.println("Value: " + str.value);
359                         System.out.println("Year: " + str.year);
360                 }
361                 StructJ[] strj = tcstub.handleStruct(data);
362                 for (StructJ str : strj) {
363                         System.out.println("Name: " + str.name);
364                         System.out.println("Value: " + str.value);
365                         System.out.println("Year: " + str.year);
366                 }
367
368
369                 EnumJ[] en = { EnumJ.APPLE, EnumJ.ORANGE, EnumJ.APPLE, EnumJ.GRAPE };
370                 EnumJ[] res = tcstub.handleEnum(en);
371                 System.out.println("Enum members: " + Arrays.toString(res));
372         }
373 }
374
375