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 IoTRMIObject rmiObj;
20 private String address;
26 private int objectId = 0; // Default value is 0
27 // This is permission system for callback objects
28 private static Integer[] object0Permission = { 0, 1 }; // object0 is the callback object
29 private List<Integer> set0Allowed;
32 * Properties and constants for Callbacks!
34 private List<CallBackInterface> listCBObj;
35 private final static int NUM_CB_OBJ = 1;
36 private static int objIdCnt = 0; // Counter for callback object Ids
42 public TestClass_Stub(int _port, String _address, int _rev, int[] _ports) throws Exception {
46 rmiCall = new IoTRMICall(_port, _address, _rev);
48 set0Allowed = Arrays.asList(object0Permission);
50 // Only for callbacks!!!
51 listCBObj = new ArrayList<CallBackInterface>();
57 * Instantiation of callback objects
59 public static int numCallbackObjects() {
61 return NUM_CB_OBJ; // Generated by the IoTCompiler
65 // Initialize callback
66 public void ___initCallBack() {
68 Thread thread = new Thread() {
71 System.out.println("Created server with port: " + ports[0]);
72 rmiObj = new IoTRMIObject(ports[0]);
75 byte[] method = rmiObj.getMethodBytes();
76 // Permission checking
77 int methId = IoTRMIObject.getMethodId(method);
78 if (!set0Allowed.contains(methId))
79 throw new Error("CallBack_CBSkeleton: This object is not allowed to access method " + methId);
81 int objId = IoTRMIObject.getObjectId(method);
82 CallBack_CBSkeleton skel = (CallBack_CBSkeleton) listCBObj.get(objId);
84 //rmiObj.setMethodBytes(method);
85 skel.invokeMethod(rmiObj);
88 } catch (Exception ex){
90 throw new Error("Error instantiating class CallBack_Skeleton!");
96 //String sign = "registercallback"; // can be any string
98 Class<?> retType = void.class;
100 Class<?>[] paramCls = new Class<?>[] { int.class, String.class, int.class };
101 Object[] paramObj = new Object[] { ports[0], address, 0 };
102 rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
106 // Single callback handling
107 public void registerCallback(CallBackInterface _cb) {
109 /*Thread thread = new Thread() {
112 CallBack_Skeleton cbskel = new CallBack_Skeleton(_cb, ports[0]);
113 } catch (Exception ex){
114 ex.printStackTrace();
115 throw new Error("Error instantiating class CallBack_Skeleton!");
121 String sign = "voidregisterCallBack(CallBackInterface)";
122 Class<?> retType = void.class;
123 // port, address, and rev
124 Class<?>[] paramCls = new Class<?>[] { int.class, String.class, int.class };
125 Object[] paramObj = new Object[] { ports[0], address, 0 };
126 rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);*/
130 public void registerCallback(CallBackInterface[] _cb) {
133 //for (int objId = 0; objId < _cb.length; objId++) {
134 for (CallBackInterface cb : _cb) {
135 CallBack_CBSkeleton skel = new CallBack_CBSkeleton(cb, objIdCnt++);
138 } catch (Exception ex){
139 ex.printStackTrace();
140 throw new Error("Class not found / instantiation / illegal access / IO error!");
143 //String sign = "voidregisterCallBack(CallBackInterface[])";
145 Class<?> retType = void.class;
146 // port, address, rev, and number of objects
147 Class<?>[] paramCls = new Class<?>[] { int.class };
148 Object[] paramObj = new Object[] { _cb.length };
149 //rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
150 rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
154 public void setA(int _int) {
156 //String sign = "voidsetA(int)";
158 Class<?> retType = void.class;
159 Class<?>[] paramCls = new Class<?>[] { int.class };
160 Object[] paramObj = new Object[] { _int };
161 rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
165 public void setB(float _float) {
168 Class<?> retType = void.class;
169 Class<?>[] paramCls = new Class<?>[] { float.class };
170 Object[] paramObj = new Object[] { _float };
171 rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
175 public void setC(String _string) {
178 Class<?> retType = void.class;
179 Class<?>[] paramCls = new Class<?>[] { String.class };
180 Object[] paramObj = new Object[] { _string };
181 rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
186 public String sumArray(String[] newA) {
189 Class<?> retType = String.class;
190 Class<?>[] paramCls = new Class<?>[] { String[].class };
191 Object[] paramObj = new Object[] { newA };
192 Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
193 return (String)retObj;
197 public int setAndGetA(int newA) {
199 Class<?> retType = int.class;
200 Class<?>[] paramCls = new Class<?>[] { int.class };
201 Object[] paramObj = new Object[] { newA };
202 Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
207 public int setACAndGetA(String newC, int newA) {
210 Class<?> retType = int.class;
211 Class<?>[] paramCls = new Class<?>[] { String.class, int.class };
212 Object[] paramObj = new Object[] { newC, newA };
213 Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
218 public int callBack() {
221 Class<?> retType = int.class;
222 Class<?>[] paramCls = new Class<?>[] { };
223 Object[] paramObj = new Object[] { };
224 Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
230 public StructJ[] handleStruct(StructJ[] data) {
233 Class<?> retType = void.class;
234 Class<?>[] paramCls = new Class<?>[] { int.class };
235 Object[] paramObj = new Object[] { data.length };
236 rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
239 Class<?> retType2 = int.class; // return type is integer if it is a struct!!!
240 // Calculate the size of the array
241 Class<?>[] paramCls2 = new Class<?>[3*data.length];
242 Object[] paramObj2 = new Object[3*data.length];
243 // Handle with for loop
245 for(int i = 0; i < data.length; i++) {
246 paramCls2[pos] = String.class;
247 paramObj2[pos++] = data[i].name;
248 paramCls2[pos] = float.class;
249 paramObj2[pos++] = data[i].value;
250 paramCls2[pos] = int.class;
251 paramObj2[pos++] = data[i].year;
253 //System.out.println(Arrays.toString(paramObj2));
254 Object retObj = rmiCall.remoteCall(objectId, methodId2, retType2, null, paramCls2, paramObj2);
256 // Get the length of the struct first
257 int structsize1 = (int) retObj;
258 // Construct the struct
259 Class<?>[] retCls = new Class<?>[3*structsize1];
260 Class<?>[] retClsVal = new Class<?>[3*structsize1];
262 for(int i=0; i < structsize1; i++) {
263 retCls[pos] = String.class;
264 retClsVal[pos++] = null;
265 retCls[pos] = float.class;
266 retClsVal[pos++] = null;
267 retCls[pos] = int.class;
268 retClsVal[pos++] = null;
270 Object[] retObj2 = rmiCall.getStructObjects(retCls,
272 StructJ[] dataRet = new StructJ[structsize1];
273 for (int i=0; i < structsize1; i++) {
274 dataRet[i] = new StructJ();
277 for(int i=0; i < structsize1; i++) {
278 dataRet[i].name = (String) retObj2[pos++];
279 dataRet[i].value = (float) retObj2[pos++];
280 dataRet[i].year = (int) retObj2[pos++];
287 public EnumJ[] handleEnum(EnumJ[] en) {
290 Class<?> retType = int[].class;
291 // Handle with for loop
292 int paramInt[] = new int[en.length];
293 for(int i = 0; i < en.length; i++) {
294 paramInt[i] = en[i].ordinal();
297 Class<?>[] paramCls = new Class<?>[] { int[].class };
298 Object[] paramObj = new Object[] { paramInt };
300 // if no return value just
301 // rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
302 // This is with return value:
304 rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
305 int[] retEnumInt = (int[]) retObj;
306 int enumsize1 = retEnumInt.length;
308 EnumJ[] enumJ = EnumJ.values();
309 EnumJ[] enRetVal = new EnumJ[enumsize1];
310 for(int i = 0; i < enumsize1; i++) {
311 enRetVal[i] = enumJ[retEnumInt[i]];
317 public static void main(String[] args) throws Exception {
319 CommunicationHandler comHan = new CommunicationHandler(true);
320 int numOfPorts = TestClass_Stub.numCallbackObjects();
321 int[] ports = comHan.getCallbackPorts(numOfPorts);
324 String address = "localhost";
327 System.out.println("Allocated ports: " + Arrays.toString(ports));
329 TestClass_Stub tcstub = new TestClass_Stub(port, address, rev, ports);
330 System.out.println("Return value: " + tcstub.setAndGetA(123));
331 System.out.println("Return value: " + tcstub.setACAndGetA("string", 123));
332 System.out.println("Return value: " + tcstub.sumArray(new String[] { "123", "456", "987" }));
334 /*CallBackInterface cb1 = new CallBack(23);
335 CallBackInterface cb2 = new CallBack(33);
336 CallBackInterface cb3 = new CallBack(43);
337 CallBackInterface[] cb = { cb1, cb2, cb3 };
338 tcstub.registerCallback(cb);
339 System.out.println("Return value from callback: " + tcstub.callBack());
340 CallBackInterface cb4 = new CallBack(10);
341 CallBackInterface cb5 = new CallBack(11);
342 CallBackInterface cb6 = new CallBack(12);
343 CallBackInterface[] cbt = { cb4, cb5, cb6 };
344 tcstub.registerCallback(cbt);
345 System.out.println("Return value from callback: " + tcstub.callBack());*/
347 StructJ[] data = new StructJ[2];
348 for (int i=0; i<2; i++) {
349 data[i] = new StructJ();
351 data[0].name = "Rahmadi";
352 data[0].value = 0.123f;
354 //data[1].name = "Trimananda";
355 //data[1].value = 0.223f;
356 //data[1].year = 2017;
358 for (StructJ str : data) {
359 System.out.println("Name: " + str.name);
360 System.out.println("Value: " + str.value);
361 System.out.println("Year: " + str.year);
363 StructJ[] strj = tcstub.handleStruct(data);
364 for (StructJ str : strj) {
365 System.out.println("Name: " + str.name);
366 System.out.println("Value: " + str.value);
367 System.out.println("Year: " + str.year);
371 EnumJ[] en = { EnumJ.APPLE, EnumJ.ORANGE, EnumJ.APPLE, EnumJ.GRAPE };
372 EnumJ[] res = tcstub.handleEnum(en);
373 System.out.println("Enum members: " + Arrays.toString(res));