X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fnet%2Fpterodactylus%2Fjsite%2Fcore%2FNodeManager.java;h=db63df93c2f20a06ac1bf35a6a579a75becc140c;hb=4f10436f68115773d86be96fa3a2040d859ade1f;hp=e8fd278576478dfb4d58a320ae2a63385c8d61de;hpb=e5e14c688e13425b77ac6bf9d952af099ea52de5;p=jSite2.git diff --git a/src/net/pterodactylus/jsite/core/NodeManager.java b/src/net/pterodactylus/jsite/core/NodeManager.java index e8fd278..db63df9 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); } } @@ -325,13 +326,13 @@ public class NodeManager implements Iterable, PropertyChangeListener { FcpClient fcpClient = new FcpClient(clientName, node.getHostname(), node.getPort()); fcpClient.connect(); nodeClients.put(node, fcpClient); - nodeListenerSupport.fireNodeConnected(node); + 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); } } @@ -347,6 +348,13 @@ public class NodeManager implements Iterable, PropertyChangeListener { 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); } /** @@ -371,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 @@ -378,14 +398,23 @@ 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; + FcpException fcpException = null; + for (FcpClient fcpClient : nodeClients.values()) { + try { + SSKKeypair sskKeypair = fcpClient.generateKeyPair(); + return new String[] { sskKeypair.getInsertURI(), sskKeypair.getRequestURI() }; + } catch (FcpException fcpe1) { + fcpException = fcpe1; + } + } + throw new JSiteException("Could not get SSK key pair from any node.", fcpException); } //