2 * fcplib - HighLevelClient.java -
3 * Copyright © 2008 David Roden
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.
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.
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.
20 package net.pterodactylus.fcp.highlevel;
22 import java.io.IOException;
23 import java.net.InetAddress;
24 import java.net.UnknownHostException;
26 import net.pterodactylus.fcp.AllData;
27 import net.pterodactylus.fcp.ClientHello;
28 import net.pterodactylus.fcp.CloseConnectionDuplicateClientName;
29 import net.pterodactylus.fcp.ConfigData;
30 import net.pterodactylus.fcp.DataFound;
31 import net.pterodactylus.fcp.EndListPeerNotes;
32 import net.pterodactylus.fcp.EndListPeers;
33 import net.pterodactylus.fcp.EndListPersistentRequests;
34 import net.pterodactylus.fcp.FCPPluginReply;
35 import net.pterodactylus.fcp.FcpConnection;
36 import net.pterodactylus.fcp.FcpListener;
37 import net.pterodactylus.fcp.FcpMessage;
38 import net.pterodactylus.fcp.FinishedCompression;
39 import net.pterodactylus.fcp.GetFailed;
40 import net.pterodactylus.fcp.IdentifierCollision;
41 import net.pterodactylus.fcp.NodeData;
42 import net.pterodactylus.fcp.NodeHello;
43 import net.pterodactylus.fcp.Peer;
44 import net.pterodactylus.fcp.PeerNote;
45 import net.pterodactylus.fcp.PeerRemoved;
46 import net.pterodactylus.fcp.PersistentGet;
47 import net.pterodactylus.fcp.PersistentPut;
48 import net.pterodactylus.fcp.PersistentPutDir;
49 import net.pterodactylus.fcp.PersistentRequestModified;
50 import net.pterodactylus.fcp.PersistentRequestRemoved;
51 import net.pterodactylus.fcp.PluginInfo;
52 import net.pterodactylus.fcp.ProtocolError;
53 import net.pterodactylus.fcp.PutFailed;
54 import net.pterodactylus.fcp.PutFetchable;
55 import net.pterodactylus.fcp.PutSuccessful;
56 import net.pterodactylus.fcp.SSKKeypair;
57 import net.pterodactylus.fcp.SimpleProgress;
58 import net.pterodactylus.fcp.StartedCompression;
59 import net.pterodactylus.fcp.SubscribedUSKUpdate;
60 import net.pterodactylus.fcp.TestDDAComplete;
61 import net.pterodactylus.fcp.TestDDAReply;
62 import net.pterodactylus.fcp.URIGenerated;
63 import net.pterodactylus.fcp.UnknownNodeIdentifier;
64 import net.pterodactylus.fcp.UnknownPeerNoteType;
67 * A high-level client that allows simple yet full-featured access to a Freenet
70 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
73 public class HighLevelClient {
75 /** Object for internal synchronization. */
76 private final Object syncObject = new Object();
78 /** The name of the client. */
79 private final String clientName;
81 /** The address of the node. */
82 private InetAddress address;
84 /** The port number of the node. */
87 /** The FCP connection to the node. */
88 private FcpConnection fcpConnection;
90 /** The listener for the connection. */
91 private HighLevelClientFcpListener highLevelClientFcpListener = new HighLevelClientFcpListener();
93 /** The callback for {@link #connect()}. */
94 private HighLevelCallback<ConnectResult> connectCallback;
97 * Creates a new high-level client that connects to a node on
98 * <code>localhost</code>.
101 * The name of the client
102 * @throws UnknownHostException
103 * if the hostname of the node can not be resolved.
105 public HighLevelClient(String clientName) throws UnknownHostException {
106 this(clientName, "localhost");
110 * Creates a new high-level client that connects to a node on the given
114 * The name of the client
116 * The hostname of the node
117 * @throws UnknownHostException
118 * if the hostname of the node can not be resolved.
120 public HighLevelClient(String clientName, String host) throws UnknownHostException {
121 this(clientName, host, FcpConnection.DEFAULT_PORT);
125 * Creates a new high-level client that connects to a node on the given
129 * The name of the client
131 * The hostname of the node
133 * The port number of the node
134 * @throws UnknownHostException
135 * if the hostname of the node can not be resolved.
137 public HighLevelClient(String clientName, String host, int port) throws UnknownHostException {
138 this(clientName, InetAddress.getByName(host), port);
142 * Creates a new high-level client that connects to a node at the given
146 * The name of the client
148 * The address of the node
150 * The port number of the node
152 public HighLevelClient(String clientName, InetAddress address, int port) {
153 this.clientName = clientName;
154 this.address = address;
167 * Connects the client.
169 * @return A callback with a connection result
170 * @throws IOException
171 * if an I/O error occurs communicating with the node
173 public HighLevelCallback<ConnectResult> connect() throws IOException {
174 fcpConnection = new FcpConnection(address, port);
175 fcpConnection.addFcpListener(highLevelClientFcpListener);
176 ClientHello clientHello = new ClientHello(clientName);
177 connectCallback = new HighLevelCallback<ConnectResult>();
178 fcpConnection.sendMessage(clientHello);
179 return connectCallback;
183 * Disconnects the client from the node.
185 public void disconnect() {
189 * FCP listener for {@link HighLevelClient}.
191 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
194 private class HighLevelClientFcpListener implements FcpListener {
197 * Creates a new FCP listener for {@link HighLevelClient}.
199 HighLevelClientFcpListener() {
203 * @see net.pterodactylus.fcp.FcpListener#connectionClosed(net.pterodactylus.fcp.FcpConnection)
205 public void connectionClosed(FcpConnection fcpConnection) {
209 * @see net.pterodactylus.fcp.FcpListener#receivedAllData(net.pterodactylus.fcp.FcpConnection,
210 * net.pterodactylus.fcp.AllData)
212 public void receivedAllData(FcpConnection fcpConnection, AllData allData) {
216 * @see net.pterodactylus.fcp.FcpListener#receivedCloseConnectionDuplicateClientName(net.pterodactylus.fcp.FcpConnection,
217 * net.pterodactylus.fcp.CloseConnectionDuplicateClientName)
219 public void receivedCloseConnectionDuplicateClientName(FcpConnection fcpConnection, CloseConnectionDuplicateClientName closeConnectionDuplicateClientName) {
223 * @see net.pterodactylus.fcp.FcpListener#receivedConfigData(net.pterodactylus.fcp.FcpConnection,
224 * net.pterodactylus.fcp.ConfigData)
226 public void receivedConfigData(FcpConnection fcpConnection, ConfigData configData) {
230 * @see net.pterodactylus.fcp.FcpListener#receivedDataFound(net.pterodactylus.fcp.FcpConnection,
231 * net.pterodactylus.fcp.DataFound)
233 public void receivedDataFound(FcpConnection fcpConnection, DataFound dataFound) {
237 * @see net.pterodactylus.fcp.FcpListener#receivedEndListPeerNotes(net.pterodactylus.fcp.FcpConnection,
238 * net.pterodactylus.fcp.EndListPeerNotes)
240 public void receivedEndListPeerNotes(FcpConnection fcpConnection, EndListPeerNotes endListPeerNotes) {
244 * @see net.pterodactylus.fcp.FcpListener#receivedEndListPeers(net.pterodactylus.fcp.FcpConnection,
245 * net.pterodactylus.fcp.EndListPeers)
247 public void receivedEndListPeers(FcpConnection fcpConnection, EndListPeers endListPeers) {
251 * @see net.pterodactylus.fcp.FcpListener#receivedEndListPersistentRequests(net.pterodactylus.fcp.FcpConnection,
252 * net.pterodactylus.fcp.EndListPersistentRequests)
254 public void receivedEndListPersistentRequests(FcpConnection fcpConnection, EndListPersistentRequests endListPersistentRequests) {
258 * @see net.pterodactylus.fcp.FcpListener#receivedFCPPluginReply(net.pterodactylus.fcp.FcpConnection,
259 * net.pterodactylus.fcp.FCPPluginReply)
261 public void receivedFCPPluginReply(FcpConnection fcpConnection, FCPPluginReply fcpPluginReply) {
265 * @see net.pterodactylus.fcp.FcpListener#receivedGetFailed(net.pterodactylus.fcp.FcpConnection,
266 * net.pterodactylus.fcp.GetFailed)
268 public void receivedGetFailed(FcpConnection fcpConnection, GetFailed getFailed) {
272 * @see net.pterodactylus.fcp.FcpListener#receivedIdentifierCollision(net.pterodactylus.fcp.FcpConnection,
273 * net.pterodactylus.fcp.IdentifierCollision)
275 public void receivedIdentifierCollision(FcpConnection fcpConnection, IdentifierCollision identifierCollision) {
279 * @see net.pterodactylus.fcp.FcpListener#receivedMessage(net.pterodactylus.fcp.FcpConnection,
280 * net.pterodactylus.fcp.FcpMessage)
282 public void receivedMessage(FcpConnection fcpConnection, FcpMessage fcpMessage) {
286 * @see net.pterodactylus.fcp.FcpListener#receivedNodeData(net.pterodactylus.fcp.FcpConnection,
287 * net.pterodactylus.fcp.NodeData)
289 public void receivedNodeData(FcpConnection fcpConnection, NodeData nodeData) {
293 * @see net.pterodactylus.fcp.FcpListener#receivedNodeHello(net.pterodactylus.fcp.FcpConnection,
294 * net.pterodactylus.fcp.NodeHello)
296 @SuppressWarnings("synthetic-access")
297 public void receivedNodeHello(FcpConnection fcpConnection, NodeHello nodeHello) {
298 if (fcpConnection != HighLevelClient.this.fcpConnection) {
301 ConnectResult connectResult = new ConnectResult();
303 synchronized (syncObject) {
304 connectCallback.setResult(connectResult);
309 * @see net.pterodactylus.fcp.FcpListener#receivedPeer(net.pterodactylus.fcp.FcpConnection,
310 * net.pterodactylus.fcp.Peer)
312 public void receivedPeer(FcpConnection fcpConnection, Peer peer) {
316 * @see net.pterodactylus.fcp.FcpListener#receivedPeerNote(net.pterodactylus.fcp.FcpConnection,
317 * net.pterodactylus.fcp.PeerNote)
319 public void receivedPeerNote(FcpConnection fcpConnection, PeerNote peerNote) {
323 * @see net.pterodactylus.fcp.FcpListener#receivedPeerRemoved(net.pterodactylus.fcp.FcpConnection,
324 * net.pterodactylus.fcp.PeerRemoved)
326 public void receivedPeerRemoved(FcpConnection fcpConnection, PeerRemoved peerRemoved) {
330 * @see net.pterodactylus.fcp.FcpListener#receivedPersistentGet(net.pterodactylus.fcp.FcpConnection,
331 * net.pterodactylus.fcp.PersistentGet)
333 public void receivedPersistentGet(FcpConnection fcpConnection, PersistentGet persistentGet) {
337 * @see net.pterodactylus.fcp.FcpListener#receivedPersistentPut(net.pterodactylus.fcp.FcpConnection,
338 * net.pterodactylus.fcp.PersistentPut)
340 public void receivedPersistentPut(FcpConnection fcpConnection, PersistentPut persistentPut) {
344 * @see net.pterodactylus.fcp.FcpListener#receivedPersistentPutDir(net.pterodactylus.fcp.FcpConnection,
345 * net.pterodactylus.fcp.PersistentPutDir)
347 public void receivedPersistentPutDir(FcpConnection fcpConnection, PersistentPutDir persistentPutDir) {
351 * @see net.pterodactylus.fcp.FcpListener#receivedPersistentRequestModified(net.pterodactylus.fcp.FcpConnection,
352 * net.pterodactylus.fcp.PersistentRequestModified)
354 public void receivedPersistentRequestModified(FcpConnection fcpConnection, PersistentRequestModified persistentRequestModified) {
358 * @see net.pterodactylus.fcp.FcpListener#receivedPersistentRequestRemoved(net.pterodactylus.fcp.FcpConnection,
359 * net.pterodactylus.fcp.PersistentRequestRemoved)
361 public void receivedPersistentRequestRemoved(FcpConnection fcpConnection, PersistentRequestRemoved persistentRequestRemoved) {
365 * @see net.pterodactylus.fcp.FcpListener#receivedPluginInfo(net.pterodactylus.fcp.FcpConnection,
366 * net.pterodactylus.fcp.PluginInfo)
368 public void receivedPluginInfo(FcpConnection fcpConnection, PluginInfo pluginInfo) {
372 * @see net.pterodactylus.fcp.FcpListener#receivedProtocolError(net.pterodactylus.fcp.FcpConnection,
373 * net.pterodactylus.fcp.ProtocolError)
375 public void receivedProtocolError(FcpConnection fcpConnection, ProtocolError protocolError) {
379 * @see net.pterodactylus.fcp.FcpListener#receivedPutFailed(net.pterodactylus.fcp.FcpConnection,
380 * net.pterodactylus.fcp.PutFailed)
382 public void receivedPutFailed(FcpConnection fcpConnection, PutFailed putFailed) {
386 * @see net.pterodactylus.fcp.FcpListener#receivedPutFetchable(net.pterodactylus.fcp.FcpConnection,
387 * net.pterodactylus.fcp.PutFetchable)
389 public void receivedPutFetchable(FcpConnection fcpConnection, PutFetchable putFetchable) {
393 * @see net.pterodactylus.fcp.FcpListener#receivedPutSuccessful(net.pterodactylus.fcp.FcpConnection,
394 * net.pterodactylus.fcp.PutSuccessful)
396 public void receivedPutSuccessful(FcpConnection fcpConnection, PutSuccessful putSuccessful) {
400 * @see net.pterodactylus.fcp.FcpListener#receivedSSKKeypair(net.pterodactylus.fcp.FcpConnection,
401 * net.pterodactylus.fcp.SSKKeypair)
403 public void receivedSSKKeypair(FcpConnection fcpConnection, SSKKeypair sskKeypair) {
407 * @see net.pterodactylus.fcp.FcpListener#receivedSimpleProgress(net.pterodactylus.fcp.FcpConnection,
408 * net.pterodactylus.fcp.SimpleProgress)
410 public void receivedSimpleProgress(FcpConnection fcpConnection, SimpleProgress simpleProgress) {
414 * @see net.pterodactylus.fcp.FcpListener#receivedStartedCompression(net.pterodactylus.fcp.FcpConnection,
415 * net.pterodactylus.fcp.StartedCompression)
417 public void receivedStartedCompression(FcpConnection fcpConnection, StartedCompression startedCompression) {
421 * @see net.pterodactylus.fcp.FcpListener#receivedSubscribedUSKUpdate(net.pterodactylus.fcp.FcpConnection,
422 * net.pterodactylus.fcp.SubscribedUSKUpdate)
424 public void receivedSubscribedUSKUpdate(FcpConnection fcpConnection, SubscribedUSKUpdate subscribedUSKUpdate) {
428 * @see net.pterodactylus.fcp.FcpListener#receivedTestDDAComplete(net.pterodactylus.fcp.FcpConnection,
429 * net.pterodactylus.fcp.TestDDAComplete)
431 public void receivedTestDDAComplete(FcpConnection fcpConnection, TestDDAComplete testDDAComplete) {
435 * @see net.pterodactylus.fcp.FcpListener#receivedTestDDAReply(net.pterodactylus.fcp.FcpConnection,
436 * net.pterodactylus.fcp.TestDDAReply)
438 public void receivedTestDDAReply(FcpConnection fcpConnection, TestDDAReply testDDAReply) {
442 * @see net.pterodactylus.fcp.FcpListener#receivedURIGenerated(net.pterodactylus.fcp.FcpConnection,
443 * net.pterodactylus.fcp.URIGenerated)
445 public void receivedURIGenerated(FcpConnection fcpConnection, URIGenerated uriGenerated) {
449 * @see net.pterodactylus.fcp.FcpListener#receivedUnknownNodeIdentifier(net.pterodactylus.fcp.FcpConnection,
450 * net.pterodactylus.fcp.UnknownNodeIdentifier)
452 public void receivedUnknownNodeIdentifier(FcpConnection fcpConnection, UnknownNodeIdentifier unknownNodeIdentifier) {
456 * @see net.pterodactylus.fcp.FcpListener#receivedUnknownPeerNoteType(net.pterodactylus.fcp.FcpConnection,
457 * net.pterodactylus.fcp.UnknownPeerNoteType)
459 public void receivedUnknownPeerNoteType(FcpConnection fcpConnection, UnknownPeerNoteType unknownPeerNoteType) {
463 * @see net.pterodactylus.fcp.FcpListener#receviedFinishedCompression(net.pterodactylus.fcp.FcpConnection,
464 * net.pterodactylus.fcp.FinishedCompression)
466 public void receviedFinishedCompression(FcpConnection fcpConnection, FinishedCompression finishedCompression) {