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.Collections;
27 import java.util.HashSet;
29 import java.util.concurrent.CountDownLatch;
31 import net.pterodactylus.fcp.AddPeer;
32 import net.pterodactylus.fcp.ClientHello;
33 import net.pterodactylus.fcp.CloseConnectionDuplicateClientName;
34 import net.pterodactylus.fcp.EndListPeerNotes;
35 import net.pterodactylus.fcp.EndListPeers;
36 import net.pterodactylus.fcp.FcpAdapter;
37 import net.pterodactylus.fcp.FcpConnection;
38 import net.pterodactylus.fcp.FcpListener;
39 import net.pterodactylus.fcp.ListPeerNotes;
40 import net.pterodactylus.fcp.ListPeers;
41 import net.pterodactylus.fcp.ModifyPeer;
42 import net.pterodactylus.fcp.ModifyPeerNote;
43 import net.pterodactylus.fcp.NodeHello;
44 import net.pterodactylus.fcp.NodeRef;
45 import net.pterodactylus.fcp.Peer;
46 import net.pterodactylus.fcp.PeerNote;
47 import net.pterodactylus.fcp.PeerRemoved;
48 import net.pterodactylus.fcp.ProtocolError;
49 import net.pterodactylus.fcp.RemovePeer;
50 import net.pterodactylus.util.thread.ObjectWrapper;
53 * High-level FCP client that hides the details of the underlying FCP
56 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
58 public class FcpClient {
60 /** Object used for synchronization. */
61 private final Object syncObject = new Object();
63 /** The name of this client. */
64 private final String name;
66 /** The underlying FCP connection. */
67 private final FcpConnection fcpConnection;
70 * Creates an FCP client with the given name.
73 * The name of the FCP client
74 * @throws UnknownHostException
75 * if the hostname “localhost” is unknown
77 public FcpClient(String name) throws UnknownHostException {
78 this(name, "localhost");
82 * Creates an FCP client.
85 * The name of the FCP client
87 * The hostname of the Freenet node
88 * @throws UnknownHostException
89 * if the given hostname can not be resolved
91 public FcpClient(String name, String hostname) throws UnknownHostException {
92 this(name, hostname, FcpConnection.DEFAULT_PORT);
96 * Creates an FCP client.
99 * The name of the FCP client
101 * The hostname of the Freenet node
103 * The Freenet node’s FCP port
104 * @throws UnknownHostException
105 * if the given hostname can not be resolved
107 public FcpClient(String name, String hostname, int port) throws UnknownHostException {
108 this(name, InetAddress.getByName(hostname), port);
112 * Creates an FCP client.
115 * The name of the FCP client
117 * The host address of the Freenet node
119 public FcpClient(String name, InetAddress host) {
120 this(name, host, FcpConnection.DEFAULT_PORT);
124 * Creates an FCP client.
127 * The name of the FCP client
129 * The host address of the Freenet node
131 * The Freenet node’s FCP port
133 public FcpClient(String name, InetAddress host, int port) {
135 fcpConnection = new FcpConnection(host, port);
143 * Connects the FCP client.
145 * @throws IOException
146 * if an I/O error occurs
147 * @throws FcpException
148 * if an FCP error occurs
150 public void connect() throws IOException, FcpException {
151 new ExtendedFcpAdapter() {
157 @SuppressWarnings("synthetic-access")
158 public void run() throws IOException {
159 fcpConnection.connect();
160 ClientHello clientHello = new ClientHello(name);
161 fcpConnection.sendMessage(clientHello);
168 public void receivedNodeHello(FcpConnection fcpConnection, NodeHello nodeHello) {
169 completionLatch.countDown();
175 * Disconnects the FCP client.
177 public void disconnect() {
178 synchronized (syncObject) {
179 fcpConnection.close();
180 syncObject.notifyAll();
189 * Returns all peers that the node has.
191 * @param withMetadata
192 * <code>true</code> to include peer metadata
193 * @param withVolatile
194 * <code>true</code> to include volatile peer data
195 * @return A set containing the node’s peers
196 * @throws IOException
197 * if an I/O error occurs
198 * @throws FcpException
199 * if an FCP error occurs
201 public Set<Peer> getPeers(final boolean withMetadata, final boolean withVolatile) throws IOException, FcpException {
202 final Set<Peer> peers = Collections.synchronizedSet(new HashSet<Peer>());
203 new ExtendedFcpAdapter() {
205 /** The ID of the “ListPeers” request. */
206 private String identifier = "list-peers-" + System.currentTimeMillis();
212 @SuppressWarnings("synthetic-access")
213 public void run() throws IOException {
214 fcpConnection.sendMessage(new ListPeers(identifier, withMetadata, withVolatile));
221 public void receivedPeer(FcpConnection fcpConnection, Peer peer) {
222 if (peer.getIdentifier().equals(identifier)) {
231 public void receivedEndListPeers(FcpConnection fcpConnection, EndListPeers endListPeers) {
232 if (endListPeers.getIdentifier().equals(identifier)) {
233 completionLatch.countDown();
241 * Adds the given peer to the node.
245 * @throws IOException
246 * if an I/O error occurs
247 * @throws FcpException
248 * if an FCP error occurs
250 public void addPeer(Peer peer) throws IOException, FcpException {
251 addPeer(peer.getNodeRef());
255 * Adds the peer defined by the noderef to the node.
258 * The noderef that defines the new peer
259 * @throws IOException
260 * if an I/O error occurs
261 * @throws FcpException
262 * if an FCP error occurs
264 public void addPeer(NodeRef nodeRef) throws IOException, FcpException {
265 addPeer(new AddPeer(nodeRef));
269 * Adds a peer, reading the noderef from the given URL.
272 * The URL to read the noderef from
273 * @throws IOException
274 * if an I/O error occurs
275 * @throws FcpException
276 * if an FCP error occurs
278 public void addPeer(URL url) throws IOException, FcpException {
279 addPeer(new AddPeer(url));
283 * Adds a peer, reading the noderef of the peer from the given file.
284 * <strong>Note:</strong> the file to read the noderef from has to reside on
285 * the same machine as the node!
288 * The name of the file containing the peer’s noderef
289 * @throws IOException
290 * if an I/O error occurs
291 * @throws FcpException
292 * if an FCP error occurs
294 public void addPeer(String file) throws IOException, FcpException {
295 addPeer(new AddPeer(file));
299 * Sends the given {@link AddPeer} message to the node. This method should
300 * not be called directly. Use one of {@link #addPeer(Peer)},
301 * {@link #addPeer(NodeRef)}, {@link #addPeer(URL)}, or
302 * {@link #addPeer(String)} instead.
305 * The “AddPeer” message
306 * @throws IOException
307 * if an I/O error occurs
308 * @throws FcpException
309 * if an FCP error occurs
311 private void addPeer(final AddPeer addPeer) throws IOException, FcpException {
312 new ExtendedFcpAdapter() {
318 @SuppressWarnings("synthetic-access")
319 public void run() throws IOException {
320 fcpConnection.sendMessage(addPeer);
327 public void receivedPeer(FcpConnection fcpConnection, Peer peer) {
328 completionLatch.countDown();
334 * Modifies the given peer.
338 * @param allowLocalAddresses
339 * <code>true</code> to allow local address, <code>false</code>
340 * to not allow local address, <code>null</code> to not change
343 * <code>true</code> to disable the peer, <code>false</code> to
344 * enable the peer, <code>null</code> to not change the setting
346 * <code>true</code> to enable “listen only” for the peer,
347 * <code>false</code> to disable it, <code>null</code> to not
349 * @throws IOException
350 * if an I/O error occurs
351 * @throws FcpException
352 * if an FCP error occurs
354 public void modifyPeer(final Peer peer, final Boolean allowLocalAddresses, final Boolean disabled, final Boolean listenOnly) throws IOException, FcpException {
355 new ExtendedFcpAdapter() {
361 @SuppressWarnings("synthetic-access")
362 public void run() throws IOException {
363 fcpConnection.sendMessage(new ModifyPeer(peer.getIdentity(), allowLocalAddresses, disabled, listenOnly));
370 public void receivedPeer(FcpConnection fcpConnection, Peer peer) {
371 completionLatch.countDown();
377 * Removes the given peer.
381 * @throws IOException
382 * if an I/O error occurs
383 * @throws FcpException
384 * if an FCP error occurs
386 public void removePeer(final Peer peer) throws IOException, FcpException {
387 new ExtendedFcpAdapter() {
393 @SuppressWarnings("synthetic-access")
394 public void run() throws IOException {
395 fcpConnection.sendMessage(new RemovePeer(peer.getIdentity()));
402 public void receivedPeerRemoved(FcpConnection fcpConnection, PeerRemoved peerRemoved) {
403 completionLatch.countDown();
409 // PEER NOTES MANAGEMENT
413 * Returns the peer note of the given peer.
416 * The peer to get the note for
417 * @return The peer’s note
418 * @throws IOException
419 * if an I/O error occurs
420 * @throws FcpException
421 * if an FCP error occurs
423 public PeerNote getPeerNote(final Peer peer) throws IOException, FcpException {
424 final ObjectWrapper<PeerNote> objectWrapper = new ObjectWrapper<PeerNote>();
425 new ExtendedFcpAdapter() {
431 @SuppressWarnings("synthetic-access")
432 public void run() throws IOException {
433 fcpConnection.sendMessage(new ListPeerNotes(peer.getIdentity()));
440 public void receivedPeerNote(FcpConnection fcpConnection, PeerNote peerNote) {
441 if (peerNote.getNodeIdentifier().equals(peer.getIdentity())) {
442 objectWrapper.set(peerNote);
450 public void receivedEndListPeerNotes(FcpConnection fcpConnection, EndListPeerNotes endListPeerNotes) {
451 completionLatch.countDown();
454 return objectWrapper.get();
458 * Replaces the peer note for the given peer.
463 * The new base64-encoded note text
465 * The type of the note (currently only <code>1</code> is
467 * @throws IOException
468 * if an I/O error occurs
469 * @throws FcpException
470 * if an FCP error occurs
472 public void modifyPeerNote(final Peer peer, final String noteText, final int noteType) throws IOException, FcpException {
473 new ExtendedFcpAdapter() {
479 @SuppressWarnings("synthetic-access")
480 public void run() throws IOException {
481 fcpConnection.sendMessage(new ModifyPeerNote(peer.getIdentity(), noteText, noteType));
488 public void receivedPeer(FcpConnection fcpConnection, Peer receivedPeer) {
489 if (receivedPeer.getIdentity().equals(peer.getIdentity())) {
490 completionLatch.countDown();
497 * Implementation of an {@link FcpListener} that can store an
498 * {@link FcpException} and wait for the arrival of a certain command.
500 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
502 private abstract class ExtendedFcpAdapter extends FcpAdapter {
504 /** The count down latch used to wait for completion. */
505 protected final CountDownLatch completionLatch = new CountDownLatch(1);
507 /** The FCP exception, if any. */
508 protected FcpException fcpException;
511 * Creates a new extended FCP adapter.
513 public ExtendedFcpAdapter() {
518 * Executes the FCP commands in {@link #run()}, wrapping the execution
519 * and catching exceptions.
521 * @throws IOException
522 * if an I/O error occurs
523 * @throws FcpException
524 * if an FCP error occurs
526 @SuppressWarnings("synthetic-access")
527 public void execute() throws IOException, FcpException {
528 fcpConnection.addFcpListener(this);
533 completionLatch.await();
535 } catch (InterruptedException ie1) {
536 /* ignore, we’ll loop. */
540 fcpConnection.removeFcpListener(this);
542 if (fcpException != null) {
548 * The FCP commands that actually get executed.
550 * @throws IOException
551 * if an I/O error occurs
553 public abstract void run() throws IOException;
559 public void connectionClosed(FcpConnection fcpConnection, Throwable throwable) {
560 fcpException = new FcpException("Connection closed", throwable);
561 completionLatch.countDown();
568 public void receivedCloseConnectionDuplicateClientName(FcpConnection fcpConnection, CloseConnectionDuplicateClientName closeConnectionDuplicateClientName) {
569 fcpException = new FcpException("Connection closed, duplicate client name");
570 completionLatch.countDown();
577 public void receivedProtocolError(FcpConnection fcpConnection, ProtocolError protocolError) {
578 fcpException = new FcpException("Protocol error (" + protocolError.getCode() + ", " + protocolError.getCodeDescription());
579 completionLatch.countDown();