NetworkServerAbstract: make LastSeenHost immutable and create method getTargeAddress...
authorTJ <hacker@iam.tj>
Sun, 3 May 2015 10:47:55 +0000 (11:47 +0100)
committerTJ <hacker@iam.tj>
Sun, 3 May 2015 10:52:26 +0000 (11:52 +0100)
src/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkServerAbstract.java

index d6f9f2b..691afc6 100644 (file)
@@ -109,9 +109,12 @@ 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 {
-        long timeInMillis;
-        InetSocketAddress address;
+        final long timeInMillis;
+        final InetSocketAddress address;
         
         LastSeenHost(InetSocketAddress address, long timeInMillis) {
             this.address = address;
@@ -121,6 +124,14 @@ public abstract class NetworkServerAbstract extends SwingWorker<Integer, Network
             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.
@@ -173,7 +184,7 @@ public abstract class NetworkServerAbstract extends SwingWorker<Integer, Network
     protected static void log(Level level, String title, String message) {
         if (LOGGER == null)
             return;
-        LOGGER.logp(level, title, null, MessageFormat.format("{1}", message));
+        LOGGER.logp(level, title, null, message);
     }
 
     /**
@@ -433,6 +444,24 @@ 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.
      *