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[])",
47 // Assign rmiCall from outside
48 public TestClass_CBStub(IoTRMICall _rmiCall, int _objectId, String _address, int[] _ports) throws IOException {
54 listCBObj = new ArrayList<CallBackInterface>();
60 * Instantiation of callback objects
62 public static int numCallbackObjects() {
64 return NUM_CB_OBJ; // Generated by the IoTCompiler
68 // Return method signatures
69 public static String[] getMethodSignatures() {
71 return methodSignatures;
75 // Initialize callback
76 public void init_CallBack() {
78 Thread thread = new Thread() {
81 String[] methodSignatures = CallBack_CBSkeleton.getMethodSignatures();
82 rmiObj = new IoTRMIObject(ports[0], methodSignatures);
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;
94 rmiObj.setMethodBytes(method);
95 retObj = skel.invokeMethod(rmiObj);
98 rmiObj.sendReturnObj(retObj);
101 } catch (Exception ex){
102 ex.printStackTrace();
103 throw new Error("Error instantiating class CallBack_Skeleton!");
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);
118 // Single callback handling
119 public void registerCallback(CallBackInterface _cb) {
121 Thread thread = new Thread() {
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!");
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);
143 public void registerCallback(CallBackInterface[] _cb) {
146 //for (int objId = 0; objId < _cb.length; objId++) {
147 for (CallBackInterface cb : _cb) {
148 CallBack_CBSkeleton skel = new CallBack_CBSkeleton(cb, objIdCnt++);
151 } catch (Exception ex){
152 ex.printStackTrace();
153 throw new Error("Class not found / instantiation / illegal access / IO error!");
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);
165 public void setA(int _int) {
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);
175 public void setB(float _float) {
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);
185 public void setC(String _string) {
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);
196 public String sumArray(String[] newA) {
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;
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);
217 public int setACAndGetA(String newC, int newA) {
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);
228 public int callBack() {
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);
240 public static void main(String[] args) throws Exception {
242 CommunicationHandler comHan = new CommunicationHandler(true);
243 int numOfPorts = TestClass_Stub.numCallbackObjects();
244 int[] ports = comHan.getCallbackPorts(numOfPorts);
247 String address = "localhost";
250 System.out.println("Allocated ports: " + Arrays.toString(ports));
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" }));
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());