X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fnet%2Fpterodactylus%2Fjsite%2Fcore%2FNodeManager.java;h=dd8440aeb75e7f17a054d8a90609987caf495ae5;hb=392d893dbfff0879ebb9d6bc2367729790f54e52;hp=9cdc60d55a57ad593d0406d4d9abc4eaf21275c3;hpb=f8ec8ae7e1c3b69ddac1cc58201803e052bd720e;p=jSite2.git diff --git a/src/net/pterodactylus/jsite/core/NodeManager.java b/src/net/pterodactylus/jsite/core/NodeManager.java index 9cdc60d..dd8440a 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; @@ -52,7 +53,7 @@ import net.pterodactylus.util.number.Hex; * * @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()); @@ -67,7 +68,7 @@ public class NodeManager implements Iterable, PropertyChangeListener, High private final Object syncObject = new Object(); /** Node listener support. */ - private final NodeListenerSupport nodeListenerSupport = new NodeListenerSupport(); + private final NodeListenerSupport nodeListenerManager = new NodeListenerSupport(); /** All nodes. */ private final List nodes = Collections.synchronizedList(new ArrayList()); @@ -75,11 +76,8 @@ public class NodeManager implements Iterable, PropertyChangeListener, High /** Map from node ID to node. */ private final Map idNodes = Collections.synchronizedMap(new HashMap()); - /** All FCP connections. */ - private final Map nodeClients = Collections.synchronizedMap(new HashMap()); - - /** Maps nodes to high-level clients. */ - private final Map clientNodes = Collections.synchronizedMap(new HashMap()); + /** Map from node to client. */ + private final Map nodeClients = Collections.synchronizedMap(new HashMap()); /** Collection of currently connected nodes. */ private final Set connectedNodes = Collections.synchronizedSet(new HashSet()); @@ -108,7 +106,7 @@ public class NodeManager implements Iterable, PropertyChangeListener, High * The listener to add */ public void addNodeListener(NodeListener nodeListener) { - nodeListenerSupport.addListener(nodeListener); + nodeListenerManager.addListener(nodeListener); } /** @@ -118,7 +116,7 @@ public class NodeManager implements Iterable, PropertyChangeListener, High * The listener to remove */ public void removeNodeListener(NodeListener nodeListener) { - nodeListenerSupport.removeListener(nodeListener); + nodeListenerManager.removeListener(nodeListener); } // @@ -147,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() { @@ -274,13 +284,9 @@ public class NodeManager implements Iterable, PropertyChangeListener, High 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); - nodeListenerSupport.fireNodeAdded(node); + nodeListenerManager.fireNodeAdded(node); return true; } @@ -297,13 +303,10 @@ public class NodeManager implements Iterable, PropertyChangeListener, High if (!nodes.contains(node)) { return; } - if (nodeClients.containsKey(node)) { - disconnect(node); - } nodes.remove(node); idNodes.remove(node.getId()); node.removePropertyChangeListener(this); - nodeListenerSupport.fireNodeRemoved(node); + nodeListenerManager.fireNodeRemoved(node); } } @@ -315,18 +318,21 @@ public class NodeManager implements Iterable, PropertyChangeListener, High */ public void connect(Node node) { logger.log(Level.FINEST, "connect(node=" + node + ")"); - HighLevelClient highLevelClient; - highLevelClient = nodeClients.get(node); - if (highLevelClient == null) { - logger.log(Level.WARNING, "was told to connect to unknown 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) { - nodeListenerSupport.fireNodeConnectionFailed(node, uhe1); + nodeListenerManager.fireNodeConnectionFailed(node, uhe1); } catch (IOException ioe1) { - nodeListenerSupport.fireNodeConnectionFailed(node, ioe1); + nodeListenerManager.fireNodeConnectionFailed(node, ioe1); + } catch (FcpException fe1) { + nodeListenerManager.fireNodeConnectionFailed(node, fe1); } } @@ -338,13 +344,17 @@ public class NodeManager implements Iterable, PropertyChangeListener, High */ public void disconnect(Node node) { logger.log(Level.FINEST, "disconnect(node=" + node + ")"); - synchronized (syncObject) { - if (!nodes.contains(node)) { - return; - } - HighLevelClient highLevelClient = nodeClients.get(node); - highLevelClient.disconnect(); + 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); } /** @@ -357,30 +367,6 @@ 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 @@ -393,6 +379,18 @@ public class NodeManager implements Iterable, PropertyChangeListener, High } /** + * Returns the FCP client for the given node. + * + * @param node + * The node to get the FCP client for + * @return The FCP client for the given node, or {@code null} if the node + * does not have an associated FCP client + */ + FcpClient getFcpClient(Node node) { + return nodeClients.get(node); + } + + /** * Generates a new SSK key pair. * * @return An array with the private key at index 0 and the @@ -400,24 +398,22 @@ public class NodeManager implements Iterable, PropertyChangeListener, High * @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."); } // @@ -425,38 +421,6 @@ public class NodeManager implements Iterable, PropertyChangeListener, High // // - // INTERFACE HighLevelClientListener - // - - /** - * {@inheritDoc} - */ - public void clientConnected(HighLevelClient highLevelClient) { - logger.log(Level.FINEST, "clientConnected(highLevelClient=" + highLevelClient + ")"); - Node node = clientNodes.get(highLevelClient); - if (node == null) { - logger.log(Level.WARNING, "got event for unknown client"); - return; - } - nodeListenerSupport.fireNodeConnected(node); - } - - /** - * {@inheritDoc} - */ - public void clientDisconnected(HighLevelClient highLevelClient, Throwable throwable) { - logger.log(Level.FINEST, "clientDisconnected(highLevelClient=" + highLevelClient + ",throwable=" + throwable + ")"); - synchronized (syncObject) { - Node node = clientNodes.get(highLevelClient); - if (node == null) { - logger.log(Level.WARNING, "got event for unknown client"); - return; - } - nodeListenerSupport.fireNodeDisconnected(node, throwable); - } - } - - // // INTERFACE PropertyChangeListener // @@ -468,22 +432,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) { - nodeListenerSupport.fireNodeConnectionFailed(node, uhe1); - } catch (IOException ioe1) { - nodeListenerSupport.fireNodeConnectionFailed(node, ioe1); - } - } + /* TODO - reconnect. */ } } }