* Class Properties
*/
private IoTRMICall rmiCall;
- private String address;
private int objectId = 0; // Default value is 0
private final static String[] methodSignatures = {
/**
* Constructors
*/
- public CallBack_CBStub(IoTRMICall _rmiCall, int _objectId, String _address) throws IOException {
+ public CallBack_CBStub(IoTRMICall _rmiCall, int _objectId) throws IOException {
- address = _address;
objectId = _objectId;
rmiCall = _rmiCall;
}
public int callBack() {
- /*int sum = 0;
+ int sum = 0;
for (CallBackInterface cb : cblist) {
sum = sum + cb.printInt();
}
- */
- final CallBackInterface cb1 = cblist.get(1);
+
+ /*final CallBackInterface cb1 = cblist.get(1);
final CallBackInterface cb2 = cblist.get(2);
Thread thread1 = new Thread() {
}
}
};
- thread2.start();
+ thread2.start();*/
- return 1;
+ //return 1;
+ return sum;
}
* Class Constants
*/
private int objectId = 0; // Default value is 0
+ private static int objIdCnt = 0; // Counter for callback object Ids
private final static String[] methodSignatures = {
"voidsetA(int)",
"intsetACAndGetA(string,int)",
"intcallBack()",
"voidregisterCallBack(CallBackInterface)",
- "voidregisterCallBack(CallBackInterface[])"
+ "voidregisterCallBack(CallBackInterface[])",
+ "registercallback"
};
private TestClassInterface tc;
private int port;
private CallBackInterface cbstub;
+ private IoTRMICall rmiCall;
/**
CallBackInterface cbstub = new CallBack_Stub((int) paramObj[0], (String) paramObj[1], (int) paramObj[2]);
registerCallback((CallBackInterface) cbstub);
} else if (methodSign.equals("voidregisterCallBack(CallBackInterface[])")) {
- paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class, String.class, int.class, int.class },
- new Class<?>[] { null, null, null, null }, new Class<?>[] { null, null, null, null });
- String[] methodSignatures = TestClass_Stub.getMethodSignatures();
- IoTRMICall rmiCall = new IoTRMICall((int) paramObj[0], (String) paramObj[1], (int) paramObj[2], methodSignatures);
- int numStubs = (int) paramObj[3];
+ paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class },
+ new Class<?>[] { null }, new Class<?>[] { null });
+ int numStubs = (int) paramObj[0];
CallBackInterface[] stub = new CallBackInterface[numStubs];
for (int objId = 0; objId < numStubs; objId++) {
- stub[objId] = new CallBack_CBStub(rmiCall, objId, (String) paramObj[1]);
+ stub[objId] = new CallBack_CBStub(rmiCall, objIdCnt);
+ objIdCnt++;
}
registerCallback(stub);
} else if (methodSign.equals("intcallBack()")) {
retObj = callBack();
+ // Special option to register callback
+ } else if (methodSign.equals("registercallback")) {
+ paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class, String.class, int.class },
+ new Class<?>[] { null, null, null }, new Class<?>[] { null, null, null });
+ String[] methodSignatures = CallBack_CBStub.getMethodSignatures();
+ rmiCall = new IoTRMICall((int) paramObj[0], (String) paramObj[1], (int) paramObj[2], methodSignatures);
+ System.out.println("Creating a new IoTRMICall object");
} else
throw new Error("Signature not recognized!");
private String address;
private int[] ports;
private List<CallBackInterface> listCBObj;
+ private IoTRMIObject rmiObj;
/**
* Class Constants
*/
private final static int NUM_CB_OBJ = 1;
private int objectId = 0; // Default value is 0
+ private static int objIdCnt = 0; // Counter for callback object Ids
private final static String[] methodSignatures = {
"voidsetA(int)",
"intsetACAndGetA(string,int)",
"intcallBack()",
"voidregisterCallBack(CallBackInterface)",
- "voidregisterCallBack(CallBackInterface[])"
+ "voidregisterCallBack(CallBackInterface[])",
+ "registercallback"
};
/**
ports = _ports;
objectId = _objectId;
rmiCall = _rmiCall;
+ listCBObj = new ArrayList<CallBackInterface>();
+ init_CallBack();
}
}
+ // Initialize callback
+ public void init_CallBack() {
+
+ Thread thread = new Thread() {
+ public void run() {
+ try{
+ String[] methodSignatures = CallBack_CBSkeleton.getMethodSignatures();
+ rmiObj = new IoTRMIObject(ports[0], methodSignatures);
+ Object retObj = null;
+ while (true) {
+ byte[] method = rmiObj.getMethodBytes();
+ int objId = IoTRMIObject.getObjectId(method);
+ CallBack_CBSkeleton skel = (CallBack_CBSkeleton) listCBObj.get(objId);
+ if (skel != null) {
+ rmiObj.setMethodBytes(method);
+ retObj = skel.invokeMethod(rmiObj);
+ }
+ if (retObj != null) {
+ rmiObj.sendReturnObj(retObj);
+ }
+ }
+ } catch (Exception ex){
+ ex.printStackTrace();
+ throw new Error("Error instantiating class CallBack_Skeleton!");
+ }
+ }
+ };
+ thread.start();
+
+ String sign = "registercallback";
+ Class<?> retType = void.class;
+ // port, address, rev, and number of objects
+ Class<?>[] paramCls = new Class<?>[] { int.class, String.class, int.class };
+ Object[] paramObj = new Object[] { ports[0], address, 0 };
+ rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
+ }
+
+
+ // Single callback handling
public void registerCallback(CallBackInterface _cb) {
Thread thread = new Thread() {
}
- // Multiple callback handling
public void registerCallback(CallBackInterface[] _cb) {
try {
- for (int objId = 0; objId < _cb.length; objId++) {
- CallBack_CBSkeleton skel = new CallBack_CBSkeleton(_cb[objId], objId);
+ //for (int objId = 0; objId < _cb.length; objId++) {
+ for (CallBackInterface cb : _cb) {
+ CallBack_CBSkeleton skel = new CallBack_CBSkeleton(cb, objIdCnt++);
listCBObj.add(skel);
}
- } catch ( ClassNotFoundException |
- InstantiationException |
- IllegalAccessException |
- IOException ex) {
+ } catch (Exception ex){
ex.printStackTrace();
throw new Error("Class not found / instantiation / illegal access / IO error!");
}
- Thread thread = new Thread() {
- public void run() {
- try{
- String[] methodSignatures = CallBack_CBSkeleton.getMethodSignatures();
- IoTRMIObject rmiObj = new IoTRMIObject(ports[0], methodSignatures);
- Object retObj = null;
- while (true) {
- byte[] method = rmiObj.getMethodBytes();
- int objId = IoTRMIObject.getObjectId(method);
- CallBack_CBSkeleton skel = (CallBack_CBSkeleton) listCBObj.get(objId);
- if (skel != null) {
- rmiObj.setMethodBytes(method);
- retObj = skel.invokeMethod(rmiObj);
- }
- if (retObj != null) {
- rmiObj.sendReturnObj(retObj);
- }
- }
- } catch (Exception ex){
- ex.printStackTrace();
- throw new Error("Error instantiating class CallBack_Skeleton!");
- }
- }
- };
- thread.start();
-
String sign = "voidregisterCallBack(CallBackInterface[])";
Class<?> retType = void.class;
// port, address, rev, and number of objects
- Class<?>[] paramCls = new Class<?>[] { int.class, String.class, int.class, int.class };
- Object[] paramObj = new Object[] { ports[0], address, 0, _cb.length };
+ Class<?>[] paramCls = new Class<?>[] { int.class };
+ Object[] paramObj = new Object[] { _cb.length };
rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
}
public static void main(String[] args) throws Exception {
+ CommunicationHandler comHan = new CommunicationHandler(true);
+ int numOfPorts = TestClass_Stub.numCallbackObjects();
+ int[] ports = comHan.getCallbackPorts(numOfPorts);
+
+ int port = 5010;
+ String address = "localhost";
+ int rev = 0;
+
+ System.out.println("Allocated ports: " + Arrays.toString(ports));
+
+ TestClass_Stub tcstub = new TestClass_Stub(port, address, rev, ports);
+ System.out.println("Return value: " + tcstub.setAndGetA(123));
+ System.out.println("Return value: " + tcstub.setACAndGetA("string", 123));
+ System.out.println("Return value: " + tcstub.sumArray(new String[] { "123", "456", "987" }));
+
+ CallBackInterface cb1 = new CallBack(23);
+ CallBackInterface cb2 = new CallBack(33);
+ CallBackInterface cb3 = new CallBack(43);
+ CallBackInterface[] cb = { cb1, cb2, cb3 };
+ tcstub.registerCallback(cb);
+ System.out.println("Return value from callback: " + tcstub.callBack());
+ CallBackInterface cb4 = new CallBack(10);
+ CallBackInterface cb5 = new CallBack(11);
+ CallBackInterface cb6 = new CallBack(12);
+ CallBackInterface[] cbt = { cb4, cb5, cb6 };
+ tcstub.registerCallback(cbt);
+ System.out.println("Return value from callback: " + tcstub.callBack());
}
}
* Class Constants
*/
private int objectId = 0; // Default value is 0
+ private static int objIdCnt = 0; // Counter for callback object Ids
private final static String[] methodSignatures = {
"voidsetA(int)",
"intsetACAndGetA(string,int)",
"intcallBack()",
"voidregisterCallBack(CallBackInterface)",
- "voidregisterCallBack(CallBackInterface[])"
+ "voidregisterCallBack(CallBackInterface[])",
+ "registercallback"
};
private TestClassInterface tc;
private int port;
private IoTRMIObject rmiObj;
+ private IoTRMICall rmiCall;
private CallBackInterface cbstub;
CallBackInterface cbstub = new CallBack_Stub((int) paramObj[0], (String) paramObj[1], (int) paramObj[2]);
registerCallback((CallBackInterface) cbstub);
} else if (methodSign.equals("voidregisterCallBack(CallBackInterface[])")) {
- paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class, String.class, int.class, int.class },
- new Class<?>[] { null, null, null, null }, new Class<?>[] { null, null, null, null });
- String[] methodSignatures = CallBack_CBStub.getMethodSignatures();
- IoTRMICall rmiCall = new IoTRMICall((int) paramObj[0], (String) paramObj[1], (int) paramObj[2], methodSignatures);
- int numStubs = (int) paramObj[3];
+ paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class },
+ new Class<?>[] { null }, new Class<?>[] { null });
+ //String[] methodSignatures = CallBack_CBStub.getMethodSignatures();
+ //IoTRMICall rmiCall = new IoTRMICall((int) paramObj[0], (String) paramObj[1], (int) paramObj[2], methodSignatures);
+ int numStubs = (int) paramObj[0];
CallBackInterface[] stub = new CallBackInterface[numStubs];
for (int objId = 0; objId < numStubs; objId++) {
- stub[objId] = new CallBack_CBStub(rmiCall, objId, (String) paramObj[1]);
+ stub[objId] = new CallBack_CBStub(rmiCall, objIdCnt);
+ objIdCnt++;
}
registerCallback(stub);
} else if (methodSign.equals("intcallBack()")) {
retObj = callBack();
+ // Special option to register callback
+ } else if (methodSign.equals("registercallback")) {
+ paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class, String.class, int.class },
+ new Class<?>[] { null, null, null }, new Class<?>[] { null, null, null });
+ String[] methodSignatures = CallBack_CBStub.getMethodSignatures();
+ rmiCall = new IoTRMICall((int) paramObj[0], (String) paramObj[1], (int) paramObj[2], methodSignatures);
+ System.out.println("Creating a new IoTRMICall object");
} else
throw new Error("Signature not recognized!");
private String address;
private int[] ports;
private List<CallBackInterface> listCBObj;
+ private IoTRMIObject rmiObj;
/**
* Class Constants
*/
private final static int NUM_CB_OBJ = 1;
private int objectId = 0; // Default value is 0
+ private static int objIdCnt = 0; // Counter for callback object Ids
private final static String[] methodSignatures = {
"voidsetA(int)",
"intsetACAndGetA(string,int)",
"intcallBack()",
"voidregisterCallBack(CallBackInterface)",
- "voidregisterCallBack(CallBackInterface[])"
+ "voidregisterCallBack(CallBackInterface[])",
+ "registercallback"
};
/**
* Constructors
*/
- public TestClass_Stub(int _port, String _address, int _rev, int[] _ports) throws IOException {
+ public TestClass_Stub(int _port, String _address, int _rev, int[] _ports) throws Exception {
address = _address;
ports = _ports;
rmiCall = new IoTRMICall(_port, _address, _rev, methodSignatures);
listCBObj = new ArrayList<CallBackInterface>();
- }
-
- // Assign rmiCall from outside
- public TestClass_Stub(IoTRMICall _rmiCall, int _objectId, String _address, int[] _ports) throws IOException {
-
- address = _address;
- ports = _ports;
- objectId = _objectId;
- rmiCall = _rmiCall;
+ init_CallBack();
}
}
+ // Initialize callback
+ public void init_CallBack() {
+
+ Thread thread = new Thread() {
+ public void run() {
+ try{
+ String[] methodSignatures = CallBack_CBSkeleton.getMethodSignatures();
+ rmiObj = new IoTRMIObject(ports[0], methodSignatures);
+ Object retObj = null;
+ while (true) {
+ byte[] method = rmiObj.getMethodBytes();
+ int objId = IoTRMIObject.getObjectId(method);
+ CallBack_CBSkeleton skel = (CallBack_CBSkeleton) listCBObj.get(objId);
+ if (skel != null) {
+ rmiObj.setMethodBytes(method);
+ retObj = skel.invokeMethod(rmiObj);
+ }
+ if (retObj != null) {
+ rmiObj.sendReturnObj(retObj);
+ }
+ }
+ } catch (Exception ex){
+ ex.printStackTrace();
+ throw new Error("Error instantiating class CallBack_Skeleton!");
+ }
+ }
+ };
+ thread.start();
+
+ String sign = "registercallback";
+ Class<?> retType = void.class;
+ // port, address, rev, and number of objects
+ Class<?>[] paramCls = new Class<?>[] { int.class, String.class, int.class };
+ Object[] paramObj = new Object[] { ports[0], address, 0 };
+ rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
+ }
+
+
// Single callback handling
public void registerCallback(CallBackInterface _cb) {
}
- // Multiple callback handling
public void registerCallback(CallBackInterface[] _cb) {
try {
- for (int objId = 0; objId < _cb.length; objId++) {
- CallBack_CBSkeleton skel = new CallBack_CBSkeleton(_cb[objId], objId);
+ //for (int objId = 0; objId < _cb.length; objId++) {
+ for (CallBackInterface cb : _cb) {
+ CallBack_CBSkeleton skel = new CallBack_CBSkeleton(cb, objIdCnt++);
listCBObj.add(skel);
}
- } catch ( ClassNotFoundException |
- InstantiationException |
- IllegalAccessException |
- IOException ex) {
+ } catch (Exception ex){
ex.printStackTrace();
throw new Error("Class not found / instantiation / illegal access / IO error!");
}
- Thread thread = new Thread() {
- public void run() {
- try{
- String[] methodSignatures = CallBack_CBSkeleton.getMethodSignatures();
- IoTRMIObject rmiObj = new IoTRMIObject(ports[0], methodSignatures);
- Object retObj = null;
- while (true) {
- byte[] method = rmiObj.getMethodBytes();
- int objId = IoTRMIObject.getObjectId(method);
- CallBack_CBSkeleton skel = (CallBack_CBSkeleton) listCBObj.get(objId);
- if (skel != null) {
- rmiObj.setMethodBytes(method);
- retObj = skel.invokeMethod(rmiObj);
- }
- if (retObj != null) {
- rmiObj.sendReturnObj(retObj);
- }
- }
- } catch (Exception ex){
- ex.printStackTrace();
- throw new Error("Error instantiating class CallBack_Skeleton!");
- }
- }
- };
- thread.start();
-
String sign = "voidregisterCallBack(CallBackInterface[])";
Class<?> retType = void.class;
// port, address, rev, and number of objects
- Class<?>[] paramCls = new Class<?>[] { int.class, String.class, int.class, int.class };
- Object[] paramObj = new Object[] { ports[0], address, 0, _cb.length };
+ Class<?>[] paramCls = new Class<?>[] { int.class };
+ Object[] paramObj = new Object[] { _cb.length };
rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
}
CallBackInterface[] cb = { cb1, cb2, cb3 };
tcstub.registerCallback(cb);
System.out.println("Return value from callback: " + tcstub.callBack());
-
+ CallBackInterface cb4 = new CallBack(10);
+ CallBackInterface cb5 = new CallBack(11);
+ CallBackInterface cb6 = new CallBack(12);
+ CallBackInterface[] cbt = { cb4, cb5, cb6 };
+ tcstub.registerCallback(cbt);
+ System.out.println("Return value from callback: " + tcstub.callBack());
}
}