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