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
30 private final static String[] methodSignatures = {
37 "intsetACAndGetA(string,int)",
39 "voidregisterCallBack(CallBackInterface)",
40 "voidregisterCallBack(CallBackInterface[])",
42 "handleStruct(StructJ[])",
49 public TestClass_Stub(int _port, String _address, int _rev, int[] _ports) throws Exception {
53 rmiCall = new IoTRMICall(_port, _address, _rev, methodSignatures);
54 listCBObj = new ArrayList<CallBackInterface>();
60 * Instantiation of callback objects
62 public static int numCallbackObjects() {
64 return NUM_CB_OBJ; // Generated by the IoTCompiler
68 // Return method signatures
69 public static String[] getMethodSignatures() {
71 return methodSignatures;
75 // Initialize callback
76 public void init_CallBack() {
78 Thread thread = new Thread() {
81 String[] methodSignatures = CallBack_CBSkeleton.getMethodSignatures();
82 rmiObj = new IoTRMIObject(ports[0], methodSignatures);
85 byte[] method = rmiObj.getMethodBytes();
86 int objId = IoTRMIObject.getObjectId(method);
87 CallBack_CBSkeleton skel = (CallBack_CBSkeleton) listCBObj.get(objId);
89 rmiObj.setMethodBytes(method);
90 retObj = skel.invokeMethod(rmiObj);
93 rmiObj.sendReturnObj(retObj);
96 } catch (Exception ex){
98 throw new Error("Error instantiating class CallBack_Skeleton!");
104 String sign = "registercallback"; // can be any string
105 Class<?> retType = void.class;
106 // port, address, 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 // Single callback handling
114 public void registerCallback(CallBackInterface _cb) {
116 Thread thread = new Thread() {
119 CallBack_Skeleton cbskel = new CallBack_Skeleton(_cb, ports[0]);
120 cbskel.waitRequestInvokeMethod();
121 } catch (Exception ex){
122 ex.printStackTrace();
123 throw new Error("Error instantiating class CallBack_Skeleton!");
129 String sign = "voidregisterCallBack(CallBackInterface)";
130 Class<?> retType = void.class;
131 // port, address, and rev
132 Class<?>[] paramCls = new Class<?>[] { int.class, String.class, int.class };
133 Object[] paramObj = new Object[] { ports[0], address, 0 };
134 rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
138 public void registerCallback(CallBackInterface[] _cb) {
141 //for (int objId = 0; objId < _cb.length; objId++) {
142 for (CallBackInterface cb : _cb) {
143 CallBack_CBSkeleton skel = new CallBack_CBSkeleton(cb, objIdCnt++);
146 } catch (Exception ex){
147 ex.printStackTrace();
148 throw new Error("Class not found / instantiation / illegal access / IO error!");
151 String sign = "voidregisterCallBack(CallBackInterface[])";
152 Class<?> retType = void.class;
153 // port, address, rev, and number of objects
154 Class<?>[] paramCls = new Class<?>[] { int.class };
155 Object[] paramObj = new Object[] { _cb.length };
156 rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
160 public void setA(int _int) {
162 String sign = "voidsetA(int)";
163 Class<?> retType = void.class;
164 Class<?>[] paramCls = new Class<?>[] { int.class };
165 Object[] paramObj = new Object[] { _int };
166 rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
170 public void setB(float _float) {
172 String sign = "voidsetB(float)";
173 Class<?> retType = void.class;
174 Class<?>[] paramCls = new Class<?>[] { float.class };
175 Object[] paramObj = new Object[] { _float };
176 rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
180 public void setC(String _string) {
182 String sign = "voidsetC(string)";
183 Class<?> retType = void.class;
184 Class<?>[] paramCls = new Class<?>[] { String.class };
185 Object[] paramObj = new Object[] { _string };
186 rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
191 public String sumArray(String[] newA) {
193 String sign = "sumArray(string[])";
194 Class<?> retType = String.class;
195 Class<?>[] paramCls = new Class<?>[] { String[].class };
196 Object[] paramObj = new Object[] { newA };
197 Object retObj = rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
198 return (String)retObj;
202 public int setAndGetA(int newA) {
203 String sign = "intsetAndGetA(int)";
204 Class<?> retType = int.class;
205 Class<?>[] paramCls = new Class<?>[] { int.class };
206 Object[] paramObj = new Object[] { newA };
207 Object retObj = rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
212 public int setACAndGetA(String newC, int newA) {
214 String sign = "intsetACAndGetA(string,int)";
215 Class<?> retType = int.class;
216 Class<?>[] paramCls = new Class<?>[] { String.class, int.class };
217 Object[] paramObj = new Object[] { newC, newA };
218 Object retObj = rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
223 public int callBack() {
225 String sign = "intcallBack()";
226 Class<?> retType = int.class;
227 Class<?>[] paramCls = new Class<?>[] { };
228 Object[] paramObj = new Object[] { };
229 Object retObj = rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
235 public void handleStruct(StructJ[] data) {
237 String sign = "structsize";
238 Class<?> retType = void.class;
239 Class<?>[] paramCls = new Class<?>[] { int.class };
240 Object[] paramObj = new Object[] { data.length };
241 rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
243 String sign2 = "handleStruct(StructJ[])";
244 Class<?> retType2 = void.class;
245 // Calculate the size of the array
246 Class<?>[] paramCls2 = new Class<?>[3*data.length];
247 Object[] paramObj2 = new Object[3*data.length];
248 // Handle with for loop
250 for(int i = 0; i < data.length; i++) {
251 paramCls2[pos] = String.class;
252 paramObj2[pos++] = data[i].name;
253 paramCls2[pos] = float.class;
254 paramObj2[pos++] = data[i].value;
255 paramCls2[pos] = int.class;
256 paramObj2[pos++] = data[i].year;
258 System.out.println(Arrays.toString(paramObj2));
259 rmiCall.remoteCall(objectId, sign2, retType2, null, null, paramCls2, paramObj2);
263 public static void main(String[] args) throws Exception {
265 CommunicationHandler comHan = new CommunicationHandler(true);
266 int numOfPorts = TestClass_Stub.numCallbackObjects();
267 int[] ports = comHan.getCallbackPorts(numOfPorts);
270 String address = "localhost";
273 System.out.println("Allocated ports: " + Arrays.toString(ports));
275 TestClass_Stub tcstub = new TestClass_Stub(port, address, rev, ports);
276 System.out.println("Return value: " + tcstub.setAndGetA(123));
277 System.out.println("Return value: " + tcstub.setACAndGetA("string", 123));
278 System.out.println("Return value: " + tcstub.sumArray(new String[] { "123", "456", "987" }));
280 /*CallBackInterface cb1 = new CallBack(23);
281 CallBackInterface cb2 = new CallBack(33);
282 CallBackInterface cb3 = new CallBack(43);
283 CallBackInterface[] cb = { cb1, cb2, cb3 };
284 tcstub.registerCallback(cb);
285 System.out.println("Return value from callback: " + tcstub.callBack());
286 CallBackInterface cb4 = new CallBack(10);
287 CallBackInterface cb5 = new CallBack(11);
288 CallBackInterface cb6 = new CallBack(12);
289 CallBackInterface[] cbt = { cb4, cb5, cb6 };
290 tcstub.registerCallback(cbt);
291 System.out.println("Return value from callback: " + tcstub.callBack());*/
293 StructJ[] data = new StructJ[2];
294 for (int i=0; i<2; i++) {
295 data[i] = new StructJ();
297 data[0].name = "Rahmadi";
298 data[0].value = 0.123f;
300 //data[1].name = "Trimananda";
301 //data[1].value = 0.223f;
302 //data[1].year = 2017;
304 for (StructJ str : data) {
305 System.out.println("Name: " + str.name);
306 System.out.println("Value: " + str.value);
307 System.out.println("Year: " + str.year);
310 tcstub.handleStruct(data);