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