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 {
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 /** The node to connect to. */
46 /** The connection to the node. */
47 private Connection connection;
50 * Sets the hostname of the node. The default port for FCP2 connections ({@link Node#DEFAULT_PORT})
54 * The hostname of the node
56 public void setNodeAddress(String hostname) {
57 node = new Node(hostname);
58 connection = new Connection(node, "jSite-" + number + "-connection-" + counter++);
62 * Sets the hostname and the port of the node.
65 * The hostname of the node
67 * The port number of the node
69 public void setNodeAddress(String hostname, int port) {
70 node = new Node(hostname, port);
71 connection = new Connection(node, "jSite-" + number + "-connection-" + counter++);
75 * Sets hostname and port from the given node.
78 * The node to get the hostname and port from
80 public void setNode(de.todesbaum.jsite.application.Node node) {
82 this.node = new Node(node.getHostname(), node.getPort());
83 connection = new Connection(node, "jSite-" + number + "-connection-" + counter++);
91 * Removes the current node from the interface.
93 public void removeNode() {
99 * Returns the node this interface is connecting to.
103 public Node getNode() {
108 * Creates a new connection to the current node with the given identifier.
111 * The identifier of the connection
112 * @return The connection to the node
114 public Connection getConnection(String identifier) {
115 return new Connection(node, identifier);
119 * Checks whether the current node is connected. If the node is not
120 * connected, a connection will be tried.
122 * @return <code>true</code> if the node is connected, <code>false</code>
124 * @throws IOException
125 * if an I/O error occurs communicating with the node
127 public boolean isNodePresent() throws IOException {
128 if (!connection.isConnected()) {
129 return connection.connect();
135 * Generates an SSK key pair.
137 * @return An array of strings, the first one being the generated private
138 * (insert) URI and the second one being the generated public
140 * @throws IOException
141 * if an I/O error occurs communicating with the node
143 public String[] generateKeyPair() throws IOException {
144 if (!isNodePresent()) {
147 GenerateSSK generateSSK = new GenerateSSK();
148 Client client = new Client(connection, generateSSK);
149 Message keypairMessage = client.readMessage();
150 return new String[] { keypairMessage.get("InsertURI"), keypairMessage.get("RequestURI") };
154 * Checks whether the interface has already been configured with a node.
156 * @return <code>true</code> if this interface already has a node set,
157 * <code>false</code> otherwise
159 public boolean hasNode() {
160 return (node != null) && (connection != null);