X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fnet%2Fpterodactylus%2Fjsite%2Fcore%2FNodeManager.java;h=4224cf16888194217c02e2401b48060f4e719200;hb=464bd5d54b6b55a84dd7245f48164ec0a72cd49c;hp=f772dc2592bf440791db738fb054a22423d92251;hpb=78dd8a015cbfdedca1a962d03d16b89ee8187b8f;p=jSite2.git diff --git a/src/net/pterodactylus/jsite/core/NodeManager.java b/src/net/pterodactylus/jsite/core/NodeManager.java index f772dc2..4224cf1 100644 --- a/src/net/pterodactylus/jsite/core/NodeManager.java +++ b/src/net/pterodactylus/jsite/core/NodeManager.java @@ -40,15 +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 { @@ -120,7 +122,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); } } @@ -132,7 +134,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); } } @@ -144,7 +146,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); } } @@ -158,7 +160,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); } } @@ -173,7 +175,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); } } @@ -238,6 +240,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…"); @@ -262,6 +268,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); @@ -270,7 +277,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); } } @@ -291,8 +298,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())); @@ -346,6 +354,7 @@ public class NodeManager implements Iterable, PropertyChangeListener, High if (nodeClients.containsKey(node)) { disconnect(node); } + nodes.remove(node); node.removePropertyChangeListener(this); fireNodeRemoved(node); } @@ -429,17 +438,22 @@ public class NodeManager implements Iterable, PropertyChangeListener, High * 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 { - if (!nodes.isEmpty()) { - Node node = nodes.get(0); - HighLevelClient highLevelClient = nodeClients.get(node); - try { - KeyGenerationResult keyGenerationResult = highLevelClient.generateKey().getResult(); - return new String[] { keyGenerationResult.getInsertURI(), keyGenerationResult.getRequestURI() }; - } catch (InterruptedException e) { - /* ignore. */ - } + 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; }