X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=iotjava%2Fiotruntime%2Fmaster%2FCommunicationHandler.java;h=6445c093e10cfae506f38385e703dd43335c930a;hb=7e8c0cabb7057ad561adbb8b6cc2a0fd5c41bbf4;hp=db6b16193d46b1372ceda8b870e2f75ea18bf031;hpb=47a251f41e05f47b4bc19c236ab9441cef5ed42c;p=iot2.git diff --git a/iotjava/iotruntime/master/CommunicationHandler.java b/iotjava/iotruntime/master/CommunicationHandler.java index db6b161..6445c09 100644 --- a/iotjava/iotruntime/master/CommunicationHandler.java +++ b/iotjava/iotruntime/master/CommunicationHandler.java @@ -11,14 +11,14 @@ import java.util.Set; /** Class CommunicationHandler is a class that maintains * a data structure that preserves a collection of host name, * port numbers, and objects that are running - * +-----------------+----+--------+------------------+--------------+----------------+ - * | HOST-ADDRESS | ...|IN-PORT | RMIREGISTRY-PORT | RMISTUB-PORT | ACTIVE OBJECTS | - * +-----------------+----+--------+------------------+--------------+----------------+ - * | XXX.XXX.XXX.XXX | | XXXXX | XXXXX | XXXXX | XXXXXXXXXXXXXX | - * | | | XXXXX | XXXXX | XXXXX | XXXXXXXXXXXXXX | - * | | | XXXXX | XXXXX | XXXXX | XXXXXXXXXXXXXX | - * | | ...| ... | ... | ... | ... | - * +-----------------+----+--------+------------------+--------------+----------------+ + * +-----------------+----+--------+------------------+--------------+----+----------------+ + * | HOST-ADDRESS | ...|IN-PORT | RMIREGISTRY-PORT | RMISTUB-PORT | ...| ACTIVE OBJECTS | + * +-----------------+----+--------+------------------+--------------+----+----------------+ + * | XXX.XXX.XXX.XXX | | XXXXX | XXXXX | XXXXX | | XXXXXXXXXXXXXX | + * | | | XXXXX | XXXXX | XXXXX | | XXXXXXXXXXXXXX | + * | | | XXXXX | XXXXX | XXXXX | | XXXXXXXXXXXXXX | + * | | ...| ... | ... | ... | ...| ... | + * +-----------------+----+--------+------------------+--------------+----+----------------+ * In this case we use ACTIVE OBJECTS names as the key * So ACTIVE OBJECTS maps to numbers and these numbers map to each other * entry in hashmaps (HostAddress can be repetitive) @@ -26,19 +26,6 @@ import java.util.Set; * ProximitySensorPS1 - 1 * TempSensorTS1 - 2 * ... - * IN-PORT / RMIREGISTRY-PORT / RMISTUB-PORT / HOST-ADDRESS: 0 - XXXXX - * 1 - XXXXX - * 2 - XXXXX - * +-------------+ - * | DEVICE-PORT | - * +-------------+ - * | XXXXX | - * | XXXXX | - * | XXXXX | - * | ... | - * +-------------+ - * We add a Set structure to handle all the other ports that are used by devices - * when communicating with their respective drivers * * @author Rahmadi Trimananda * @version 1.0 @@ -67,8 +54,7 @@ public final class CommunicationHandler { private Map hmRMIRegPort; private Map hmRMIStubPort; private Set hsDevicePort; - private Set hsCallbackPort; - private Map hmCallbackPort; + private Map hmAdditionalPort; private int iNumOfObjects; private int iNumOfHosts; private boolean bVerbose; @@ -80,7 +66,7 @@ public final class CommunicationHandler { private final int INT_MIN_PORT = 10000; /** - * Empty constructor + * Constructor */ public CommunicationHandler(boolean _bVerbose) { @@ -97,13 +83,39 @@ public final class CommunicationHandler { hmRMIRegPort = new HashMap(); hmRMIStubPort = new HashMap(); hsDevicePort = new HashSet(); - hsCallbackPort = new HashSet(); - hmCallbackPort = new HashMap(); + hmAdditionalPort = new HashMap(); iNumOfObjects = 0; iNumOfHosts = 0; bVerbose = _bVerbose; RuntimeOutput.print("CommunicationHandler: Creating a new CommunicationHandler object!", bVerbose); } + + /** + * Method clearCommunicationHandler() + *

+ * 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,21 +150,21 @@ public final class CommunicationHandler { int iComPort = 0; do { iComPort = random.nextInt(INT_MAX_PORT - INT_MIN_PORT + 1) + INT_MIN_PORT; - // Check port existence in HashMap + // Check port existence in HashMap } while (portIsAvailable(iComPort) == false); hmComPort.put(iNumOfObjects, iComPort); int iRMIRegPort = 0; do { iRMIRegPort = random.nextInt(INT_MAX_PORT - INT_MIN_PORT + 1) + INT_MIN_PORT; - // Check port existence in HashMap + // Check port existence in HashMap } while (portIsAvailable(iRMIRegPort) == false); hmRMIRegPort.put(iNumOfObjects, iRMIRegPort); int iRMIStubPort = 0; do { iRMIStubPort = random.nextInt(INT_MAX_PORT - INT_MIN_PORT + 1) + INT_MIN_PORT; - // Check port existence in HashMap + // Check port existence in HashMap } while (portIsAvailable(iRMIStubPort) == false); hmRMIStubPort.put(iNumOfObjects, iRMIStubPort); @@ -184,74 +196,42 @@ public final class CommunicationHandler { listObjCrtInfo.add(objCrtInfo); } - /** - * Method getCallbackPort() + * Method addDevicePort() *

- * 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); } - /** - * Method getCallbackPorts() + * Method addAdditionalPort() *

- * 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() - *

- * 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; + // Check port existence in HashMap + } while (portIsAvailable(iAdditionalPort) == false); + hmAdditionalPort.put(iNumOfObjects, iAdditionalPort); - hsDevicePort.add(iDevPort); + iNumOfObjects++; + return iAdditionalPort; } /** @@ -272,9 +252,9 @@ 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; @@ -367,6 +347,19 @@ public final class CommunicationHandler { return hmComPort.get(hmActiveObj.get(sAObject)); } + /** + * Method getAdditionalPort() + *

+ * 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() *

@@ -479,13 +472,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); } }