Initial version that handles multiple callback objects through 1 socket
[iot2.git] / iotjava / iotrmi / Java / sample / TestClass_Stub.java
index 1eb77d4d528ad9d1e0b8c65ecf143df9f5b95faa..d7b9d515fea312f1f63cda28519a18115b71f225 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,14 @@ public class TestClass_Stub implements TestClassInterface {
        private IoTRMICall rmiCall;
        private String address;
        private int[] ports;
+       private List<CallBackInterface> listCBObj;
 
        /**
         * Class Constants
         */
        private final static int NUM_CB_OBJ = 1;
-
-       private String[] methodSignatures = {
+       private int objectId = 0;       // Default value is 0
+       private final static String[] methodSignatures = {
 
                "voidsetA(int)",
                "voidsetB(float)",
@@ -29,7 +34,8 @@ public class TestClass_Stub implements TestClassInterface {
                "intsetAndGetA(int)",
                "intsetACAndGetA(string,int)",
                "intcallBack()",
-               "voidregisterCallBack(CallBackInterface)"
+               "voidregisterCallBack(CallBackInterface)",
+               "voidregisterCallBack(CallBackInterface[])"
        };
 
        /**
@@ -40,6 +46,16 @@ public class TestClass_Stub implements TestClassInterface {
                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;
        }
 
 
@@ -52,10 +68,15 @@ public class TestClass_Stub implements TestClassInterface {
        }
 
 
-       public void registerCallback(CallBackInterface _cb) {
+       // Return method signatures
+       public static String[] getMethodSignatures() {
+
+               return methodSignatures;
+       }
+
 
-               //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 +96,58 @@ 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);
+       }
+
+
+       // 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);
+                               listCBObj.add(skel);
+                       }
+               } catch (       ClassNotFoundException | 
+                                       InstantiationException |
+                                       IllegalAccessException |
+                                       IOException 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 };
+               rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
        }
 
 
@@ -85,7 +157,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 +167,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 +177,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 +188,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 +198,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 +209,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 +220,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 +232,25 @@ 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());
+
        }
 }
+
+