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;
26 private final static int NUM_CB_OBJ = 1;
27 private int objectId = 0; // Default value is 0
28 private final static String[] methodSignatures = {
35 "intsetACAndGetA(string,int)",
37 "voidregisterCallBack(CallBackInterface)",
38 "voidregisterCallBack(CallBackInterface[])"
44 public TestClass_Stub(int _port, String _address, int _rev, int[] _ports) throws IOException {
48 rmiCall = new IoTRMICall(_port, _address, _rev, methodSignatures);
49 listCBObj = new ArrayList<CallBackInterface>();
52 // Assign rmiCall from outside
53 public TestClass_Stub(IoTRMICall _rmiCall, int _objectId, String _address, int[] _ports) throws IOException {
63 * Instantiation of callback objects
65 public static int numCallbackObjects() {
67 return NUM_CB_OBJ; // Generated by the IoTCompiler
71 // Return method signatures
72 public static String[] getMethodSignatures() {
74 return methodSignatures;
78 // Single callback handling
79 public void registerCallback(CallBackInterface _cb) {
81 Thread thread = new Thread() {
84 CallBack_Skeleton cbskel = new CallBack_Skeleton(_cb, ports[0]);
85 cbskel.waitRequestInvokeMethod();
86 } catch (Exception ex){
88 throw new Error("Error instantiating class CallBack_Skeleton!");
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);
103 // Multiple callback handling
104 public void registerCallback(CallBackInterface[] _cb) {
107 for (int objId = 0; objId < _cb.length; objId++) {
108 CallBack_CBSkeleton skel = new CallBack_CBSkeleton(_cb[objId], objId);
111 } catch ( ClassNotFoundException |
112 InstantiationException |
113 IllegalAccessException |
115 ex.printStackTrace();
116 throw new Error("Class not found / instantiation / illegal access / IO error!");
119 Thread thread = new Thread() {
122 String[] methodSignatures = CallBack_CBSkeleton.getMethodSignatures();
123 IoTRMIObject rmiObj = new IoTRMIObject(ports[0], methodSignatures);
124 Object retObj = null;
126 byte[] method = rmiObj.getMethodBytes();
127 int objId = IoTRMIObject.getObjectId(method);
128 CallBack_CBSkeleton skel = (CallBack_CBSkeleton) listCBObj.get(objId);
130 rmiObj.setMethodBytes(method);
131 retObj = skel.invokeMethod(rmiObj);
133 if (retObj != null) {
134 rmiObj.sendReturnObj(retObj);
137 } catch (Exception ex){
138 ex.printStackTrace();
139 throw new Error("Error instantiating class CallBack_Skeleton!");
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);
154 public void setA(int _int) {
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);
164 public void setB(float _float) {
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);
174 public void setC(String _string) {
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);
185 public String sumArray(String[] newA) {
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;
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);
206 public int setACAndGetA(String newC, int newA) {
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);
217 public int callBack() {
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);
229 public static void main(String[] args) throws Exception {
231 CommunicationHandler comHan = new CommunicationHandler(true);
232 int numOfPorts = TestClass_Stub.numCallbackObjects();
233 int[] ports = comHan.getCallbackPorts(numOfPorts);
236 String address = "localhost";
239 System.out.println("Allocated ports: " + Arrays.toString(ports));
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" }));
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());