2 * jSite2 - FcpCollector.java -
3 * Copyright © 2008 David Roden
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 package net.pterodactylus.jsite.core;
22 import java.beans.PropertyChangeEvent;
23 import java.beans.PropertyChangeListener;
25 import java.io.FileInputStream;
26 import java.io.FileOutputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.io.OutputStream;
30 import java.net.UnknownHostException;
31 import java.util.ArrayList;
32 import java.util.Collections;
33 import java.util.HashMap;
34 import java.util.Iterator;
35 import java.util.List;
37 import java.util.Properties;
38 import java.util.logging.Level;
39 import java.util.logging.Logger;
41 import net.pterodactylus.fcp.highlevel.HighLevelClient;
42 import net.pterodactylus.fcp.highlevel.HighLevelClientListener;
43 import net.pterodactylus.fcp.highlevel.HighLevelException;
44 import net.pterodactylus.fcp.highlevel.KeyGenerationResult;
45 import net.pterodactylus.jsite.util.IdGenerator;
46 import net.pterodactylus.util.io.Closer;
47 import net.pterodactylus.util.logging.Logging;
48 import net.pterodactylus.util.number.Hex;
53 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
55 public class NodeManager implements Iterable<Node>, PropertyChangeListener, HighLevelClientListener {
58 private static final Logger logger = Logging.getLogger(NodeManager.class.getName());
60 /** The FCP client name. */
61 private final String clientName;
63 /** The directory for the configuration. */
64 private final String directory;
66 /** Object used for synchronization. */
67 private final Object syncObject = new Object();
69 /** Node listeners. */
70 private List<NodeListener> nodeListeners = Collections.synchronizedList(new ArrayList<NodeListener>());
73 private List<Node> nodes = Collections.synchronizedList(new ArrayList<Node>());
75 /** All FCP connections. */
76 private Map<Node, HighLevelClient> nodeClients = Collections.synchronizedMap(new HashMap<Node, HighLevelClient>());
78 /** Maps nodes to high-level clients. */
79 private Map<HighLevelClient, Node> clientNodes = Collections.synchronizedMap(new HashMap<HighLevelClient, Node>());
82 * Creates a new FCP collector.
85 * The name of the FCP client
87 * The directory in which to store the nodes
89 public NodeManager(String clientName, String directory) {
90 this.clientName = clientName;
91 this.directory = directory;
99 * Adds the given listener to the list of listeners.
101 * @param nodeListener
102 * The listener to add
104 public void addNodeListener(NodeListener nodeListener) {
105 nodeListeners.add(nodeListener);
109 * Removes the given listener from the list of listeners.
111 * @param nodeListener
112 * The listener to remove
114 public void removeNodeListener(NodeListener nodeListener) {
115 nodeListeners.remove(nodeListener);
119 * Notifies all listeners that a node was added.
122 * The node that was added.
124 private void fireNodeAdded(Node node) {
125 for (NodeListener nodeListener : nodeListeners) {
126 nodeListener.nodeAdded(node);
131 * Notifies all listeners that a node was removed.
134 * The node that was removed
136 private void fireNodeRemoved(Node node) {
137 for (NodeListener nodeListener : nodeListeners) {
138 nodeListener.nodeRemoved(node);
143 * Notifies all listeners that the given node was connected.
146 * The node that is now connected
148 private void fireNodeConnected(Node node) {
149 for (NodeListener nodeListener : nodeListeners) {
150 nodeListener.nodeConnected(node);
155 * Notifies all listeners that a connection to a node has failed.
158 * The node that could not be connected
160 * The cause of the failure
162 private void fireNodeConnectionFailed(Node node, Throwable cause) {
163 for (NodeListener nodeListener : nodeListeners) {
164 nodeListener.nodeConnectionFailed(node, cause);
169 * Notifies all listeners that the given node was disconnected.
172 * The node that is now disconnected
174 * The exception that caused the disconnect, or <code>null</code>
175 * if there was no exception
177 private void fireNodeDisconnected(Node node, Throwable throwable) {
178 for (NodeListener nodeListener : nodeListeners) {
179 nodeListener.nodeDisconnected(node, throwable);
188 * Returns the directory in which the nodes are stored.
190 * @return The directory the nodes are stored in
192 public String getDirectory() {
197 * Checks whether the given node is already connected.
201 * @return <code>true</code> if the node is already connected,
202 * <code>false</code> otherwise
204 public boolean hasNode(Node node) {
205 return nodes.contains(node);
211 public Iterator<Node> iterator() {
212 return nodes.iterator();
222 * @throws IOException
223 * if an I/O error occurs loading the nodes
225 public void load() throws IOException {
226 File directoryFile = new File(directory);
227 File nodeFile = new File(directoryFile, "nodes.properties");
228 if (!nodeFile.exists() || !nodeFile.isFile() || !nodeFile.canRead()) {
231 Properties nodeProperties = new Properties();
232 InputStream nodeInputStream = null;
234 nodeInputStream = new FileInputStream(nodeFile);
235 nodeProperties.load(nodeInputStream);
237 Closer.close(nodeInputStream);
240 List<Node> loadedNodes = new ArrayList<Node>();
241 while (nodeProperties.containsKey("nodes." + ++nodeIndex + ".name")) {
242 String nodePrefix = "nodes." + nodeIndex;
243 String nodeId = nodeProperties.getProperty(nodePrefix + ".id");
244 if (nodeId == null) {
245 nodeId = Hex.toHex(IdGenerator.generateId());
247 String nodeName = nodeProperties.getProperty(nodePrefix + ".name");
248 if (!Verifier.verifyNodeName(nodeName)) {
249 logger.log(Level.WARNING, "invalid node name “" + nodeName + "”, skipping…");
252 String nodeHostname = nodeProperties.getProperty(nodePrefix + ".hostname");
253 if (!Verifier.verifyHostname(nodeHostname)) {
254 logger.log(Level.WARNING, "invalid host name “" + nodeHostname + "”");
255 /* not fatal, might be valid later on. */
257 String nodePortString = nodeProperties.getProperty(nodePrefix + ".port");
258 if (!Verifier.verifyPort(nodePortString)) {
259 logger.log(Level.WARNING, "invalid port number “" + nodePortString + "”, skipping…");
264 nodePort = Integer.valueOf(nodePortString);
265 } catch (NumberFormatException nfe1) {
266 /* shouldn't happen, port number was checked before. */
267 logger.log(Level.SEVERE, "invalid port number “" + nodePortString + "”, check failed! skipping…");
270 Node newNode = new Node();
271 newNode.setId(nodeId);
272 newNode.setName(nodeName);
273 newNode.setHostname(nodeHostname);
274 newNode.setPort(nodePort);
275 loadedNodes.add(newNode);
277 logger.fine("loaded " + loadedNodes.size() + " nodes from config");
278 synchronized (syncObject) {
280 for (Node node : loadedNodes) {
287 * Saves all configured nodes.
289 * @throws IOException
290 * if an I/O error occurs saving the nodes
292 public void save() throws IOException {
293 File directoryFile = new File(directory);
294 if (!directoryFile.exists()) {
295 if (!directoryFile.mkdirs()) {
296 throw new IOException("could not create directory: " + directory);
299 Properties nodeProperties = new Properties();
301 for (Node node : nodes) {
302 String nodePrefix = "nodes." + ++nodeIndex;
303 nodeProperties.setProperty(nodePrefix + ".id", node.getId());
304 nodeProperties.setProperty(nodePrefix + ".name", node.getName());
305 nodeProperties.setProperty(nodePrefix + ".hostname", node.getHostname());
306 nodeProperties.setProperty(nodePrefix + ".port", String.valueOf(node.getPort()));
308 File projectFile = new File(directoryFile, "nodes.properties");
309 OutputStream nodeOutputStream = null;
311 nodeOutputStream = new FileOutputStream(projectFile);
312 nodeProperties.store(nodeOutputStream, "jSite nodes");
314 Closer.close(nodeOutputStream);
319 * Adds the given node to this manager.
321 * @see #connect(Node)
323 * The node to connect to
324 * @return <code>true</code> if the node was added, <code>false</code>
325 * if the node was not added because it was already known
327 public boolean addNode(Node node) {
328 if (nodes.contains(node)) {
329 logger.warning("was told to add already known node: " + node);
332 node.addPropertyChangeListener(this);
333 HighLevelClient highLevelClient = new HighLevelClient(clientName);
335 clientNodes.put(highLevelClient, node);
336 nodeClients.put(node, highLevelClient);
337 highLevelClient.addHighLevelClientListener(this);
343 * Removes the given node from the node manager, disconnecting it if it is
344 * currently connected.
349 public void removeNode(Node node) {
350 synchronized (syncObject) {
351 if (!nodes.contains(node)) {
354 if (nodeClients.containsKey(node)) {
358 node.removePropertyChangeListener(this);
359 fireNodeRemoved(node);
364 * Tries to establish a connection with the given node.
367 * The node to connect to
369 public void connect(Node node) {
370 HighLevelClient highLevelClient;
371 highLevelClient = nodeClients.get(node);
372 if (highLevelClient == null) {
373 logger.warning("was told to connect to unknown node: " + node);
377 highLevelClient.connect(node.getHostname(), node.getPort());
378 } catch (UnknownHostException uhe1) {
379 fireNodeConnectionFailed(node, uhe1);
380 } catch (IOException ioe1) {
381 fireNodeConnectionFailed(node, ioe1);
386 * Disconnects the given node without removing it.
389 * The node to disconnect
391 public void disconnect(Node node) {
392 synchronized (syncObject) {
393 if (!nodes.contains(node)) {
396 HighLevelClient highLevelClient = nodeClients.get(node);
397 highLevelClient.disconnect();
402 * Returns a list of all nodes.
404 * @return A list of all nodes
406 public List<Node> getNodes() {
407 return Collections.unmodifiableList(nodes);
411 * Returns the high-level client for a given node.
414 * The node to get a high-level client for
415 * @return The high-level client for a node, or <code>null</code> if the
416 * node was disconnected or removed
418 public HighLevelClient getHighLevelClient(Node node) {
419 return nodeClients.get(node);
423 * Returns the node for a high-level client.
425 * @param highLevelClient
426 * The high-level client to get the node for
427 * @return The node for the high-level client, or <code>null</code> if the
428 * high-level client is not known
430 public Node getNode(HighLevelClient highLevelClient) {
431 return clientNodes.get(highLevelClient);
435 * Generates a new SSK key pair.
437 * @return An array with the private key at index <code>0</code> and the
438 * public key at index <code>1</code>
439 * @throws IOException
440 * if an I/O error occurs communicating with the node
441 * @throws JSiteException
442 * if there is a problem with the node
444 public String[] generateKeyPair() throws IOException, JSiteException {
445 if (nodes.isEmpty()) {
446 throw new NoNodeException("no node configured");
448 Node node = nodes.get(0);
449 HighLevelClient highLevelClient = nodeClients.get(node);
451 KeyGenerationResult keyGenerationResult = highLevelClient.generateKey().getResult();
452 return new String[] { keyGenerationResult.getInsertURI(), keyGenerationResult.getRequestURI() };
453 } catch (HighLevelException hle1) {
454 throw new BackendException(hle1);
455 } catch (InterruptedException e) {
466 // INTERFACE HighLevelClientListener
472 public void clientConnected(HighLevelClient highLevelClient) {
473 logger.log(Level.FINER, "clientConnected(c=" + highLevelClient + ")");
474 Node node = clientNodes.get(highLevelClient);
476 logger.log(Level.WARNING, "got event for unknown client");
479 fireNodeConnected(node);
485 public void clientDisconnected(HighLevelClient highLevelClient, Throwable throwable) {
486 logger.log(Level.FINER, "clientDisconnected(c=" + highLevelClient + ",t=" + throwable + ")");
487 synchronized (syncObject) {
488 Node node = clientNodes.get(highLevelClient);
490 logger.log(Level.WARNING, "got event for unknown client");
493 fireNodeDisconnected(node, throwable);
498 // INTERFACE PropertyChangeListener
504 public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
505 Object eventSource = propertyChangeEvent.getSource();
506 if (eventSource instanceof Node) {
507 String propertyName = propertyChangeEvent.getPropertyName();
508 if ("hostname".equals(propertyName) || "port".equals(propertyName)) {
509 Node node = (Node) eventSource;
510 HighLevelClient highLevelClient = nodeClients.get(node);
511 if (highLevelClient == null) {
512 logger.log(Level.WARNING, "got property change event for unknown node: " + node);
515 if (highLevelClient.isConnected()) {
516 highLevelClient.disconnect();
518 highLevelClient.connect(node.getHostname(), node.getPort());
519 } catch (UnknownHostException uhe1) {
520 fireNodeConnectionFailed(node, uhe1);
521 } catch (IOException ioe1) {
522 fireNodeConnectionFailed(node, ioe1);