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.ListPeers;
36 import net.pterodactylus.fcp.NodeHello;
37 import net.pterodactylus.fcp.Peer;
38 import net.pterodactylus.fcp.ProtocolError;
41 * High-level FCP client that hides the details of the underlying FCP
44 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
46 public class FcpClient {
48 /** Object used for synchronization. */
49 private final Object syncObject = new Object();
51 /** The name of this client. */
52 private final String name;
54 /** The underlying FCP connection. */
55 private final FcpConnection fcpConnection;
58 * Creates an FCP client with the given name.
61 * The name of the FCP client
62 * @throws UnknownHostException
63 * if the hostname “localhost” is unknown
65 public FcpClient(String name) throws UnknownHostException {
66 this(name, "localhost");
70 * Creates an FCP client.
73 * The name of the FCP client
75 * The hostname of the Freenet node
76 * @throws UnknownHostException
77 * if the given hostname can not be resolved
79 public FcpClient(String name, String hostname) throws UnknownHostException {
80 this(name, hostname, FcpConnection.DEFAULT_PORT);
84 * Creates an FCP client.
87 * The name of the FCP client
89 * The hostname of the Freenet node
91 * The Freenet node’s FCP port
92 * @throws UnknownHostException
93 * if the given hostname can not be resolved
95 public FcpClient(String name, String hostname, int port) throws UnknownHostException {
96 this(name, InetAddress.getByName(hostname), port);
100 * Creates an FCP client.
103 * The name of the FCP client
105 * The host address of the Freenet node
107 public FcpClient(String name, InetAddress host) {
108 this(name, host, FcpConnection.DEFAULT_PORT);
112 * Creates an FCP client.
115 * The name of the FCP client
117 * The host address of the Freenet node
119 * The Freenet node’s FCP port
121 public FcpClient(String name, InetAddress host, int port) {
123 fcpConnection = new FcpConnection(host, port);
131 * Connects the FCP client.
133 * @throws IOException
134 * if an I/O error occurs
135 * @throws FcpException
136 * if an FCP error occurs
138 public void connect() throws IOException, FcpException {
139 ExtendedFcpAdapter fcpListener = new ExtendedFcpAdapter() {
145 public void receivedNodeHello(FcpConnection fcpConnection, NodeHello nodeHello) {
146 completionLatch.countDown();
149 fcpConnection.addFcpListener(fcpListener);
151 fcpConnection.connect();
152 ClientHello clientHello = new ClientHello(name);
153 fcpConnection.sendMessage(clientHello);
156 fcpListener.complete();
158 } catch (InterruptedException e) {
159 /* ignore, we’ll loop. */
163 fcpConnection.removeFcpListener(fcpListener);
165 if (fcpListener.getFcpException() != null) {
166 throw fcpListener.getFcpException();
171 * Disconnects the FCP client.
173 public void disconnect() {
174 synchronized (syncObject) {
175 fcpConnection.close();
176 syncObject.notifyAll();
185 * Returns all peers that the node has.
187 * @return A set containing the node’s peers
188 * @throws IOException
189 * if an I/O error occurs
190 * @throws FcpException
191 * if an FCP error occurs
193 public Set<Peer> getPeers() throws IOException, FcpException {
194 final Set<Peer> peers = new HashSet<Peer>();
195 ExtendedFcpAdapter fcpAdapter = new ExtendedFcpAdapter() {
201 public void receivedPeer(FcpConnection fcpConnection, Peer peer) {
209 public void receivedEndListPeers(FcpConnection fcpConnection, EndListPeers endListPeers) {
210 completionLatch.countDown();
213 fcpConnection.addFcpListener(fcpAdapter);
214 fcpConnection.sendMessage(new ListPeers("list-peers"));
218 fcpAdapter.complete();
220 } catch (InterruptedException e) {
221 /* ignore, we’ll loop. */
225 fcpConnection.removeFcpListener(fcpAdapter);
227 if (fcpListener.getFcpException() != null) {
228 throw fcpListener.getFcpException();
234 * Implementation of an {@link FcpListener} that can store an
235 * {@link FcpException} and wait for the arrival of a certain command.
237 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
239 private static class ExtendedFcpAdapter extends FcpAdapter {
241 /** The count down latch used to wait for completion. */
242 protected final CountDownLatch completionLatch = new CountDownLatch(1);
244 /** The FCP exception, if any. */
245 protected FcpException fcpException;
248 * Creates a new extended FCP adapter.
250 public ExtendedFcpAdapter() {
255 * Returns the FCP exception that occured. If no FCP exception occured,
256 * <code>null</code> is returned.
258 * @return The FCP exception that occured, or <code>null</code>
260 public FcpException getFcpException() {
265 * Waits for the completion of the command.
267 * @throws InterruptedException
268 * if {@link CountDownLatch#await()} is interrupted
270 public void complete() throws InterruptedException {
271 completionLatch.await();
278 public void connectionClosed(FcpConnection fcpConnection, Throwable throwable) {
279 fcpException = new FcpException("Connection closed", throwable);
280 completionLatch.countDown();
287 public void receivedCloseConnectionDuplicateClientName(FcpConnection fcpConnection, CloseConnectionDuplicateClientName closeConnectionDuplicateClientName) {
288 fcpException = new FcpException("Connection closed, duplicate client name");
289 completionLatch.countDown();
296 public void receivedProtocolError(FcpConnection fcpConnection, ProtocolError protocolError) {
297 fcpException = new FcpException("Protocol error (" + protocolError.getCode() + ", " + protocolError.getCodeDescription());
298 completionLatch.countDown();