1 package iotrmi.Java.sample;
3 import java.io.IOException;
4 import iotrmi.Java.IoTRMICall;
5 import iotruntime.master.CommunicationHandler;
7 import java.util.Arrays;
9 import java.util.ArrayList;
11 import iotrmi.Java.IoTRMIObject;
13 public class TestClass_CBStub implements TestClassInterface {
18 private IoTRMICall rmiCall;
19 private String address;
21 private List<CallBackInterface> listCBObj;
22 private IoTRMIObject rmiObj;
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 = {
37 "intsetACAndGetA(string,int)",
39 "voidregisterCallBack(CallBackInterface)",
40 "voidregisterCallBack(CallBackInterface[])",
42 "handleStruct(StructJ)"
48 // Assign rmiCall from outside
49 public TestClass_CBStub(IoTRMICall _rmiCall, int _objectId, String _address, int[] _ports) throws IOException {
55 listCBObj = new ArrayList<CallBackInterface>();
61 * Instantiation of callback objects
63 public static int numCallbackObjects() {
65 return NUM_CB_OBJ; // Generated by the IoTCompiler
69 // Return method signatures
70 public static String[] getMethodSignatures() {
72 return methodSignatures;
76 // Initialize callback
77 public void init_CallBack() {
79 Thread thread = new Thread() {
82 String[] methodSignatures = CallBack_CBSkeleton.getMethodSignatures();
83 rmiObj = new IoTRMIObject(ports[0], methodSignatures);
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;
95 rmiObj.setMethodBytes(method);
96 retObj = skel.invokeMethod(rmiObj);
99 rmiObj.sendReturnObj(retObj);
102 } catch (Exception ex){
103 ex.printStackTrace();
104 throw new Error("Error instantiating class CallBack_Skeleton!");
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);
119 // Single callback handling
120 public void registerCallback(CallBackInterface _cb) {
122 Thread thread = new Thread() {
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!");
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);
144 public void registerCallback(CallBackInterface[] _cb) {
147 //for (int objId = 0; objId < _cb.length; objId++) {
148 for (CallBackInterface cb : _cb) {
149 CallBack_CBSkeleton skel = new CallBack_CBSkeleton(cb, objIdCnt++);
152 } catch (Exception ex){
153 ex.printStackTrace();
154 throw new Error("Class not found / instantiation / illegal access / IO error!");
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);
166 public void setA(int _int) {
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);
176 public void setB(float _float) {
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);
186 public void setC(String _string) {
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);
197 public String sumArray(String[] newA) {
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;
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);
218 public int setACAndGetA(String newC, int newA) {
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);
229 public int callBack() {
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);
240 public void handleStruct(StructJ[] data) {
245 public static void main(String[] args) throws Exception {
247 CommunicationHandler comHan = new CommunicationHandler(true);
248 int numOfPorts = TestClass_Stub.numCallbackObjects();
249 int[] ports = comHan.getCallbackPorts(numOfPorts);
252 String address = "localhost";
255 System.out.println("Allocated ports: " + Arrays.toString(ports));
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" }));
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());