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