Adjusting runtime system back after paper evaluation - putting back RMI port and...
[iot2.git] / iotjava / iotruntime / master / CommunicationHandler.java
index db6b16193d46b1372ceda8b870e2f75ea18bf031..5d257722772b03d9480bb67df92e36967755b6b0 100644 (file)
@@ -67,12 +67,19 @@ public final class CommunicationHandler {
        private Map<Integer, Integer> hmRMIRegPort;
        private Map<Integer, Integer> hmRMIStubPort;
        private Set<Integer> hsDevicePort;
-       private Set<Integer> hsCallbackPort;
-       private Map<Integer, Integer[]> hmCallbackPort;
+       private Set<Integer> hsAllPorts;
+       private Map<Integer, Integer> hmAdditionalPort;
        private int iNumOfObjects;
        private int iNumOfHosts;
        private boolean bVerbose;
 
+       // TODO: THIS IS HACKING FOR IOTSEC
+       // Replace random
+       private int comPortCount = 10000;
+       private int rmiRegCount = 20000;
+       private int rmiStubCount = 30000;
+       private int additionalCount = 40000;
+
        /**
         * CommunicationHandler class constants
         */
@@ -97,13 +104,40 @@ public final class CommunicationHandler {
                hmRMIRegPort = new HashMap<Integer, Integer>();
                hmRMIStubPort = new HashMap<Integer, Integer>();
                hsDevicePort = new HashSet<Integer>();
-               hsCallbackPort = new HashSet<Integer>();
-               hmCallbackPort = new HashMap<Integer, Integer[]>();
+               hsAllPorts = new HashSet<Integer>();
+               hmAdditionalPort = new HashMap<Integer, Integer>();
                iNumOfObjects = 0;
                iNumOfHosts = 0;
                bVerbose = _bVerbose;
                RuntimeOutput.print("CommunicationHandler: Creating a new CommunicationHandler object!", bVerbose);
        }
+       
+       /**
+        * Method clearCommunicationHandler()
+        * <p>
+        * Clear the data structure
+        *
+        * @return  void
+        */
+       public void clearCommunicationHandler() {
+
+               listActiveControllerObj.clear();
+               listFieldObjectID.clear();
+               listObjCrtInfo.clear();
+               listArrFieldValues.clear();
+               listArrFieldClasses.clear();
+               hmActiveObj.clear();
+               hmHostAdd.clear();
+               hmHostList.clear();
+               hmComPort.clear();
+               hmRMIRegPort.clear();
+               hmRMIStubPort.clear();
+               hsDevicePort.clear();
+               hmAdditionalPort.clear();
+               iNumOfObjects = 0;
+               iNumOfHosts = 0;
+               RuntimeOutput.print("CommunicationHandler: Clearing CommunicationHandler object's data structure!", bVerbose);
+    }
 
        /**
         * Method addPortConnection()
@@ -138,23 +172,33 @@ public final class CommunicationHandler {
                int iComPort = 0;
                do {
                        iComPort = random.nextInt(INT_MAX_PORT - INT_MIN_PORT + 1) + INT_MIN_PORT;
+                       //System.out.println("DEBUG: Assigning comPort: " + comPortCount);
+                       //iComPort = comPortCount++;
                        // Check port existence in HashMap
                } while (portIsAvailable(iComPort) == false);
                hmComPort.put(iNumOfObjects, iComPort);
+               // hsAllPorts tracks all the existing and used port numbers
+               hsAllPorts.add(iComPort);
 
                int iRMIRegPort = 0;
                do {
                        iRMIRegPort = random.nextInt(INT_MAX_PORT - INT_MIN_PORT + 1) + INT_MIN_PORT;
+                       //System.out.println("DEBUG: Assigning regPort: " + rmiRegCount);
+                       //iRMIRegPort = rmiRegCount++;
                        // Check port existence in HashMap
                } while (portIsAvailable(iRMIRegPort) == false);
                hmRMIRegPort.put(iNumOfObjects, iRMIRegPort);
+               hsAllPorts.add(iRMIRegPort);
 
                int iRMIStubPort = 0;
                do {
                        iRMIStubPort = random.nextInt(INT_MAX_PORT - INT_MIN_PORT + 1) + INT_MIN_PORT;
+                       //System.out.println("DEBUG: Assigning stubPort: " + rmiStubCount);
+                       //iRMIStubPort = rmiStubCount++;
                        // Check port existence in HashMap
                } while (portIsAvailable(iRMIStubPort) == false);
                hmRMIStubPort.put(iNumOfObjects, iRMIStubPort);
+               hsAllPorts.add(iRMIStubPort);
 
                iNumOfObjects++;
        }
@@ -184,74 +228,48 @@ public final class CommunicationHandler {
                listObjCrtInfo.add(objCrtInfo);
        }
 
-
        /**
-        * Method getCallbackPort()
+        * Method addDevicePort()
         * <p>
-        * Get a new port for new connections for callback objects in the program.
-        * This newly generated port number will be recorded.
+        * Add a port that is used by a device when communicating with its driver
+        * This port will be taken into account when checking for port availability
         *
-        * @return  int[]       A set of callback ports
+        * @param   iDevPort  Device port number
+        * @return  void
         */
-       public int[] getCallbackPorts(int numOfPorts) {
-
-               int[] ports = new int[numOfPorts];
-
-               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]);
-               }
+       public void addDevicePort(int iDevPort) {
 
-               return ports;
+               hsDevicePort.add(iDevPort);
+               // Track this port number
+        hsAllPorts.add(iDevPort);
        }
 
