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.concurrent.CountDownLatch;
27 import net.pterodactylus.fcp.ClientHello;
28 import net.pterodactylus.fcp.CloseConnectionDuplicateClientName;
29 import net.pterodactylus.fcp.FcpAdapter;
30 import net.pterodactylus.fcp.FcpConnection;
31 import net.pterodactylus.fcp.FcpListener;
32 import net.pterodactylus.fcp.NodeHello;
33 import net.pterodactylus.fcp.ProtocolError;
36 * High-level FCP client that hides the details of the underlying FCP
39 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
41 public class FcpClient {
43 /** Object used for synchronization. */
44 private final Object syncObject = new Object();
46 /** The name of this client. */
47 private final String name;
49 /** The underlying FCP connection. */
50 private final FcpConnection fcpConnection;
53 * Creates an FCP client with the given name.
56 * The name of the FCP client
57 * @throws UnknownHostException
58 * if the hostname “localhost” is unknown
60 public FcpClient(String name) throws UnknownHostException {
61 this(name, "localhost");
65 * Creates an FCP client.
68 * The name of the FCP client
70 * The hostname of the Freenet node
71 * @throws UnknownHostException
72 * if the given hostname can not be resolved
74 public FcpClient(String name, String hostname) throws UnknownHostException {
75 this(name, hostname, FcpConnection.DEFAULT_PORT);
79 * Creates an FCP client.
82 * The name of the FCP client
84 * The hostname of the Freenet node
86 * The Freenet node’s FCP port
87 * @throws UnknownHostException
88 * if the given hostname can not be resolved
90 public FcpClient(String name, String hostname, int port) throws UnknownHostException {
91 this(name, InetAddress.getByName(hostname), port);
95 * Creates an FCP client.
98 * The name of the FCP client
100 * The host address of the Freenet node
102 public FcpClient(String name, InetAddress host) {
103 this(name, host, FcpConnection.DEFAULT_PORT);
107 * Creates an FCP client.
110 * The name of the FCP client
112 * The host address of the Freenet node
114 * The Freenet node’s FCP port
116 public FcpClient(String name, InetAddress host, int port) {
118 fcpConnection = new FcpConnection(host, port);
126 * Connects the FCP client.
128 * @throws IOException
129 * if an I/O error occurs
130 * @throws FcpException
131 * if an FCP error occurs
133 public void connect() throws IOException, FcpException {
134 ExtendedFcpAdapter fcpListener = new ExtendedFcpAdapter() {
140 public void receivedNodeHello(FcpConnection fcpConnection, NodeHello nodeHello) {
141 completionLatch.countDown();
144 fcpConnection.addFcpListener(fcpListener);
145 fcpConnection.connect();
146 ClientHello clientHello = new ClientHello(name);
147 fcpConnection.sendMessage(clientHello);
150 fcpListener.complete();
152 } catch (InterruptedException e) {
153 /* ignore, we’ll loop. */
156 fcpConnection.removeFcpListener(fcpListener);
157 if (fcpListener.getFcpException() != null) {
158 throw fcpListener.getFcpException();
163 * Disconnects the FCP client.
165 public void disconnect() {
166 synchronized (syncObject) {
167 fcpConnection.close();
168 syncObject.notifyAll();
173 * Implementation of an {@link FcpListener} that can store an
174 * {@link FcpException} and wait for the arrival of a certain command.
176 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
178 private static class ExtendedFcpAdapter extends FcpAdapter {
180 /** The count down latch used to wait for completion. */
181 protected final CountDownLatch completionLatch = new CountDownLatch(1);
183 /** The FCP exception, if any. */
184 protected FcpException fcpException;
187 * Creates a new extended FCP adapter.
189 public ExtendedFcpAdapter() {
194 * Returns the FCP exception that occured. If no FCP exception occured,
195 * <code>null</code> is returned.
197 * @return The FCP exception that occured, or <code>null</code>
199 public FcpException getFcpException() {
204 * Waits for the completion of the command.
206 * @throws InterruptedException
207 * if {@link CountDownLatch#await()} is interrupted
209 public void complete() throws InterruptedException {
210 completionLatch.await();
217 public void connectionClosed(FcpConnection fcpConnection, Throwable throwable) {
218 fcpException = new FcpException("Connection closed", throwable);
219 completionLatch.countDown();
226 public void receivedCloseConnectionDuplicateClientName(FcpConnection fcpConnection, CloseConnectionDuplicateClientName closeConnectionDuplicateClientName) {
227 fcpException = new FcpException("Connection closed, duplicate client name");
228 completionLatch.countDown();
235 public void receivedProtocolError(FcpConnection fcpConnection, ProtocolError protocolError) {
236 fcpException = new FcpException("Protocol error (" + protocolError.getCode() + ", " + protocolError.getCodeDescription());
237 completionLatch.countDown();