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 = {
33 "voidsetB(float)", // 1
34 "voidsetC(string)", // 2
35 "sumArray(string[])", // 3
36 "intsetAndGetA(int)", // 4
37 "intsetACAndGetA(string,int)", // 5
39 "voidregisterCallBack(CallBackInterface)", // 7
40 "voidregisterCallBack(CallBackInterface[])", // 8
41 "registercallback", // 9
42 "handleStruct(StructJ[])", // 10
49 public TestClass_Stub(int _port, String _address, int _rev, int[] _ports) throws Exception {
53 //rmiCall = new IoTRMICall(_port, _address, _rev, methodSignatures);
54 rmiCall = new IoTRMICall(_port, _address, _rev);
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 ___initCallBack() {
79 Thread thread = new Thread() {
82 //String[] methodSignatures = CallBack_CBSkeleton.getMethodSignatures();
83 //rmiObj = new IoTRMIObject(ports[0], methodSignatures);
84 rmiObj = new IoTRMIObject(ports[0]);
87 byte[] method = rmiObj.getMethodBytes();
88 int objId = IoTRMIObject.getObjectId(method);
89 CallBack_CBSkeleton skel = (CallBack_CBSkeleton) listCBObj.get(objId);
91 //rmiObj.setMethodBytes(method);
92 skel.invokeMethod(rmiObj);
95 } catch (Exception ex){
97 throw new Error("Error instantiating class CallBack_Skeleton!");
103 //String sign = "registercallback"; // can be any string
105 Class<?> retType = void.class;
106 // port, address, rev
107 Class<?>[] paramCls = new Class<?>[] { int.class, String.class, int.class };
108 Object[] paramObj = new Object[] { ports[0], address, 0 };
109 //rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
110 rmiCall.remoteCall(objectId, methodId, retType, null, null, paramCls, paramObj);
114 // Single callback handling
115 public void registerCallback(CallBackInterface _cb) {
117 /*Thread thread = new Thread() {
120 CallBack_Skeleton cbskel = new CallBack_Skeleton(_cb, ports[0]);
121 } catch (Exception ex){
122 ex.printStackTrace();
123 throw new Error("Error instantiating class CallBack_Skeleton!");
129 String sign = "voidregisterCallBack(CallBackInterface)";
130 Class<?> retType = void.class;
131 // port, address, and rev
132 Class<?>[] paramCls = new Class<?>[] { int.class, String.class, int.class };
133 Object[] paramObj = new Object[] { ports[0], address, 0 };
134 rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);*/
138 public void registerCallback(CallBackInterface[] _cb) {
141 //for (int objId = 0; objId < _cb.length; objId++) {
142 for (CallBackInterface cb : _cb) {
143 CallBack_CBSkeleton skel = new CallBack_CBSkeleton(cb, objIdCnt++);
146 } catch (Exception ex){
147 ex.printStackTrace();
148 throw new Error("Class not found / instantiation / illegal access / IO error!");
151 //String sign = "voidregisterCallBack(CallBackInterface[])";
153 Class<?> retType = void.class;
154 // port, address, rev, and number of objects
155 Class<?>[] paramCls = new Class<?>[] { int.class };
156 Object[] paramObj = new Object[] { _cb.length };
157 //rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
158 rmiCall.remoteCall(objectId, methodId, retType, null, null, paramCls, paramObj);
162 public void setA(int _int) {
164 //String sign = "voidsetA(int)";
166 Class<?> retType = void.class;
167 Class<?>[] paramCls = new Class<?>[] { int.class };
168 Object[] paramObj = new Object[] { _int };
169 rmiCall.remoteCall(objectId, methodId, retType, null, null, paramCls, paramObj);
173 public void setB(float _float) {
176 Class<?> retType = void.class;
177 Class<?>[] paramCls = new Class<?>[] { float.class };
178 Object[] paramObj = new Object[] { _float };
179 rmiCall.remoteCall(objectId, methodId, retType, null, null, paramCls, paramObj);
183 public void setC(String _string) {
186 Class<?> retType = void.class;
187 Class<?>[] paramCls = new Class<?>[] { String.class };
188 Object[] paramObj = new Object[] { _string };
189 rmiCall.remoteCall(objectId, methodId, retType, null, null, paramCls, paramObj);
194 public String sumArray(String[] newA) {
197 Class<?> retType = String.class;
198 Class<?>[] paramCls = new Class<?>[] { String[].class };
199 Object[] paramObj = new Object[] { newA };
200 Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, null, paramCls, paramObj);
201 return (String)retObj;
205 public int setAndGetA(int newA) {
207 Class<?> retType = int.class;
208 Class<?>[] paramCls = new Class<?>[] { int.class };
209 Object[] paramObj = new Object[] { newA };
210 Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, null, paramCls, paramObj);
215 public int setACAndGetA(String newC, int newA) {
218 Class<?> retType = int.class;
219 Class<?>[] paramCls = new Class<?>[] { String.class, int.class };
220 Object[] paramObj = new Object[] { newC, newA };
221 Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, null, paramCls, paramObj);
226 public int callBack() {
229 Class<?> retType = int.class;
230 Class<?>[] paramCls = new Class<?>[] { };
231 Object[] paramObj = new Object[] { };
232 Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, null, paramCls, paramObj);
238 public void handleStruct(StructJ[] data) {
241 Class<?> retType = void.class;
242 Class<?>[] paramCls = new Class<?>[] { int.class };
243 Object[] paramObj = new Object[] { data.length };
244 rmiCall.remoteCall(objectId, methodId, retType, null, null, paramCls, paramObj);
247 Class<?> retType2 = void.class;
248 // Calculate the size of the array
249 Class<?>[] paramCls2 = new Class<?>[3*data.length];
250 Object[] paramObj2 = new Object[3*data.length];
251 // Handle with for loop
253 for(int i = 0; i < data.length; i++) {
254 paramCls2[pos] = String.class;
255 paramObj2[pos++] = data[i].name;
256 paramCls2[pos] = float.class;
257 paramObj2[pos++] = data[i].value;
258 paramCls2[pos] = int.class;
259 paramObj2[pos++] = data[i].year;
261 System.out.println(Arrays.toString(paramObj2));
262 rmiCall.remoteCall(objectId, methodId2, retType2, null, null, paramCls2, paramObj2);
266 public static void main(String[] args) throws Exception {
268 CommunicationHandler comHan = new CommunicationHandler(true);
269 int numOfPorts = TestClass_Stub.numCallbackObjects();
270 int[] ports = comHan.getCallbackPorts(numOfPorts);
273 String address = "localhost";
276 System.out.println("Allocated ports: " + Arrays.toString(ports));
278 TestClass_Stub tcstub = new TestClass_Stub(port, address, rev, ports);
279 System.out.println("Return value: " + tcstub.setAndGetA(123));
280 System.out.println("Return value: " + tcstub.setACAndGetA("string", 123));
281 System.out.println("Return value: " + tcstub.sumArray(new String[] { "123", "456", "987" }));
283 CallBackInterface cb1 = new CallBack(23);
284 CallBackInterface cb2 = new CallBack(33);
285 CallBackInterface cb3 = new CallBack(43);
286 CallBackInterface[] cb = { cb1, cb2, cb3 };
287 tcstub.registerCallback(cb);
288 System.out.println("Return value from callback: " + tcstub.callBack());
289 CallBackInterface cb4 = new CallBack(10);
290 CallBackInterface cb5 = new CallBack(11);
291 CallBackInterface cb6 = new CallBack(12);
292 CallBackInterface[] cbt = { cb4, cb5, cb6 };
293 tcstub.registerCallback(cbt);
294 System.out.println("Return value from callback: " + tcstub.callBack());
296 StructJ[] data = new StructJ[2];
297 for (int i=0; i<2; i++) {
298 data[i] = new StructJ();
300 data[0].name = "Rahmadi";
301 data[0].value = 0.123f;
303 //data[1].name = "Trimananda";
304 //data[1].value = 0.223f;
305 //data[1].year = 2017;
307 for (StructJ str : data) {
308 System.out.println("Name: " + str.name);
309 System.out.println("Value: " + str.value);
310 System.out.println("Year: " + str.year);
313 tcstub.handleStruct(data);