X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fnet%2Fpterodactylus%2Fjsite%2Fcore%2FNodeManager.java;h=f7b231d49b3afc25824b2f7ab111b29c1e2c5150;hb=a8bc3ffc9cf0cb7f18bc9dde44825e948ed3add2;hp=c251232d246364ea618a34009f8bdb15ee4546fd;hpb=954a1a33392ef83e007b1045e2aaeac173361289;p=jSite2.git diff --git a/src/net/pterodactylus/jsite/core/NodeManager.java b/src/net/pterodactylus/jsite/core/NodeManager.java index c251232..f7b231d 100644 --- a/src/net/pterodactylus/jsite/core/NodeManager.java +++ b/src/net/pterodactylus/jsite/core/NodeManager.java @@ -31,17 +31,18 @@ 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; -import net.pterodactylus.fcp.highlevel.HighLevelClient; -import net.pterodactylus.fcp.highlevel.HighLevelClientListener; -import net.pterodactylus.fcp.highlevel.HighLevelException; -import net.pterodactylus.fcp.highlevel.KeyGenerationResult; +import net.pterodactylus.fcp.SSKKeypair; +import net.pterodactylus.fcp.highlevel.FcpClient; +import net.pterodactylus.fcp.highlevel.FcpException; import net.pterodactylus.jsite.util.IdGenerator; import net.pterodactylus.util.io.Closer; import net.pterodactylus.util.logging.Logging; @@ -49,10 +50,10 @@ import net.pterodactylus.util.number.Hex; /** * TODO - * + * * @author David ‘Bombe’ Roden <bombe@freenetproject.org> */ -public class NodeManager implements Iterable, PropertyChangeListener, HighLevelClientListener { +public class NodeManager implements Iterable, PropertyChangeListener { /** Logger. */ private static final Logger logger = Logging.getLogger(NodeManager.class.getName()); @@ -66,24 +67,24 @@ 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 nodeListenerManager = 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()); + /** Map from node to client. */ + private final Map nodeClients = Collections.synchronizedMap(new HashMap()); - /** Maps nodes to high-level clients. */ - private 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,87 +101,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); + nodeListenerManager.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) { - 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) { - 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) { - 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) { - 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) { - for (NodeListener nodeListener : nodeListeners) { - nodeListener.nodeDisconnected(node, throwable); - } + nodeListenerManager.removeListener(nodeListener); } // @@ -189,7 +125,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() { @@ -198,7 +134,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, @@ -209,6 +145,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() { @@ -221,11 +169,12 @@ public class NodeManager implements Iterable, PropertyChangeListener, High /** * Loads nodes. - * + * * @throws IOException * if an I/O error occurs loading the nodes */ public void load() throws IOException { + logger.log(Level.FINEST, "load()"); File directoryFile = new File(directory); File nodeFile = new File(directoryFile, "nodes.properties"); if (!nodeFile.exists() || !nodeFile.isFile() || !nodeFile.canRead()) { @@ -277,7 +226,7 @@ public class NodeManager implements Iterable, PropertyChangeListener, High newNode.setPort(nodePort); loadedNodes.add(newNode); } - logger.fine("loaded " + loadedNodes.size() + " nodes from config"); + logger.log(Level.FINE, "loaded " + loadedNodes.size() + " nodes from config"); synchronized (syncObject) { nodes.clear(); for (Node node : loadedNodes) { @@ -288,11 +237,12 @@ public class NodeManager implements Iterable, PropertyChangeListener, High /** * Saves all configured nodes. - * + * * @throws IOException * if an I/O error occurs saving the nodes */ public void save() throws IOException { + logger.log(Level.FINEST, "save()"); File directoryFile = new File(directory); if (!directoryFile.exists()) { if (!directoryFile.mkdirs()) { @@ -320,92 +270,96 @@ 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 + ")"); if (nodes.contains(node)) { - logger.warning("was told to add already known node: " + node); + logger.log(Level.WARNING, "was told to add already known node: " + node); return false; } node.addPropertyChangeListener(this); - HighLevelClient highLevelClient = new HighLevelClient(clientName); nodes.add(node); idNodes.put(node.getId(), node); - clientNodes.put(highLevelClient, node); - nodeClients.put(node, highLevelClient); - highLevelClient.addHighLevelClientListener(this); - fireNodeAdded(node); + nodeListenerManager.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 */ public void removeNode(Node node) { + logger.log(Level.FINEST, "removeNode(node=" + node + ")"); synchronized (syncObject) { if (!nodes.contains(node)) { return; } - if (nodeClients.containsKey(node)) { - disconnect(node); - } nodes.remove(node); idNodes.remove(node.getId()); node.removePropertyChangeListener(this); - fireNodeRemoved(node); + nodeListenerManager.fireNodeRemoved(node); } } /** * Tries to establish a connection with the given node. - * + * * @param 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); + logger.log(Level.FINEST, "connect(node=" + node + ")"); + if (!nodes.contains(node)) { + logger.log(Level.WARNING, "Was told to connect to node (" + node + ") I don’t know about!"); return; } try { - highLevelClient.connect(node.getHostname(), node.getPort()); + FcpClient fcpClient = new FcpClient(clientName, node.getHostname(), node.getPort()); + fcpClient.connect(); + nodeClients.put(node, fcpClient); + nodeListenerManager.fireNodeConnected(node); } catch (UnknownHostException uhe1) { - fireNodeConnectionFailed(node, uhe1); + nodeListenerManager.fireNodeConnectionFailed(node, uhe1); } catch (IOException ioe1) { - fireNodeConnectionFailed(node, ioe1); + nodeListenerManager.fireNodeConnectionFailed(node, ioe1); + } catch (FcpException fe1) { + nodeListenerManager.fireNodeConnectionFailed(node, fe1); } } /** * Disconnects the given node without removing it. - * + * * @param node * The node to disconnect */ public void disconnect(Node node) { - synchronized (syncObject) { - if (!nodes.contains(node)) { - return; - } - HighLevelClient highLevelClient = nodeClients.get(node); - highLevelClient.disconnect(); + logger.log(Level.FINEST, "disconnect(node=" + node + ")"); + if (!nodes.contains(node)) { + logger.log(Level.WARNING, "Was told to disconnect from a node (" + node + ") I don’t know about!"); + return; } + FcpClient fcpClient = nodeClients.remove(node); + if (fcpClient == null) { + logger.log(Level.WARNING, "No FCP client for node (" + node + ")!"); + return; + } + fcpClient.disconnect(); + nodeListenerManager.fireNodeDisconnected(node, null); } /** * Returns a list of all nodes. - * + * * @return A list of all nodes */ public List getNodes() { @@ -413,36 +367,12 @@ 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 - * node was disconnected or removed - */ - public HighLevelClient getHighLevelClient(Node node) { - return nodeClients.get(node); - } - - /** - * 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 - * high-level client is not known - */ - public Node getNode(HighLevelClient highLevelClient) { - return clientNodes.get(highLevelClient); - } - - /** * 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); @@ -450,29 +380,28 @@ 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 * if an I/O error occurs communicating with the node * @throws JSiteException - * if there is a problem with the node + * if there is no connected node */ public String[] generateKeyPair() throws IOException, JSiteException { + logger.log(Level.FINEST, "generateKeyPair()"); if (nodes.isEmpty()) { throw new NoNodeException("no node configured"); } - Node node = nodes.get(0); - HighLevelClient highLevelClient = nodeClients.get(node); - try { - KeyGenerationResult keyGenerationResult = highLevelClient.generateKey().getResult(); - return new String[] { keyGenerationResult.getInsertURI(), keyGenerationResult.getRequestURI() }; - } catch (HighLevelException hle1) { - throw new BackendException(hle1); - } catch (InterruptedException e) { - /* ignore. */ + for (FcpClient fcpClient : nodeClients.values()) { + try { + SSKKeypair sskKeypair = fcpClient.generateKeyPair(); + return new String[] { sskKeypair.getInsertURI(), sskKeypair.getRequestURI() }; + } catch (FcpException fcpe1) { + /* ignore, we’ll throw later on if needs be. */ + } } - return null; + throw new JSiteException("Could not get SSK key pair from any node."); } // @@ -480,38 +409,6 @@ public class NodeManager implements Iterable, PropertyChangeListener, High // // - // INTERFACE HighLevelClientListener - // - - /** - * {@inheritDoc} - */ - public void clientConnected(HighLevelClient highLevelClient) { - logger.log(Level.FINER, "clientConnected(c=" + highLevelClient + ")"); - Node node = clientNodes.get(highLevelClient); - if (node == null) { - logger.log(Level.WARNING, "got event for unknown client"); - return; - } - fireNodeConnected(node); - } - - /** - * {@inheritDoc} - */ - public void clientDisconnected(HighLevelClient highLevelClient, Throwable throwable) { - logger.log(Level.FINER, "clientDisconnected(c=" + highLevelClient + ",t=" + throwable + ")"); - synchronized (syncObject) { - Node node = clientNodes.get(highLevelClient); - if (node == null) { - logger.log(Level.WARNING, "got event for unknown client"); - return; - } - fireNodeDisconnected(node, throwable); - } - } - - // // INTERFACE PropertyChangeListener // @@ -523,22 +420,7 @@ public class NodeManager implements Iterable, PropertyChangeListener, High 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); - } - } + /* TODO - reconnect. */ } } }