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