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_Stub 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 public TestClass_Stub(int _port, String _address, int _rev, int[] _ports) throws Exception {
51 rmiCall = new IoTRMICall(_port, _address, _rev, methodSignatures);
52 listCBObj = new ArrayList<CallBackInterface>();
58 * Instantiation of callback objects
60 public static int numCallbackObjects() {
62 return NUM_CB_OBJ; // Generated by the IoTCompiler
66 // Return method signatures
67 public static String[] getMethodSignatures() {
69 return methodSignatures;
73 // Initialize callback
74 public void init_CallBack() {
76 Thread thread = new Thread() {
79 String[] methodSignatures = CallBack_CBSkeleton.getMethodSignatures();
80 rmiObj = new IoTRMIObject(ports[0], methodSignatures);
83 byte[] method = rmiObj.getMethodBytes();
84 int objId = IoTRMIObject.getObjectId(method);
85 CallBack_CBSkeleton skel = (CallBack_CBSkeleton) listCBObj.get(objId);
87 rmiObj.setMethodBytes(method);
88 retObj = skel.invokeMethod(rmiObj);
91 rmiObj.sendReturnObj(retObj);
94 } catch (Exception ex){
96 throw new Error("Error instantiating class CallBack_Skeleton!");
102 String sign = "registercallback";
103 Class<?> retType = void.class;
104 // port, address, rev, and number of objects
105 Class<?>[] paramCls = new Class<?>[] { int.class, String.class, int.class };
106 Object[] paramObj = new Object[] { ports[0], address, 0 };
107 rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
111 // Single callback handling
112 public void registerCallback(CallBackInterface _cb) {
114 Thread thread = new Thread() {
117 CallBack_Skeleton cbskel = new CallBack_Skeleton(_cb, ports[0]);
118 cbskel.waitRequestInvokeMethod();
119 } catch (Exception ex){
120 ex.printStackTrace();
121 throw new Error("Error instantiating class CallBack_Skeleton!");
127 String sign = "voidregisterCallBack(CallBackInterface)";
128 Class<?> retType = void.class;
129 // port, address, and rev
130 Class<?>[] paramCls = new Class<?>[] { int.class, String.class, int.class };
131 Object[] paramObj = new Object[] { ports[0], address, 0 };
132 rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
136 public void registerCallback(CallBackInterface[] _cb) {
139 //for (int objId = 0; objId < _cb.length; objId++) {
140 for (CallBackInterface cb : _cb) {
141 CallBack_CBSkeleton skel = new CallBack_CBSkeleton(cb, objIdCnt++);
144 } catch (Exception ex){
145 ex.printStackTrace();
146 throw new Error("Class not found / instantiation / illegal access / IO error!");
149 String sign = "voidregisterCallBack(CallBackInterface[])";
150 Class<?> retType = void.class;
151 // port, address, rev, and number of objects
152 Class<?>[] paramCls = new Class<?>[] { int.class };
153 Object[] paramObj = new Object[] { _cb.length };
154 rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
158 public void setA(int _int) {
160 String sign = "voidsetA(int)";
161 Class<?> retType = void.class;
162 Class<?>[] paramCls = new Class<?>[] { int.class };
163 Object[] paramObj = new Object[] { _int };
164 rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
168 public void setB(float _float) {
170 String sign = "voidsetB(float)";
171 Class<?> retType = void.class;
172 Class<?>[] paramCls = new Class<?>[] { float.class };
173 Object[] paramObj = new Object[] { _float };
174 rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
178 public void setC(String _string) {
180 String sign = "voidsetC(string)";
181 Class<?> retType = void.class;
182 Class<?>[] paramCls = new Class<?>[] { String.class };
183 Object[] paramObj = new Object[] { _string };
184 rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
189 public String sumArray(String[] newA) {
191 String sign = "sumArray(string[])";
192 Class<?> retType = String.class;
193 Class<?>[] paramCls = new Class<?>[] { String[].class };
194 Object[] paramObj = new Object[] { newA };
195 Object retObj = rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
196 return (String)retObj;
200 public int setAndGetA(int newA) {
201 String sign = "intsetAndGetA(int)";
202 Class<?> retType = int.class;
203 Class<?>[] paramCls = new Class<?>[] { int.class };
204 Object[] paramObj = new Object[] { newA };
205 Object retObj = rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
210 public int setACAndGetA(String newC, int newA) {
212 String sign = "intsetACAndGetA(string,int)";
213 Class<?> retType = int.class;
214 Class<?>[] paramCls = new Class<?>[] { String.class, int.class };
215 Object[] paramObj = new Object[] { newC, newA };
216 Object retObj = rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
221 public int callBack() {
223 String sign = "intcallBack()";
224 Class<?> retType = int.class;
225 Class<?>[] paramCls = new Class<?>[] { };
226 Object[] paramObj = new Object[] { };
227 Object retObj = rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
233 public static void main(String[] args) throws Exception {
235 CommunicationHandler comHan = new CommunicationHandler(true);
236 int numOfPorts = TestClass_Stub.numCallbackObjects();
237 int[] ports = comHan.getCallbackPorts(numOfPorts);
240 String address = "localhost";
243 System.out.println("Allocated ports: " + Arrays.toString(ports));
245 TestClass_Stub tcstub = new TestClass_Stub(port, address, rev, ports);
246 System.out.println("Return value: " + tcstub.setAndGetA(123));
247 System.out.println("Return value: " + tcstub.setACAndGetA("string", 123));
248 System.out.println("Return value: " + tcstub.sumArray(new String[] { "123", "456", "987" }));
250 CallBackInterface cb1 = new CallBack(23);
251 CallBackInterface cb2 = new CallBack(33);
252 CallBackInterface cb3 = new CallBack(43);
253 CallBackInterface[] cb = { cb1, cb2, cb3 };
254 tcstub.registerCallback(cb);
255 System.out.println("Return value from callback: " + tcstub.callBack());
256 CallBackInterface cb4 = new CallBack(10);
257 CallBackInterface cb5 = new CallBack(11);
258 CallBackInterface cb6 = new CallBack(12);
259 CallBackInterface[] cbt = { cb4, cb5, cb6 };
260 tcstub.registerCallback(cbt);
261 System.out.println("Return value from callback: " + tcstub.callBack());