X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fnet%2Fpterodactylus%2Fjsite%2Fcore%2FNodeManager.java;h=f7b231d49b3afc25824b2f7ab111b29c1e2c5150;hb=52608018e26c1d6bffc3c8561cae9500c141a636;hp=ac47628c7b3f3764b50cf91d5b25a5a7ab29610d;hpb=36a9e6e7134485038f739181e2edaac044c32176;p=jSite2.git diff --git a/src/net/pterodactylus/jsite/core/NodeManager.java b/src/net/pterodactylus/jsite/core/NodeManager.java index ac47628..f7b231d 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); } } @@ -315,6 +322,18 @@ public class NodeManager implements Iterable, PropertyChangeListener { 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); + } } /** @@ -325,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); } /** @@ -356,14 +386,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."); } //