X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fnet%2Fpterodactylus%2Fjsite%2Fcore%2FNodeManager.java;h=9cdc60d55a57ad593d0406d4d9abc4eaf21275c3;hb=f8ec8ae7e1c3b69ddac1cc58201803e052bd720e;hp=922a85a1ab78a5c6a49513d30c19121f4d3a7d97;hpb=dca15b39fe385e878fc17c684c3efd77a193bf0a;p=jSite2.git diff --git a/src/net/pterodactylus/jsite/core/NodeManager.java b/src/net/pterodactylus/jsite/core/NodeManager.java index 922a85a..9cdc60d 100644 --- a/src/net/pterodactylus/jsite/core/NodeManager.java +++ b/src/net/pterodactylus/jsite/core/NodeManager.java @@ -49,7 +49,7 @@ import net.pterodactylus.util.number.Hex; /** * TODO - * + * * @author David ‘Bombe’ Roden <bombe@freenetproject.org> */ public class NodeManager implements Iterable, PropertyChangeListener, HighLevelClientListener { @@ -66,24 +66,27 @@ 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. - * + * * @param clientName * The name of the FCP client * @param directory @@ -100,92 +103,22 @@ public class NodeManager implements Iterable, PropertyChangeListener, High /** * Adds the given listener to the list of listeners. - * + * * @param nodeListener * The listener to add */ public void addNodeListener(NodeListener nodeListener) { - nodeListeners.add(nodeListener); + nodeListenerSupport.addListener(nodeListener); } /** * Removes the given listener from the list of listeners. - * + * * @param nodeListener * 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); } // @@ -194,7 +127,7 @@ public class NodeManager implements Iterable, PropertyChangeListener, High /** * Returns the directory in which the nodes are stored. - * + * * @return The directory the nodes are stored in */ public String getDirectory() { @@ -203,7 +136,7 @@ public class NodeManager implements Iterable, PropertyChangeListener, High /** * Checks whether the given node is already connected. - * + * * @param node * The node to check * @return true if the node is already connected, @@ -226,7 +159,7 @@ public class NodeManager implements Iterable, PropertyChangeListener, High /** * Loads nodes. - * + * * @throws IOException * if an I/O error occurs loading the nodes */ @@ -294,7 +227,7 @@ public class NodeManager implements Iterable, PropertyChangeListener, High /** * Saves all configured nodes. - * + * * @throws IOException * if an I/O error occurs saving the nodes */ @@ -327,12 +260,12 @@ public class NodeManager implements Iterable, PropertyChangeListener, High /** * Adds the given node to this manager. - * + * * @see #connect(Node) * @param node * The node to connect to - * @return true if the node was added, false - * if the node was not added because it was already known + * @return true if the node was added, false if + * the node was not added because it was already known */ public boolean addNode(Node node) { logger.log(Level.FINEST, "addNode(node=" + node + ")"); @@ -347,14 +280,14 @@ 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; } /** * Removes the given node from the node manager, disconnecting it if it is * currently connected. - * + * * @param node * The node to remove */ @@ -370,13 +303,13 @@ public class NodeManager implements Iterable, PropertyChangeListener, High nodes.remove(node); idNodes.remove(node.getId()); node.removePropertyChangeListener(this); - fireNodeRemoved(node); + nodeListenerSupport.fireNodeRemoved(node); } } /** * Tries to establish a connection with the given node. - * + * * @param node * The node to connect to */ @@ -391,15 +324,15 @@ 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); } } /** * Disconnects the given node without removing it. - * + * * @param node * The node to disconnect */ @@ -416,7 +349,7 @@ public class NodeManager implements Iterable, PropertyChangeListener, High /** * Returns a list of all nodes. - * + * * @return A list of all nodes */ public List getNodes() { @@ -425,7 +358,7 @@ public class NodeManager implements Iterable, PropertyChangeListener, High /** * Returns the high-level client for a given node. - * + * * @param node * The node to get a high-level client for * @return The high-level client for a node, or null if the @@ -437,7 +370,7 @@ public class NodeManager implements Iterable, PropertyChangeListener, High /** * Returns the node for a high-level client. - * + * * @param highLevelClient * The high-level client to get the node for * @return The node for the high-level client, or null if the @@ -449,11 +382,11 @@ public class NodeManager implements Iterable, PropertyChangeListener, High /** * Returns the node identified by the given ID. - * + * * @param id * The ID of the node - * @return The node with the given ID, or null if no such - * node was found + * @return The node with the given ID, or null if no such node + * was found */ Node getNode(String id) { return idNodes.get(id); @@ -461,7 +394,7 @@ public class NodeManager implements Iterable, PropertyChangeListener, High /** * Generates a new SSK key pair. - * + * * @return An array with the private key at index 0 and the * public key at index 1 * @throws IOException @@ -505,7 +438,7 @@ public class NodeManager implements Iterable, PropertyChangeListener, High logger.log(Level.WARNING, "got event for unknown client"); return; } - fireNodeConnected(node); + nodeListenerSupport.fireNodeConnected(node); } /** @@ -519,7 +452,7 @@ public class NodeManager implements Iterable, PropertyChangeListener, High logger.log(Level.WARNING, "got event for unknown client"); return; } - fireNodeDisconnected(node, throwable); + nodeListenerSupport.fireNodeDisconnected(node, throwable); } } @@ -546,9 +479,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); } } }