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
34 public TestClass_Stub(int _port, String _address, int _rev, int[] _ports) throws Exception {
38 rmiCall = new IoTRMICall(_port, _address, _rev);
39 listCBObj = new ArrayList<CallBackInterface>();
45 * Instantiation of callback objects
47 public static int numCallbackObjects() {
49 return NUM_CB_OBJ; // Generated by the IoTCompiler
53 // Initialize callback
54 public void ___initCallBack() {
56 Thread thread = new Thread() {
59 System.out.println("Created server with port: " + ports[0]);
60 rmiObj = new IoTRMIObject(ports[0]);
63 byte[] method = rmiObj.getMethodBytes();
64 int objId = IoTRMIObject.getObjectId(method);
65 CallBack_CBSkeleton skel = (CallBack_CBSkeleton) listCBObj.get(objId);
67 //rmiObj.setMethodBytes(method);
68 skel.invokeMethod(rmiObj);
71 } catch (Exception ex){
73 throw new Error("Error instantiating class CallBack_Skeleton!");
79 //String sign = "registercallback"; // can be any string
81 Class<?> retType = void.class;
83 Class<?>[] paramCls = new Class<?>[] { int.class, String.class, int.class };
84 Object[] paramObj = new Object[] { ports[0], address, 0 };
85 rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
89 // Single callback handling
90 public void registerCallback(CallBackInterface _cb) {
92 /*Thread thread = new Thread() {
95 CallBack_Skeleton cbskel = new CallBack_Skeleton(_cb, ports[0]);
96 } catch (Exception ex){
98 throw new Error("Error instantiating class CallBack_Skeleton!");
104 String sign = "voidregisterCallBack(CallBackInterface)";
105 Class<?> retType = void.class;
106 // port, address, and 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);*/
113 public void registerCallback(CallBackInterface[] _cb) {
116 //for (int objId = 0; objId < _cb.length; objId++) {
117 for (CallBackInterface cb : _cb) {
118 CallBack_CBSkeleton skel = new CallBack_CBSkeleton(cb, objIdCnt++);
121 } catch (Exception ex){
122 ex.printStackTrace();
123 throw new Error("Class not found / instantiation / illegal access / IO error!");
126 //String sign = "voidregisterCallBack(CallBackInterface[])";
128 Class<?> retType = void.class;
129 // port, address, rev, and number of objects
130 Class<?>[] paramCls = new Class<?>[] { int.class };
131 Object[] paramObj = new Object[] { _cb.length };
132 //rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
133 rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
137 public void setA(int _int) {
139 //String sign = "voidsetA(int)";
141 Class<?> retType = void.class;
142 Class<?>[] paramCls = new Class<?>[] { int.class };
143 Object[] paramObj = new Object[] { _int };
144 rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
148 public void setB(float _float) {
151 Class<?> retType = void.class;
152 Class<?>[] paramCls = new Class<?>[] { float.class };
153 Object[] paramObj = new Object[] { _float };
154 rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
158 public void setC(String _string) {
161 Class<?> retType = void.class;
162 Class<?>[] paramCls = new Class<?>[] { String.class };
163 Object[] paramObj = new Object[] { _string };
164 rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
169 public String sumArray(String[] newA) {
172 Class<?> retType = String.class;
173 Class<?>[] paramCls = new Class<?>[] { String[].class };
174 Object[] paramObj = new Object[] { newA };
175 Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
176 return (String)retObj;
180 public int setAndGetA(int newA) {
182 Class<?> retType = int.class;
183 Class<?>[] paramCls = new Class<?>[] { int.class };
184 Object[] paramObj = new Object[] { newA };
185 Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
190 public int setACAndGetA(String newC, int newA) {
193 Class<?> retType = int.class;
194 Class<?>[] paramCls = new Class<?>[] { String.class, int.class };
195 Object[] paramObj = new Object[] { newC, newA };
196 Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
201 public int callBack() {
204 Class<?> retType = int.class;
205 Class<?>[] paramCls = new Class<?>[] { };
206 Object[] paramObj = new Object[] { };
207 Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
213 public StructJ[] handleStruct(StructJ[] data) {
216 Class<?> retType = void.class;
217 Class<?>[] paramCls = new Class<?>[] { int.class };
218 Object[] paramObj = new Object[] { data.length };
219 rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
222 Class<?> retType2 = int.class; // return type is integer if it is a struct!!!
223 // Calculate the size of the array
224 Class<?>[] paramCls2 = new Class<?>[3*data.length];
225 Object[] paramObj2 = new Object[3*data.length];
226 // Handle with for loop
228 for(int i = 0; i < data.length; i++) {
229 paramCls2[pos] = String.class;
230 paramObj2[pos++] = data[i].name;
231 paramCls2[pos] = float.class;
232 paramObj2[pos++] = data[i].value;
233 paramCls2[pos] = int.class;
234 paramObj2[pos++] = data[i].year;
236 System.out.println(Arrays.toString(paramObj2));
237 Object retObj = rmiCall.remoteCall(objectId, methodId2, retType2, null, paramCls2, paramObj2);
239 // Get the length of the struct first
240 int structsize1 = (int) retObj;
241 // Construct the struct
242 Class<?>[] retCls = new Class<?>[3*structsize1];
243 Class<?>[] retClsVal = new Class<?>[3*structsize1];
245 for(int i=0; i < structsize1; i++) {
246 retCls[pos] = String.class;
247 retClsVal[pos++] = null;
248 retCls[pos] = float.class;
249 retClsVal[pos++] = null;
250 retCls[pos] = int.class;
251 retClsVal[pos++] = null;
253 Object[] retObj2 = rmiCall.getStructObjects(retCls,
255 StructJ[] dataRet = new StructJ[structsize1];
256 for (int i=0; i < structsize1; i++) {
257 dataRet[i] = new StructJ();
260 for(int i=0; i < structsize1; i++) {
261 dataRet[i].name = (String) retObj2[pos++];
262 dataRet[i].value = (float) retObj2[pos++];
263 dataRet[i].year = (int) retObj2[pos++];
270 public EnumJ[] handleEnum(EnumJ[] en) {
273 Class<?> retType = int[].class;
274 // Handle with for loop
275 int paramInt[] = new int[en.length];
276 for(int i = 0; i < en.length; i++) {
277 paramInt[i] = en[i].ordinal();
280 Class<?>[] paramCls = new Class<?>[] { int[].class };
281 Object[] paramObj = new Object[] { paramInt };
283 // if no return value just
284 // rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
285 // This is with return value:
287 rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
288 int[] retEnumInt = (int[]) retObj;
289 int enumsize1 = retEnumInt.length;
291 EnumJ[] enumJ = EnumJ.values();
292 EnumJ[] enRetVal = new EnumJ[enumsize1];
293 for(int i = 0; i < enumsize1; i++) {
294 enRetVal[i] = enumJ[retEnumInt[i]];
300 public static void main(String[] args) throws Exception {
302 CommunicationHandler comHan = new CommunicationHandler(true);
303 int numOfPorts = TestClass_Stub.numCallbackObjects();
304 int[] ports = comHan.getCallbackPorts(numOfPorts);
307 String address = "localhost";
310 System.out.println("Allocated ports: " + Arrays.toString(ports));
312 TestClass_Stub tcstub = new TestClass_Stub(port, address, rev, ports);
313 System.out.println("Return value: " + tcstub.setAndGetA(123));
314 System.out.println("Return value: " + tcstub.setACAndGetA("string", 123));
315 System.out.println("Return value: " + tcstub.sumArray(new String[] { "123", "456", "987" }));
317 /*CallBackInterface cb1 = new CallBack(23);
318 CallBackInterface cb2 = new CallBack(33);
319 CallBackInterface cb3 = new CallBack(43);
320 CallBackInterface[] cb = { cb1, cb2, cb3 };
321 tcstub.registerCallback(cb);
322 System.out.println("Return value from callback: " + tcstub.callBack());
323 CallBackInterface cb4 = new CallBack(10);
324 CallBackInterface cb5 = new CallBack(11);
325 CallBackInterface cb6 = new CallBack(12);
326 CallBackInterface[] cbt = { cb4, cb5, cb6 };
327 tcstub.registerCallback(cbt);
328 System.out.println("Return value from callback: " + tcstub.callBack());*/
330 StructJ[] data = new StructJ[2];
331 for (int i=0; i<2; i++) {
332 data[i] = new StructJ();
334 data[0].name = "Rahmadi";
335 data[0].value = 0.123f;
337 //data[1].name = "Trimananda";
338 //data[1].value = 0.223f;
339 //data[1].year = 2017;
341 for (StructJ str : data) {
342 System.out.println("Name: " + str.name);
343 System.out.println("Value: " + str.value);
344 System.out.println("Year: " + str.year);
346 StructJ[] strj = tcstub.handleStruct(data);
347 for (StructJ str : strj) {
348 System.out.println("Name: " + str.name);
349 System.out.println("Value: " + str.value);
350 System.out.println("Year: " + str.year);
354 EnumJ[] en = { EnumJ.APPLE, EnumJ.ORANGE, EnumJ.APPLE, EnumJ.GRAPE };
355 EnumJ[] res = tcstub.handleEnum(en);
356 System.out.println("Enum members: " + Arrays.toString(res));