X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fnet%2Fpterodactylus%2Fjsite%2Fcore%2FNodeManager.java;h=922a85a1ab78a5c6a49513d30c19121f4d3a7d97;hb=dca15b39fe385e878fc17c684c3efd77a193bf0a;hp=d81416bc9457d891a8a88d3a02067d34728f7d51;hpb=cf127faffabd3f07b0acc6de9ea746059d571b56;p=jSite2.git diff --git a/src/net/pterodactylus/jsite/core/NodeManager.java b/src/net/pterodactylus/jsite/core/NodeManager.java index d81416b..922a85a 100644 --- a/src/net/pterodactylus/jsite/core/NodeManager.java +++ b/src/net/pterodactylus/jsite/core/NodeManager.java @@ -42,8 +42,10 @@ 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 @@ -70,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()); @@ -120,7 +125,8 @@ public class NodeManager implements Iterable, PropertyChangeListener, High * The node that was added. */ private void fireNodeAdded(Node node) { - for (NodeListener nodeListener: nodeListeners) { + logger.log(Level.FINEST, "firing nodeAdded event with [node=" + node + "]"); + for (NodeListener nodeListener : nodeListeners) { nodeListener.nodeAdded(node); } } @@ -132,7 +138,8 @@ public class NodeManager implements Iterable, PropertyChangeListener, High * The node that was removed */ private void fireNodeRemoved(Node node) { - for (NodeListener nodeListener: nodeListeners) { + logger.log(Level.FINEST, "firing nodeRemoved event with [node=" + node + "]"); + for (NodeListener nodeListener : nodeListeners) { nodeListener.nodeRemoved(node); } } @@ -144,7 +151,8 @@ public class NodeManager implements Iterable, PropertyChangeListener, High * The node that is now connected */ private void fireNodeConnected(Node node) { - for (NodeListener nodeListener: nodeListeners) { + logger.log(Level.FINEST, "firing nodeConnected event with [node=" + node + "]"); + for (NodeListener nodeListener : nodeListeners) { nodeListener.nodeConnected(node); } } @@ -158,7 +166,8 @@ public class NodeManager implements Iterable, PropertyChangeListener, High * The cause of the failure */ private void fireNodeConnectionFailed(Node node, Throwable cause) { - for (NodeListener nodeListener: nodeListeners) { + logger.log(Level.FINEST, "firing nodeConnectionFailed event with [node=" + node + ",cause=" + cause + "]"); + for (NodeListener nodeListener : nodeListeners) { nodeListener.nodeConnectionFailed(node, cause); } } @@ -173,7 +182,8 @@ public class NodeManager implements Iterable, PropertyChangeListener, High * if there was no exception */ private void fireNodeDisconnected(Node node, Throwable throwable) { - for (NodeListener nodeListener: nodeListeners) { + logger.log(Level.FINEST, "firing nodeDisconnected event with [node=" + node + ",throwable=" + throwable + "]"); + for (NodeListener nodeListener : nodeListeners) { nodeListener.nodeDisconnected(node, throwable); } } @@ -221,6 +231,7 @@ public class NodeManager implements Iterable, PropertyChangeListener, High * if an I/O error occurs loading the nodes */ public void load() throws IOException { + logger.log(Level.FINEST, "load()"); File directoryFile = new File(directory); File nodeFile = new File(directoryFile, "nodes.properties"); if (!nodeFile.exists() || !nodeFile.isFile() || !nodeFile.canRead()) { @@ -238,6 +249,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,15 +277,16 @@ public class NodeManager implements Iterable, PropertyChangeListener, High continue; } Node newNode = new Node(); + newNode.setId(nodeId); newNode.setName(nodeName); newNode.setHostname(nodeHostname); newNode.setPort(nodePort); loadedNodes.add(newNode); } - logger.fine("loaded " + loadedNodes.size() + " nodes from config"); + logger.log(Level.FINE, "loaded " + loadedNodes.size() + " nodes from config"); synchronized (syncObject) { nodes.clear(); - for (Node node: loadedNodes) { + for (Node node : loadedNodes) { addNode(node); } } @@ -283,6 +299,7 @@ public class NodeManager implements Iterable, PropertyChangeListener, High * if an I/O error occurs saving the nodes */ public void save() throws IOException { + logger.log(Level.FINEST, "save()"); File directoryFile = new File(directory); if (!directoryFile.exists()) { if (!directoryFile.mkdirs()) { @@ -291,8 +308,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())); @@ -317,13 +335,15 @@ public class NodeManager implements Iterable, PropertyChangeListener, High * if the node was not added because it was already known */ public boolean addNode(Node node) { + logger.log(Level.FINEST, "addNode(node=" + node + ")"); if (nodes.contains(node)) { - logger.warning("was told to add already known node: " + node); + logger.log(Level.WARNING, "was told to add already known node: " + node); return false; } 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); @@ -339,6 +359,7 @@ public class NodeManager implements Iterable, PropertyChangeListener, High * The node to remove */ public void removeNode(Node node) { + logger.log(Level.FINEST, "removeNode(node=" + node + ")"); synchronized (syncObject) { if (!nodes.contains(node)) { return; @@ -347,6 +368,7 @@ public class NodeManager implements Iterable, PropertyChangeListener, High disconnect(node); } nodes.remove(node); + idNodes.remove(node.getId()); node.removePropertyChangeListener(this); fireNodeRemoved(node); } @@ -359,10 +381,11 @@ public class NodeManager implements Iterable, PropertyChangeListener, High * The node to connect to */ public void connect(Node node) { + logger.log(Level.FINEST, "connect(node=" + node + ")"); HighLevelClient highLevelClient; highLevelClient = nodeClients.get(node); if (highLevelClient == null) { - logger.warning("was told to connect to unknown node: " + node); + logger.log(Level.WARNING, "was told to connect to unknown node: " + node); return; } try { @@ -381,6 +404,7 @@ public class NodeManager implements Iterable, PropertyChangeListener, High * The node to disconnect */ public void disconnect(Node node) { + logger.log(Level.FINEST, "disconnect(node=" + node + ")"); synchronized (syncObject) { if (!nodes.contains(node)) { return; @@ -424,6 +448,18 @@ public class NodeManager implements Iterable, PropertyChangeListener, High } /** + * 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 @@ -434,6 +470,7 @@ public class NodeManager implements Iterable, PropertyChangeListener, High * if there is a problem with the node */ public String[] generateKeyPair() throws IOException, JSiteException { + logger.log(Level.FINEST, "generateKeyPair()"); if (nodes.isEmpty()) { throw new NoNodeException("no node configured"); } @@ -462,7 +499,7 @@ public class NodeManager implements Iterable, PropertyChangeListener, High * {@inheritDoc} */ public void clientConnected(HighLevelClient highLevelClient) { - logger.log(Level.FINER, "clientConnected(c=" + highLevelClient + ")"); + logger.log(Level.FINEST, "clientConnected(highLevelClient=" + highLevelClient + ")"); Node node = clientNodes.get(highLevelClient); if (node == null) { logger.log(Level.WARNING, "got event for unknown client"); @@ -475,7 +512,7 @@ public class NodeManager implements Iterable, PropertyChangeListener, High * {@inheritDoc} */ public void clientDisconnected(HighLevelClient highLevelClient, Throwable throwable) { - logger.log(Level.FINER, "clientDisconnected(c=" + highLevelClient + ",t=" + throwable + ")"); + logger.log(Level.FINEST, "clientDisconnected(highLevelClient=" + highLevelClient + ",throwable=" + throwable + ")"); synchronized (syncObject) { Node node = clientNodes.get(highLevelClient); if (node == null) {