-
        /**
-        * Method getCallbackPorts()
+        * Method addAdditionalPort()
         * <p>
-        * Get a set of new ports for new connections for callback objects in the program.
+        * Add a new port for new connections for any objects in the program.
         * This newly generated port number will be recorded.
         *
-        * @return  int[]       A set of callback ports
+        * @return  int         One new port
         */
-       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;
-       }
+       public int addAdditionalPort(String sAObject) {
 
+               hmActiveObj.put(sAObject, iNumOfObjects);
 
-       /**
-        * Method addDevicePort()
-        * <p>
-        * Add a port that is used by a device when communicating with its driver
-        * This port will be taken into account when checking for port availability
-        *
-        * @param   iDevPort  Device port number
-        * @return  void
-        */
-       public void addDevicePort(int iDevPort) {
+               int iAdditionalPort = 0;
+               do {
+                       iAdditionalPort = random.nextInt(INT_MAX_PORT - INT_MIN_PORT + 1) + INT_MIN_PORT;
+                       //System.out.println("DEBUG: Assigning additionalPort: " + additionalCount);
+                       //iAdditionalPort = additionalCount++;
+                       // Check port existence in HashMap
+               } while (portIsAvailable(iAdditionalPort) == false);
+               hmAdditionalPort.put(iNumOfObjects, iAdditionalPort);
+               // Track this port number
+               hsAllPorts.add(iAdditionalPort);
 
-               hsDevicePort.add(iDevPort);
+               iNumOfObjects++;
 
+               return iAdditionalPort;
        }
 
        /**
@@ -272,13 +290,18 @@ public final class CommunicationHandler {
                        return false;
                } else if (hmRMIStubPort.containsValue(iPortNumber) == true) {
                        return false;
-               } else if (hsDevicePort.contains(iPortNumber) == true) {
+               } else if (hmAdditionalPort.containsValue(iPortNumber) == true) {
                        return false;
-               } else if (hsCallbackPort.contains(iPortNumber) == true) {
+               } else if (hsDevicePort.contains(iPortNumber) == true) {
                        return false;
                } else {
                        return true;
                }
+               //if (hsAllPorts.contains(iPortNumber)) {
+               //    return false;
+               //} else {
+               //    return true;
+               //}
        }
 
        /**
@@ -367,6 +390,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>
@@ -479,13 +515,13 @@ public final class CommunicationHandler {
                                RuntimeOutput.print("Communication Port: " + hmComPort.get(iIndex), bVerbose);
                                RuntimeOutput.print("RMI Registry Port: " + hmRMIRegPort.get(iIndex), bVerbose);
                                RuntimeOutput.print("RMI Stub Port: " + hmRMIStubPort.get(iIndex), bVerbose);
+                               RuntimeOutput.print("\n", bVerbose);
                        }
                }
 
                for(int iPort : hsDevicePort) {
-
                        RuntimeOutput.print("Device Port: " + iPort, bVerbose);
-
                }
+               RuntimeOutput.print("\n", bVerbose);
        }
 }