Separating socket creation from callback method, so that this method can be called...
authorrtrimana <rtrimana@uci.edu>
Wed, 2 Nov 2016 18:36:26 +0000 (11:36 -0700)
committerrtrimana <rtrimana@uci.edu>
Wed, 2 Nov 2016 18:36:26 +0000 (11:36 -0700)
iotjava/iotrmi/Java/sample/CallBack_CBStub.java
iotjava/iotrmi/Java/sample/TestClass.java
iotjava/iotrmi/Java/sample/TestClass_CBSkeleton.java
iotjava/iotrmi/Java/sample/TestClass_CBStub.java
iotjava/iotrmi/Java/sample/TestClass_Skeleton.java
iotjava/iotrmi/Java/sample/TestClass_Stub.java

index fea29b1..199595c 100644 (file)
@@ -9,7 +9,6 @@ public class CallBack_CBStub implements CallBackInterface {
         * Class Properties
         */
        private IoTRMICall rmiCall;
-       private String address;
 
        private int objectId = 0;       // Default value is 0
        private final static String[] methodSignatures = {
@@ -21,9 +20,8 @@ public class CallBack_CBStub implements CallBackInterface {
        /**
         * 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;
        }
index f23ffd0..1c95b3a 100644 (file)
@@ -103,12 +103,12 @@ public class TestClass implements TestClassInterface {
 
        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() {
@@ -139,9 +139,10 @@ public class TestClass implements TestClassInterface {
                    }
                }
            };
-               thread2.start();
+               thread2.start();*/
 
-               return 1;
+               //return 1;
+               return sum;
        }
 
 
index 770696b..10f136d 100644 (file)
@@ -15,6 +15,7 @@ public class TestClass_CBSkeleton implements TestClassInterface {
         * 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)",
@@ -25,12 +26,14 @@ public class TestClass_CBSkeleton implements TestClassInterface {
                "intsetACAndGetA(string,int)",
                "intcallBack()",
                "voidregisterCallBack(CallBackInterface)",
-               "voidregisterCallBack(CallBackInterface[])"
+               "voidregisterCallBack(CallBackInterface[])",
+               "registercallback"
        };
 
        private TestClassInterface tc;
        private int port;
        private CallBackInterface cbstub;
+       private IoTRMICall rmiCall;
 
 
        /**
@@ -83,18 +86,24 @@ public class TestClass_CBSkeleton implements TestClassInterface {
                        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!");
 
index 798e398..80e5830 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());
        }
 }
 
index 02b279a..acab4ac 100644 (file)
@@ -15,6 +15,7 @@ public class TestClass_Skeleton implements TestClassInterface {
         * 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)",
@@ -25,12 +26,14 @@ public class TestClass_Skeleton implements TestClassInterface {
                "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;
 
 
@@ -91,18 +94,26 @@ public class TestClass_Skeleton implements TestClassInterface {
                                        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!");
 
index d7b9d51..d071d13 100644 (file)
@@ -19,12 +19,14 @@ public class TestClass_Stub 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,27 +37,20 @@ public class TestClass_Stub implements TestClassInterface {
                "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();
        }
 
 
@@ -75,6 +70,44 @@ public class TestClass_Stub 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) {
 
@@ -100,53 +133,24 @@ public class TestClass_Stub 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);
        }
 
@@ -249,7 +253,12 @@ public class TestClass_Stub implements TestClassInterface {
                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());
        }
 }