3 * Copyright (C) 2006 David Roden
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 package de.todesbaum.jsite.application;
22 import java.io.IOException;
24 import de.todesbaum.util.freenet.fcp2.Client;
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 {
38 private static int counter = 0;
40 /** The node to connect to. */
43 /** The connection to the node. */
44 private Connection connection;
47 * Sets the hostname of the node. The default port for FCP2 connections ({@link Node#DEFAULT_PORT})
51 * The hostname of the node
53 public void setNodeAddress(String hostname) {
54 node = new Node(hostname);
55 connection = new Connection(node, "connection-" + counter++);
59 * Sets the hostname and the port of the node.
62 * The hostname of the node
64 * The port number of the node
66 public void setNodeAddress(String hostname, int port) {
67 node = new Node(hostname, port);
68 connection = new Connection(node, "connection-" + counter++);
72 * Sets hostname and port from the given node.
75 * The node to get the hostname and port from
77 public void setNode(de.todesbaum.jsite.application.Node node) {
79 this.node = new Node(node.getHostname(), node.getPort());
80 connection = new Connection(node, "connection-" + counter++);
88 * Removes the current node from the interface.
90 public void removeNode() {
96 * Returns the node this interface is connecting to.
100 public Node getNode() {
105 * Creates a new connection to the current node with the given identifier.
108 * The identifier of the connection
109 * @return The connection to the node
111 public Connection getConnection(String identifier) {
112 return new Connection(node, identifier);
116 * Checks whether the current node is connected. If the node is not
117 * connected, a connection will be tried.
119 * @return <code>true</code> if the node is connected, <code>false</code>
121 * @throws IOException
122 * if an I/O error occurs communicating with the node
124 public boolean isNodePresent() throws IOException {
125 if (!connection.isConnected()) {
126 return connection.connect();
132 * Generates an SSK key pair.
134 * @return An array of strings, the first one being the generated private
135 * (insert) URI and the second one being the generated public
137 * @throws IOException
138 * if an I/O error occurs communicating with the node
140 public String[] generateKeyPair() throws IOException {
141 if (!isNodePresent()) {
144 GenerateSSK generateSSK = new GenerateSSK();
145 Client client = new Client(connection, generateSSK);
146 Message keypairMessage = client.readMessage();
147 return new String[] { keypairMessage.get("InsertURI"), keypairMessage.get("RequestURI") };
151 * Checks whether the interface has already been configured with a node.
153 * @return <code>true</code> if this interface already has a node set,
154 * <code>false</code> otherwise
156 public boolean hasNode() {
157 return (node != null) && (connection != null);