2 * jSite - Freenet7Interface.java - Copyright © 2006–2011 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.Connection;
25 import de.todesbaum.util.freenet.fcp2.GenerateSSK;
26 import de.todesbaum.util.freenet.fcp2.Message;
27 import de.todesbaum.util.freenet.fcp2.Node;
30 * Interface for freenet-related operations.
32 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
34 public class Freenet7Interface {
36 /** Random number to differentiate several jSites. */
37 private static final int number = (int) (Math.random() * Integer.MAX_VALUE);
40 private static int counter = 0;
42 /** The node to connect to. */
45 /** The connection to the node. */
46 private Connection connection;
49 * Sets the hostname of the node. The default port for FCP2 connections ({@link Node#DEFAULT_PORT})
53 * The hostname of the node
55 public void setNodeAddress(String hostname) {
56 node = new Node(hostname);
57 connection = new Connection(node, "jSite-" + number + "-connection-" + counter++);
61 * Sets the hostname and the port of the node.
64 * The hostname of the node
66 * The port number of the node
68 public void setNodeAddress(String hostname, int port) {
69 node = new Node(hostname, port);
70 connection = new Connection(node, "jSite-" + number + "-connection-" + counter++);
74 * Sets hostname and port from the given node.
77 * The node to get the hostname and port from
79 public void setNode(de.todesbaum.jsite.application.Node node) {
81 this.node = new Node(node.getHostname(), node.getPort());
82 connection = new Connection(node, "jSite-" + number + "-connection-" + counter++);
90 * Removes the current node from the interface.
92 public void removeNode() {
98 * Returns the node this interface is connecting to.
102 public Node getNode() {
107 * Creates a new connection to the current node with the given identifier.
110 * The identifier of the connection
111 * @return The connection to the node
113 public Connection getConnection(String identifier) {
114 return new Connection(node, identifier);
118 * Checks whether the current node is connected. If the node is not
119 * connected, a connection will be tried.
121 * @return <code>true</code> if the node is connected, <code>false</code>
123 * @throws IOException
124 * if an I/O error occurs communicating with the node
126 public boolean isNodePresent() throws IOException {
127 if (!connection.isConnected()) {
128 return connection.connect();
134 * Generates an SSK key pair.
136 * @return An array of strings, the first one being the generated private
137 * (insert) URI and the second one being the generated public
139 * @throws IOException
140 * if an I/O error occurs communicating with the node
142 public String[] generateKeyPair() throws IOException {
143 if (!isNodePresent()) {
144 throw new IOException("Node is offline.");
146 GenerateSSK generateSSK = new GenerateSSK();
147 Client client = new Client(connection, generateSSK);
148 Message keypairMessage = client.readMessage();
149 return new String[] { keypairMessage.get("InsertURI"), keypairMessage.get("RequestURI") };
153 * Checks whether the interface has already been configured with a node.
155 * @return <code>true</code> if this interface already has a node set,
156 * <code>false</code> otherwise
158 public boolean hasNode() {
159 return (node != null) && (connection != null);