*/
WSYD_SocketAddress _socketAddress;
+ protected ServiceAddressMap _serviceToHostMap;
/**
* Thread safe First In, First Out Queue of NetworkMessage objects waiting to be sent.
*
*/
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
*/
this._connectionCount = 0;
this._title = null;
this._socketAddress = null;
+ this._serviceToHostMap = null;
}
/**
*
* @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;
}
*
* @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);
}
/**
@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.
*
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;
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.
*