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