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.SSKKeypair;
+import net.pterodactylus.fcp.highlevel.FcpClient;
+import net.pterodactylus.fcp.highlevel.FcpException;
import net.pterodactylus.jsite.util.IdGenerator;
import net.pterodactylus.util.io.Closer;
import net.pterodactylus.util.logging.Logging;
private final Object syncObject = new Object();
/** Node listener support. */
- private final NodeListenerSupport nodeListenerSupport = new NodeListenerSupport();
+ private final NodeListenerSupport nodeListenerManager = new NodeListenerSupport();
/** All nodes. */
private final List<Node> nodes = Collections.synchronizedList(new ArrayList<Node>());
/** Map from node ID to node. */
private final Map<String, Node> idNodes = Collections.synchronizedMap(new HashMap<String, Node>());
+ /** Map from node to client. */
+ private final Map<Node, FcpClient> nodeClients = Collections.synchronizedMap(new HashMap<Node, FcpClient>());
+
/** Collection of currently connected nodes. */
private final Set<Node> connectedNodes = Collections.synchronizedSet(new HashSet<Node>());
* The listener to add
*/
public void addNodeListener(NodeListener nodeListener) {
- nodeListenerSupport.addListener(nodeListener);
+ nodeListenerManager.addListener(nodeListener);
}
/**
* The listener to remove
*/
public void removeNodeListener(NodeListener nodeListener) {
- nodeListenerSupport.removeListener(nodeListener);
+ nodeListenerManager.removeListener(nodeListener);
}
//
node.addPropertyChangeListener(this);
nodes.add(node);
idNodes.put(node.getId(), node);
- nodeListenerSupport.fireNodeAdded(node);
+ nodeListenerManager.fireNodeAdded(node);
return true;
}
nodes.remove(node);
idNodes.remove(node.getId());
node.removePropertyChangeListener(this);
- nodeListenerSupport.fireNodeRemoved(node);
+ nodeListenerManager.fireNodeRemoved(node);
}
}
logger.log(Level.WARNING, "Was told to connect to node (" + node + ") I don’t know about!");
return;
}
+ try {
+ FcpClient fcpClient = new FcpClient(clientName, node.getHostname(), node.getPort());
+ fcpClient.connect();
+ nodeClients.put(node, fcpClient);
+ nodeListenerManager.fireNodeConnected(node);
+ } catch (UnknownHostException uhe1) {
+ nodeListenerManager.fireNodeConnectionFailed(node, uhe1);
+ } catch (IOException ioe1) {
+ nodeListenerManager.fireNodeConnectionFailed(node, ioe1);
+ } catch (FcpException fe1) {
+ nodeListenerManager.fireNodeConnectionFailed(node, fe1);
+ }
}
/**
*/
public void disconnect(Node node) {
logger.log(Level.FINEST, "disconnect(node=" + node + ")");
+ if (!nodes.contains(node)) {
+ logger.log(Level.WARNING, "Was told to disconnect from a node (" + node + ") I don’t know about!");
+ return;
+ }
+ FcpClient fcpClient = nodeClients.remove(node);
+ if (fcpClient == null) {
+ logger.log(Level.WARNING, "No FCP client for node (" + node + ")!");
+ return;
+ }
+ fcpClient.disconnect();
+ nodeListenerManager.fireNodeDisconnected(node, null);
}
/**
}
/**
+ * Returns the FCP client for the given node.
+ *
+ * @param node
+ * The node to get the FCP client for
+ * @return The FCP client for the given node, or {@code null} if the node
+ * does not have an associated FCP client
+ */
+ FcpClient getFcpClient(Node node) {
+ return nodeClients.get(node);
+ }
+
+ /**
* Generates a new SSK key pair.
*
* @return An array with the private key at index <code>0</code> and the
* @throws IOException
* if an I/O error occurs communicating with the node
* @throws JSiteException
- * if there is a problem with the node
+ * if there is no connected node
*/
public String[] generateKeyPair() throws IOException, JSiteException {
logger.log(Level.FINEST, "generateKeyPair()");
if (nodes.isEmpty()) {
throw new NoNodeException("no node configured");
}
- return null;
+ FcpException fcpException = null;
+ for (FcpClient fcpClient : nodeClients.values()) {
+ try {
+ SSKKeypair sskKeypair = fcpClient.generateKeyPair();
+ return new String[] { sskKeypair.getInsertURI(), sskKeypair.getRequestURI() };
+ } catch (FcpException fcpe1) {
+ fcpException = fcpe1;
+ }
+ }
+ throw new JSiteException("Could not get SSK key pair from any node.", fcpException);
}
//