Separating socket creation from callback method, so that this method can be called...
[iot2.git] / iotjava / iotrmi / Java / sample / TestClass_CBStub.java
index 798e398e3dc5894b85e35b7995be463b0a7f9ddf..80e5830549766db7c4e40838fcf5856fcbe03a59 100644 (file)
@@ -19,12 +19,14 @@ public class TestClass_CBStub implements TestClassInterface {
        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)",
@@ -35,7 +37,8 @@ public class TestClass_CBStub implements TestClassInterface {
                "intsetACAndGetA(string,int)",
                "intcallBack()",
                "voidregisterCallBack(CallBackInterface)",
-               "voidregisterCallBack(CallBackInterface[])"
+               "voidregisterCallBack(CallBackInterface[])",
+               "registercallback"
        };
 
        /**
@@ -48,6 +51,8 @@ public class TestClass_CBStub implements TestClassInterface {
                ports = _ports;
                objectId = _objectId;
                rmiCall = _rmiCall;
+               listCBObj = new ArrayList<CallBackInterface>();
+               init_CallBack();
        }
 
 
@@ -67,6 +72,45 @@ public class TestClass_CBStub implements TestClassInterface {
        }
 
 
+       // 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() {
@@ -91,53 +135,24 @@ public class TestClass_CBStub implements TestClassInterface {
        }
 
 
-       // 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);
        }
 
@@ -219,6 +234,33 @@ public class TestClass_CBStub implements TestClassInterface {
 
        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());
        }
 }