Removing callback port feature from master and slave
[iot2.git] / iotjava / iotruntime / slave / IoTSlave.java
index 856431a1f1fbe5d95f1c02ea854df100a01402cc..1a6fa9f30090b878f9ee64ce229536f005f6debe 100644 (file)
@@ -66,6 +66,7 @@ public class IoTSlave {
        private Socket socket;
        private ObjectOutputStream outStream;
        private ObjectInputStream inStream;
+       private Map<String,Object> mapObjNameStub;
 
        /**
         * IoTSet object, e.g. IoTSet<ProximitySensor> proximity_sensors;
@@ -122,6 +123,7 @@ public class IoTSlave {
                socket = null;
                outStream = null;
                inStream = null;
+               mapObjNameStub = new HashMap<String,Object>();
 
                STR_JAR_FILE_PATH = null;
                STR_OBJ_CLS_PFX = null;
@@ -212,10 +214,9 @@ public class IoTSlave {
                Class<?> clsSkel = Class.forName(strObjSkelName);
                Class<?> clsInt = Class.forName(STR_OBJ_CLS_PFX + "." + STR_INTERFACE_PFX + 
                        "." + sMessage.getObjectInterfaceName());
-               Class[] clsSkelParams = { clsInt, String.class, int.class };    // Port number is integer
+               Class[] clsSkelParams = { clsInt, int.class, int.class };       // Port number is integer
                Constructor<?> objSkelCons = clsSkel.getDeclaredConstructor(clsSkelParams);
-               String callbackAddress = InetAddress.getLocalHost().getHostAddress();   // Callback address is this machine's address
-               Object objSkelParams[] = { objMainCls, callbackAddress, iRMIStubPort };
+               Object objSkelParams[] = { objMainCls, iRMIStubPort, iRMIRegPort };
                // Create a new thread for each skeleton
                Thread objectThread = new Thread(new Runnable() {
                        public void run() {
@@ -433,30 +434,33 @@ public class IoTSlave {
 
                // Translating into the actual Message class
                MessageGetObject sMessage = (MessageGetObject) sIoTMasterMsg;
-               // Instantiate the stub and put in the object
-               String strObjStubName = sMainObjectName + "." + sMessage.getObjectStubInterfaceName() + STUB_CLASS_SUFFIX;
-               RuntimeOutput.print("IoTSlave: Stub object: " + strObjStubName, BOOL_VERBOSE);
-               Class<?> clsStub = Class.forName(strObjStubName);       // Port number is integer
-               Class[] clsStubParams = { int.class, String.class, String.class, int.class, int[].class };
-               Constructor<?> objStubCons = clsStub.getDeclaredConstructor(clsStubParams);
-               Integer[] portsInteger = sMessage.getRMICallbackPorts();
-               int[] ports = Arrays.stream(portsInteger).mapToInt(Integer::intValue).toArray();
-               int rev = 0;
-               String callbackAddress = InetAddress.getLocalHost().getHostAddress();   // Callback address is this machine's address
-               Object objStubParams[] = { sMessage.getRMIStubPort(), sMessage.getHostAddress(), callbackAddress,
-                                                                       rev, ports };
-               RuntimeOutput.print("IoTSlave: Creating RMI stub: " +
-                       sMessage.getHostAddress() + ":" + sMessage.getRMIRegPort() +
-                       " with callback address: " + callbackAddress + " and RMI stub port: " + sMessage.getRMIStubPort(), BOOL_VERBOSE);
-               Object stubObj = objStubCons.newInstance(objStubParams);
-               RuntimeOutput.print("IoTSlave: Object name: " + sMessage.getObjectName(), BOOL_VERBOSE);
-               RuntimeOutput.print("IoTSlave: Stub address: " + callbackAddress, BOOL_VERBOSE);
-               // Class conversion to interface class of this class,
-               // e.g. ProximitySensorImpl has ProximitySensor interface
+               Object stubObjConv = null;
+               String strObjectName = sMessage.getObjectName();
                String strObjClassInterfaceName = STR_OBJ_CLS_PFX + "." + STR_INTERFACE_PFX + "." +
                        sMessage.getObjectStubInterfaceName();
                Class<?> clsInf = Class.forName(strObjClassInterfaceName);
-               Object stubObjConv = clsInf.cast(stubObj);
+               if (mapObjNameStub.containsKey(strObjectName)) {
+                       RuntimeOutput.print("IoTSlave: Getting back object on slave: " + strObjectName, BOOL_VERBOSE);
+                       stubObjConv = clsInf.cast(mapObjNameStub.get(strObjectName));
+               } else {
+                       // Instantiate the stub and put in the object
+                       String strObjStubName = sMainObjectName + "." + sMessage.getObjectStubInterfaceName() + STUB_CLASS_SUFFIX;
+                       Class<?> clsStub = Class.forName(strObjStubName);       // Port number is integer
+                       Class[] clsStubParams = { int.class, int.class, int.class, int.class, String.class, int.class };
+                       Constructor<?> objStubCons = clsStub.getDeclaredConstructor(clsStubParams);
+
+                       int rev = 0;
+                       Object objStubParams[] = { 0, 0, sMessage.getRMIStubPort(), sMessage.getRMIRegPort(), sMessage.getHostAddress(), rev };
+                       RuntimeOutput.print("IoTSlave: Creating RMI stub: " +
+                               sMessage.getHostAddress() + ":" + sMessage.getRMIRegPort() + 
+                               " and RMI stub port: " + sMessage.getRMIStubPort(), BOOL_VERBOSE);
+                       Object stubObj = objStubCons.newInstance(objStubParams);
+                       // Class conversion to interface class of this class,
+                       // e.g. ProximitySensorImpl has ProximitySensor interface
+                       RuntimeOutput.print("IoTSlave: Registering new stub object: " + strObjectName, BOOL_VERBOSE);
+                       mapObjNameStub.put(strObjectName, stubObj);
+                       stubObjConv = clsInf.cast(stubObj);
+               }
 
                return stubObjConv;
        }
@@ -469,7 +473,6 @@ public class IoTSlave {
        private void getIoTSetObject() throws IOException,
                ClassNotFoundException, RemoteException, NotBoundException, NoSuchMethodException,
                InstantiationException, IllegalAccessException, InvocationTargetException {
-
                Object objRegistry = null;
                if (CAPAB_BASED_RMI)
                        objRegistry = getObjectFromStub();
@@ -491,7 +494,6 @@ public class IoTSlave {
        private void getIoTRelationFirstObject() throws IOException,
                ClassNotFoundException, RemoteException, NotBoundException, NoSuchMethodException,
                InstantiationException, IllegalAccessException, InvocationTargetException {
-
                Object objRegistry = null;
                if (CAPAB_BASED_RMI)
                        objRegistry = getObjectFromStub();
@@ -513,7 +515,6 @@ public class IoTSlave {
        private void getIoTRelationSecondObject() throws IOException,
                ClassNotFoundException, RemoteException, NotBoundException, NoSuchMethodException,
                InstantiationException, IllegalAccessException, InvocationTargetException {
-
                Object objRegistry = null;
                if (CAPAB_BASED_RMI)
                        objRegistry = getObjectFromStub();
@@ -541,7 +542,6 @@ public class IoTSlave {
 
                // Reinitialize IoTSet field after getting all the objects
                iotsetObject = new IoTSet<Object>(isetObject.values());
-
                // Private fields need getDeclaredField(), while public fields use getField()
                Field fld = clsMain.getDeclaredField(strFieldName);
                boolean bAccess = fld.isAccessible();
@@ -566,7 +566,6 @@ public class IoTSlave {
 
                // Reinitialize IoTSet field after getting all the objects
                iotrelObject = new IoTRelation<Object,Object>(irelObject.relationMap(), irelObject.size());
-
                // Private fields need getDeclaredField(), while public fields use getField()
                Field fld = clsMain.getDeclaredField(strFieldName);
                boolean bAccess = fld.isAccessible();
@@ -592,7 +591,6 @@ public class IoTSlave {
 
                // Translating into the actual Message class
                MessageGetDeviceObject sMessage = (MessageGetDeviceObject) sIoTMasterMsg;
-
                // Get IoTSet objects for IP address set on device driver/controller
                IoTDeviceAddress objDeviceAddress = new IoTDeviceAddress(sMessage.getHostAddress(),
                        sMessage.getSourceDeviceDriverPort(),
@@ -620,7 +618,6 @@ public class IoTSlave {
 
                // Translating into the actual Message class
                MessageGetSimpleDeviceObject sMessage = (MessageGetSimpleDeviceObject) sIoTMasterMsg;
-
                // Get IoTSet objects for IP address set on device driver/controller
                IoTZigbeeAddress objZBDevAddress = new IoTZigbeeAddress(sMessage.getHostAddress());
                RuntimeOutput.print("IoTSlave: Device address transferred: " + sMessage.getHostAddress(), BOOL_VERBOSE);
@@ -643,7 +640,6 @@ public class IoTSlave {
 
                // Translating into the actual Message class
                MessageGetSimpleDeviceObject sMessage = (MessageGetSimpleDeviceObject) sIoTMasterMsg;
-
                // Get IoTSet objects for IP address set on device driver/controller
                IoTAddress objAddress = new IoTAddress(sMessage.getHostAddress());
                RuntimeOutput.print("IoTSlave: Address transferred: " + sMessage.getHostAddress(), BOOL_VERBOSE);