2 * jSite - Freenet7Interface.java - Copyright © 2006–2014 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 hostname and port from the given node.
52 * The node to get the hostname and port from
54 public void setNode(de.todesbaum.jsite.application.Node node) {
56 this.node = new Node(node.getHostname(), node.getPort());
57 connection = new Connection(node, "jSite-" + number + "-connection-" + counter++);
65 * Returns the node this interface is connecting to.
69 public Node getNode() {
74 * Creates a new connection to the current node with the given identifier.
77 * The identifier of the connection
78 * @return The connection to the node
80 public Connection getConnection(String identifier) {
81 return new Connection(node, identifier);
85 * Checks whether the current node is connected. If the node is not
86 * connected, a connection will be tried.
88 * @return <code>true</code> if the node is connected, <code>false</code>
91 * if an I/O error occurs communicating with the node
93 public boolean isNodePresent() throws IOException {
94 if (!connection.isConnected()) {
95 return connection.connect();
101 * Generates an SSK key pair.
103 * @return An array of strings, the first one being the generated private
104 * (insert) URI and the second one being the generated public
106 * @throws IOException
107 * if an I/O error occurs communicating with the node
109 public String[] generateKeyPair() throws IOException {
110 if (!isNodePresent()) {
111 throw new IOException("Node is offline.");
113 GenerateSSK generateSSK = new GenerateSSK();
114 Client client = new Client(connection, generateSSK);
115 Message keypairMessage = client.readMessage();
116 return new String[] { keypairMessage.get("InsertURI"), keypairMessage.get("RequestURI") };
120 * Checks whether the interface has already been configured with a node.
122 * @return <code>true</code> if this interface already has a node set,
123 * <code>false</code> otherwise
125 public boolean hasNode() {
126 return (node != null) && (connection != null);