X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fnet%2Fpterodactylus%2Fjsite%2Fcore%2FNodeManager.java;h=dd8440aeb75e7f17a054d8a90609987caf495ae5;hb=a70826c63fc16069cc7ea11c3957e221e79545c4;hp=3a1f68bac7e733f73de8ccda100f7ff5dd0fc4c1;hpb=708d0d9f54df2f6c02f31c75555a029ec8702614;p=jSite2.git diff --git a/src/net/pterodactylus/jsite/core/NodeManager.java b/src/net/pterodactylus/jsite/core/NodeManager.java index 3a1f68b..dd8440a 100644 --- a/src/net/pterodactylus/jsite/core/NodeManager.java +++ b/src/net/pterodactylus/jsite/core/NodeManager.java @@ -40,6 +40,7 @@ import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; +import net.pterodactylus.fcp.SSKKeypair; import net.pterodactylus.fcp.highlevel.FcpClient; import net.pterodactylus.fcp.highlevel.FcpException; import net.pterodactylus.jsite.util.IdGenerator; @@ -67,7 +68,7 @@ public class NodeManager implements Iterable, PropertyChangeListener { 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()); @@ -105,7 +106,7 @@ public class NodeManager implements Iterable, PropertyChangeListener { * The listener to add */ public void addNodeListener(NodeListener nodeListener) { - nodeListenerSupport.addListener(nodeListener); + nodeListenerManager.addListener(nodeListener); } /** @@ -115,7 +116,7 @@ public class NodeManager implements Iterable, PropertyChangeListener { * The listener to remove */ public void removeNodeListener(NodeListener nodeListener) { - nodeListenerSupport.removeListener(nodeListener); + nodeListenerManager.removeListener(nodeListener); } // @@ -285,7 +286,7 @@ public class NodeManager implements Iterable, PropertyChangeListener { node.addPropertyChangeListener(this); nodes.add(node); idNodes.put(node.getId(), node); - nodeListenerSupport.fireNodeAdded(node); + nodeListenerManager.fireNodeAdded(node); return true; } @@ -305,7 +306,7 @@ public class NodeManager implements Iterable, PropertyChangeListener { nodes.remove(node); idNodes.remove(node.getId()); node.removePropertyChangeListener(this); - nodeListenerSupport.fireNodeRemoved(node); + nodeListenerManager.fireNodeRemoved(node); } } @@ -324,13 +325,14 @@ public class NodeManager implements Iterable, PropertyChangeListener { try { FcpClient fcpClient = new FcpClient(clientName, node.getHostname(), node.getPort()); fcpClient.connect(); - nodeListenerSupport.fireNodeConnected(node); + 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) { - nodeListenerSupport.fireNodeConnectionFailed(node, fe1); + nodeListenerManager.fireNodeConnectionFailed(node, fe1); } } @@ -342,6 +344,17 @@ public class NodeManager implements Iterable, PropertyChangeListener { */ public void disconnect(Node node) { 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); } /** @@ -366,6 +379,18 @@ public class NodeManager implements Iterable, PropertyChangeListener { } /** + * 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 @@ -373,14 +398,22 @@ public class NodeManager implements Iterable, PropertyChangeListener { * @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"); } - return null; + 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. */ + } + } + throw new JSiteException("Could not get SSK key pair from any node."); } //