X-Git-Url: https://git.pterodactylus.net/?p=jSite2.git;a=blobdiff_plain;f=src%2Fnet%2Fpterodactylus%2Fjsite%2Fcore%2FNodeManager.java;h=c251232d246364ea618a34009f8bdb15ee4546fd;hp=c67628920316d401276eef13b7dd06c840b897da;hb=f2aa4d06e218670fe40b602ea068db3c50446358;hpb=f58c676a286a7cd8d37c3f510e787144a9bff5ad diff --git a/src/net/pterodactylus/jsite/core/NodeManager.java b/src/net/pterodactylus/jsite/core/NodeManager.java index c676289..c251232 100644 --- a/src/net/pterodactylus/jsite/core/NodeManager.java +++ b/src/net/pterodactylus/jsite/core/NodeManager.java @@ -40,14 +40,17 @@ 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.jsite.util.IdGenerator; import net.pterodactylus.util.io.Closer; import net.pterodactylus.util.logging.Logging; +import net.pterodactylus.util.number.Hex; /** * TODO * * @author David ‘Bombe’ Roden <bombe@freenetproject.org> - * @version $Id$ */ public class NodeManager implements Iterable, PropertyChangeListener, HighLevelClientListener { @@ -69,6 +72,9 @@ public class NodeManager implements Iterable, PropertyChangeListener, High /** All nodes. */ private List nodes = Collections.synchronizedList(new ArrayList()); + /** Map from node ID to node. */ + private Map idNodes = Collections.synchronizedMap(new HashMap()); + /** All FCP connections. */ private Map nodeClients = Collections.synchronizedMap(new HashMap()); @@ -119,7 +125,7 @@ public class NodeManager implements Iterable, PropertyChangeListener, High * The node that was added. */ private void fireNodeAdded(Node node) { - for (NodeListener nodeListener: nodeListeners) { + for (NodeListener nodeListener : nodeListeners) { nodeListener.nodeAdded(node); } } @@ -131,7 +137,7 @@ public class NodeManager implements Iterable, PropertyChangeListener, High * The node that was removed */ private void fireNodeRemoved(Node node) { - for (NodeListener nodeListener: nodeListeners) { + for (NodeListener nodeListener : nodeListeners) { nodeListener.nodeRemoved(node); } } @@ -143,7 +149,7 @@ public class NodeManager implements Iterable, PropertyChangeListener, High * The node that is now connected */ private void fireNodeConnected(Node node) { - for (NodeListener nodeListener: nodeListeners) { + for (NodeListener nodeListener : nodeListeners) { nodeListener.nodeConnected(node); } } @@ -157,7 +163,7 @@ public class NodeManager implements Iterable, PropertyChangeListener, High * The cause of the failure */ private void fireNodeConnectionFailed(Node node, Throwable cause) { - for (NodeListener nodeListener: nodeListeners) { + for (NodeListener nodeListener : nodeListeners) { nodeListener.nodeConnectionFailed(node, cause); } } @@ -172,7 +178,7 @@ public class NodeManager implements Iterable, PropertyChangeListener, High * if there was no exception */ private void fireNodeDisconnected(Node node, Throwable throwable) { - for (NodeListener nodeListener: nodeListeners) { + for (NodeListener nodeListener : nodeListeners) { nodeListener.nodeDisconnected(node, throwable); } } @@ -237,6 +243,10 @@ public class NodeManager implements Iterable, PropertyChangeListener, High List loadedNodes = new ArrayList(); while (nodeProperties.containsKey("nodes." + ++nodeIndex + ".name")) { String nodePrefix = "nodes." + nodeIndex; + String nodeId = nodeProperties.getProperty(nodePrefix + ".id"); + if (nodeId == null) { + nodeId = Hex.toHex(IdGenerator.generateId()); + } String nodeName = nodeProperties.getProperty(nodePrefix + ".name"); if (!Verifier.verifyNodeName(nodeName)) { logger.log(Level.WARNING, "invalid node name “" + nodeName + "”, skipping…"); @@ -261,6 +271,7 @@ public class NodeManager implements Iterable, PropertyChangeListener, High continue; } Node newNode = new Node(); + newNode.setId(nodeId); newNode.setName(nodeName); newNode.setHostname(nodeHostname); newNode.setPort(nodePort); @@ -269,7 +280,7 @@ public class NodeManager implements Iterable, PropertyChangeListener, High logger.fine("loaded " + loadedNodes.size() + " nodes from config"); synchronized (syncObject) { nodes.clear(); - for (Node node: loadedNodes) { + for (Node node : loadedNodes) { addNode(node); } } @@ -290,8 +301,9 @@ public class NodeManager implements Iterable, PropertyChangeListener, High } Properties nodeProperties = new Properties(); int nodeIndex = -1; - for (Node node: nodes) { + for (Node node : nodes) { String nodePrefix = "nodes." + ++nodeIndex; + nodeProperties.setProperty(nodePrefix + ".id", node.getId()); nodeProperties.setProperty(nodePrefix + ".name", node.getName()); nodeProperties.setProperty(nodePrefix + ".hostname", node.getHostname()); nodeProperties.setProperty(nodePrefix + ".port", String.valueOf(node.getPort())); @@ -323,6 +335,7 @@ public class NodeManager implements Iterable, PropertyChangeListener, High 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); @@ -345,6 +358,8 @@ public class NodeManager implements Iterable, PropertyChangeListener, High if (nodeClients.containsKey(node)) { disconnect(node); } + nodes.remove(node); + idNodes.remove(node.getId()); node.removePropertyChangeListener(this); fireNodeRemoved(node); } @@ -409,6 +424,57 @@ public class NodeManager implements Iterable, PropertyChangeListener, High 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 + * The ID of the node + * @return The node with the given ID, or null if no such + * node was found + */ + Node getNode(String id) { + return idNodes.get(id); + } + + /** + * Generates a new SSK key pair. + * + * @return An array with the private key at index 0 and the + * public key at index 1 + * @throws IOException + * if an I/O error occurs communicating with the node + * @throws JSiteException + * if there is a problem with the node + */ + public String[] generateKeyPair() throws IOException, JSiteException { + 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. */ + } + return null; + } + // // PRIVATE METHODS //