Separating socket creation from callback method, so that this method can be called...
[iot2.git] / iotjava / iotrmi / Java / sample / TestClass_Stub.java
index 1eb77d4d528ad9d1e0b8c65ecf143df9f5b95faa..d071d132f66a6d977cd51b5b48c77a9eac1f7c3a 100644 (file)
@@ -5,6 +5,10 @@ import iotrmi.Java.IoTRMICall;
 import iotruntime.master.CommunicationHandler;
 
 import java.util.Arrays;
+import java.util.List;
+import java.util.ArrayList;
+
+import iotrmi.Java.IoTRMIObject;
 
 public class TestClass_Stub implements TestClassInterface {
 
@@ -14,13 +18,16 @@ public class TestClass_Stub implements TestClassInterface {
        private IoTRMICall rmiCall;
        private String address;
        private int[] ports;
+       private List<CallBackInterface> listCBObj;
+       private IoTRMIObject rmiObj;
 
        /**
         * Class Constants
         */
        private final static int NUM_CB_OBJ = 1;
-
-       private String[] methodSignatures = {
+       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)",
                "voidsetB(float)",
@@ -29,17 +36,21 @@ public class TestClass_Stub implements TestClassInterface {
                "intsetAndGetA(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>();
+               init_CallBack();
        }
 
 
@@ -52,10 +63,53 @@ public class TestClass_Stub implements TestClassInterface {
        }
 
 
-       public void registerCallback(CallBackInterface _cb) {
+       // Return method signatures
+       public static String[] getMethodSignatures() {
+
+               return methodSignatures;
+       }
+
+
+       // 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);
+       }
 
-               //int port = 5011;      // Send this info to the other end to start the stub
-               //String address = "localhost";
+
+       // Single callback handling
+       public void registerCallback(CallBackInterface _cb) {
 
                Thread thread = new Thread() {
                        public void run() {
@@ -75,7 +129,29 @@ public class TestClass_Stub implements TestClassInterface {
                // port, address, and rev
                Class<?>[] paramCls = new Class<?>[] { int.class, String.class, int.class };
                Object[] paramObj = new Object[] { ports[0], address, 0 };
-               rmiCall.remoteCall(sign, retType, null, null, paramCls, paramObj);
+               rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
+       }
+
+
+       public void registerCallback(CallBackInterface[] _cb) {
+
+               try {
+                       //for (int objId = 0; objId < _cb.length; objId++) {
+                       for (CallBackInterface cb : _cb) {
+                               CallBack_CBSkeleton skel = new CallBack_CBSkeleton(cb, objIdCnt++);
+                               listCBObj.add(skel);
+                       }
+               } catch (Exception ex){
+                       ex.printStackTrace();
+                       throw new Error("Class not found / instantiation / illegal access / IO error!");
+               }
+
+               String sign = "voidregisterCallBack(CallBackInterface[])";
+               Class<?> retType = void.class;
+               // port, address, rev, and number of objects
+               Class<?>[] paramCls = new Class<?>[] { int.class };
+               Object[] paramObj = new Object[] { _cb.length };
+               rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
        }
 
 
@@ -85,7 +161,7 @@ public class TestClass_Stub implements TestClassInterface {
                Class<?> retType = void.class;
                Class<?>[] paramCls = new Class<?>[] { int.class };
                Object[] paramObj = new Object[] { _int };
-               rmiCall.remoteCall(sign, retType, null, null, paramCls, paramObj);
+               rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
        }
 
 
@@ -95,7 +171,7 @@ public class TestClass_Stub implements TestClassInterface {
                Class<?> retType = void.class;
                Class<?>[] paramCls = new Class<?>[] { float.class };
                Object[] paramObj = new Object[] { _float };
-               rmiCall.remoteCall(sign, retType, null, null, paramCls, paramObj);
+               rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
        }
 
 
@@ -105,7 +181,7 @@ public class TestClass_Stub implements TestClassInterface {
                Class<?> retType = void.class;
                Class<?>[] paramCls = new Class<?>[] { String.class };
                Object[] paramObj = new Object[] { _string };
-               rmiCall.remoteCall(sign, retType, null, null, paramCls, paramObj);
+               rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
        }
 
 
@@ -116,7 +192,7 @@ public class TestClass_Stub implements TestClassInterface {
                Class<?> retType = String.class;
                Class<?>[] paramCls = new Class<?>[] { String[].class };
                Object[] paramObj = new Object[] { newA };
-               Object retObj = rmiCall.remoteCall(sign, retType, null, null, paramCls, paramObj);
+               Object retObj = rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
                return (String)retObj;
        }
 
@@ -126,7 +202,7 @@ public class TestClass_Stub implements TestClassInterface {
                Class<?> retType = int.class;
                Class<?>[] paramCls = new Class<?>[] { int.class };
                Object[] paramObj = new Object[] { newA };
-               Object retObj = rmiCall.remoteCall(sign, retType, null, null, paramCls, paramObj);
+               Object retObj = rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
                return (int)retObj;
        }
 
@@ -137,7 +213,7 @@ public class TestClass_Stub implements TestClassInterface {
                Class<?> retType = int.class;
                Class<?>[] paramCls = new Class<?>[] { String.class, int.class };
                Object[] paramObj = new Object[] { newC, newA };
-               Object retObj = rmiCall.remoteCall(sign, retType, null, null, paramCls, paramObj);
+               Object retObj = rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
                return (int)retObj;
        }
 
@@ -148,7 +224,7 @@ public class TestClass_Stub implements TestClassInterface {
                Class<?> retType = int.class;
                Class<?>[] paramCls = new Class<?>[] { };
                Object[] paramObj = new Object[] { };
-               Object retObj = rmiCall.remoteCall(sign, retType, null, null, paramCls, paramObj);
+               Object retObj = rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
                return (int)retObj;
 
        }
@@ -160,20 +236,30 @@ public class TestClass_Stub implements TestClassInterface {
                int numOfPorts = TestClass_Stub.numCallbackObjects();
                int[] ports = comHan.getCallbackPorts(numOfPorts);
 
-               System.out.println("Allocated ports: " + Arrays.toString(ports));
-
                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" }));
 
-               /*CallBack cb = new CallBack(23);
+               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());*/
-               //System.out.println("Return value: " + tcstub.setAndGetA(1234));
+               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());
        }
 }
+
+