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
30 * Properties and constants for Callbacks!
32 private List<CallBackInterface> listCBObj;
33 private final static int NUM_CB_OBJ = 1;
34 private static int objIdCnt = 0; // Counter for callback object Ids
40 public TestClass_Stub(int _port, String _address, int _rev, int[] _ports) throws Exception {
44 rmiCall = new IoTRMICall(_port, _address, _rev);
46 // Only for callbacks!!!
47 listCBObj = new ArrayList<CallBackInterface>();
53 * Instantiation of callback objects
55 public static int numCallbackObjects() {
57 return NUM_CB_OBJ; // Generated by the IoTCompiler
61 // Initialize callback
62 public void ___initCallBack() {
64 Thread thread = new Thread() {
67 System.out.println("Created server with port: " + ports[0]);
68 rmiObj = new IoTRMIObject(ports[0]);
71 byte[] method = rmiObj.getMethodBytes();
72 int objId = IoTRMIObject.getObjectId(method);
73 CallBack_CBSkeleton skel = (CallBack_CBSkeleton) listCBObj.get(objId);
75 //rmiObj.setMethodBytes(method);
76 skel.invokeMethod(rmiObj);
79 } catch (Exception ex){
81 throw new Error("Error instantiating class CallBack_Skeleton!");
87 //String sign = "registercallback"; // can be any string
89 Class<?> retType = void.class;
91 Class<?>[] paramCls = new Class<?>[] { int.class, String.class, int.class };
92 Object[] paramObj = new Object[] { ports[0], address, 0 };
93 rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
97 // Single callback handling
98 public void registerCallback(CallBackInterface _cb) {
100 /*Thread thread = new Thread() {
103 CallBack_Skeleton cbskel = new CallBack_Skeleton(_cb, ports[0]);
104 } catch (Exception ex){
105 ex.printStackTrace();
106 throw new Error("Error instantiating class CallBack_Skeleton!");
112 String sign = "voidregisterCallBack(CallBackInterface)";
113 Class<?> retType = void.class;
114 // port, address, and rev
115 Class<?>[] paramCls = new Class<?>[] { int.class, String.class, int.class };
116 Object[] paramObj = new Object[] { ports[0], address, 0 };
117 rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);*/
121 public void registerCallback(CallBackInterface[] _cb) {
124 //for (int objId = 0; objId < _cb.length; objId++) {
125 for (CallBackInterface cb : _cb) {
126 CallBack_CBSkeleton skel = new CallBack_CBSkeleton(cb, objIdCnt++);
129 } catch (Exception ex){
130 ex.printStackTrace();
131 throw new Error("Class not found / instantiation / illegal access / IO error!");
134 //String sign = "voidregisterCallBack(CallBackInterface[])";
136 Class<?> retType = void.class;
137 // port, address, rev, and number of objects
138 Class<?>[] paramCls = new Class<?>[] { int.class };
139 Object[] paramObj = new Object[] { _cb.length };
140 //rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
141 rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
145 public void setA(int _int) {
147 //String sign = "voidsetA(int)";
149 Class<?> retType = void.class;
150 Class<?>[] paramCls = new Class<?>[] { int.class };
151 Object[] paramObj = new Object[] { _int };
152 rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
156 public void setB(float _float) {
159 Class<?> retType = void.class;
160 Class<?>[] paramCls = new Class<?>[] { float.class };
161 Object[] paramObj = new Object[] { _float };
162 rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
166 public void setC(String _string) {
169 Class<?> retType = void.class;
170 Class<?>[] paramCls = new Class<?>[] { String.class };
171 Object[] paramObj = new Object[] { _string };
172 rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
177 public String sumArray(String[] newA) {
180 Class<?> retType = String.class;
181 Class<?>[] paramCls = new Class<?>[] { String[].class };
182 Object[] paramObj = new Object[] { newA };
183 Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
184 return (String)retObj;
188 public int setAndGetA(int newA) {
190 Class<?> retType = int.class;
191 Class<?>[] paramCls = new Class<?>[] { int.class };
192 Object[] paramObj = new Object[] { newA };
193 Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
198 public int setACAndGetA(String newC, int newA) {
201 Class<?> retType = int.class;
202 Class<?>[] paramCls = new Class<?>[] { String.class, int.class };
203 Object[] paramObj = new Object[] { newC, newA };
204 Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
209 public int callBack() {
212 Class<?> retType = int.class;
213 Class<?>[] paramCls = new Class<?>[] { };
214 Object[] paramObj = new Object[] { };
215 Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
221 public StructJ[] handleStruct(StructJ[] data) {
224 Class<?> retType = void.class;
225 Class<?>[] paramCls = new Class<?>[] { int.class };
226 Object[] paramObj = new Object[] { data.length };
227 rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
230 Class<?> retType2 = int.class; // return type is integer if it is a struct!!!
231 // Calculate the size of the array
232 Class<?>[] paramCls2 = new Class<?>[3*data.length];
233 Object[] paramObj2 = new Object[3*data.length];
234 // Handle with for loop
236 for(int i = 0; i < data.length; i++) {
237 paramCls2[pos] = String.class;
238 paramObj2[pos++] = data[i].name;
239 paramCls2[pos] = float.class;
240 paramObj2[pos++] = data[i].value;
241 paramCls2[pos] = int.class;
242 paramObj2[pos++] = data[i].year;
244 System.out.println(Arrays.toString(paramObj2));
245 Object retObj = rmiCall.remoteCall(objectId, methodId2, retType2, null, paramCls2, paramObj2);
247 // Get the length of the struct first
248 int structsize1 = (int) retObj;
249 // Construct the struct
250 Class<?>[] retCls = new Class<?>[3*structsize1];
251 Class<?>[] retClsVal = new Class<?>[3*structsize1];
253 for(int i=0; i < structsize1; i++) {
254 retCls[pos] = String.class;
255 retClsVal[pos++] = null;
256 retCls[pos] = float.class;
257 retClsVal[pos++] = null;
258 retCls[pos] = int.class;
259 retClsVal[pos++] = null;
261 Object[] retObj2 = rmiCall.getStructObjects(retCls,
263 StructJ[] dataRet = new StructJ[structsize1];
264 for (int i=0; i < structsize1; i++) {
265 dataRet[i] = new StructJ();
268 for(int i=0; i < structsize1; i++) {
269 dataRet[i].name = (String) retObj2[pos++];
270 dataRet[i].value = (float) retObj2[pos++];
271 dataRet[i].year = (int) retObj2[pos++];
278 public EnumJ[] handleEnum(EnumJ[] en) {
281 Class<?> retType = int[].class;
282 // Handle with for loop
283 int paramInt[] = new int[en.length];
284 for(int i = 0; i < en.length; i++) {
285 paramInt[i] = en[i].ordinal();
288 Class<?>[] paramCls = new Class<?>[] { int[].class };
289 Object[] paramObj = new Object[] { paramInt };
291 // if no return value just
292 // rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
293 // This is with return value:
295 rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
296 int[] retEnumInt = (int[]) retObj;
297 int enumsize1 = retEnumInt.length;
299 EnumJ[] enumJ = EnumJ.values();
300 EnumJ[] enRetVal = new EnumJ[enumsize1];
301 for(int i = 0; i < enumsize1; i++) {
302 enRetVal[i] = enumJ[retEnumInt[i]];
308 public static void main(String[] args) throws Exception {
310 CommunicationHandler comHan = new CommunicationHandler(true);
311 int numOfPorts = TestClass_Stub.numCallbackObjects();
312 int[] ports = comHan.getCallbackPorts(numOfPorts);
315 String address = "localhost";
318 System.out.println("Allocated ports: " + Arrays.toString(ports));
320 TestClass_Stub tcstub = new TestClass_Stub(port, address, rev, ports);
321 System.out.println("Return value: " + tcstub.setAndGetA(123));
322 System.out.println("Return value: " + tcstub.setACAndGetA("string", 123));
323 System.out.println("Return value: " + tcstub.sumArray(new String[] { "123", "456", "987" }));
325 /*CallBackInterface cb1 = new CallBack(23);
326 CallBackInterface cb2 = new CallBack(33);
327 CallBackInterface cb3 = new CallBack(43);
328 CallBackInterface[] cb = { cb1, cb2, cb3 };
329 tcstub.registerCallback(cb);
330 System.out.println("Return value from callback: " + tcstub.callBack());
331 CallBackInterface cb4 = new CallBack(10);
332 CallBackInterface cb5 = new CallBack(11);
333 CallBackInterface cb6 = new CallBack(12);
334 CallBackInterface[] cbt = { cb4, cb5, cb6 };
335 tcstub.registerCallback(cbt);
336 System.out.println("Return value from callback: " + tcstub.callBack());*/
338 StructJ[] data = new StructJ[2];
339 for (int i=0; i<2; i++) {
340 data[i] = new StructJ();
342 data[0].name = "Rahmadi";
343 data[0].value = 0.123f;
345 //data[1].name = "Trimananda";
346 //data[1].value = 0.223f;
347 //data[1].year = 2017;
349 for (StructJ str : data) {
350 System.out.println("Name: " + str.name);
351 System.out.println("Value: " + str.value);
352 System.out.println("Year: " + str.year);
354 StructJ[] strj = tcstub.handleStruct(data);
355 for (StructJ str : strj) {
356 System.out.println("Name: " + str.name);
357 System.out.println("Value: " + str.value);
358 System.out.println("Year: " + str.year);
362 EnumJ[] en = { EnumJ.APPLE, EnumJ.ORANGE, EnumJ.APPLE, EnumJ.GRAPE };
363 EnumJ[] res = tcstub.handleEnum(en);
364 System.out.println("Enum members: " + Arrays.toString(res));