/** The project manager. */
private ProjectManager projectManager;
- /** The node list. */
- private List<Node> configuredNodes = new ArrayList<Node>();
-
- /** List of currently connected nodes. */
- private List<Node> connectedNodes = new ArrayList<Node>();
+ /** The node manager. */
+ private NodeManager nodeManager;
//
// LISTENER MANAGEMENT
}
/**
+ * Notifies all listeners that the projects were loaded successfully.
+ *
+ * @param directory
+ * The directory the projects were loaded from
+ */
+ private void fireLoadingProjectsDone(String directory) {
+ for (CoreListener coreListener: coreListeners) {
+ coreListener.loadingProjectsDone(directory);
+ }
+ }
+
+ /**
* Notifies all core listeners that loading the projects from the given
* directory has failed.
*
}
/**
+ * Notifies all listeners that the nodes were successfully loaded.
+ *
+ * @param directory
+ * The directory the nodes were loaded from
+ */
+ private void fireLoadingNodesDone(String directory) {
+ for (CoreListener coreListener: coreListeners) {
+ coreListener.loadingNodesDone(directory);
+ }
+ }
+
+ /**
+ * Notifies all listeners that loading the nodes has failed.
+ *
+ * @param directory
+ * The directory the nodes were loaded from
+ * @param throwable
+ * The exception that occured while loading the nodes
+ */
+ private void fireLoadingNodesFailed(String directory, Throwable throwable) {
+ for (CoreListener coreListener: coreListeners) {
+ coreListener.loadingNodesFailed(directory, throwable);
+ }
+ }
+
+ /**
+ * Notifies all listeners that the nodes were saved successfully.
+ *
+ * @param directory
+ * The directory the nodes were saved to
+ */
+ private void fireSavingNodesDone(String directory) {
+ for (CoreListener coreListener: coreListeners) {
+ coreListener.savingNodesDone(directory);
+ }
+ }
+
+ /**
+ * Notifies all listeners that saving the nodes has failed.
+ *
+ * @param directory
+ * The directory the nodes were saved to
+ * @param throwable
+ * The exception that occured while saving the nodes
+ */
+ private void fireSavingNodesFailed(String directory, Throwable throwable) {
+ for (CoreListener coreListener: coreListeners) {
+ coreListener.savingProjectsFailed(directory, throwable);
+ }
+ }
+
+ /**
* Notifies all core listeners that the core has loaded and is ready to run.
*/
private void fireCoreLoaded() {
}
/**
+ * Returns the node manager.
+ *
+ * @return The node manager
+ */
+ public NodeManager getNodeManager() {
+ return nodeManager;
+ }
+
+ /**
+ * Sets the node manager to use.
+ *
+ * @param nodeManager
+ * The node manager to use
+ */
+ public void setNodeManager(NodeManager nodeManager) {
+ this.nodeManager = nodeManager;
+ }
+
+ /**
* Returns the list of all configured nodes.
*
* @return All configured nodes
*/
public List<Node> getNodes() {
- return configuredNodes;
+ return nodeManager.getNodes();
}
/**
* node, <code>false</code> otherwise
*/
public boolean isNodeConnected(Node node) {
- return connectedNodes.contains(node);
+ return nodeManager.hasNode(node);
}
//
public void start() {
try {
projectManager.load();
+ fireLoadingProjectsDone(projectManager.getDirectory());
} catch (IOException ioe1) {
fireLoadingProjectsFailed(projectManager.getDirectory(), ioe1);
}
+ try {
+ nodeManager.load();
+ fireLoadingNodesDone(nodeManager.getDirectory());
+ } catch (IOException ioe1) {
+ fireLoadingNodesFailed(nodeManager.getDirectory(), ioe1);
+ }
fireCoreLoaded();
}
} catch (IOException ioe1) {
fireSavingProjectsFailed(projectManager.getDirectory(), ioe1);
}
+ try {
+ nodeManager.save();
+ fireSavingNodesDone(nodeManager.getDirectory());
+ } catch (IOException ioe1) {
+ fireSavingNodesFailed(nodeManager.getDirectory(), ioe1);
+ }
fireCoreStopped();
}
--- /dev/null
+/*
+ * jSite2 - FcpCollector.java -
+ * Copyright © 2008 David Roden
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+package net.pterodactylus.jsite.core;
+
+import java.io.File;
+import java.io.FileInputStream;
+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;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import net.pterodactylus.fcp.highlevel.ConnectResult;
+import net.pterodactylus.fcp.highlevel.HighLevelCallback;
+import net.pterodactylus.fcp.highlevel.HighLevelClient;
+import net.pterodactylus.util.io.Closer;
+
+/**
+ * TODO
+ *
+ * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
+ * @version $Id$
+ */
+public class NodeManager {
+
+ /** Logger. */
+ private static final Logger logger = Logger.getLogger(NodeManager.class.getName());
+
+ /** The FCP client name. */
+ private final String clientName;
+
+ /** The directory for the configuration. */
+ private final String directory;
+
+ /** Object used for synchronization. */
+ private final Object syncObject = new Object();
+
+ /** All nodes. */
+ private List<Node> nodes = Collections.synchronizedList(new ArrayList<Node>());
+
+ /** All FCP connections. */
+ private Map<Node, HighLevelClient> nodeConnections = Collections.synchronizedMap(new HashMap<Node, HighLevelClient>());
+
+ /** Keeps track of which connection is in use right now. */
+ private Set<HighLevelClient> usedConnections = Collections.synchronizedSet(new HashSet<HighLevelClient>());
+
+ /** Maps nodes to high-level clients. */
+ private Map<HighLevelClient, Node> clientNodes = Collections.synchronizedMap(new HashMap<HighLevelClient, Node>());
+
+ /**
+ * Creates a new FCP collector.
+ *
+ * @param clientName
+ * The name of the FCP client
+ * @param directory
+ * The directory in which to store the nodes
+ */
+ public NodeManager(String clientName, String directory) {
+ this.clientName = clientName;
+ this.directory = directory;
+ }
+
+ //
+ // ACCESSORS
+ //
+
+ /**
+ * Returns the directory in which the nodes are stored.
+ *
+ * @return The directory the nodes are stored in
+ */
+ public String getDirectory() {
+ return directory;
+ }
+
+ /**
+ * Checks whether the given node is already connected.
+ *
+ * @param node
+ * The node to check
+ * @return <code>true</code> if the node is already connected,
+ * <code>false</code> otherwise
+ */
+ public boolean hasNode(Node node) {
+ return nodes.contains(node);
+ }
+
+ //
+ // ACTIONS
+ //
+
+ /**
+ * Loads nodes.
+ *
+ * @throws IOException
+ * if an I/O error occurs loading the nodes
+ */
+ public void load() throws IOException {
+ File directoryFile = new File(directory);
+ File nodeFile = new File(directoryFile, "nodes.properties");
+ if (!nodeFile.exists() || !nodeFile.isFile() || !nodeFile.canRead()) {
+ return;
+ }
+ Properties nodeProperties = new Properties();
+ InputStream nodeInputStream = null;
+ try {
+ nodeInputStream = new FileInputStream(nodeFile);
+ nodeProperties.load(nodeInputStream);
+ } finally {
+ Closer.close(nodeInputStream);
+ }
+ int nodeIndex = -1;
+ List<Node> loadedNodes = new ArrayList<Node>();
+ while (nodeProperties.containsKey("nodes." + ++nodeIndex + ".name")) {
+ String nodePrefix = "nodes." + nodeIndex;
+ String nodeName = nodeProperties.getProperty(nodePrefix + ".name");
+ if (!Verifier.verifyNodeName(nodeName)) {
+ logger.log(Level.WARNING, "invalid node name “" + nodeName + "”, skipping…");
+ continue;
+ }
+ String nodeHostname = nodeProperties.getProperty(nodePrefix + ".hostname");
+ if (!Verifier.verifyHostname(nodeHostname)) {
+ logger.log(Level.WARNING, "invalid host name “" + nodeHostname + "”");
+ /* not fatal, might be valid later on. */
+ }
+ String nodePortString = nodeProperties.getProperty(nodePrefix + ".port");
+ if (!Verifier.verifyPort(nodePortString)) {
+ logger.log(Level.WARNING, "invalid port number “" + nodePortString + "”, skipping…");
+ continue;
+ }
+ int nodePort = -1;
+ try {
+ nodePort = Integer.valueOf(nodePortString);
+ } catch (NumberFormatException nfe1) {
+ /* shouldn't happen, port number was checked before. */
+ logger.log(Level.SEVERE, "invalid port number “" + nodePortString + "”, check failed! skipping…");
+ continue;
+ }
+ Node newNode = new Node();
+ newNode.setName(nodeName);
+ newNode.setHostname(nodeHostname);
+ newNode.setPort(nodePort);
+ loadedNodes.add(newNode);
+ }
+ synchronized (syncObject) {
+ nodes.clear();
+ nodes.addAll(loadedNodes);
+ }
+ }
+
+ /**
+ * Saves all configured nodes.
+ *
+ * @throws IOException
+ * if an I/O error occurs saving the nodes
+ */
+ public void save() throws IOException {
+ File directoryFile = new File(directory);
+ if (!directoryFile.exists()) {
+ if (!directoryFile.mkdirs()) {
+ throw new IOException("could not create directory: " + directory);
+ }
+ }
+ Properties nodeProperties = new Properties();
+ int nodeIndex = -1;
+ for (Node node: nodes) {
+ String nodePrefix = "nodes." + ++nodeIndex;
+ nodeProperties.setProperty(nodePrefix + ".name", node.getName());
+ nodeProperties.setProperty(nodePrefix + ".hostname", node.getHostname());
+ nodeProperties.setProperty(nodePrefix + ".port", String.valueOf(node.getPort()));
+ }
+ File projectFile = new File(directoryFile, "nodes.properties");
+ OutputStream nodeOutputStream = null;
+ try {
+ nodeOutputStream = new FileOutputStream(projectFile);
+ nodeProperties.store(nodeOutputStream, "jSite nodes");
+ } finally {
+ Closer.close(nodeOutputStream);
+ }
+ }
+
+ /**
+ * Adds a connection to the given node. The connection is made instantly so
+ * this method may block. If the node can not be connected, it will not be
+ * added to the list of nodes.
+ *
+ * @param node
+ * The node to connect to
+ * @return <code>true</code> if the connection to the node could be
+ * established
+ * @throws UnknownHostException
+ * if the hostname of the node can not be resolved
+ * @throws IOException
+ * if an I/O error occurs connecting to the node
+ */
+ public boolean addNode(Node node) throws UnknownHostException, IOException {
+ if (nodes.contains(node)) {
+ return true;
+ }
+ HighLevelClient highLevelClient = new HighLevelClient(clientName, node.getHostname(), node.getPort());
+ HighLevelCallback<ConnectResult> connectCallback = highLevelClient.connect();
+ ConnectResult connectResult = null;
+ while (connectResult == null) {
+ try {
+ connectResult = connectCallback.getResult();
+ } catch (InterruptedException e) {
+ /* ignore. */
+ }
+ }
+ if (connectResult.isConnected()) {
+ synchronized (syncObject) {
+ nodes.add(node);
+ nodeConnections.put(node, highLevelClient);
+ clientNodes.put(highLevelClient, node);
+ }
+ }
+ return connectResult.isConnected();
+ }
+
+ /**
+ * Returns a list of all nodes.
+ *
+ * @return A list of all nodes
+ */
+ public List<Node> getNodes() {
+ return new ArrayList<Node>(clientNodes.values());
+ }
+
+ //
+ // PRIVATE METHODS
+ //
+
+ /**
+ * Finds a currently unused high-level client, optionally waiting until a
+ * client is free and marking it used.
+ *
+ * @param wait
+ * <code>true</code> to wait for a free connection,
+ * <code>false</code> to return <code>null</code>
+ * @param markAsUsed
+ * <code>true</code> to mark the connection as used before
+ * returning it, <code>false</code> not to mark it
+ * @return An unused FCP connection, or <code>null</code> if no connection
+ * could be found
+ */
+ @SuppressWarnings("unused")
+ private HighLevelClient findUnusedClient(boolean wait, boolean markAsUsed) {
+ synchronized (syncObject) {
+ HighLevelClient freeHighLevelClient = null;
+ while (freeHighLevelClient == null) {
+ for (HighLevelClient highLevelClient: nodeConnections.values()) {
+ if (!usedConnections.contains(highLevelClient)) {
+ freeHighLevelClient = highLevelClient;
+ break;
+ }
+ }
+ if (freeHighLevelClient != null) {
+ if (markAsUsed) {
+ usedConnections.add(freeHighLevelClient);
+ }
+ return freeHighLevelClient;
+ }
+ if (!wait) {
+ return null;
+ }
+ }
+ /* we never get here, but the compiler doesn't realize. */
+ return null;
+ }
+ }
+
+}