2 * jSite - Freenet7Interface.java - Copyright © 2006–2019 David Roden
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 package de.todesbaum.jsite.application;
21 import java.io.IOException;
23 import de.todesbaum.util.freenet.fcp2.Client;
24 import de.todesbaum.util.freenet.fcp2.Command;
25 import de.todesbaum.util.freenet.fcp2.Connection;
26 import de.todesbaum.util.freenet.fcp2.GenerateSSK;
27 import de.todesbaum.util.freenet.fcp2.Message;
28 import de.todesbaum.util.freenet.fcp2.Node;
31 * Interface for freenet-related operations.
33 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
35 public class Freenet7Interface {
37 /** Random number to differentiate several jSites. */
38 private static final int number = (int) (Math.random() * Integer.MAX_VALUE);
41 private static int counter = 0;
43 private final NodeSupplier nodeSupplier;
44 private final ConnectionSupplier connectionSupplier;
45 private final ClientSupplier clientSupplier;
47 /** The node to connect to. */
50 /** The connection to the node. */
51 private Connection connection;
53 public Freenet7Interface() {
54 this(new DefaultNodeSupplier(), new DefaultConnectionSupplier(), new DefaultClientSupplier());
57 Freenet7Interface(NodeSupplier nodeSupplier, ConnectionSupplier connectionSupplier, ClientSupplier clientSupplier) {
58 this.nodeSupplier = nodeSupplier;
59 this.connectionSupplier = connectionSupplier;
60 this.clientSupplier = clientSupplier;
64 * Sets hostname and port from the given node.
67 * The node to get the hostname and port from
69 public void setNode(de.todesbaum.jsite.application.Node node) {
71 this.node = nodeSupplier.supply(node.getHostname(), node.getPort());
72 connection = connectionSupplier.supply(node, "jSite-" + number + "-connection-" + counter++);
80 * Returns the node this interface is connecting to.
84 public Node getNode() {
89 * Creates a new connection to the current node with the given identifier.
92 * The identifier of the connection
93 * @return The connection to the node
95 public Connection getConnection(String identifier) {
96 return connectionSupplier.supply(node, identifier);
100 * Checks whether the current node is connected. If the node is not
101 * connected, a connection will be tried.
103 * @return <code>true</code> if the node is connected, <code>false</code>
105 * @throws IOException
106 * if an I/O error occurs communicating with the node
108 public boolean isNodePresent() throws IOException {
109 if (!connection.isConnected()) {
110 return connection.connect();
116 * Generates an SSK key pair.
118 * @return An array of strings, the first one being the generated private
119 * (insert) URI and the second one being the generated public
121 * @throws IOException
122 * if an I/O error occurs communicating with the node
124 public String[] generateKeyPair() throws IOException {
125 if (!isNodePresent()) {
126 throw new IOException("Node is offline.");
128 GenerateSSK generateSSK = new GenerateSSK();
129 Client client = clientSupplier.supply(connection, generateSSK);
130 Message keypairMessage = client.readMessage();
131 return new String[] { keypairMessage.get("InsertURI"), keypairMessage.get("RequestURI") };
135 * Checks whether the interface has already been configured with a node.
137 * @return <code>true</code> if this interface already has a node set,
138 * <code>false</code> otherwise
140 public boolean hasNode() {
141 return (node != null) && (connection != null);
144 public interface NodeSupplier {
146 Node supply(String hostname, int port);
150 public static class DefaultNodeSupplier implements NodeSupplier {
153 public Node supply(String hostname, int port) {
154 return new Node(hostname, port);
159 public interface ConnectionSupplier {
161 Connection supply(Node node, String identifier);
165 public static class DefaultConnectionSupplier implements ConnectionSupplier {
168 public Connection supply(Node node, String identifier) {
169 return new Connection(node, identifier);
174 public interface ClientSupplier {
176 Client supply(Connection connection, Command command) throws IOException;
180 public static class DefaultClientSupplier implements ClientSupplier {
183 public Client supply(Connection connection, Command command) throws IOException {
184 return new Client(connection, command);