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