Move ServiceToHostMap into stand-alone class ServiceAddressMap and refactor accordingly
[WeStealzYourDataz.git] / src / uk / ac / ntu / n0521366 / wsyd / libs / net / NetworkServerAbstract.java
index 691afc6..4119a5c 100644 (file)
@@ -101,6 +101,7 @@ public abstract class NetworkServerAbstract extends SwingWorker<Integer, Network
      */
     WSYD_SocketAddress _socketAddress;
 
+    protected ServiceAddressMap _serviceToHostMap;
     /**
      * Thread safe First In, First Out Queue of NetworkMessage objects waiting to be sent.
      * 
@@ -109,44 +110,6 @@ public abstract class NetworkServerAbstract extends SwingWorker<Integer, Network
      */
     protected ConcurrentLinkedQueue<NetworkMessage> _sendMessageQueue = new ConcurrentLinkedQueue<>();
 
-    /**
-     * Encapsulates a unique network host and the last time it was seen.
-     */
-    protected class LastSeenHost {
-        final long timeInMillis;
-        final InetSocketAddress address;
-        
-        LastSeenHost(InetSocketAddress address, long timeInMillis) {
-            this.address = address;
-            this.timeInMillis = timeInMillis;
-        }
-        LastSeenHost(InetSocketAddress host) {
-            this(host, System.currentTimeMillis());
-        }
-        
-        /**
-         * Formatted string representation of IneAddress and timestamp.
-         * @return the representation
-         */
-        @Override
-        public String toString() {
-            return MessageFormat.format("{0}:{1,number,integer}@{2}", this.address.getHostString(), this.address.getPort(), this.timeInMillis);
-        }
-    };
-    /**
-     * Maps service _title to its parent network host.
-     * <p>
-     * Used by methods on the Owner Thread to determine the list of valid service
-     * names it can submit messages to (by iterating the keys using keySet()).</p>
-     * <p>
-     * New service names can be added in two ways:<br/>
-     * <ol>
-     *  <li>by the Worker Thread from received messages</li>
-     *  <li>by the Owner or (other thread) from a service discovery helper (such as multicast discovery)</li>
-     * </ol>
-     */
-    protected ConcurrentHashMap<String, LastSeenHost> _serviceToHostMap = new ConcurrentHashMap<>();;
-
     /**
      * Wrapper for filtering NetworkMessageEvents based on the message intent
      */
@@ -209,6 +172,7 @@ public abstract class NetworkServerAbstract extends SwingWorker<Integer, Network
         this._connectionCount = 0;
         this._title = null;
         this._socketAddress = null;
+        this._serviceToHostMap = null;
     }
     
     /**
@@ -218,12 +182,14 @@ public abstract class NetworkServerAbstract extends SwingWorker<Integer, Network
      * 
      * @param socketAddress The socket to listen on
      * @param title source identifier for use in log messages and sent NetworkMessage objects
+     * @param serviceToHostMap the map object used for host <> InetSocketAddress lookups
      * @param logger An instance of Logger to be used by all objects of this class
      */
-    public NetworkServerAbstract(WSYD_SocketAddress socketAddress, String title, Logger logger) {
+    public NetworkServerAbstract(WSYD_SocketAddress socketAddress, String title, ServiceAddressMap serviceToHostMap, Logger logger) {
         this._connectionCount = 0;
         this._title = title;
         this._socketAddress = socketAddress;
+        this._serviceToHostMap = serviceToHostMap;
         if (LOGGER == null) // do not replace existing logger reference
             LOGGER = logger;
     }
@@ -235,9 +201,10 @@ public abstract class NetworkServerAbstract extends SwingWorker<Integer, Network
      * 
      * @param socketAddress The socket to listen on
      * @param title source identifier for use in log messages and sent NetworkMessage objects
+     * @param serviceToHostMap the map object used for host <> InetSocketAddress lookups
      */
-    public NetworkServerAbstract(WSYD_SocketAddress socketAddress, String title) {
-        this(socketAddress, title, null);
+    public NetworkServerAbstract(WSYD_SocketAddress socketAddress, String title, ServiceAddressMap serviceToHostMap) {
+        this(socketAddress, title, serviceToHostMap, null);
     }
 
     /**
@@ -403,16 +370,6 @@ public abstract class NetworkServerAbstract extends SwingWorker<Integer, Network
     @Override
     protected abstract void done();
 
-
-    /**
-     * Ensure service is in the map of known hosts.
-     * @param service the service name to check
-     * @return true is the target service is known
-     */
-    protected boolean isServiceValid(String service) {
-        return this._serviceToHostMap.containsKey(service);
-    }
-
     /**
      * Adds a message to the queue of pending messages.
      * 
@@ -429,7 +386,7 @@ public abstract class NetworkServerAbstract extends SwingWorker<Integer, Network
             String target = message.getTarget();
             if (target == null)
                 throw new IllegalArgumentException("target cannot be null");
-            if(!isServiceValid(target))
+            if(!_serviceToHostMap.isServiceValid(target))
                 throw new IllegalArgumentException("target service does not exist: " + target);
             
             NetworkMessage temp;
@@ -444,24 +401,6 @@ public abstract class NetworkServerAbstract extends SwingWorker<Integer, Network
         return result;
     }
 
-    /**
-     * Get the current InetAddress of a target from the services map.
-     * 
-     * @param target name of the service
-     * @return IP address and port of the service
-     */
-    public InetSocketAddress getTargetAddress(String target) {
-        InetSocketAddress result = null;
-        
-        if (target != null && target.length() > 0) {
-            LastSeenHost host = this._serviceToHostMap.get(target);
-            if (host != null)
-                result = host.address;
-        }
-        
-        return result;
-    }
-
     /**
      * Add a NetworkMessageEvent listener.
      *