import java.io.IOException;
import de.todesbaum.util.freenet.fcp2.Client;
+import de.todesbaum.util.freenet.fcp2.Command;
import de.todesbaum.util.freenet.fcp2.Connection;
import de.todesbaum.util.freenet.fcp2.GenerateSSK;
import de.todesbaum.util.freenet.fcp2.Message;
/** Counter. */
private static int counter = 0;
+ private final NodeSupplier nodeSupplier;
+ private final ConnectionSupplier connectionSupplier;
+ private final ClientSupplier clientSupplier;
+
/** The node to connect to. */
private Node node;
/** The connection to the node. */
private Connection connection;
+ public Freenet7Interface() {
+ this(new DefaultNodeSupplier(), new DefaultConnectionSupplier(), new DefaultClientSupplier());
+ }
+
+ Freenet7Interface(NodeSupplier nodeSupplier, ConnectionSupplier connectionSupplier, ClientSupplier clientSupplier) {
+ this.nodeSupplier = nodeSupplier;
+ this.connectionSupplier = connectionSupplier;
+ this.clientSupplier = clientSupplier;
+ }
+
/**
* Sets hostname and port from the given node.
*
*/
public void setNode(de.todesbaum.jsite.application.Node node) {
if (node != null) {
- this.node = new Node(node.getHostname(), node.getPort());
- connection = new Connection(node, "jSite-" + number + "-connection-" + counter++);
+ this.node = nodeSupplier.supply(node.getHostname(), node.getPort());
+ connection = connectionSupplier.supply(node, "jSite-" + number + "-connection-" + counter++);
} else {
this.node = null;
connection = null;
* @return The connection to the node
*/
public Connection getConnection(String identifier) {
- return new Connection(node, identifier);
+ return connectionSupplier.supply(node, identifier);
}
/**
throw new IOException("Node is offline.");
}
GenerateSSK generateSSK = new GenerateSSK();
- Client client = new Client(connection, generateSSK);
+ Client client = clientSupplier.supply(connection, generateSSK);
Message keypairMessage = client.readMessage();
return new String[] { keypairMessage.get("InsertURI"), keypairMessage.get("RequestURI") };
}
return (node != null) && (connection != null);
}
+ public interface NodeSupplier {
+
+ Node supply(String hostname, int port);
+
+ }
+
+ public static class DefaultNodeSupplier implements NodeSupplier {
+
+ @Override
+ public Node supply(String hostname, int port) {
+ return new Node(hostname, port);
+ }
+
+ }
+
+ public interface ConnectionSupplier {
+
+ Connection supply(Node node, String identifier);
+
+ }
+
+ public static class DefaultConnectionSupplier implements ConnectionSupplier {
+
+ @Override
+ public Connection supply(Node node, String identifier) {
+ return new Connection(node, identifier);
+ }
+
+ }
+
+ public interface ClientSupplier {
+
+ Client supply(Connection connection, Command command) throws IOException;
+
+ }
+
+ public static class DefaultClientSupplier implements ClientSupplier {
+
+ @Override
+ public Client supply(Connection connection, Command command) throws IOException {
+ return new Client(connection, command);
+ }
+
+ }
+
}