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 ___initCallBack() {
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 } catch (Exception ex){
121 ex.printStackTrace();
122 throw new Error("Error instantiating class CallBack_Skeleton!");
128 String sign = "voidregisterCallBack(CallBackInterface)";
129 Class<?> retType = void.class;
130 // port, address, and rev
131 Class<?>[] paramCls = new Class<?>[] { int.class, String.class, int.class };
132 Object[] paramObj = new Object[] { ports[0], address, 0 };
133 rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
137 public void registerCallback(CallBackInterface[] _cb) {
140 //for (int objId = 0; objId < _cb.length; objId++) {
141 for (CallBackInterface cb : _cb) {
142 CallBack_CBSkeleton skel = new CallBack_CBSkeleton(cb, objIdCnt++);
145 } catch (Exception ex){
146 ex.printStackTrace();
147 throw new Error("Class not found / instantiation / illegal access / IO error!");
150 String sign = "voidregisterCallBack(CallBackInterface[])";
151 Class<?> retType = void.class;
152 // port, address, rev, and number of objects
153 Class<?>[] paramCls = new Class<?>[] { int.class };
154 Object[] paramObj = new Object[] { _cb.length };
155 rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
159 public void setA(int _int) {
161 String sign = "voidsetA(int)";
162 Class<?> retType = void.class;
163 Class<?>[] paramCls = new Class<?>[] { int.class };
164 Object[] paramObj = new Object[] { _int };
165 rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
169 public void setB(float _float) {
171 String sign = "voidsetB(float)";
172 Class<?> retType = void.class;
173 Class<?>[] paramCls = new Class<?>[] { float.class };
174 Object[] paramObj = new Object[] { _float };
175 rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
179 public void setC(String _string) {
181 String sign = "voidsetC(string)";
182 Class<?> retType = void.class;
183 Class<?>[] paramCls = new Class<?>[] { String.class };
184 Object[] paramObj = new Object[] { _string };
185 rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
190 public String sumArray(String[] newA) {
192 String sign = "sumArray(string[])";
193 Class<?> retType = String.class;
194 Class<?>[] paramCls = new Class<?>[] { String[].class };
195 Object[] paramObj = new Object[] { newA };
196 Object retObj = rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
197 return (String)retObj;
201 public int setAndGetA(int newA) {
202 String sign = "intsetAndGetA(int)";
203 Class<?> retType = int.class;
204 Class<?>[] paramCls = new Class<?>[] { int.class };
205 Object[] paramObj = new Object[] { newA };
206 Object retObj = rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
211 public int setACAndGetA(String newC, int newA) {
213 String sign = "intsetACAndGetA(string,int)";
214 Class<?> retType = int.class;
215 Class<?>[] paramCls = new Class<?>[] { String.class, int.class };
216 Object[] paramObj = new Object[] { newC, newA };
217 Object retObj = rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
222 public int callBack() {
224 String sign = "intcallBack()";
225 Class<?> retType = int.class;
226 Class<?>[] paramCls = new Class<?>[] { };
227 Object[] paramObj = new Object[] { };
228 Object retObj = rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
234 public void handleStruct(StructJ[] data) {
236 String sign = "structsize";
237 Class<?> retType = void.class;
238 Class<?>[] paramCls = new Class<?>[] { int.class };
239 Object[] paramObj = new Object[] { data.length };
240 rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
242 String sign2 = "handleStruct(StructJ[])";
243 Class<?> retType2 = void.class;
244 // Calculate the size of the array
245 Class<?>[] paramCls2 = new Class<?>[3*data.length];
246 Object[] paramObj2 = new Object[3*data.length];
247 // Handle with for loop
249 for(int i = 0; i < data.length; i++) {
250 paramCls2[pos] = String.class;
251 paramObj2[pos++] = data[i].name;
252 paramCls2[pos] = float.class;
253 paramObj2[pos++] = data[i].value;
254 paramCls2[pos] = int.class;
255 paramObj2[pos++] = data[i].year;
257 System.out.println(Arrays.toString(paramObj2));
258 rmiCall.remoteCall(objectId, sign2, retType2, null, null, paramCls2, paramObj2);
262 public static void main(String[] args) throws Exception {
264 CommunicationHandler comHan = new CommunicationHandler(true);
265 int numOfPorts = TestClass_Stub.numCallbackObjects();
266 int[] ports = comHan.getCallbackPorts(numOfPorts);
269 String address = "localhost";
272 System.out.println("Allocated ports: " + Arrays.toString(ports));
274 TestClass_Stub tcstub = new TestClass_Stub(port, address, rev, ports);
275 System.out.println("Return value: " + tcstub.setAndGetA(123));
276 System.out.println("Return value: " + tcstub.setACAndGetA("string", 123));
277 System.out.println("Return value: " + tcstub.sumArray(new String[] { "123", "456", "987" }));
279 /*CallBackInterface cb1 = new CallBack(23);
280 CallBackInterface cb2 = new CallBack(33);
281 CallBackInterface cb3 = new CallBack(43);
282 CallBackInterface[] cb = { cb1, cb2, cb3 };
283 tcstub.registerCallback(cb);
284 System.out.println("Return value from callback: " + tcstub.callBack());
285 CallBackInterface cb4 = new CallBack(10);
286 CallBackInterface cb5 = new CallBack(11);
287 CallBackInterface cb6 = new CallBack(12);
288 CallBackInterface[] cbt = { cb4, cb5, cb6 };
289 tcstub.registerCallback(cbt);
290 System.out.println("Return value from callback: " + tcstub.callBack());*/
292 StructJ[] data = new StructJ[2];
293 for (int i=0; i<2; i++) {
294 data[i] = new StructJ();
296 data[0].name = "Rahmadi";
297 data[0].value = 0.123f;
299 //data[1].name = "Trimananda";
300 //data[1].value = 0.223f;
301 //data[1].year = 2017;
303 for (StructJ str : data) {
304 System.out.println("Name: " + str.name);
305 System.out.println("Value: " + str.value);
306 System.out.println("Year: " + str.year);
309 tcstub.handleStruct(data);