ae5c967764d12062cab2e6567dd56a759dda5c62
[jSite.git] / src / de / todesbaum / jsite / application / Freenet7Interface.java
1 /*
2  * jSite -
3  * Copyright (C) 2006 David Roden
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 package de.todesbaum.jsite.application;
21
22 import java.io.IOException;
23
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;
29
30 /**
31  * Interface for freenet-related operations.
32  *
33  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
34  */
35 public class Freenet7Interface {
36
37         /** Random number to differentiate several jSites. */
38         private static final int number = (int) (Math.random() * Integer.MAX_VALUE);
39
40         /** Counter. */
41         private static int counter = 0;
42
43         /** The node to connect to. */
44         private Node node;
45
46         /** The connection to the node. */
47         private Connection connection;
48
49         /**
50          * Sets the hostname of the node. The default port for FCP2 connections ({@link Node#DEFAULT_PORT})
51          * is used.
52          *
53          * @param hostname
54          *            The hostname of the node
55          */
56         public void setNodeAddress(String hostname) {
57                 node = new Node(hostname);
58                 connection = new Connection(node, "jSite-" + number + "-connection-" + counter++);
59         }
60
61         /**
62          * Sets the hostname and the port of the node.
63          *
64          * @param hostname
65          *            The hostname of the node
66          * @param port
67          *            The port number of the node
68          */
69         public void setNodeAddress(String hostname, int port) {
70                 node = new Node(hostname, port);
71                 connection = new Connection(node, "jSite-" + number + "-connection-" + counter++);
72         }
73
74         /**
75          * Sets hostname and port from the given node.
76          *
77          * @param node
78          *            The node to get the hostname and port from
79          */
80         public void setNode(de.todesbaum.jsite.application.Node node) {
81                 if (node != null) {
82                         this.node = new Node(node.getHostname(), node.getPort());
83                         connection = new Connection(node, "jSite-" + number + "-connection-" + counter++);
84                 } else {
85                         this.node = null;
86                         connection = null;
87                 }
88         }
89
90         /**
91          * Removes the current node from the interface.
92          */
93         public void removeNode() {
94                 node = null;
95                 connection = null;
96         }
97
98         /**
99          * Returns the node this interface is connecting to.
100          *
101          * @return The node
102          */
103         public Node getNode() {
104                 return node;
105         }
106
107         /**
108          * Creates a new connection to the current node with the given identifier.
109          *
110          * @param identifier
111          *            The identifier of the connection
112          * @return The connection to the node
113          */
114         public Connection getConnection(String identifier) {
115                 return new Connection(node, identifier);
116         }
117
118         /**
119          * Checks whether the current node is connected. If the node is not
120          * connected, a connection will be tried.
121          *
122          * @return <code>true</code> if the node is connected, <code>false</code>
123          *         otherwise
124          * @throws IOException
125          *             if an I/O error occurs communicating with the node
126          */
127         public boolean isNodePresent() throws IOException {
128                 if (!connection.isConnected()) {
129                         return connection.connect();
130                 }
131                 return true;
132         }
133
134         /**
135          * Generates an SSK key pair.
136          *
137          * @return An array of strings, the first one being the generated private
138          *         (insert) URI and the second one being the generated public
139          *         (request) URI
140          * @throws IOException
141          *             if an I/O error occurs communicating with the node
142          */
143         public String[] generateKeyPair() throws IOException {
144                 if (!isNodePresent()) {
145                         throw new IOException("Node is offline.");
146                 }
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") };
151         }
152
153         /**
154          * Checks whether the interface has already been configured with a node.
155          *
156          * @return <code>true</code> if this interface already has a node set,
157          *         <code>false</code> otherwise
158          */
159         public boolean hasNode() {
160                 return (node != null) && (connection != null);
161         }
162
163 }