Applying changes for 3rd benchmark (wildcard addresses) in IoTMaster
[iot2.git] / iotjava / iotruntime / master / CommunicationHandler.java
index 6f9c67a230f9cfe550c758aac665349cc42b7591..def3e77b653e6e723f421ad7516b1e8bcd645df9 100644 (file)
@@ -69,6 +69,7 @@ public final class CommunicationHandler {
        private Set<Integer> hsDevicePort;
        private Set<Integer> hsCallbackPort;
        private Map<Integer, Integer[]> hmCallbackPort;
+       private Map<Integer, Integer> hmAdditionalPort;
        private int iNumOfObjects;
        private int iNumOfHosts;
        private boolean bVerbose;
@@ -99,6 +100,7 @@ public final class CommunicationHandler {
                hsDevicePort = new HashSet<Integer>();
                hsCallbackPort = new HashSet<Integer>();
                hmCallbackPort = new HashMap<Integer, Integer[]>();
+               hmAdditionalPort = new HashMap<Integer, Integer>();
                iNumOfObjects = 0;
                iNumOfHosts = 0;
                bVerbose = _bVerbose;
@@ -254,6 +256,30 @@ public final class CommunicationHandler {
 
        }
 
+       /**
+        * Method addAdditionalPort()
+        * <p>
+        * Add a new port for new connections for any objects in the program.
+        * This newly generated port number will be recorded.
+        *
+        * @return  int         One new port
+        */
+       public int addAdditionalPort(String sAObject) {
+
+               hmActiveObj.put(sAObject, iNumOfObjects);
+
+               int iAdditionalPort = 0;
+               do {
+                       iAdditionalPort = random.nextInt(INT_MAX_PORT - INT_MIN_PORT + 1) + INT_MIN_PORT;
+                       // Check port existence in HashMap
+               } while (portIsAvailable(iAdditionalPort) == false);
+               hmAdditionalPort.put(iNumOfObjects, iAdditionalPort);
+
+               iNumOfObjects++;
+
+               return iAdditionalPort;
+       }
+
        /**
         * Method portIsAvailable()
         * <p>
@@ -272,6 +298,8 @@ public final class CommunicationHandler {
                        return false;
                } else if (hmRMIStubPort.containsValue(iPortNumber) == true) {
                        return false;
+               } else if (hmAdditionalPort.containsValue(iPortNumber) == true) {
+                       return false;
                } else if (hsDevicePort.contains(iPortNumber) == true) {
                        return false;
                } else if (hsCallbackPort.contains(iPortNumber) == true) {
@@ -367,6 +395,19 @@ public final class CommunicationHandler {
                return hmComPort.get(hmActiveObj.get(sAObject));
        }
 
+       /**
+        * Method getAdditionalPort()
+        * <p>
+        * User finds a port number using Object name
+        *
+        * @param   sAObject  String active object name
+        * @return  Integer
+        */
+       public Integer getAdditionalPort(String sAObject) {
+
+               return hmAdditionalPort.get(hmActiveObj.get(sAObject));
+       }
+
        /**
         * Method getRMIRegPort()
         * <p>