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