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 rmiObj = new IoTRMIObject(ports[0]);
73 byte[] method = rmiObj.getMethodBytes();
74 // Permission checking
75 int methId = IoTRMIObject.getMethodId(method);
76 if (!set0Allowed.contains(methId))
77 throw new Error("CallBack_CBSkeleton: This object is not allowed to access method " + methId);
79 int objId = IoTRMIObject.getObjectId(method);
80 CallBack_CBSkeleton skel = (CallBack_CBSkeleton) listCBObj.get(objId);
82 //rmiObj.setMethodBytes(method);
83 skel.invokeMethod(rmiObj);
86 } catch (Exception ex){
88 throw new Error("Error instantiating class CallBack_Skeleton!");
94 //String sign = "registercallback"; // can be any string
96 Class<?> retType = void.class;
98 Class<?>[] paramCls = new Class<?>[] { int.class, String.class, int.class };
99 Object[] paramObj = new Object[] { ports[0], address, 0 };
100 rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
104 // Single callback handling
105 public void registerCallback(CallBackInterface _cb) {
107 /*Thread thread = new Thread() {
110 CallBack_Skeleton cbskel = new CallBack_Skeleton(_cb, ports[0]);
111 } catch (Exception ex){
112 ex.printStackTrace();
113 throw new Error("Error instantiating class CallBack_Skeleton!");
119 String sign = "voidregisterCallBack(CallBackInterface)";
120 Class<?> retType = void.class;
121 // port, address, and rev
122 Class<?>[] paramCls = new Class<?>[] { int.class, String.class, int.class };
123 Object[] paramObj = new Object[] { ports[0], address, 0 };
124 rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);*/
128 public void registerCallback(CallBackInterface[] _cb) {
131 //for (int objId = 0; objId < _cb.length; objId++) {
132 for (CallBackInterface cb : _cb) {
133 CallBack_CBSkeleton skel = new CallBack_CBSkeleton(cb, objIdCnt++);
136 } catch (Exception ex){
137 ex.printStackTrace();
138 throw new Error("Class not found / instantiation / illegal access / IO error!");
141 //String sign = "voidregisterCallBack(CallBackInterface[])";
143 Class<?> retType = void.class;
144 // port, address, rev, and number of objects
145 Class<?>[] paramCls = new Class<?>[] { int.class };
146 Object[] paramObj = new Object[] { _cb.length };
147 //rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
148 rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
152 public void setA(int _int) {
154 //String sign = "voidsetA(int)";
156 Class<?> retType = void.class;
157 Class<?>[] paramCls = new Class<?>[] { int.class };
158 Object[] paramObj = new Object[] { _int };
159 rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
163 public void setB(float _float) {
166 Class<?> retType = void.class;
167 Class<?>[] paramCls = new Class<?>[] { float.class };
168 Object[] paramObj = new Object[] { _float };
169 rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
173 public void setC(String _string) {
176 Class<?> retType = void.class;
177 Class<?>[] paramCls = new Class<?>[] { String.class };
178 Object[] paramObj = new Object[] { _string };
179 rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
184 public String sumArray(String[] newA) {
187 Class<?> retType = String.class;
188 Class<?>[] paramCls = new Class<?>[] { String[].class };
189 Object[] paramObj = new Object[] { newA };
190 Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
191 return (String)retObj;
195 public int setAndGetA(int newA) {
197 Class<?> retType = int.class;
198 Class<?>[] paramCls = new Class<?>[] { int.class };
199 Object[] paramObj = new Object[] { newA };
200 Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
205 public int setACAndGetA(String newC, int newA) {
208 Class<?> retType = int.class;
209 Class<?>[] paramCls = new Class<?>[] { String.class, int.class };
210 Object[] paramObj = new Object[] { newC, newA };
211 Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
216 public int callBack() {
219 Class<?> retType = int.class;
220 Class<?>[] paramCls = new Class<?>[] { };
221 Object[] paramObj = new Object[] { };
222 Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
228 public StructJ[] handleStruct(StructJ[] data) {
231 Class<?> retType = void.class;
232 Class<?>[] paramCls = new Class<?>[] { int.class };
233 Object[] paramObj = new Object[] { data.length };
234 rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
237 Class<?> retType2 = int.class; // return type is integer if it is a struct!!!
238 // Calculate the size of the array
239 Class<?>[] paramCls2 = new Class<?>[3*data.length];
240 Object[] paramObj2 = new Object[3*data.length];
241 // Handle with for loop
243 for(int i = 0; i < data.length; i++) {
244 paramCls2[pos] = String.class;
245 paramObj2[pos++] = data[i].name;
246 paramCls2[pos] = float.class;
247 paramObj2[pos++] = data[i].value;
248 paramCls2[pos] = int.class;
249 paramObj2[pos++] = data[i].year;
251 //System.out.println(Arrays.toString(paramObj2));
252 Object retObj = rmiCall.remoteCall(objectId, methodId2, retType2, null, paramCls2, paramObj2);
254 // Get the length of the struct first
255 int structsize1 = (int) retObj;
256 // Construct the struct
257 Class<?>[] retCls = new Class<?>[3*structsize1];
258 Class<?>[] retClsVal = new Class<?>[3*structsize1];
260 for(int i=0; i < structsize1; i++) {
261 retCls[pos] = String.class;
262 retClsVal[pos++] = null;
263 retCls[pos] = float.class;
264 retClsVal[pos++] = null;
265 retCls[pos] = int.class;
266 retClsVal[pos++] = null;
268 Object[] retObj2 = rmiCall.getStructObjects(retCls,
270 StructJ[] dataRet = new StructJ[structsize1];
271 for (int i=0; i < structsize1; i++) {
272 dataRet[i] = new StructJ();
275 for(int i=0; i < structsize1; i++) {
276 dataRet[i].name = (String) retObj2[pos++];
277 dataRet[i].value = (float) retObj2[pos++];
278 dataRet[i].year = (int) retObj2[pos++];
285 public EnumJ[] handleEnum(EnumJ[] en) {
288 Class<?> retType = int[].class;
289 // Handle with for loop
290 int paramInt[] = new int[en.length];
291 for(int i = 0; i < en.length; i++) {
292 paramInt[i] = en[i].ordinal();
295 Class<?>[] paramCls = new Class<?>[] { int[].class };
296 Object[] paramObj = new Object[] { paramInt };
298 // if no return value just
299 // rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
300 // This is with return value:
302 rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
303 int[] retEnumInt = (int[]) retObj;
304 int enumsize1 = retEnumInt.length;
306 EnumJ[] enumJ = EnumJ.values();
307 EnumJ[] enRetVal = new EnumJ[enumsize1];
308 for(int i = 0; i < enumsize1; i++) {
309 enRetVal[i] = enumJ[retEnumInt[i]];
315 public static void main(String[] args) throws Exception {
317 CommunicationHandler comHan = new CommunicationHandler(true);
318 int numOfPorts = TestClass_Stub.numCallbackObjects();
319 int[] ports = comHan.getCallbackPorts(numOfPorts);
322 String address = "localhost";
325 System.out.println("Allocated ports: " + Arrays.toString(ports));
327 TestClass_Stub tcstub = new TestClass_Stub(port, address, rev, ports);
328 System.out.println("Return value: " + tcstub.setAndGetA(123));
329 System.out.println("Return value: " + tcstub.setACAndGetA("string", 123));
330 System.out.println("Return value: " + tcstub.sumArray(new String[] { "123", "456", "987" }));
332 /*CallBackInterface cb1 = new CallBack(23);
333 CallBackInterface cb2 = new CallBack(33);
334 CallBackInterface cb3 = new CallBack(43);
335 CallBackInterface[] cb = { cb1, cb2, cb3 };
336 tcstub.registerCallback(cb);
337 System.out.println("Return value from callback: " + tcstub.callBack());
338 CallBackInterface cb4 = new CallBack(10);
339 CallBackInterface cb5 = new CallBack(11);
340 CallBackInterface cb6 = new CallBack(12);
341 CallBackInterface[] cbt = { cb4, cb5, cb6 };
342 tcstub.registerCallback(cbt);
343 System.out.println("Return value from callback: " + tcstub.callBack());*/
345 StructJ[] data = new StructJ[2];
346 for (int i=0; i<2; i++) {
347 data[i] = new StructJ();
349 data[0].name = "Rahmadi";
350 data[0].value = 0.123f;
352 //data[1].name = "Trimananda";
353 //data[1].value = 0.223f;
354 //data[1].year = 2017;
356 for (StructJ str : data) {
357 System.out.println("Name: " + str.name);
358 System.out.println("Value: " + str.value);
359 System.out.println("Year: " + str.year);
361 StructJ[] strj = tcstub.handleStruct(data);
362 for (StructJ str : strj) {
363 System.out.println("Name: " + str.name);
364 System.out.println("Value: " + str.value);
365 System.out.println("Year: " + str.year);
369 EnumJ[] en = { EnumJ.APPLE, EnumJ.ORANGE, EnumJ.APPLE, EnumJ.GRAPE };
370 EnumJ[] res = tcstub.handleEnum(en);
371 System.out.println("Enum members: " + Arrays.toString(res));