📄 Update year in copyright line
[jSite.git] / src / main / java / de / todesbaum / jsite / application / Freenet7Interface.java
1 /*
2  * jSite - Freenet7Interface.java - Copyright Â© 2006–2019 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.Command;
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         private final NodeSupplier nodeSupplier;
44         private final ConnectionSupplier connectionSupplier;
45         private final ClientSupplier clientSupplier;
46
47         /** The node to connect to. */
48         private Node node;
49
50         /** The connection to the node. */
51         private Connection connection;
52
53         public Freenet7Interface() {
54                 this(new DefaultNodeSupplier(), new DefaultConnectionSupplier(), new DefaultClientSupplier());
55         }
56
57         Freenet7Interface(NodeSupplier nodeSupplier, ConnectionSupplier connectionSupplier, ClientSupplier clientSupplier) {
58                 this.nodeSupplier = nodeSupplier;
59                 this.connectionSupplier = connectionSupplier;
60                 this.clientSupplier = clientSupplier;
61         }
62
63         /**
64          * Sets hostname and port from the given node.
65          *
66          * @param node
67          *            The node to get the hostname and port from
68          */
69         public void setNode(de.todesbaum.jsite.application.Node node) {
70                 if (node != null) {
71                         this.node = nodeSupplier.supply(node.getHostname(), node.getPort());
72                         connection = connectionSupplier.supply(node, "jSite-" + number + "-connection-" + counter++);
73                 } else {
74                         this.node = null;
75                         connection = null;
76                 }
77         }
78
79         /**
80          * Returns the node this interface is connecting to.
81          *
82          * @return The node
83          */
84         public Node getNode() {
85                 return node;
86         }
87
88         /**
89          * Creates a new connection to the current node with the given identifier.
90          *
91          * @param identifier
92          *            The identifier of the connection
93          * @return The connection to the node
94          */
95         public Connection getConnection(String identifier) {
96                 return connectionSupplier.supply(node, identifier);
97         }
98
99         /**
100          * Checks whether the current node is connected. If the node is not
101          * connected, a connection will be tried.
102          *
103          * @return <code>true</code> if the node is connected, <code>false</code>
104          *         otherwise
105          * @throws IOException
106          *             if an I/O error occurs communicating with the node
107          */
108         public boolean isNodePresent() throws IOException {
109                 if (!connection.isConnected()) {
110                         return connection.connect();
111                 }
112                 return true;
113         }
114
115         /**
116          * Generates an SSK key pair.
117          *
118          * @return An array of strings, the first one being the generated private
119          *         (insert) URI and the second one being the generated public
120          *         (request) URI
121          * @throws IOException
122          *             if an I/O error occurs communicating with the node
123          */
124         public String[] generateKeyPair() throws IOException {
125                 if (!isNodePresent()) {
126                         throw new IOException("Node is offline.");
127                 }
128                 GenerateSSK generateSSK = new GenerateSSK();
129                 Client client = clientSupplier.supply(connection, generateSSK);
130                 Message keypairMessage = client.readMessage();
131                 return new String[] { keypairMessage.get("InsertURI"), keypairMessage.get("RequestURI") };
132         }
133
134         /**
135          * Checks whether the interface has already been configured with a node.
136          *
137          * @return <code>true</code> if this interface already has a node set,
138          *         <code>false</code> otherwise
139          */
140         public boolean hasNode() {
141                 return (node != null) && (connection != null);
142         }
143
144         public interface NodeSupplier {
145
146                 Node supply(String hostname, int port);
147
148         }
149
150         public static class DefaultNodeSupplier implements NodeSupplier {
151
152                 @Override
153                 public Node supply(String hostname, int port) {
154                         return new Node(hostname, port);
155                 }
156
157         }
158
159         public interface ConnectionSupplier {
160
161                 Connection supply(Node node, String identifier);
162
163         }
164
165         public static class DefaultConnectionSupplier implements ConnectionSupplier {
166
167                 @Override
168                 public Connection supply(Node node, String identifier) {
169                         return new Connection(node, identifier);
170                 }
171
172         }
173
174         public interface ClientSupplier {
175
176                 Client supply(Connection connection, Command command) throws IOException;
177
178         }
179
180         public static class DefaultClientSupplier implements ClientSupplier {
181
182                 @Override
183                 public Client supply(Connection connection, Command command) throws IOException {
184                         return new Client(connection, command);
185                 }
186
187         }
188
189 }