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 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();
172 * Disconnects the FCP client.
174 public void disconnect() {
175 synchronized (syncObject) {
176 fcpConnection.close();
177 syncObject.notifyAll();
186 * Returns all peers that the node has.
188 * @param withMetadata
189 * <code>true</code> to include peer metadata
190 * @param withVolatile
191 * <code>true</code> to include volatile peer data
192 * @return A set containing the node’s peers
193 * @throws IOException
194 * if an I/O error occurs
195 * @throws FcpException
196 * if an FCP error occurs
198 public Set<Peer> getPeers(final boolean withMetadata, final boolean withVolatile) throws IOException, FcpException {
199 final Set<Peer> peers = new HashSet<Peer>();
200 new ExtendedFcpAdapter() {
206 @SuppressWarnings("synthetic-access")
207 public void run() throws IOException {
208 fcpConnection.sendMessage(new ListPeers("list-peers", withMetadata, withVolatile));
215 public void receivedPeer(FcpConnection fcpConnection, Peer peer) {
223 public void receivedEndListPeers(FcpConnection fcpConnection, EndListPeers endListPeers) {
224 completionLatch.countDown();
231 * Adds the given peer to the node.
235 * @throws IOException
236 * if an I/O error occurs
237 * @throws FcpException
238 * if an FCP error occurs
240 public void addPeer(Peer peer) throws IOException, FcpException {
241 addPeer(peer.getNodeRef());
245 * Adds the peer defined by the noderef to the node.
248 * The noderef that defines the new peer
249 * @throws IOException
250 * if an I/O error occurs
251 * @throws FcpException
252 * if an FCP error occurs
254 public void addPeer(NodeRef nodeRef) throws IOException, FcpException {
255 addPeer(new AddPeer(nodeRef));
259 * Adds a peer, reading the noderef from the given URL.
262 * The URL to read the noderef from
263 * @throws IOException
264 * if an I/O error occurs
265 * @throws FcpException
266 * if an FCP error occurs
268 public void addPeer(URL url) throws IOException, FcpException {
269 addPeer(new AddPeer(url));
273 * Adds a peer, reading the noderef of the peer from the given file.
274 * <strong>Note:</strong> the file to read the noderef from has to reside on
275 * the same machine as the node!
278 * The name of the file containing the peer’s noderef
279 * @throws IOException
280 * if an I/O error occurs
281 * @throws FcpException
282 * if an FCP error occurs
284 public void addPeer(String file) throws IOException, FcpException {
285 addPeer(new AddPeer(file));
289 * Sends the given {@link AddPeer} message to the node. This method should
290 * not be called directly. Use one of {@link #addPeer(Peer)},
291 * {@link #addPeer(NodeRef)}, {@link #addPeer(URL)}, or
292 * {@link #addPeer(String)} instead.
295 * The “AddPeer” message
296 * @throws IOException
297 * if an I/O error occurs
298 * @throws FcpException
299 * if an FCP error occurs
301 private void addPeer(final AddPeer addPeer) throws IOException, FcpException {
302 new ExtendedFcpAdapter() {
308 @SuppressWarnings("synthetic-access")
309 public void run() throws IOException {
310 fcpConnection.sendMessage(addPeer);
317 public void receivedPeer(FcpConnection fcpConnection, Peer peer) {
318 completionLatch.countDown();
324 * Modifies the given peer.
328 * @param allowLocalAddresses
329 * <code>true</code> to allow local address, <code>false</code>
330 * to not allow local address, <code>null</code> to not change
333 * <code>true</code> to disable the peer, <code>false</code> to
334 * enable the peer, <code>null</code> to not change the setting
336 * <code>true</code> to enable “listen only” for the peer,
337 * <code>false</code> to disable it, <code>null</code> to not
339 * @throws IOException
340 * if an I/O error occurs
341 * @throws FcpException
342 * if an FCP error occurs
344 public void modifyPeer(final Peer peer, final Boolean allowLocalAddresses, final Boolean disabled, final Boolean listenOnly) throws IOException, FcpException {
345 new ExtendedFcpAdapter() {
351 @SuppressWarnings("synthetic-access")
352 public void run() throws IOException {
353 fcpConnection.sendMessage(new ModifyPeer(peer.getIdentity(), allowLocalAddresses, disabled, listenOnly));
360 public void receivedPeer(FcpConnection fcpConnection, Peer peer) {
361 completionLatch.countDown();
367 * Removes the given peer.
371 * @throws IOException
372 * if an I/O error occurs
373 * @throws FcpException
374 * if an FCP error occurs
376 public void removePeer(final Peer peer) throws IOException, FcpException {
377 new ExtendedFcpAdapter() {
383 @SuppressWarnings("synthetic-access")
384 public void run() throws IOException {
385 fcpConnection.sendMessage(new RemovePeer(peer.getIdentity()));
392 public void receivedPeerRemoved(FcpConnection fcpConnection, PeerRemoved peerRemoved) {
393 completionLatch.countDown();
399 // PEER NOTES MANAGEMENT
403 * Returns the peer note of the given peer.
406 * The peer to get the note for
407 * @return The peer’s note
408 * @throws IOException
409 * if an I/O error occurs
410 * @throws FcpException
411 * if an FCP error occurs
413 public PeerNote getPeerNote(final Peer peer) throws IOException, FcpException {
414 final ObjectWrapper<PeerNote> objectWrapper = new ObjectWrapper<PeerNote>();
415 new ExtendedFcpAdapter() {
421 @SuppressWarnings("synthetic-access")
422 public void run() throws IOException {
423 fcpConnection.sendMessage(new ListPeerNotes(peer.getIdentity()));
430 public void receivedPeerNote(FcpConnection fcpConnection, PeerNote peerNote) {
431 objectWrapper.set(peerNote);
434 return objectWrapper.get();
438 * Implementation of an {@link FcpListener} that can store an
439 * {@link FcpException} and wait for the arrival of a certain command.
441 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
443 private abstract class ExtendedFcpAdapter extends FcpAdapter {
445 /** The count down latch used to wait for completion. */
446 protected final CountDownLatch completionLatch = new CountDownLatch(1);
448 /** The FCP exception, if any. */
449 protected FcpException fcpException;
452 * Creates a new extended FCP adapter.
454 public ExtendedFcpAdapter() {
459 * Executes the FCP commands in {@link #run()}, wrapping the execution
460 * and catching exceptions.
462 * @throws IOException
463 * if an I/O error occurs
464 * @throws FcpException
465 * if an FCP error occurs
467 @SuppressWarnings("synthetic-access")
468 public void execute() throws IOException, FcpException {
469 fcpConnection.addFcpListener(this);
474 completionLatch.await();
476 } catch (InterruptedException ie1) {
477 /* ignore, we’ll loop. */
481 fcpConnection.removeFcpListener(this);
483 if (fcpException != null) {
489 * The FCP commands that actually get executed.
491 * @throws IOException
492 * if an I/O error occurs
494 public abstract void run() throws IOException;
500 public void connectionClosed(FcpConnection fcpConnection, Throwable throwable) {
501 fcpException = new FcpException("Connection closed", throwable);
502 completionLatch.countDown();
509 public void receivedCloseConnectionDuplicateClientName(FcpConnection fcpConnection, CloseConnectionDuplicateClientName closeConnectionDuplicateClientName) {
510 fcpException = new FcpException("Connection closed, duplicate client name");
511 completionLatch.countDown();
518 public void receivedProtocolError(FcpConnection fcpConnection, ProtocolError protocolError) {
519 fcpException = new FcpException("Protocol error (" + protocolError.getCode() + ", " + protocolError.getCodeDescription());
520 completionLatch.countDown();