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());
}
}