a3d80fdfe79b84d37b048f720801edb547bb2a57
[iot2.git] / iotjava / iotrmi / Java / sample / TestClass_CBStub.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_CBStub 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                 "handleStruct(StructJ)"
43         };
44
45         /**
46          * Constructors
47          */
48         // Assign rmiCall from outside
49         public TestClass_CBStub(IoTRMICall _rmiCall, int _objectId, String _address, int[] _ports) throws IOException {
50
51                 address = _address;
52                 ports = _ports;
53                 objectId = _objectId;
54                 rmiCall = _rmiCall;
55                 listCBObj = new ArrayList<CallBackInterface>();
56                 init_CallBack();
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 init_CallBack() {
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                                         Object retObj = null;
85                                         while (true) {
86                                                 byte[] method = rmiObj.getMethodBytes();
87                                                 int objId = IoTRMIObject.getObjectId(method);
88                                                 //CallBack_CBSkeleton skel = (CallBack_CBSkeleton) listCBObj.get(objId);
89                                                 CallBackInterface cb = listCBObj.get(objId);
90                                                 CallBack_CBSkeleton skel = null;
91                                                 if (cb instanceof CallBack_CBSkeleton) { // in case we have multiple callback classes
92                                                         skel = (CallBack_CBSkeleton) cb;
93                                                 }
94                                                 if (skel != null) {
95                                                         rmiObj.setMethodBytes(method);
96                                                         retObj = skel.invokeMethod(rmiObj);
97                                                 }
98                                                 if (retObj != null) {
99                                                         rmiObj.sendReturnObj(retObj);
100                                                 }
101                                         }
102                                 } catch (Exception ex){
103                                         ex.printStackTrace();
104                                         throw new Error("Error instantiating class CallBack_Skeleton!");
105                         }
106                     }
107                 };
108                 thread.start();
109
110                 String sign = "registercallback";
111                 Class<?> retType = void.class;
112                 // port, address, rev, and number of objects
113                 Class<?>[] paramCls = new Class<?>[] { int.class, String.class, int.class };
114                 Object[] paramObj = new Object[] { ports[0], address, 0 };
115                 rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
116         }
117
118
119         // Single callback handling
120         public void registerCallback(CallBackInterface _cb) {
121
122                 Thread thread = new Thread() {
123                         public void run() {
124                     try{
125                                         CallBack_Skeleton cbskel = new CallBack_Skeleton(_cb, ports[0]);
126                                         cbskel.waitRequestInvokeMethod();
127                                 } catch (Exception ex){
128                                         ex.printStackTrace();
129                                         throw new Error("Error instantiating class CallBack_Skeleton!");
130                     }
131                 }
132             };
133                 thread.start();
134
135                 String sign = "voidregisterCallBack(CallBackInterface)";
136                 Class<?> retType = void.class;
137                 // port, address, and rev
138                 Class<?>[] paramCls = new Class<?>[] { int.class, String.class, int.class };
139                 Object[] paramObj = new Object[] { ports[0], address, 0 };
140                 rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
141         }
142
143
144         public void registerCallback(CallBackInterface[] _cb) {
145
146                 try {
147                         //for (int objId = 0; objId < _cb.length; objId++) {
148                         for (CallBackInterface cb : _cb) {
149                                 CallBack_CBSkeleton skel = new CallBack_CBSkeleton(cb, objIdCnt++);
150                                 listCBObj.add(skel);
151                         }
152                 } catch (Exception ex){
153                         ex.printStackTrace();
154                         throw new Error("Class not found / instantiation / illegal access / IO error!");
155                 }
156
157                 String sign = "voidregisterCallBack(CallBackInterface[])";
158                 Class<?> retType = void.class;
159                 // port, address, rev, and number of objects
160                 Class<?>[] paramCls = new Class<?>[] { int.class };
161                 Object[] paramObj = new Object[] { _cb.length };
162                 rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
163         }
164
165
166         public void setA(int _int) {
167
168                 String sign = "voidsetA(int)";
169                 Class<?> retType = void.class;
170                 Class<?>[] paramCls = new Class<?>[] { int.class };
171                 Object[] paramObj = new Object[] { _int };
172                 rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
173         }
174
175
176         public void setB(float _float) {
177
178                 String sign = "voidsetB(float)";
179                 Class<?> retType = void.class;
180                 Class<?>[] paramCls = new Class<?>[] { float.class };
181                 Object[] paramObj = new Object[] { _float };
182                 rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
183         }
184
185
186         public void setC(String _string) {
187
188                 String sign = "voidsetC(string)";
189                 Class<?> retType = void.class;
190                 Class<?>[] paramCls = new Class<?>[] { String.class };
191                 Object[] paramObj = new Object[] { _string };
192                 rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
193         }
194
195
196         // Getters
197         public String sumArray(String[] newA) {
198
199                 String sign = "sumArray(string[])";
200                 Class<?> retType = String.class;
201                 Class<?>[] paramCls = new Class<?>[] { String[].class };
202                 Object[] paramObj = new Object[] { newA };
203                 Object retObj = rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
204                 return (String)retObj;
205         }
206
207
208         public int setAndGetA(int newA) {
209                 String sign = "intsetAndGetA(int)";
210                 Class<?> retType = int.class;
211                 Class<?>[] paramCls = new Class<?>[] { int.class };
212                 Object[] paramObj = new Object[] { newA };
213                 Object retObj = rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
214                 return (int)retObj;
215         }
216
217
218         public int setACAndGetA(String newC, int newA) {
219
220                 String sign = "intsetACAndGetA(string,int)";
221                 Class<?> retType = int.class;
222                 Class<?>[] paramCls = new Class<?>[] { String.class, int.class };
223                 Object[] paramObj = new Object[] { newC, newA };
224                 Object retObj = rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
225                 return (int)retObj;
226         }
227
228
229         public int callBack() {
230
231                 String sign = "intcallBack()";
232                 Class<?> retType = int.class;
233                 Class<?>[] paramCls = new Class<?>[] { };
234                 Object[] paramObj = new Object[] { };
235                 Object retObj = rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
236                 return (int)retObj;
237
238         }
239
240         public void handleStruct(StructJ[] data) {
241
242         }
243
244
245         public static void main(String[] args) throws Exception {
246
247                 CommunicationHandler comHan = new CommunicationHandler(true);
248                 int numOfPorts = TestClass_Stub.numCallbackObjects();
249                 int[] ports = comHan.getCallbackPorts(numOfPorts);
250
251                 int port = 5010;
252                 String address = "localhost";
253                 int rev = 0;
254
255                 System.out.println("Allocated ports: " + Arrays.toString(ports));
256
257                 TestClass_Stub tcstub = new TestClass_Stub(port, address, rev, ports);
258                 System.out.println("Return value: " + tcstub.setAndGetA(123));
259                 System.out.println("Return value: " + tcstub.setACAndGetA("string", 123));
260                 System.out.println("Return value: " + tcstub.sumArray(new String[] { "123", "456", "987" }));
261
262                 CallBackInterface cb1 = new CallBack(23);
263                 CallBackInterface cb2 = new CallBack(33);
264                 CallBackInterface cb3 = new CallBack(43);
265                 CallBackInterface[] cb = { cb1, cb2, cb3 };
266                 tcstub.registerCallback(cb);
267                 System.out.println("Return value from callback: " + tcstub.callBack());
268                 CallBackInterface cb4 = new CallBack(10);
269                 CallBackInterface cb5 = new CallBack(11);
270                 CallBackInterface cb6 = new CallBack(12);
271                 CallBackInterface[] cbt = { cb4, cb5, cb6 };
272                 tcstub.registerCallback(cbt);
273                 System.out.println("Return value from callback: " + tcstub.callBack());
274         }
275 }
276
277