add javadoc
[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         /** Counter. */
38         private static int counter = 0;
39
40         /** The node to connect to. */
41         private Node node;
42
43         /** The connection to the node. */
44         private Connection connection;
45
46         /**
47          * Sets the hostname of the node. The default port for FCP2 connections ({@link Node#DEFAULT_PORT})
48          * is used.
49          * 
50          * @param hostname
51          *            The hostname of the node
52          */
53         public void setNodeAddress(String hostname) {
54                 node = new Node(hostname);
55                 connection = new Connection(node, "connection-" + counter++);
56         }
57
58         /**
59          * Sets the hostname and the port of the node.
60          * 
61          * @param hostname
62          *            The hostname of the node
63          * @param port
64          *            The port number of the node
65          */
66         public void setNodeAddress(String hostname, int port) {
67                 node = new Node(hostname, port);
68                 connection = new Connection(node, "connection-" + counter++);
69         }
70
71         /**
72          * Sets hostname and port from the given node.
73          * 
74          * @param node
75          *            The node to get the hostname and port from
76          */
77         public void setNode(de.todesbaum.jsite.application.Node node) {
78                 if (node != null) {
79                         this.node = new Node(node.getHostname(), node.getPort());
80                         connection = new Connection(node, "connection-" + counter++);
81                 } else {
82                         this.node = null;
83                         connection = null;
84                 }
85         }
86
87         /**
88          * Removes the current node from the interface.
89          */
90         public void removeNode() {
91                 node = null;
92                 connection = null;
93         }
94
95         /**
96          * Returns the node this interface is connecting to.
97          * 
98          * @return The node
99          */
100         public Node getNode() {
101                 return node;
102         }
103
104         /**
105          * Creates a new connection to the current node with the given identifier.
106          * 
107          * @param identifier
108          *            The identifier of the connection
109          * @return The connection to the node
110          */
111         public Connection getConnection(String identifier) {
112                 return new Connection(node, identifier);
113         }
114
115         /**
116          * Checks whether the current node is connected. If the node is not
117          * connected, a connection will be tried.
118          * 
119          * @return <code>true</code> if the node is connected, <code>false</code>
120          *         otherwise
121          * @throws IOException
122          *             if an I/O error occurs communicating with the node
123          */
124         public boolean isNodePresent() throws IOException {
125                 if (!connection.isConnected()) {
126                         return connection.connect();
127                 }
128                 return true;
129         }
130
131         /**
132          * Generates an SSK key pair.
133          * 
134          * @return An array of strings, the first one being the generated private
135          *         (insert) URI and the second one being the generated public
136          *         (request) URI
137          * @throws IOException
138          *             if an I/O error occurs communicating with the node
139          */
140         public String[] generateKeyPair() throws IOException {
141                 if (!isNodePresent()) {
142                         return null;
143                 }
144                 GenerateSSK generateSSK = new GenerateSSK();
145                 Client client = new Client(connection, generateSSK);
146                 Message keypairMessage = client.readMessage();
147                 return new String[] { keypairMessage.get("InsertURI"), keypairMessage.get("RequestURI") };
148         }
149
150         /**
151          * Checks whether the interface has already been configured with a node.
152          * 
153          * @return <code>true</code> if this interface already has a node set,
154          *         <code>false</code> otherwise
155          */
156         public boolean hasNode() {
157                 return (node != null) && (connection != null);
158         }
159
160 }