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, methodSignatures);
39 rmiCall = new IoTRMICall(_port, _address, _rev);
40 listCBObj = new ArrayList<CallBackInterface>();
46 * Instantiation of callback objects
48 public static int numCallbackObjects() {
50 return NUM_CB_OBJ; // Generated by the IoTCompiler
54 // Initialize callback
55 public void ___initCallBack() {
57 Thread thread = new Thread() {
60 //String[] methodSignatures = CallBack_CBSkeleton.getMethodSignatures();
61 //rmiObj = new IoTRMIObject(ports[0], methodSignatures);
62 rmiObj = new IoTRMIObject(ports[0]);
65 byte[] method = rmiObj.getMethodBytes();
66 int objId = IoTRMIObject.getObjectId(method);
67 CallBack_CBSkeleton skel = (CallBack_CBSkeleton) listCBObj.get(objId);
69 //rmiObj.setMethodBytes(method);
70 skel.invokeMethod(rmiObj);
73 } catch (Exception ex){
75 throw new Error("Error instantiating class CallBack_Skeleton!");
81 //String sign = "registercallback"; // can be any string
83 Class<?> retType = void.class;
85 Class<?>[] paramCls = new Class<?>[] { int.class, String.class, int.class };
86 Object[] paramObj = new Object[] { ports[0], address, 0 };
87 //rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
88 rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
92 // Single callback handling
93 public void registerCallback(CallBackInterface _cb) {
95 /*Thread thread = new Thread() {
98 CallBack_Skeleton cbskel = new CallBack_Skeleton(_cb, ports[0]);
99 } catch (Exception ex){
100 ex.printStackTrace();
101 throw new Error("Error instantiating class CallBack_Skeleton!");
107 String sign = "voidregisterCallBack(CallBackInterface)";
108 Class<?> retType = void.class;
109 // port, address, and rev
110 Class<?>[] paramCls = new Class<?>[] { int.class, String.class, int.class };
111 Object[] paramObj = new Object[] { ports[0], address, 0 };
112 rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);*/
116 public void registerCallback(CallBackInterface[] _cb) {
119 //for (int objId = 0; objId < _cb.length; objId++) {
120 for (CallBackInterface cb : _cb) {
121 CallBack_CBSkeleton skel = new CallBack_CBSkeleton(cb, objIdCnt++);
124 } catch (Exception ex){
125 ex.printStackTrace();
126 throw new Error("Class not found / instantiation / illegal access / IO error!");
129 //String sign = "voidregisterCallBack(CallBackInterface[])";
131 Class<?> retType = void.class;
132 // port, address, rev, and number of objects
133 Class<?>[] paramCls = new Class<?>[] { int.class };
134 Object[] paramObj = new Object[] { _cb.length };
135 //rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
136 rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
140 public void setA(int _int) {
142 //String sign = "voidsetA(int)";
144 Class<?> retType = void.class;
145 Class<?>[] paramCls = new Class<?>[] { int.class };
146 Object[] paramObj = new Object[] { _int };
147 rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
151 public void setB(float _float) {
154 Class<?> retType = void.class;
155 Class<?>[] paramCls = new Class<?>[] { float.class };
156 Object[] paramObj = new Object[] { _float };
157 rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
161 public void setC(String _string) {
164 Class<?> retType = void.class;
165 Class<?>[] paramCls = new Class<?>[] { String.class };
166 Object[] paramObj = new Object[] { _string };
167 rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
172 public String sumArray(String[] newA) {
175 Class<?> retType = String.class;
176 Class<?>[] paramCls = new Class<?>[] { String[].class };
177 Object[] paramObj = new Object[] { newA };
178 Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
179 return (String)retObj;
183 public int setAndGetA(int newA) {
185 Class<?> retType = int.class;
186 Class<?>[] paramCls = new Class<?>[] { int.class };
187 Object[] paramObj = new Object[] { newA };
188 Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
193 public int setACAndGetA(String newC, int newA) {
196 Class<?> retType = int.class;
197 Class<?>[] paramCls = new Class<?>[] { String.class, int.class };
198 Object[] paramObj = new Object[] { newC, newA };
199 Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
204 public int callBack() {
207 Class<?> retType = int.class;
208 Class<?>[] paramCls = new Class<?>[] { };
209 Object[] paramObj = new Object[] { };
210 Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
216 public void handleStruct(StructJ[] data) {
219 Class<?> retType = void.class;
220 Class<?>[] paramCls = new Class<?>[] { int.class };
221 Object[] paramObj = new Object[] { data.length };
222 rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
225 Class<?> retType2 = void.class;
226 // Calculate the size of the array
227 Class<?>[] paramCls2 = new Class<?>[3*data.length];
228 Object[] paramObj2 = new Object[3*data.length];
229 // Handle with for loop
231 for(int i = 0; i < data.length; i++) {
232 paramCls2[pos] = String.class;
233 paramObj2[pos++] = data[i].name;
234 paramCls2[pos] = float.class;
235 paramObj2[pos++] = data[i].value;
236 paramCls2[pos] = int.class;
237 paramObj2[pos++] = data[i].year;
239 System.out.println(Arrays.toString(paramObj2));
240 rmiCall.remoteCall(objectId, methodId2, retType2, null, paramCls2, paramObj2);
244 public void handleEnum(EnumJ[] en) {
246 // Send length info first
248 Class<?> retType = void.class;
249 Class<?>[] paramCls = new Class<?>[] { int.class };
250 Object[] paramObj = new Object[] { en.length };
251 rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
254 Class<?> retType2 = void.class;
255 // Calculate the size of the array
256 Class<?>[] paramCls2 = new Class<?>[en.length];
257 Object[] paramObj2 = new Object[en.length];
258 // Handle with for loop
259 for(int i = 0; i < en.length; i++) {
260 paramCls2[i] = int.class;
261 paramObj2[i] = en[i].ordinal();
263 System.out.println(Arrays.toString(paramObj2));
264 rmiCall.remoteCall(objectId, methodId2, retType2, null, paramCls2, paramObj2);
268 public static void main(String[] args) throws Exception {
270 CommunicationHandler comHan = new CommunicationHandler(true);
271 int numOfPorts = TestClass_Stub.numCallbackObjects();
272 int[] ports = comHan.getCallbackPorts(numOfPorts);
275 String address = "localhost";
278 System.out.println("Allocated ports: " + Arrays.toString(ports));
280 TestClass_Stub tcstub = new TestClass_Stub(port, address, rev, ports);
281 System.out.println("Return value: " + tcstub.setAndGetA(123));
282 System.out.println("Return value: " + tcstub.setACAndGetA("string", 123));
283 System.out.println("Return value: " + tcstub.sumArray(new String[] { "123", "456", "987" }));
285 CallBackInterface cb1 = new CallBack(23);
286 CallBackInterface cb2 = new CallBack(33);
287 CallBackInterface cb3 = new CallBack(43);
288 CallBackInterface[] cb = { cb1, cb2, cb3 };
289 tcstub.registerCallback(cb);
290 System.out.println("Return value from callback: " + tcstub.callBack());
291 CallBackInterface cb4 = new CallBack(10);
292 CallBackInterface cb5 = new CallBack(11);
293 CallBackInterface cb6 = new CallBack(12);
294 CallBackInterface[] cbt = { cb4, cb5, cb6 };
295 tcstub.registerCallback(cbt);
296 System.out.println("Return value from callback: " + tcstub.callBack());
298 StructJ[] data = new StructJ[2];
299 for (int i=0; i<2; i++) {
300 data[i] = new StructJ();
302 data[0].name = "Rahmadi";
303 data[0].value = 0.123f;
305 //data[1].name = "Trimananda";
306 //data[1].value = 0.223f;
307 //data[1].year = 2017;
309 for (StructJ str : data) {
310 System.out.println("Name: " + str.name);
311 System.out.println("Value: " + str.value);
312 System.out.println("Year: " + str.year);
314 tcstub.handleStruct(data);
316 EnumJ[] en = { EnumJ.APPLE, EnumJ.ORANGE, EnumJ.APPLE, EnumJ.GRAPE };
317 tcstub.handleEnum(en);