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