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;
25 import java.net.UnknownHostException;
26 import java.util.HashSet;
28 import java.util.concurrent.CountDownLatch;
30 import net.pterodactylus.fcp.AddPeer;
31 import net.pterodactylus.fcp.ClientHello;
32 import net.pterodactylus.fcp.CloseConnectionDuplicateClientName;
33 import net.pterodactylus.fcp.EndListPeers;
34 import net.pterodactylus.fcp.FcpAdapter;
35 import net.pterodactylus.fcp.FcpConnection;
36 import net.pterodactylus.fcp.FcpListener;
37 import net.pterodactylus.fcp.ListPeerNotes;
38 import net.pterodactylus.fcp.ListPeers;
39 import net.pterodactylus.fcp.ModifyPeer;
40 import net.pterodactylus.fcp.NodeHello;
41 import net.pterodactylus.fcp.NodeRef;
42 import net.pterodactylus.fcp.Peer;
43 import net.pterodactylus.fcp.PeerNote;
44 import net.pterodactylus.fcp.PeerRemoved;
45 import net.pterodactylus.fcp.ProtocolError;
46 import net.pterodactylus.fcp.RemovePeer;
47 import net.pterodactylus.util.thread.ObjectWrapper;
50 * High-level FCP client that hides the details of the underlying FCP
53 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
55 public class FcpClient {
57 /** Object used for synchronization. */
58 private final Object syncObject = new Object();
60 /** The name of this client. */
61 private final String name;
63 /** The underlying FCP connection. */
64 private final FcpConnection fcpConnection;
67 * Creates an FCP client with the given name.
70 * The name of the FCP client
71 * @throws UnknownHostException
72 * if the hostname “localhost” is unknown
74 public FcpClient(String name) throws UnknownHostException {
75 this(name, "localhost");
79 * Creates an FCP client.
82 * The name of the FCP client
84 * The hostname of the Freenet node
85 * @throws UnknownHostException
86 * if the given hostname can not be resolved
88 public FcpClient(String name, String hostname) throws UnknownHostException {
89 this(name, hostname, FcpConnection.DEFAULT_PORT);
93 * Creates an FCP client.
96 * The name of the FCP client
98 * The hostname of the Freenet node
100 * The Freenet node’s FCP port
101 * @throws UnknownHostException
102 * if the given hostname can not be resolved
104 public FcpClient(String name, String hostname, int port) throws UnknownHostException {
105 this(name, InetAddress.getByName(hostname), port);
109 * Creates an FCP client.
112 * The name of the FCP client
114 * The host address of the Freenet node
116 public FcpClient(String name, InetAddress host) {
117 this(name, host, FcpConnection.DEFAULT_PORT);
121 * Creates an FCP client.
124 * The name of the FCP client
126 * The host address of the Freenet node
128 * The Freenet node’s FCP port
130 public FcpClient(String name, InetAddress host, int port) {
132 fcpConnection = new FcpConnection(host, port);
140 * Connects the FCP client.
142 * @throws IOException
143 * if an I/O error occurs
144 * @throws FcpException
145 * if an FCP error occurs
147 public void connect() throws IOException, FcpException {
148 ExtendedFcpAdapter fcpListener = new ExtendedFcpAdapter() {
154 @SuppressWarnings("synthetic-access")
155 public void run() throws IOException {
156 fcpConnection.connect();
157 ClientHello clientHello = new ClientHello(name);
158 fcpConnection.sendMessage(clientHello);
165 public void receivedNodeHello(FcpConnection fcpConnection, NodeHello nodeHello) {
166 completionLatch.countDown();
169 fcpListener.execute();
173 * Disconnects the FCP client.
175 public void disconnect() {
176 synchronized (syncObject) {
177 fcpConnection.close();
178 syncObject.notifyAll();
187 * Returns all peers that the node has.
189 * @param withMetadata
190 * <code>true</code> to include peer metadata
191 * @param withVolatile
192 * <code>true</code> to include volatile peer data
193 * @return A set containing the node’s peers
194 * @throws IOException
195 * if an I/O error occurs
196 * @throws FcpException
197 * if an FCP error occurs
199 public Set<Peer> getPeers(final boolean withMetadata, final boolean withVolatile) throws IOException, FcpException {
200 final Set<Peer> peers = new HashSet<Peer>();
201 ExtendedFcpAdapter fcpListener = new ExtendedFcpAdapter() {
207 @SuppressWarnings("synthetic-access")
208 public void run() throws IOException {
209 fcpConnection.sendMessage(new ListPeers("list-peers", withMetadata, withVolatile));
216 public void receivedPeer(FcpConnection fcpConnection, Peer peer) {
224 public void receivedEndListPeers(FcpConnection fcpConnection, EndListPeers endListPeers) {
225 completionLatch.countDown();
228 fcpListener.execute();
233 * Adds the given peer to the node.
237 * @throws IOException
238 * if an I/O error occurs
239 * @throws FcpException
240 * if an FCP error occurs
242 public void addPeer(Peer peer) throws IOException, FcpException {
243 addPeer(peer.getNodeRef());
247 * Adds the peer defined by the noderef to the node.
250 * The noderef that defines the new peer
251 * @throws IOException
252 * if an I/O error occurs
253 * @throws FcpException
254 * if an FCP error occurs
256 public void addPeer(NodeRef nodeRef) throws IOException, FcpException {
257 addPeer(new AddPeer(nodeRef));
261 * Adds a peer, reading the noderef from the given URL.
264 * The URL to read the noderef from
265 * @throws IOException
266 * if an I/O error occurs
267 * @throws FcpException
268 * if an FCP error occurs
270 public void addPeer(URL url) throws IOException, FcpException {
271 addPeer(new AddPeer(url));
275 * Adds a peer, reading the noderef of the peer from the given file.
276 * <strong>Note:</strong> the file to read the noderef from has to reside on
277 * the same machine as the node!
280 * The name of the file containing the peer’s noderef
281 * @throws IOException
282 * if an I/O error occurs
283 * @throws FcpException
284 * if an FCP error occurs
286 public void addPeer(String file) throws IOException, FcpException {
287 addPeer(new AddPeer(file));
291 * Sends the given {@link AddPeer} message to the node. This method should
292 * not be called directly. Use one of {@link #addPeer(Peer)},
293 * {@link #addPeer(NodeRef)}, {@link #addPeer(URL)}, or
294 * {@link #addPeer(String)} instead.
297 * The “AddPeer” message
298 * @throws IOException
299 * if an I/O error occurs
300 * @throws FcpException
301 * if an FCP error occurs
303 private void addPeer(final AddPeer addPeer) throws IOException, FcpException {
304 ExtendedFcpAdapter fcpListener = new ExtendedFcpAdapter() {
310 @SuppressWarnings("synthetic-access")
311 public void run() throws IOException {
312 fcpConnection.sendMessage(addPeer);
319 public void receivedPeer(FcpConnection fcpConnection, Peer peer) {
320 completionLatch.countDown();
323 fcpListener.execute();
327 * Modifies the given peer.
331 * @param allowLocalAddresses
332 * <code>true</code> to allow local address, <code>false</code>
333 * to not allow local address, <code>null</code> to not change
336 * <code>true</code> to disable the peer, <code>false</code> to
337 * enable the peer, <code>null</code> to not change the setting
339 * <code>true</code> to enable “listen only” for the peer,
340 * <code>false</code> to disable it, <code>null</code> to not
342 * @throws IOException
343 * if an I/O error occurs
344 * @throws FcpException
345 * if an FCP error occurs
347 public void modifyPeer(final Peer peer, final Boolean allowLocalAddresses, final Boolean disabled, final Boolean listenOnly) throws IOException, FcpException {
348 ExtendedFcpAdapter fcpListener = new ExtendedFcpAdapter() {
354 @SuppressWarnings("synthetic-access")
355 public void run() throws IOException {
356 fcpConnection.sendMessage(new ModifyPeer(peer.getIdentity(), allowLocalAddresses, disabled, listenOnly));
363 public void receivedPeer(FcpConnection fcpConnection, Peer peer) {
364 completionLatch.countDown();
367 fcpListener.execute();
371 * Removes the given peer.
375 * @throws IOException
376 * if an I/O error occurs
377 * @throws FcpException
378 * if an FCP error occurs
380 public void removePeer(final Peer peer) throws IOException, FcpException {
381 ExtendedFcpAdapter fcpListener = new ExtendedFcpAdapter() {
387 @SuppressWarnings("synthetic-access")
388 public void run() throws IOException {
389 fcpConnection.sendMessage(new RemovePeer(peer.getIdentity()));
396 public void receivedPeerRemoved(FcpConnection fcpConnection, PeerRemoved peerRemoved) {
397 completionLatch.countDown();
400 fcpListener.execute();
404 // PEER NOTES MANAGEMENT
408 * Returns the peer note of the given peer.
411 * The peer to get the note for
412 * @return The peer’s note
413 * @throws IOException
414 * if an I/O error occurs
415 * @throws FcpException
416 * if an FCP error occurs
418 public PeerNote getPeerNote(final Peer peer) throws IOException, FcpException {
419 final ObjectWrapper<PeerNote> objectWrapper = new ObjectWrapper<PeerNote>();
420 new ExtendedFcpAdapter() {
426 @SuppressWarnings("synthetic-access")
427 public void run() throws IOException {
428 fcpConnection.sendMessage(new ListPeerNotes(peer.getIdentity()));
435 public void receivedPeerNote(FcpConnection fcpConnection, PeerNote peerNote) {
436 objectWrapper.set(peerNote);
439 return objectWrapper.get();
443 * Implementation of an {@link FcpListener} that can store an
444 * {@link FcpException} and wait for the arrival of a certain command.
446 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
448 private abstract class ExtendedFcpAdapter extends FcpAdapter {
450 /** The count down latch used to wait for completion. */
451 protected final CountDownLatch completionLatch = new CountDownLatch(1);
453 /** The FCP exception, if any. */
454 protected FcpException fcpException;
457 * Creates a new extended FCP adapter.
459 public ExtendedFcpAdapter() {
464 * Executes the FCP commands in {@link #run()}, wrapping the execution
465 * and catching exceptions.
467 * @throws IOException
468 * if an I/O error occurs
469 * @throws FcpException
470 * if an FCP error occurs
472 @SuppressWarnings("synthetic-access")
473 public void execute() throws IOException, FcpException {
474 fcpConnection.addFcpListener(this);
479 completionLatch.await();
481 } catch (InterruptedException ie1) {
482 /* ignore, we’ll loop. */
486 fcpConnection.removeFcpListener(this);
488 if (fcpException != null) {
494 * The FCP commands that actually get executed.
496 * @throws IOException
497 * if an I/O error occurs
499 public abstract void run() throws IOException;
505 public void connectionClosed(FcpConnection fcpConnection, Throwable throwable) {
506 fcpException = new FcpException("Connection closed", throwable);
507 completionLatch.countDown();
514 public void receivedCloseConnectionDuplicateClientName(FcpConnection fcpConnection, CloseConnectionDuplicateClientName closeConnectionDuplicateClientName) {
515 fcpException = new FcpException("Connection closed, duplicate client name");
516 completionLatch.countDown();
523 public void receivedProtocolError(FcpConnection fcpConnection, ProtocolError protocolError) {
524 fcpException = new FcpException("Protocol error (" + protocolError.getCode() + ", " + protocolError.getCodeDescription());
525 completionLatch.countDown();