Adjustments in stubs and skeletons for callback in callback; using different ports...
[iot2.git] / iotjava / iotruntime / master / CommunicationHandler.java
index 104f3d9918eff7c4f82e9e55a51cec613a64d276..db6b16193d46b1372ceda8b870e2f75ea18bf031 100644 (file)
@@ -68,6 +68,7 @@ public final class CommunicationHandler {
        private Map<Integer, Integer> hmRMIStubPort;
        private Set<Integer> hsDevicePort;
        private Set<Integer> hsCallbackPort;
+       private Map<Integer, Integer[]> hmCallbackPort;
        private int iNumOfObjects;
        private int iNumOfHosts;
        private boolean bVerbose;
@@ -97,6 +98,7 @@ public final class CommunicationHandler {
                hmRMIStubPort = new HashMap<Integer, Integer>();
                hsDevicePort = new HashSet<Integer>();
                hsCallbackPort = new HashSet<Integer>();
+               hmCallbackPort = new HashMap<Integer, Integer[]>();
                iNumOfObjects = 0;
                iNumOfHosts = 0;
                bVerbose = _bVerbose;
@@ -207,6 +209,36 @@ public final class CommunicationHandler {
        }
 
 
+       /**
+        * Method getCallbackPorts()
+        * <p>
+        * Get a set of new ports for new connections for callback objects in the program.
+        * This newly generated port number will be recorded.
+        *
+        * @return  int[]       A set of callback ports
+        */
+       public Integer[] getCallbackPorts(String sAObject, int numOfPorts) {
+
+               Integer[] ports = new Integer[numOfPorts];
+               int iNumOfObject = hmActiveObj.get(sAObject);
+
+               if (!hmCallbackPort.containsKey(iNumOfObject)) {
+                       for(int i = 0; i < numOfPorts; i++) {
+                               do {
+                                       ports[i] = random.nextInt(INT_MAX_PORT - INT_MIN_PORT + 1) + INT_MIN_PORT;
+                                       // Check port existence in HashMap
+                               } while (portIsAvailable(ports[i]) == false);
+                               hsCallbackPort.add(ports[i]);
+                       }
+                       hmCallbackPort.put(iNumOfObject, ports);
+               } else {
+                       ports = hmCallbackPort.get(iNumOfObject);
+               }
+
+               return ports;
+       }
+
+
        /**
         * Method addDevicePort()
         * <p>