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;
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 // Assign rmiCall from outside
45 public TestClass_CBStub(IoTRMICall _rmiCall, int _objectId, String _address, int[] _ports) throws IOException {
55 * Instantiation of callback objects
57 public static int numCallbackObjects() {
59 return NUM_CB_OBJ; // Generated by the IoTCompiler
63 // Return method signatures
64 public static String[] getMethodSignatures() {
66 return methodSignatures;
70 public void registerCallback(CallBackInterface _cb) {
72 Thread thread = new Thread() {
75 CallBack_Skeleton cbskel = new CallBack_Skeleton(_cb, ports[0]);
76 cbskel.waitRequestInvokeMethod();
77 } catch (Exception ex){
79 throw new Error("Error instantiating class CallBack_Skeleton!");
85 String sign = "voidregisterCallBack(CallBackInterface)";
86 Class<?> retType = void.class;
87 // port, address, and rev
88 Class<?>[] paramCls = new Class<?>[] { int.class, String.class, int.class };
89 Object[] paramObj = new Object[] { ports[0], address, 0 };
90 rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
94 // Multiple callback handling
95 public void registerCallback(CallBackInterface[] _cb) {
98 for (int objId = 0; objId < _cb.length; objId++) {
99 CallBack_CBSkeleton skel = new CallBack_CBSkeleton(_cb[objId], objId);
102 } catch ( ClassNotFoundException |
103 InstantiationException |
104 IllegalAccessException |
106 ex.printStackTrace();
107 throw new Error("Class not found / instantiation / illegal access / IO error!");
110 Thread thread = new Thread() {
113 String[] methodSignatures = CallBack_CBSkeleton.getMethodSignatures();
114 IoTRMIObject rmiObj = new IoTRMIObject(ports[0], methodSignatures);
115 Object retObj = null;
117 byte[] method = rmiObj.getMethodBytes();
118 int objId = IoTRMIObject.getObjectId(method);
119 CallBack_CBSkeleton skel = (CallBack_CBSkeleton) listCBObj.get(objId);
121 rmiObj.setMethodBytes(method);
122 retObj = skel.invokeMethod(rmiObj);
124 if (retObj != null) {
125 rmiObj.sendReturnObj(retObj);
128 } catch (Exception ex){
129 ex.printStackTrace();
130 throw new Error("Error instantiating class CallBack_Skeleton!");
136 String sign = "voidregisterCallBack(CallBackInterface[])";
137 Class<?> retType = void.class;
138 // port, address, rev, and number of objects
139 Class<?>[] paramCls = new Class<?>[] { int.class, String.class, int.class, int.class };
140 Object[] paramObj = new Object[] { ports[0], address, 0, _cb.length };
141 rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
145 public void setA(int _int) {
147 String sign = "voidsetA(int)";
148 Class<?> retType = void.class;
149 Class<?>[] paramCls = new Class<?>[] { int.class };
150 Object[] paramObj = new Object[] { _int };
151 rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
155 public void setB(float _float) {
157 String sign = "voidsetB(float)";
158 Class<?> retType = void.class;
159 Class<?>[] paramCls = new Class<?>[] { float.class };
160 Object[] paramObj = new Object[] { _float };
161 rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
165 public void setC(String _string) {
167 String sign = "voidsetC(string)";
168 Class<?> retType = void.class;
169 Class<?>[] paramCls = new Class<?>[] { String.class };
170 Object[] paramObj = new Object[] { _string };
171 rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
176 public String sumArray(String[] newA) {
178 String sign = "sumArray(string[])";
179 Class<?> retType = String.class;
180 Class<?>[] paramCls = new Class<?>[] { String[].class };
181 Object[] paramObj = new Object[] { newA };
182 Object retObj = rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
183 return (String)retObj;
187 public int setAndGetA(int newA) {
188 String sign = "intsetAndGetA(int)";
189 Class<?> retType = int.class;
190 Class<?>[] paramCls = new Class<?>[] { int.class };
191 Object[] paramObj = new Object[] { newA };
192 Object retObj = rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
197 public int setACAndGetA(String newC, int newA) {
199 String sign = "intsetACAndGetA(string,int)";
200 Class<?> retType = int.class;
201 Class<?>[] paramCls = new Class<?>[] { String.class, int.class };
202 Object[] paramObj = new Object[] { newC, newA };
203 Object retObj = rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
208 public int callBack() {
210 String sign = "intcallBack()";
211 Class<?> retType = int.class;
212 Class<?>[] paramCls = new Class<?>[] { };
213 Object[] paramObj = new Object[] { };
214 Object retObj = rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
220 public static void main(String[] args) throws Exception {