X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fnet%2Fpterodactylus%2Fjsite%2Fcore%2FNodeManager.java;h=dd8440aeb75e7f17a054d8a90609987caf495ae5;hb=a70826c63fc16069cc7ea11c3957e221e79545c4;hp=8745cbaadd49c15e5ae0988b1d8b0f04fc4901ae;hpb=57ceb171afaa8bf304fa5c1da0360f285702e1b3;p=jSite2.git diff --git a/src/net/pterodactylus/jsite/core/NodeManager.java b/src/net/pterodactylus/jsite/core/NodeManager.java index 8745cba..dd8440a 100644 --- a/src/net/pterodactylus/jsite/core/NodeManager.java +++ b/src/net/pterodactylus/jsite/core/NodeManager.java @@ -27,6 +27,7 @@ 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; @@ -39,6 +40,9 @@ 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; import net.pterodactylus.util.io.Closer; import net.pterodactylus.util.logging.Logging; @@ -64,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()); @@ -72,6 +76,9 @@ public class NodeManager implements Iterable, PropertyChangeListener { /** Map from node ID to node. */ private final Map idNodes = 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()); @@ -99,7 +106,7 @@ public class NodeManager implements Iterable, PropertyChangeListener { * The listener to add */ public void addNodeListener(NodeListener nodeListener) { - nodeListenerSupport.addListener(nodeListener); + nodeListenerManager.addListener(nodeListener); } /** @@ -109,7 +116,7 @@ public class NodeManager implements Iterable, PropertyChangeListener { * The listener to remove */ public void removeNodeListener(NodeListener nodeListener) { - nodeListenerSupport.removeListener(nodeListener); + nodeListenerManager.removeListener(nodeListener); } // @@ -279,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; } @@ -299,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); } } @@ -311,6 +318,22 @@ public class NodeManager implements Iterable, PropertyChangeListener { */ public void connect(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 { + FcpClient fcpClient = new FcpClient(clientName, node.getHostname(), node.getPort()); + fcpClient.connect(); + nodeClients.put(node, fcpClient); + nodeListenerManager.fireNodeConnected(node); + } catch (UnknownHostException uhe1) { + nodeListenerManager.fireNodeConnectionFailed(node, uhe1); + } catch (IOException ioe1) { + nodeListenerManager.fireNodeConnectionFailed(node, ioe1); + } catch (FcpException fe1) { + nodeListenerManager.fireNodeConnectionFailed(node, fe1); + } } /** @@ -321,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); } /** @@ -345,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 @@ -352,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."); } //