2 * jFCPlib - FcpClient.java -
3 * Copyright © 2009 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;
25 import java.util.HashSet;
27 import java.util.concurrent.CountDownLatch;
29 import net.pterodactylus.fcp.ClientHello;
30 import net.pterodactylus.fcp.CloseConnectionDuplicateClientName;
31 import net.pterodactylus.fcp.EndListPeers;
32 import net.pterodactylus.fcp.FcpAdapter;
33 import net.pterodactylus.fcp.FcpConnection;
34 import net.pterodactylus.fcp.FcpListener;
35 import net.pterodactylus.fcp.NodeHello;
36 import net.pterodactylus.fcp.Peer;
37 import net.pterodactylus.fcp.ProtocolError;
40 * High-level FCP client that hides the details of the underlying FCP
43 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
45 public class FcpClient {
47 /** Object used for synchronization. */
48 private final Object syncObject = new Object();
50 /** The name of this client. */
51 private final String name;
53 /** The underlying FCP connection. */
54 private final FcpConnection fcpConnection;
57 * Creates an FCP client with the given name.
60 * The name of the FCP client
61 * @throws UnknownHostException
62 * if the hostname “localhost” is unknown
64 public FcpClient(String name) throws UnknownHostException {
65 this(name, "localhost");
69 * Creates an FCP client.
72 * The name of the FCP client
74 * The hostname of the Freenet node
75 * @throws UnknownHostException
76 * if the given hostname can not be resolved
78 public FcpClient(String name, String hostname) throws UnknownHostException {
79 this(name, hostname, FcpConnection.DEFAULT_PORT);
83 * Creates an FCP client.
86 * The name of the FCP client
88 * The hostname of the Freenet node
90 * The Freenet node’s FCP port
91 * @throws UnknownHostException
92 * if the given hostname can not be resolved
94 public FcpClient(String name, String hostname, int port) throws UnknownHostException {
95 this(name, InetAddress.getByName(hostname), port);
99 * Creates an FCP client.
102 * The name of the FCP client
104 * The host address of the Freenet node
106 public FcpClient(String name, InetAddress host) {
107 this(name, host, FcpConnection.DEFAULT_PORT);
111 * Creates an FCP client.
114 * The name of the FCP client
116 * The host address of the Freenet node
118 * The Freenet node’s FCP port
120 public FcpClient(String name, InetAddress host, int port) {
122 fcpConnection = new FcpConnection(host, port);
130 * Connects the FCP client.
132 * @throws IOException
133 * if an I/O error occurs
134 * @throws FcpException
135 * if an FCP error occurs
137 public void connect() throws IOException, FcpException {
138 ExtendedFcpAdapter fcpListener = new ExtendedFcpAdapter() {
144 public void receivedNodeHello(FcpConnection fcpConnection, NodeHello nodeHello) {
145 completionLatch.countDown();
148 fcpConnection.addFcpListener(fcpListener);
150 fcpConnection.connect();
151 ClientHello clientHello = new ClientHello(name);
152 fcpConnection.sendMessage(clientHello);
155 fcpListener.complete();
157 } catch (InterruptedException e) {
158 /* ignore, we’ll loop. */
162 fcpConnection.removeFcpListener(fcpListener);
164 if (fcpListener.getFcpException() != null) {
165 throw fcpListener.getFcpException();
170 * Disconnects the FCP client.
172 public void disconnect() {
173 synchronized (syncObject) {
174 fcpConnection.close();
175 syncObject.notifyAll();
184 * Returns all peers that the node has.
186 * @return A set containing the node’s peers
187 * @throws IOException
188 * if an I/O error occurs
189 * @throws FcpException
190 * if an FCP error occurs
192 public Set<Peer> getPeers() throws IOException, FcpException {
193 final Set<Peer> peers = new HashSet<Peer>();
194 ExtendedFcpAdapter fcpAdapter = new ExtendedFcpAdapter() {
200 public void receivedPeer(FcpConnection fcpConnection, Peer peer) {
208 public void receivedEndListPeers(FcpConnection fcpConnection, EndListPeers endListPeers) {
209 completionLatch.countDown();
212 fcpConnection.addFcpListener(fcpAdapter);
216 fcpAdapter.complete();
218 } catch (InterruptedException e) {
219 /* ignore, we’ll loop. */
223 fcpConnection.removeFcpListener(fcpAdapter);
229 * Implementation of an {@link FcpListener} that can store an
230 * {@link FcpException} and wait for the arrival of a certain command.
232 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
234 private static class ExtendedFcpAdapter extends FcpAdapter {
236 /** The count down latch used to wait for completion. */
237 protected final CountDownLatch completionLatch = new CountDownLatch(1);
239 /** The FCP exception, if any. */
240 protected FcpException fcpException;
243 * Creates a new extended FCP adapter.
245 public ExtendedFcpAdapter() {
250 * Returns the FCP exception that occured. If no FCP exception occured,
251 * <code>null</code> is returned.
253 * @return The FCP exception that occured, or <code>null</code>
255 public FcpException getFcpException() {
260 * Waits for the completion of the command.
262 * @throws InterruptedException
263 * if {@link CountDownLatch#await()} is interrupted
265 public void complete() throws InterruptedException {
266 completionLatch.await();
273 public void connectionClosed(FcpConnection fcpConnection, Throwable throwable) {
274 fcpException = new FcpException("Connection closed", throwable);
275 completionLatch.countDown();
282 public void receivedCloseConnectionDuplicateClientName(FcpConnection fcpConnection, CloseConnectionDuplicateClientName closeConnectionDuplicateClientName) {
283 fcpException = new FcpException("Connection closed, duplicate client name");
284 completionLatch.countDown();
291 public void receivedProtocolError(FcpConnection fcpConnection, ProtocolError protocolError) {
292 fcpException = new FcpException("Protocol error (" + protocolError.getCode() + ", " + protocolError.getCodeDescription());
293 completionLatch.countDown();