Manage currently connected nodes.
[jSite2.git] / src / net / pterodactylus / jsite / core / NodeManager.java
index 02f03b3..957e1bc 100644 (file)
@@ -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;
 
@@ -70,16 +72,19 @@ public class NodeManager implements Iterable<Node>, PropertyChangeListener, High
        private final NodeListenerSupport nodeListenerSupport = new NodeListenerSupport();
 
        /** All nodes. */
-       private List<Node> nodes = Collections.synchronizedList(new ArrayList<Node>());
+       private final List<Node> nodes = Collections.synchronizedList(new ArrayList<Node>());
 
        /** Map from node ID to node. */
-       private Map<String, Node> idNodes = Collections.synchronizedMap(new HashMap<String, Node>());
+       private final Map<String, Node> idNodes = Collections.synchronizedMap(new HashMap<String, Node>());
 
        /** All FCP connections. */
-       private Map<Node, HighLevelClient> nodeClients = Collections.synchronizedMap(new HashMap<Node, HighLevelClient>());
+       private final Map<Node, HighLevelClient> nodeClients = Collections.synchronizedMap(new HashMap<Node, HighLevelClient>());
 
        /** Maps nodes to high-level clients. */
-       private Map<HighLevelClient, Node> clientNodes = Collections.synchronizedMap(new HashMap<HighLevelClient, Node>());
+       private final Map<HighLevelClient, Node> clientNodes = Collections.synchronizedMap(new HashMap<HighLevelClient, Node>());
+
+       /** Collection of currently connected nodes. */
+       private final Set<Node> connectedNodes = Collections.synchronizedSet(new HashSet<Node>());
 
        /**
         * Creates a new FCP collector.
@@ -144,6 +149,18 @@ public class NodeManager implements Iterable<Node>, PropertyChangeListener, High
        }
 
        /**
+        * Returns whether the given node is currently connected.
+        *
+        * @param node
+        *            The node to check
+        * @return <code>true</code> if the node is currently connected,
+        *         <code>false</code> otherwise
+        */
+       public boolean isNodeConnected(Node node) {
+               return connectedNodes.contains(node);
+       }
+
+       /**
         * {@inheritDoc}
         */
        public Iterator<Node> iterator() {
@@ -435,6 +452,7 @@ public class NodeManager implements Iterable<Node>, PropertyChangeListener, High
                        logger.log(Level.WARNING, "got event for unknown client");
                        return;
                }
+               connectedNodes.add(node);
                nodeListenerSupport.fireNodeConnected(node);
        }
 
@@ -449,6 +467,7 @@ public class NodeManager implements Iterable<Node>, PropertyChangeListener, High
                                logger.log(Level.WARNING, "got event for unknown client");
                                return;
                        }
+                       connectedNodes.remove(node);
                        nodeListenerSupport.fireNodeDisconnected(node, throwable);
                }
        }