X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fnet%2Fpterodactylus%2Fjsite%2Fcore%2FNodeManager.java;h=957e1bc33a92f9574a954f6169007bcb38ae411f;hb=f09a940c1098882fb137ad0b96e8bc9835170d3f;hp=40e4190f3067e7927f283051f23566456c27bff8;hpb=3ac91e8b526e04d6be1fc1bb16154633a0ad957e;p=jSite2.git diff --git a/src/net/pterodactylus/jsite/core/NodeManager.java b/src/net/pterodactylus/jsite/core/NodeManager.java index 40e4190..957e1bc 100644 --- a/src/net/pterodactylus/jsite/core/NodeManager.java +++ b/src/net/pterodactylus/jsite/core/NodeManager.java @@ -31,10 +31,12 @@ import java.net.UnknownHostException; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Properties; +import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; @@ -66,20 +68,23 @@ public class NodeManager implements Iterable, PropertyChangeListener, High /** Object used for synchronization. */ private final Object syncObject = new Object(); - /** Node listeners. */ - private List nodeListeners = Collections.synchronizedList(new ArrayList()); + /** Node listener support. */ + private final NodeListenerSupport nodeListenerSupport = new NodeListenerSupport(); /** All nodes. */ - private List nodes = Collections.synchronizedList(new ArrayList()); + private final List nodes = Collections.synchronizedList(new ArrayList()); /** Map from node ID to node. */ - private Map idNodes = Collections.synchronizedMap(new HashMap()); + private final Map idNodes = Collections.synchronizedMap(new HashMap()); /** All FCP connections. */ - private Map nodeClients = Collections.synchronizedMap(new HashMap()); + private final Map nodeClients = Collections.synchronizedMap(new HashMap()); /** Maps nodes to high-level clients. */ - private Map clientNodes = Collections.synchronizedMap(new HashMap()); + private final Map clientNodes = Collections.synchronizedMap(new HashMap()); + + /** Collection of currently connected nodes. */ + private final Set connectedNodes = Collections.synchronizedSet(new HashSet()); /** * Creates a new FCP collector. @@ -105,7 +110,7 @@ public class NodeManager implements Iterable, PropertyChangeListener, High * The listener to add */ public void addNodeListener(NodeListener nodeListener) { - nodeListeners.add(nodeListener); + nodeListenerSupport.addListener(nodeListener); } /** @@ -115,77 +120,7 @@ public class NodeManager implements Iterable, PropertyChangeListener, High * The listener to remove */ public void removeNodeListener(NodeListener nodeListener) { - nodeListeners.remove(nodeListener); - } - - /** - * Notifies all listeners that a node was added. - * - * @param node - * The node that was added. - */ - private void fireNodeAdded(Node node) { - logger.log(Level.FINEST, "firing nodeAdded event with [node=" + node + "]"); - for (NodeListener nodeListener : nodeListeners) { - nodeListener.nodeAdded(node); - } - } - - /** - * Notifies all listeners that a node was removed. - * - * @param node - * The node that was removed - */ - private void fireNodeRemoved(Node node) { - logger.log(Level.FINEST, "firing nodeRemoved event with [node=" + node + "]"); - for (NodeListener nodeListener : nodeListeners) { - nodeListener.nodeRemoved(node); - } - } - - /** - * Notifies all listeners that the given node was connected. - * - * @param node - * The node that is now connected - */ - private void fireNodeConnected(Node node) { - logger.log(Level.FINEST, "firing nodeConnected event with [node=" + node + "]"); - for (NodeListener nodeListener : nodeListeners) { - nodeListener.nodeConnected(node); - } - } - - /** - * Notifies all listeners that a connection to a node has failed. - * - * @param node - * The node that could not be connected - * @param cause - * The cause of the failure - */ - private void fireNodeConnectionFailed(Node node, Throwable cause) { - logger.log(Level.FINEST, "firing nodeConnectionFailed event with [node=" + node + ",cause=" + cause + "]"); - for (NodeListener nodeListener : nodeListeners) { - nodeListener.nodeConnectionFailed(node, cause); - } - } - - /** - * Notifies all listeners that the given node was disconnected. - * - * @param node - * The node that is now disconnected - * @param throwable - * The exception that caused the disconnect, or null - * if there was no exception - */ - private void fireNodeDisconnected(Node node, Throwable throwable) { - logger.log(Level.FINEST, "firing nodeDisconnected event with [node=" + node + ",throwable=" + throwable + "]"); - for (NodeListener nodeListener : nodeListeners) { - nodeListener.nodeDisconnected(node, throwable); - } + nodeListenerSupport.removeListener(nodeListener); } // @@ -214,6 +149,18 @@ public class NodeManager implements Iterable, PropertyChangeListener, High } /** + * Returns whether the given node is currently connected. + * + * @param node + * The node to check + * @return true if the node is currently connected, + * false otherwise + */ + public boolean isNodeConnected(Node node) { + return connectedNodes.contains(node); + } + + /** * {@inheritDoc} */ public Iterator iterator() { @@ -347,7 +294,7 @@ public class NodeManager implements Iterable, PropertyChangeListener, High clientNodes.put(highLevelClient, node); nodeClients.put(node, highLevelClient); highLevelClient.addHighLevelClientListener(this); - fireNodeAdded(node); + nodeListenerSupport.fireNodeAdded(node); return true; } @@ -370,7 +317,7 @@ public class NodeManager implements Iterable, PropertyChangeListener, High nodes.remove(node); idNodes.remove(node.getId()); node.removePropertyChangeListener(this); - fireNodeRemoved(node); + nodeListenerSupport.fireNodeRemoved(node); } } @@ -391,9 +338,9 @@ public class NodeManager implements Iterable, PropertyChangeListener, High try { highLevelClient.connect(node.getHostname(), node.getPort()); } catch (UnknownHostException uhe1) { - fireNodeConnectionFailed(node, uhe1); + nodeListenerSupport.fireNodeConnectionFailed(node, uhe1); } catch (IOException ioe1) { - fireNodeConnectionFailed(node, ioe1); + nodeListenerSupport.fireNodeConnectionFailed(node, ioe1); } } @@ -505,7 +452,8 @@ public class NodeManager implements Iterable, PropertyChangeListener, High logger.log(Level.WARNING, "got event for unknown client"); return; } - fireNodeConnected(node); + connectedNodes.add(node); + nodeListenerSupport.fireNodeConnected(node); } /** @@ -519,7 +467,8 @@ public class NodeManager implements Iterable, PropertyChangeListener, High logger.log(Level.WARNING, "got event for unknown client"); return; } - fireNodeDisconnected(node, throwable); + connectedNodes.remove(node); + nodeListenerSupport.fireNodeDisconnected(node, throwable); } } @@ -546,9 +495,9 @@ public class NodeManager implements Iterable, PropertyChangeListener, High try { highLevelClient.connect(node.getHostname(), node.getPort()); } catch (UnknownHostException uhe1) { - fireNodeConnectionFailed(node, uhe1); + nodeListenerSupport.fireNodeConnectionFailed(node, uhe1); } catch (IOException ioe1) { - fireNodeConnectionFailed(node, ioe1); + nodeListenerSupport.fireNodeConnectionFailed(node, ioe1); } } }