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.EndListPeerNotes;
34 import net.pterodactylus.fcp.EndListPeers;
35 import net.pterodactylus.fcp.FcpAdapter;
36 import net.pterodactylus.fcp.FcpConnection;
37 import net.pterodactylus.fcp.FcpListener;
38 import net.pterodactylus.fcp.ListPeerNotes;
39 import net.pterodactylus.fcp.ListPeers;
40 import net.pterodactylus.fcp.ModifyPeer;
41 import net.pterodactylus.fcp.NodeHello;
42 import net.pterodactylus.fcp.NodeRef;
43 import net.pterodactylus.fcp.Peer;
44 import net.pterodactylus.fcp.PeerNote;
45 import net.pterodactylus.fcp.PeerRemoved;
46 import net.pterodactylus.fcp.ProtocolError;
47 import net.pterodactylus.fcp.RemovePeer;
48 import net.pterodactylus.util.thread.ObjectWrapper;
51 * High-level FCP client that hides the details of the underlying FCP
54 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
56 public class FcpClient {
58 /** Object used for synchronization. */
59 private final Object syncObject = new Object();
61 /** The name of this client. */
62 private final String name;
64 /** The underlying FCP connection. */
65 private final FcpConnection fcpConnection;
68 * Creates an FCP client with the given name.
71 * The name of the FCP client
72 * @throws UnknownHostException
73 * if the hostname “localhost” is unknown
75 public FcpClient(String name) throws UnknownHostException {
76 this(name, "localhost");
80 * Creates an FCP client.
83 * The name of the FCP client
85 * The hostname of the Freenet node
86 * @throws UnknownHostException
87 * if the given hostname can not be resolved
89 public FcpClient(String name, String hostname) throws UnknownHostException {
90 this(name, hostname, FcpConnection.DEFAULT_PORT);
94 * Creates an FCP client.
97 * The name of the FCP client
99 * The hostname of the Freenet node
101 * The Freenet node’s FCP port
102 * @throws UnknownHostException
103 * if the given hostname can not be resolved
105 public FcpClient(String name, String hostname, int port) throws UnknownHostException {
106 this(name, InetAddress.getByName(hostname), port);
110 * Creates an FCP client.
113 * The name of the FCP client
115 * The host address of the Freenet node
117 public FcpClient(String name, InetAddress host) {
118 this(name, host, FcpConnection.DEFAULT_PORT);
122 * Creates an FCP client.
125 * The name of the FCP client
127 * The host address of the Freenet node
129 * The Freenet node’s FCP port
131 public FcpClient(String name, InetAddress host, int port) {
133 fcpConnection = new FcpConnection(host, port);
141 * Connects the FCP client.
143 * @throws IOException
144 * if an I/O error occurs
145 * @throws FcpException
146 * if an FCP error occurs
148 public void connect() throws IOException, FcpException {
149 new ExtendedFcpAdapter() {
155 @SuppressWarnings("synthetic-access")
156 public void run() throws IOException {
157 fcpConnection.connect();
158 ClientHello clientHello = new ClientHello(name);
159 fcpConnection.sendMessage(clientHello);
166 public void receivedNodeHello(FcpConnection fcpConnection, NodeHello nodeHello) {
167 completionLatch.countDown();
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 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();
232 * Adds the given peer to the node.
236 * @throws IOException
237 * if an I/O error occurs
238 * @throws FcpException
239 * if an FCP error occurs
241 public void addPeer(Peer peer) throws IOException, FcpException {
242 addPeer(peer.getNodeRef());
246 * Adds the peer defined by the noderef to the node.
249 * The noderef that defines the new peer
250 * @throws IOException
251 * if an I/O error occurs
252 * @throws FcpException
253 * if an FCP error occurs
255 public void addPeer(NodeRef nodeRef) throws IOException, FcpException {
256 addPeer(new AddPeer(nodeRef));
260 * Adds a peer, reading the noderef from the given URL.
263 * The URL to read the noderef from
264 * @throws IOException
265 * if an I/O error occurs
266 * @throws FcpException
267 * if an FCP error occurs
269 public void addPeer(URL url) throws IOException, FcpException {
270 addPeer(new AddPeer(url));
274 * Adds a peer, reading the noderef of the peer from the given file.
275 * <strong>Note:</strong> the file to read the noderef from has to reside on
276 * the same machine as the node!
279 * The name of the file containing the peer’s noderef
280 * @throws IOException
281 * if an I/O error occurs
282 * @throws FcpException
283 * if an FCP error occurs
285 public void addPeer(String file) throws IOException, FcpException {
286 addPeer(new AddPeer(file));
290 * Sends the given {@link AddPeer} message to the node. This method should
291 * not be called directly. Use one of {@link #addPeer(Peer)},
292 * {@link #addPeer(NodeRef)}, {@link #addPeer(URL)}, or
293 * {@link #addPeer(String)} instead.
296 * The “AddPeer” message
297 * @throws IOException
298 * if an I/O error occurs
299 * @throws FcpException
300 * if an FCP error occurs
302 private void addPeer(final AddPeer addPeer) throws IOException, FcpException {
303 new ExtendedFcpAdapter() {
309 @SuppressWarnings("synthetic-access")
310 public void run() throws IOException {
311 fcpConnection.sendMessage(addPeer);
318 public void receivedPeer(FcpConnection fcpConnection, Peer peer) {
319 completionLatch.countDown();
325 * Modifies the given peer.
329 * @param allowLocalAddresses
330 * <code>true</code> to allow local address, <code>false</code>
331 * to not allow local address, <code>null</code> to not change
334 * <code>true</code> to disable the peer, <code>false</code> to
335 * enable the peer, <code>null</code> to not change the setting
337 * <code>true</code> to enable “listen only” for the peer,
338 * <code>false</code> to disable it, <code>null</code> to not
340 * @throws IOException
341 * if an I/O error occurs
342 * @throws FcpException
343 * if an FCP error occurs
345 public void modifyPeer(final Peer peer, final Boolean allowLocalAddresses, final Boolean disabled, final Boolean listenOnly) throws IOException, FcpException {
346 new ExtendedFcpAdapter() {
352 @SuppressWarnings("synthetic-access")
353 public void run() throws IOException {
354 fcpConnection.sendMessage(new ModifyPeer(peer.getIdentity(), allowLocalAddresses, disabled, listenOnly));
361 public void receivedPeer(FcpConnection fcpConnection, Peer peer) {
362 completionLatch.countDown();
368 * Removes the given peer.
372 * @throws IOException
373 * if an I/O error occurs
374 * @throws FcpException
375 * if an FCP error occurs
377 public void removePeer(final Peer peer) throws IOException, FcpException {
378 new ExtendedFcpAdapter() {
384 @SuppressWarnings("synthetic-access")
385 public void run() throws IOException {
386 fcpConnection.sendMessage(new RemovePeer(peer.getIdentity()));
393 public void receivedPeerRemoved(FcpConnection fcpConnection, PeerRemoved peerRemoved) {
394 completionLatch.countDown();
400 // PEER NOTES MANAGEMENT
404 * Returns the peer note of the given peer.
407 * The peer to get the note for
408 * @return The peer’s note
409 * @throws IOException
410 * if an I/O error occurs
411 * @throws FcpException
412 * if an FCP error occurs
414 public PeerNote getPeerNote(final Peer peer) throws IOException, FcpException {
415 final ObjectWrapper<PeerNote> objectWrapper = new ObjectWrapper<PeerNote>();
416 new ExtendedFcpAdapter() {
422 @SuppressWarnings("synthetic-access")
423 public void run() throws IOException {
424 fcpConnection.sendMessage(new ListPeerNotes(peer.getIdentity()));
431 public void receivedPeerNote(FcpConnection fcpConnection, PeerNote peerNote) {
432 if (peerNote.getNodeIdentifier().equals(peer.getIdentity())) {
433 objectWrapper.set(peerNote);
441 public void receivedEndListPeerNotes(FcpConnection fcpConnection, EndListPeerNotes endListPeerNotes) {
442 completionLatch.countDown();
445 return objectWrapper.get();
449 * Implementation of an {@link FcpListener} that can store an
450 * {@link FcpException} and wait for the arrival of a certain command.
452 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
454 private abstract class ExtendedFcpAdapter extends FcpAdapter {
456 /** The count down latch used to wait for completion. */
457 protected final CountDownLatch completionLatch = new CountDownLatch(1);
459 /** The FCP exception, if any. */
460 protected FcpException fcpException;
463 * Creates a new extended FCP adapter.
465 public ExtendedFcpAdapter() {
470 * Executes the FCP commands in {@link #run()}, wrapping the execution
471 * and catching exceptions.
473 * @throws IOException
474 * if an I/O error occurs
475 * @throws FcpException
476 * if an FCP error occurs
478 @SuppressWarnings("synthetic-access")
479 public void execute() throws IOException, FcpException {
480 fcpConnection.addFcpListener(this);
485 completionLatch.await();
487 } catch (InterruptedException ie1) {
488 /* ignore, we’ll loop. */
492 fcpConnection.removeFcpListener(this);
494 if (fcpException != null) {
500 * The FCP commands that actually get executed.
502 * @throws IOException
503 * if an I/O error occurs
505 public abstract void run() throws IOException;
511 public void connectionClosed(FcpConnection fcpConnection, Throwable throwable) {
512 fcpException = new FcpException("Connection closed", throwable);
513 completionLatch.countDown();
520 public void receivedCloseConnectionDuplicateClientName(FcpConnection fcpConnection, CloseConnectionDuplicateClientName closeConnectionDuplicateClientName) {
521 fcpException = new FcpException("Connection closed, duplicate client name");
522 completionLatch.countDown();
529 public void receivedProtocolError(FcpConnection fcpConnection, ProtocolError protocolError) {
530 fcpException = new FcpException("Protocol error (" + protocolError.getCode() + ", " + protocolError.getCodeDescription());
531 completionLatch.countDown();