package net.pterodactylus.jsite.core;
+import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+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;
* @author David ‘Bombe’ Roden <bombe@freenetproject.org>
* @version $Id$
*/
-public class NodeManager implements Iterable<Node>, HighLevelClientListener {
+public class NodeManager implements Iterable<Node>, PropertyChangeListener, HighLevelClientListener {
/** Logger. */
private static final Logger logger = Logging.getLogger(NodeManager.class.getName());
/** All FCP connections. */
private Map<Node, HighLevelClient> nodeClients = Collections.synchronizedMap(new HashMap<Node, HighLevelClient>());
- /** Keeps track of which connection is in use right now. */
- private Set<HighLevelClient> usedConnections = Collections.synchronizedSet(new HashSet<HighLevelClient>());
-
/** Maps nodes to high-level clients. */
private Map<HighLevelClient, Node> clientNodes = Collections.synchronizedMap(new HashMap<HighLevelClient, 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) {
+ for (NodeListener nodeListener: nodeListeners) {
+ nodeListener.nodeConnectionFailed(node, cause);
+ }
+ }
+
+ /**
* Notifies all listeners that the given node was disconnected.
*
* @param node
logger.fine("loaded " + loadedNodes.size() + " nodes from config");
synchronized (syncObject) {
nodes.clear();
- nodes.addAll(loadedNodes);
- }
- for (Node node: nodes) {
- fireNodeAdded(node);
+ for (Node node: loadedNodes) {
+ addNode(node);
+ }
}
}
* @see #connect(Node)
* @param node
* The node to connect to
+ * @return <code>true</code> if the node was added, <code>false</code>
+ * if the node was not added because it was already known
*/
- public void addNode(Node node) {
- synchronized (syncObject) {
- if (!nodes.contains(node)) {
- nodes.add(node);
- fireNodeAdded(node);
- }
+ public boolean addNode(Node node) {
+ if (nodes.contains(node)) {
+ logger.warning("was told to add already known node: " + node);
+ return false;
}
+ node.addPropertyChangeListener(this);
+ HighLevelClient highLevelClient = new HighLevelClient(clientName);
+ nodes.add(node);
+ clientNodes.put(highLevelClient, node);
+ nodeClients.put(node, highLevelClient);
+ highLevelClient.addHighLevelClientListener(this);
+ fireNodeAdded(node);
+ return true;
}
/**
if (nodeClients.containsKey(node)) {
disconnect(node);
}
+ node.removePropertyChangeListener(this);
fireNodeRemoved(node);
}
}
* The node to connect to
*/
public void connect(Node node) {
+ HighLevelClient highLevelClient;
+ highLevelClient = nodeClients.get(node);
+ if (highLevelClient == null) {
+ logger.warning("was told to connect to unknown node: " + node);
+ return;
+ }
try {
- HighLevelClient highLevelClient = new HighLevelClient(clientName, node.getHostname(), node.getPort());
- synchronized (syncObject) {
- clientNodes.put(highLevelClient, node);
- nodeClients.put(node, highLevelClient);
- }
- highLevelClient.addHighLevelClientListener(this);
- highLevelClient.connect();
+ highLevelClient.connect(node.getHostname(), node.getPort());
+ } catch (UnknownHostException uhe1) {
+ fireNodeConnectionFailed(node, uhe1);
} catch (IOException ioe1) {
- fireNodeDisconnected(node, ioe1);
+ fireNodeConnectionFailed(node, ioe1);
}
}
}
/**
- * “Borrows” a high-level client for the given node. A borrowed client
- * <strong>has</strong> to be returned to the node manager using
- * {@link #returnHighLevelClient(HighLevelClient)} when it is no longer in
- * use, i.e. after a message has been sent! This method will block until a
- * high-level client for the given node is available.
+ * 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 <code>null</code> if the
* node was disconnected or removed
*/
- public HighLevelClient borrowHighLevelClient(Node node) {
- synchronized (syncObject) {
- if (!nodeClients.containsKey(node)) {
- return null;
- }
- HighLevelClient highLevelClient = nodeClients.get(node);
- while (nodeClients.containsKey(node) && usedConnections.contains(highLevelClient)) {
- try {
- syncObject.wait();
- } catch (InterruptedException ie1) {
- /* ignore. TODO - check. */
- }
- }
- if (!nodeClients.containsKey(node)) {
- return null;
- }
- usedConnections.add(highLevelClient);
- return highLevelClient;
- }
+ public HighLevelClient getHighLevelClient(Node node) {
+ return nodeClients.get(node);
}
/**
- * Returns a borrowed high-level client.
+ * Returns the node for a high-level client.
*
- * @see #borrowHighLevelClient(Node)
* @param highLevelClient
- * The high-level client to return
+ * The high-level client to get the node for
+ * @return The node for the high-level client, or <code>null</code> if the
+ * high-level client is not known
*/
- public void returnHighLevelClient(HighLevelClient highLevelClient) {
- synchronized (syncObject) {
- if (!clientNodes.containsKey(highLevelClient)) {
- return;
- }
- usedConnections.remove(highLevelClient);
- syncObject.notifyAll();
- }
+ public Node getNode(HighLevelClient highLevelClient) {
+ return clientNodes.get(highLevelClient);
}
//
// PRIVATE METHODS
//
- /**
- * Finds a currently unused high-level client, optionally waiting until a
- * client is free and marking it used.
- *
- * @param wait
- * <code>true</code> to wait for a free connection,
- * <code>false</code> to return <code>null</code>
- * @param markAsUsed
- * <code>true</code> to mark the connection as used before
- * returning it, <code>false</code> not to mark it
- * @return An unused FCP connection, or <code>null</code> if no connection
- * could be found
- */
- @SuppressWarnings("unused")
- private HighLevelClient findUnusedClient(boolean wait, boolean markAsUsed) {
- synchronized (syncObject) {
- HighLevelClient freeHighLevelClient = null;
- while (freeHighLevelClient == null) {
- for (HighLevelClient highLevelClient: nodeClients.values()) {
- if (!usedConnections.contains(highLevelClient)) {
- freeHighLevelClient = highLevelClient;
- break;
- }
- }
- if (freeHighLevelClient != null) {
- if (markAsUsed) {
- usedConnections.add(freeHighLevelClient);
- }
- return freeHighLevelClient;
- }
- if (!wait) {
- return null;
- }
- try {
- syncObject.wait();
- } catch (InterruptedException e) {
- /* ignore, just re-check. */
- }
- }
- /* we never get here, but the compiler doesn't realize. */
- return null;
- }
- }
-
//
// INTERFACE HighLevelClientListener
//
public void clientDisconnected(HighLevelClient highLevelClient, Throwable throwable) {
logger.log(Level.FINER, "clientDisconnected(c=" + highLevelClient + ",t=" + throwable + ")");
synchronized (syncObject) {
- Node node = clientNodes.remove(highLevelClient);
+ Node node = clientNodes.get(highLevelClient);
if (node == null) {
logger.log(Level.WARNING, "got event for unknown client");
return;
}
- nodeClients.remove(node);
- usedConnections.remove(highLevelClient);
fireNodeDisconnected(node, throwable);
}
}
+ //
+ // INTERFACE PropertyChangeListener
+ //
+
+ /**
+ * {@inheritDoc}
+ */
+ public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
+ Object eventSource = propertyChangeEvent.getSource();
+ if (eventSource instanceof Node) {
+ String propertyName = propertyChangeEvent.getPropertyName();
+ if ("hostname".equals(propertyName) || "port".equals(propertyName)) {
+ Node node = (Node) eventSource;
+ HighLevelClient highLevelClient = nodeClients.get(node);
+ if (highLevelClient == null) {
+ logger.log(Level.WARNING, "got property change event for unknown node: " + node);
+ return;
+ }
+ if (highLevelClient.isConnected()) {
+ highLevelClient.disconnect();
+ try {
+ highLevelClient.connect(node.getHostname(), node.getPort());
+ } catch (UnknownHostException uhe1) {
+ fireNodeConnectionFailed(node, uhe1);
+ } catch (IOException ioe1) {
+ fireNodeConnectionFailed(node, ioe1);
+ }
+ }
+ }
+ }
+ }
+
}