Adding object ID and tons of minor adjustments for callback support
[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)",
33                 "voidsetB(float)",
34                 "voidsetC(string)",
35                 "sumArray(string[])",
36                 "intsetAndGetA(int)",
37                 "intsetACAndGetA(string,int)",
38                 "intcallBack()",
39                 "voidregisterCallBack(CallBackInterface)",
40                 "voidregisterCallBack(CallBackInterface[])",
41                 "registercallback"
42         };
43
44         /**
45          * Constructors
46          */
47         public TestClass_Stub(int _port, String _address, int _rev, int[] _ports) throws Exception {
48
49                 address = _address;
50                 ports = _ports;
51                 rmiCall = new IoTRMICall(_port, _address, _rev, methodSignatures);
52                 listCBObj = new ArrayList<CallBackInterface>();
53                 init_CallBack();
54         }
55
56
57         /**
58          * Instantiation of callback objects
59          */
60         public static int numCallbackObjects() {
61
62                 return NUM_CB_OBJ;      // Generated by the IoTCompiler
63         }
64
65
66         // Return method signatures
67         public static String[] getMethodSignatures() {
68
69                 return methodSignatures;
70         }
71
72
73         // Initialize callback
74         public void init_CallBack() {
75
76                 Thread thread = new Thread() {
77                         public void run() {
78                         try{
79                                         String[] methodSignatures = CallBack_CBSkeleton.getMethodSignatures();
80                                         rmiObj = new IoTRMIObject(ports[0], methodSignatures);
81                                         Object retObj = null;
82                                         while (true) {
83                                                 byte[] method = rmiObj.getMethodBytes();
84                                                 int objId = IoTRMIObject.getObjectId(method);
85                                                 CallBack_CBSkeleton skel = (CallBack_CBSkeleton) listCBObj.get(objId);
86                                                 if (skel != null) {
87                                                         rmiObj.setMethodBytes(method);
88                                                         retObj = skel.invokeMethod(rmiObj);
89                                                 }
90                                                 if (retObj != null) {
91                                                         rmiObj.sendReturnObj(retObj);
92                                                 }
93                                         }
94                                 } catch (Exception ex){
95                                         ex.printStackTrace();
96                                         throw new Error("Error instantiating class CallBack_Skeleton!");
97                         }
98                     }
99                 };
100                 thread.start();
101
102                 String sign = "registercallback";       // can be any string
103                 Class<?> retType = void.class;
104                 // port, address, rev, and number of objects
105                 Class<?>[] paramCls = new Class<?>[] { int.class, String.class, int.class };
106                 Object[] paramObj = new Object[] { ports[0], address, 0 };
107                 rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
108         }
109
110
111         // Single callback handling
112         public void registerCallback(CallBackInterface _cb) {
113
114                 Thread thread = new Thread() {
115                         public void run() {
116                     try{
117                                         CallBack_Skeleton cbskel = new CallBack_Skeleton(_cb, ports[0]);
118                                         cbskel.waitRequestInvokeMethod();
119                                 } catch (Exception ex){
120                                         ex.printStackTrace();
121                                         throw new Error("Error instantiating class CallBack_Skeleton!");
122                     }
123                 }
124             };
125                 thread.start();
126
127                 String sign = "voidregisterCallBack(CallBackInterface)";
128                 Class<?> retType = void.class;
129                 // port, address, and rev
130                 Class<?>[] paramCls = new Class<?>[] { int.class, String.class, int.class };
131                 Object[] paramObj = new Object[] { ports[0], address, 0 };
132                 rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
133         }
134
135
136         public void registerCallback(CallBackInterface[] _cb) {
137
138                 try {
139                         //for (int objId = 0; objId < _cb.length; objId++) {
140                         for (CallBackInterface cb : _cb) {
141                                 CallBack_CBSkeleton skel = new CallBack_CBSkeleton(cb, objIdCnt++);
142                                 listCBObj.add(skel);
143                         }
144                 } catch (Exception ex){
145                         ex.printStackTrace();
146                         throw new Error("Class not found / instantiation / illegal access / IO error!");
147                 }
148
149                 String sign = "voidregisterCallBack(CallBackInterface[])";
150                 Class<?> retType = void.class;
151                 // port, address, rev, and number of objects
152                 Class<?>[] paramCls = new Class<?>[] { int.class };
153                 Object[] paramObj = new Object[] { _cb.length };
154                 rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
155         }
156
157
158         public void setA(int _int) {
159
160                 String sign = "voidsetA(int)";
161                 Class<?> retType = void.class;
162                 Class<?>[] paramCls = new Class<?>[] { int.class };
163                 Object[] paramObj = new Object[] { _int };
164                 rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
165         }
166
167
168         public void setB(float _float) {
169
170                 String sign = "voidsetB(float)";
171                 Class<?> retType = void.class;
172                 Class<?>[] paramCls = new Class<?>[] { float.class };
173                 Object[] paramObj = new Object[] { _float };
174                 rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
175         }
176
177
178         public void setC(String _string) {
179
180                 String sign = "voidsetC(string)";
181                 Class<?> retType = void.class;
182                 Class<?>[] paramCls = new Class<?>[] { String.class };
183                 Object[] paramObj = new Object[] { _string };
184                 rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
185         }
186
187
188         // Getters
189         public String sumArray(String[] newA) {
190
191                 String sign = "sumArray(string[])";
192                 Class<?> retType = String.class;
193                 Class<?>[] paramCls = new Class<?>[] { String[].class };
194                 Object[] paramObj = new Object[] { newA };
195                 Object retObj = rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
196                 return (String)retObj;
197         }
198
199
200         public int setAndGetA(int newA) {
201                 String sign = "intsetAndGetA(int)";
202                 Class<?> retType = int.class;
203                 Class<?>[] paramCls = new Class<?>[] { int.class };
204                 Object[] paramObj = new Object[] { newA };
205                 Object retObj = rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
206                 return (int)retObj;
207         }
208
209
210         public int setACAndGetA(String newC, int newA) {
211
212                 String sign = "intsetACAndGetA(string,int)";
213                 Class<?> retType = int.class;
214                 Class<?>[] paramCls = new Class<?>[] { String.class, int.class };
215                 Object[] paramObj = new Object[] { newC, newA };
216                 Object retObj = rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
217                 return (int)retObj;
218         }
219
220
221         public int callBack() {
222
223                 String sign = "intcallBack()";
224                 Class<?> retType = int.class;
225                 Class<?>[] paramCls = new Class<?>[] { };
226                 Object[] paramObj = new Object[] { };
227                 Object retObj = rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
228                 return (int)retObj;
229
230         }
231
232
233         public static void main(String[] args) throws Exception {
234
235                 CommunicationHandler comHan = new CommunicationHandler(true);
236                 int numOfPorts = TestClass_Stub.numCallbackObjects();
237                 int[] ports = comHan.getCallbackPorts(numOfPorts);
238
239                 int port = 5010;
240                 String address = "localhost";
241                 int rev = 0;
242
243                 System.out.println("Allocated ports: " + Arrays.toString(ports));
244
245                 TestClass_Stub tcstub = new TestClass_Stub(port, address, rev, ports);
246                 System.out.println("Return value: " + tcstub.setAndGetA(123));
247                 System.out.println("Return value: " + tcstub.setACAndGetA("string", 123));
248                 System.out.println("Return value: " + tcstub.sumArray(new String[] { "123", "456", "987" }));
249
250                 CallBackInterface cb1 = new CallBack(23);
251                 CallBackInterface cb2 = new CallBack(33);
252                 CallBackInterface cb3 = new CallBack(43);
253                 CallBackInterface[] cb = { cb1, cb2, cb3 };
254                 tcstub.registerCallback(cb);
255                 System.out.println("Return value from callback: " + tcstub.callBack());
256                 CallBackInterface cb4 = new CallBack(10);
257                 CallBackInterface cb5 = new CallBack(11);
258                 CallBackInterface cb6 = new CallBack(12);
259                 CallBackInterface[] cbt = { cb4, cb5, cb6 };
260                 tcstub.registerCallback(cbt);
261                 System.out.println("Return value from callback: " + tcstub.callBack());
262         }
263 }
264
265