dae72fb38d017b2e73dd5770661adfc469b53aa3
[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 hostname and port from the given node.
50          *
51          * @param node
52          *            The node to get the hostname and port from
53          */
54         public void setNode(de.todesbaum.jsite.application.Node node) {
55                 if (node != null) {
56                         this.node = new Node(node.getHostname(), node.getPort());
57                         connection = new Connection(node, "jSite-" + number + "-connection-" + counter++);
58                 } else {
59                         this.node = null;
60                         connection = null;
61                 }
62         }
63
64         /**
65          * Returns the node this interface is connecting to.
66          *
67          * @return The node
68          */
69         public Node getNode() {
70                 return node;
71         }
72
73         /**
74          * Creates a new connection to the current node with the given identifier.
75          *
76          * @param identifier
77          *            The identifier of the connection
78          * @return The connection to the node
79          */
80         public Connection getConnection(String identifier) {
81                 return new Connection(node, identifier);
82         }
83
84         /**
85          * Checks whether the current node is connected. If the node is not
86          * connected, a connection will be tried.
87          *
88          * @return <code>true</code> if the node is connected, <code>false</code>
89          *         otherwise
90          * @throws IOException
91          *             if an I/O error occurs communicating with the node
92          */
93         public boolean isNodePresent() throws IOException {
94                 if (!connection.isConnected()) {
95                         return connection.connect();
96                 }
97                 return true;
98         }
99
100         /**
101          * Generates an SSK key pair.
102          *
103          * @return An array of strings, the first one being the generated private
104          *         (insert) URI and the second one being the generated public
105          *         (request) URI
106          * @throws IOException
107          *             if an I/O error occurs communicating with the node
108          */
109         public String[] generateKeyPair() throws IOException {
110                 if (!isNodePresent()) {
111                         throw new IOException("Node is offline.");
112                 }
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") };
117         }
118
119         /**
120          * Checks whether the interface has already been configured with a node.
121          *
122          * @return <code>true</code> if this interface already has a node set,
123          *         <code>false</code> otherwise
124          */
125         public boolean hasNode() {
126                 return (node != null) && (connection != null);
127         }
128
129 }