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.GenerateSSK;
40 import net.pterodactylus.fcp.ListPeerNotes;
41 import net.pterodactylus.fcp.ListPeers;
42 import net.pterodactylus.fcp.ModifyPeer;
43 import net.pterodactylus.fcp.ModifyPeerNote;
44 import net.pterodactylus.fcp.NodeHello;
45 import net.pterodactylus.fcp.NodeRef;
46 import net.pterodactylus.fcp.Peer;
47 import net.pterodactylus.fcp.PeerNote;
48 import net.pterodactylus.fcp.PeerRemoved;
49 import net.pterodactylus.fcp.ProtocolError;
50 import net.pterodactylus.fcp.RemovePeer;
51 import net.pterodactylus.fcp.SSKKeypair;
52 import net.pterodactylus.util.thread.ObjectWrapper;
55 * High-level FCP client that hides the details of the underlying FCP
58 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
60 public class FcpClient {
62 /** Object used for synchronization. */
63 private final Object syncObject = new Object();
65 /** The name of this client. */
66 private final String name;
68 /** The underlying FCP connection. */
69 private final FcpConnection fcpConnection;
72 * Creates an FCP client with the given name.
75 * The name of the FCP client
76 * @throws UnknownHostException
77 * if the hostname “localhost” is unknown
79 public FcpClient(String name) throws UnknownHostException {
80 this(name, "localhost");
84 * Creates an FCP client.
87 * The name of the FCP client
89 * The hostname of the Freenet node
90 * @throws UnknownHostException
91 * if the given hostname can not be resolved
93 public FcpClient(String name, String hostname) throws UnknownHostException {
94 this(name, hostname, FcpConnection.DEFAULT_PORT);
98 * Creates an FCP client.
101 * The name of the FCP client
103 * The hostname of the Freenet node
105 * The Freenet node’s FCP port
106 * @throws UnknownHostException
107 * if the given hostname can not be resolved
109 public FcpClient(String name, String hostname, int port) throws UnknownHostException {
110 this(name, InetAddress.getByName(hostname), port);
114 * Creates an FCP client.
117 * The name of the FCP client
119 * The host address of the Freenet node
121 public FcpClient(String name, InetAddress host) {
122 this(name, host, FcpConnection.DEFAULT_PORT);
126 * Creates an FCP client.
129 * The name of the FCP client
131 * The host address of the Freenet node
133 * The Freenet node’s FCP port
135 public FcpClient(String name, InetAddress host, int port) {
137 fcpConnection = new FcpConnection(host, port);
145 * Connects the FCP client.
147 * @throws IOException
148 * if an I/O error occurs
149 * @throws FcpException
150 * if an FCP error occurs
152 public void connect() throws IOException, FcpException {
153 new ExtendedFcpAdapter() {
159 @SuppressWarnings("synthetic-access")
160 public void run() throws IOException {
161 fcpConnection.connect();
162 ClientHello clientHello = new ClientHello(name);
163 fcpConnection.sendMessage(clientHello);
170 public void receivedNodeHello(FcpConnection fcpConnection, NodeHello nodeHello) {
171 completionLatch.countDown();
177 * Disconnects the FCP client.
179 public void disconnect() {
180 synchronized (syncObject) {
181 fcpConnection.close();
182 syncObject.notifyAll();
191 * Returns all peers that the node has.
193 * @param withMetadata
194 * <code>true</code> to include peer metadata
195 * @param withVolatile
196 * <code>true</code> to include volatile peer data
197 * @return A set containing the node’s peers
198 * @throws IOException
199 * if an I/O error occurs
200 * @throws FcpException
201 * if an FCP error occurs
203 public Set<Peer> getPeers(final boolean withMetadata, final boolean withVolatile) throws IOException, FcpException {
204 final Set<Peer> peers = Collections.synchronizedSet(new HashSet<Peer>());
205 new ExtendedFcpAdapter() {
207 /** The ID of the “ListPeers” request. */
208 private String identifier = "list-peers-" + System.currentTimeMillis();
214 @SuppressWarnings("synthetic-access")
215 public void run() throws IOException {
216 fcpConnection.sendMessage(new ListPeers(identifier, withMetadata, withVolatile));
223 public void receivedPeer(FcpConnection fcpConnection, Peer peer) {
224 if (peer.getIdentifier().equals(identifier)) {
233 public void receivedEndListPeers(FcpConnection fcpConnection, EndListPeers endListPeers) {
234 if (endListPeers.getIdentifier().equals(identifier)) {
235 completionLatch.countDown();
243 * Adds the given peer to the node.
247 * @throws IOException
248 * if an I/O error occurs
249 * @throws FcpException
250 * if an FCP error occurs
252 public void addPeer(Peer peer) throws IOException, FcpException {
253 addPeer(peer.getNodeRef());
257 * Adds the peer defined by the noderef to the node.
260 * The noderef that defines the new peer
261 * @throws IOException
262 * if an I/O error occurs
263 * @throws FcpException
264 * if an FCP error occurs
266 public void addPeer(NodeRef nodeRef) throws IOException, FcpException {
267 addPeer(new AddPeer(nodeRef));
271 * Adds a peer, reading the noderef from the given URL.
274 * The URL to read the noderef from
275 * @throws IOException
276 * if an I/O error occurs
277 * @throws FcpException
278 * if an FCP error occurs
280 public void addPeer(URL url) throws IOException, FcpException {
281 addPeer(new AddPeer(url));
285 * Adds a peer, reading the noderef of the peer from the given file.
286 * <strong>Note:</strong> the file to read the noderef from has to reside on
287 * the same machine as the node!
290 * The name of the file containing the peer’s noderef
291 * @throws IOException
292 * if an I/O error occurs
293 * @throws FcpException
294 * if an FCP error occurs
296 public void addPeer(String file) throws IOException, FcpException {
297 addPeer(new AddPeer(file));
301 * Sends the given {@link AddPeer} message to the node. This method should
302 * not be called directly. Use one of {@link #addPeer(Peer)},
303 * {@link #addPeer(NodeRef)}, {@link #addPeer(URL)}, or
304 * {@link #addPeer(String)} instead.
307 * The “AddPeer” message
308 * @throws IOException
309 * if an I/O error occurs
310 * @throws FcpException
311 * if an FCP error occurs
313 private void addPeer(final AddPeer addPeer) throws IOException, FcpException {
314 new ExtendedFcpAdapter() {
320 @SuppressWarnings("synthetic-access")
321 public void run() throws IOException {
322 fcpConnection.sendMessage(addPeer);
329 public void receivedPeer(FcpConnection fcpConnection, Peer peer) {
330 completionLatch.countDown();
336 * Modifies the given peer.
340 * @param allowLocalAddresses
341 * <code>true</code> to allow local address, <code>false</code>
342 * to not allow local address, <code>null</code> to not change
345 * <code>true</code> to disable the peer, <code>false</code> to
346 * enable the peer, <code>null</code> to not change the setting
348 * <code>true</code> to enable “listen only” for the peer,
349 * <code>false</code> to disable it, <code>null</code> to not
351 * @throws IOException
352 * if an I/O error occurs
353 * @throws FcpException
354 * if an FCP error occurs
356 public void modifyPeer(final Peer peer, final Boolean allowLocalAddresses, final Boolean disabled, final Boolean listenOnly) throws IOException, FcpException {
357 new ExtendedFcpAdapter() {
363 @SuppressWarnings("synthetic-access")
364 public void run() throws IOException {
365 fcpConnection.sendMessage(new ModifyPeer(peer.getIdentity(), allowLocalAddresses, disabled, listenOnly));
372 public void receivedPeer(FcpConnection fcpConnection, Peer peer) {
373 completionLatch.countDown();
379 * Removes the given peer.
383 * @throws IOException
384 * if an I/O error occurs
385 * @throws FcpException
386 * if an FCP error occurs
388 public void removePeer(final Peer peer) throws IOException, FcpException {
389 new ExtendedFcpAdapter() {
395 @SuppressWarnings("synthetic-access")
396 public void run() throws IOException {
397 fcpConnection.sendMessage(new RemovePeer(peer.getIdentity()));
404 public void receivedPeerRemoved(FcpConnection fcpConnection, PeerRemoved peerRemoved) {
405 completionLatch.countDown();
411 // PEER NOTES MANAGEMENT
415 * Returns the peer note of the given peer.
418 * The peer to get the note for
419 * @return The peer’s note
420 * @throws IOException
421 * if an I/O error occurs
422 * @throws FcpException
423 * if an FCP error occurs
425 public PeerNote getPeerNote(final Peer peer) throws IOException, FcpException {
426 final ObjectWrapper<PeerNote> objectWrapper = new ObjectWrapper<PeerNote>();
427 new ExtendedFcpAdapter() {
433 @SuppressWarnings("synthetic-access")
434 public void run() throws IOException {
435 fcpConnection.sendMessage(new ListPeerNotes(peer.getIdentity()));
442 public void receivedPeerNote(FcpConnection fcpConnection, PeerNote peerNote) {
443 if (peerNote.getNodeIdentifier().equals(peer.getIdentity())) {
444 objectWrapper.set(peerNote);
452 public void receivedEndListPeerNotes(FcpConnection fcpConnection, EndListPeerNotes endListPeerNotes) {
453 completionLatch.countDown();
456 return objectWrapper.get();
460 * Replaces the peer note for the given peer.
465 * The new base64-encoded note text
467 * The type of the note (currently only <code>1</code> is
469 * @throws IOException
470 * if an I/O error occurs
471 * @throws FcpException
472 * if an FCP error occurs
474 public void modifyPeerNote(final Peer peer, final String noteText, final int noteType) throws IOException, FcpException {
475 new ExtendedFcpAdapter() {
481 @SuppressWarnings("synthetic-access")
482 public void run() throws IOException {
483 fcpConnection.sendMessage(new ModifyPeerNote(peer.getIdentity(), noteText, noteType));
490 public void receivedPeer(FcpConnection fcpConnection, Peer receivedPeer) {
491 if (receivedPeer.getIdentity().equals(peer.getIdentity())) {
492 completionLatch.countDown();
503 * Generates a new SSK key pair.
505 * @return The generated key pair
506 * @throws IOException
507 * if an I/O error occurs
508 * @throws FcpException
509 * if an FCP error occurs
511 public SSKKeypair generateKeyPair() throws IOException, FcpException {
512 final ObjectWrapper<SSKKeypair> sskKeypairWrapper = new ObjectWrapper<SSKKeypair>();
513 new ExtendedFcpAdapter() {
519 @SuppressWarnings("synthetic-access")
520 public void run() throws IOException {
521 fcpConnection.sendMessage(new GenerateSSK());
528 public void receivedSSKKeypair(FcpConnection fcpConnection, SSKKeypair sskKeypair) {
529 sskKeypairWrapper.set(sskKeypair);
530 completionLatch.countDown();
533 return sskKeypairWrapper.get();
537 * Implementation of an {@link FcpListener} that can store an
538 * {@link FcpException} and wait for the arrival of a certain command.
540 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
542 private abstract class ExtendedFcpAdapter extends FcpAdapter {
544 /** The count down latch used to wait for completion. */
545 protected final CountDownLatch completionLatch = new CountDownLatch(1);
547 /** The FCP exception, if any. */
548 protected FcpException fcpException;
551 * Creates a new extended FCP adapter.
553 public ExtendedFcpAdapter() {
558 * Executes the FCP commands in {@link #run()}, wrapping the execution
559 * and catching exceptions.
561 * @throws IOException
562 * if an I/O error occurs
563 * @throws FcpException
564 * if an FCP error occurs
566 @SuppressWarnings("synthetic-access")
567 public void execute() throws IOException, FcpException {
568 fcpConnection.addFcpListener(this);
573 completionLatch.await();
575 } catch (InterruptedException ie1) {
576 /* ignore, we’ll loop. */
580 fcpConnection.removeFcpListener(this);
582 if (fcpException != null) {
588 * The FCP commands that actually get executed.
590 * @throws IOException
591 * if an I/O error occurs
593 public abstract void run() throws IOException;
599 public void connectionClosed(FcpConnection fcpConnection, Throwable throwable) {
600 fcpException = new FcpException("Connection closed", throwable);
601 completionLatch.countDown();
608 public void receivedCloseConnectionDuplicateClientName(FcpConnection fcpConnection, CloseConnectionDuplicateClientName closeConnectionDuplicateClientName) {
609 fcpException = new FcpException("Connection closed, duplicate client name");
610 completionLatch.countDown();
617 public void receivedProtocolError(FcpConnection fcpConnection, ProtocolError protocolError) {
618 fcpException = new FcpException("Protocol error (" + protocolError.getCode() + ", " + protocolError.getCodeDescription());
619 completionLatch.countDown();