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.logging.Level;
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;
*
* @author David ‘Bombe’ Roden <bombe@freenetproject.org>
*/
-public class NodeManager implements Iterable<Node>, PropertyChangeListener, HighLevelClientListener {
+public class NodeManager implements Iterable<Node>, PropertyChangeListener {
/** Logger. */
private static final Logger logger = Logging.getLogger(NodeManager.class.getName());
/** Map from node ID to node. */
private final Map<String, Node> idNodes = Collections.synchronizedMap(new HashMap<String, Node>());
- /** All FCP connections. */
- private final Map<Node, HighLevelClient> nodeClients = Collections.synchronizedMap(new HashMap<Node, HighLevelClient>());
-
- /** Maps nodes to high-level clients. */
- private final Map<HighLevelClient, Node> clientNodes = Collections.synchronizedMap(new HashMap<HighLevelClient, Node>());
-
/** Collection of currently connected nodes. */
private final Set<Node> connectedNodes = Collections.synchronizedSet(new HashSet<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);
nodeListenerSupport.fireNodeAdded(node);
return true;
}
if (!nodes.contains(node)) {
return;
}
- if (nodeClients.containsKey(node)) {
- disconnect(node);
- }
nodes.remove(node);
idNodes.remove(node.getId());
node.removePropertyChangeListener(this);
*/
public void connect(Node node) {
logger.log(Level.FINEST, "connect(node=" + node + ")");
- HighLevelClient highLevelClient;
- highLevelClient = nodeClients.get(node);
- if (highLevelClient == null) {
- logger.log(Level.WARNING, "was told to connect to unknown node: " + node);
- return;
- }
- try {
- highLevelClient.connect(node.getHostname(), node.getPort());
- } catch (UnknownHostException uhe1) {
- nodeListenerSupport.fireNodeConnectionFailed(node, uhe1);
- } catch (IOException ioe1) {
- nodeListenerSupport.fireNodeConnectionFailed(node, ioe1);
- }
}
/**
*/
public void disconnect(Node node) {
logger.log(Level.FINEST, "disconnect(node=" + node + ")");
- synchronized (syncObject) {
- if (!nodes.contains(node)) {
- return;
- }
- HighLevelClient highLevelClient = nodeClients.get(node);
- highLevelClient.disconnect();
- }
}
/**
}
/**
- * Returns the high-level client for a given node.
- *
- * @param node
- * The node to get a high-level client for
- * @return The high-level client for a node, or <code>null</code> if the
- * node was disconnected or removed
- */
- public HighLevelClient getHighLevelClient(Node node) {
- 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 <code>null</code> 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
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;
}
//
//
- // INTERFACE HighLevelClientListener
- //
-
- /**
- * {@inheritDoc}
- */
- public void clientConnected(HighLevelClient 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");
- return;
- }
- connectedNodes.add(node);
- nodeListenerSupport.fireNodeConnected(node);
- }
-
- /**
- * {@inheritDoc}
- */
- public void clientDisconnected(HighLevelClient highLevelClient, Throwable throwable) {
- logger.log(Level.FINEST, "clientDisconnected(highLevelClient=" + highLevelClient + ",throwable=" + throwable + ")");
- synchronized (syncObject) {
- Node node = clientNodes.get(highLevelClient);
- if (node == null) {
- logger.log(Level.WARNING, "got event for unknown client");
- return;
- }
- connectedNodes.remove(node);
- nodeListenerSupport.fireNodeDisconnected(node, throwable);
- }
- }
-
- //
// INTERFACE PropertyChangeListener
//
if (eventSource instanceof Node) {
String propertyName = propertyChangeEvent.getPropertyName();
if ("hostname".equals(propertyName) || "port".equals(propertyName)) {
- Node node = (Node) eventSource;
- HighLevelClient highLevelClient = nodeClients.get(node);
- if (highLevelClient == null) {
- logger.log(Level.WARNING, "got property change event for unknown node: " + node);
- return;
- }
- if (highLevelClient.isConnected()) {
- highLevelClient.disconnect();
- try {
- highLevelClient.connect(node.getHostname(), node.getPort());
- } catch (UnknownHostException uhe1) {
- nodeListenerSupport.fireNodeConnectionFailed(node, uhe1);
- } catch (IOException ioe1) {
- nodeListenerSupport.fireNodeConnectionFailed(node, ioe1);
- }
- }
+ /* TODO - reconnect. */
}
}
}