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.EndListPersistentRequests;
37 import net.pterodactylus.fcp.FcpAdapter;
38 import net.pterodactylus.fcp.FcpConnection;
39 import net.pterodactylus.fcp.FcpListener;
40 import net.pterodactylus.fcp.GenerateSSK;
41 import net.pterodactylus.fcp.ListPeerNotes;
42 import net.pterodactylus.fcp.ListPeers;
43 import net.pterodactylus.fcp.ListPersistentRequests;
44 import net.pterodactylus.fcp.ModifyPeer;
45 import net.pterodactylus.fcp.ModifyPeerNote;
46 import net.pterodactylus.fcp.NodeHello;
47 import net.pterodactylus.fcp.NodeRef;
48 import net.pterodactylus.fcp.Peer;
49 import net.pterodactylus.fcp.PeerNote;
50 import net.pterodactylus.fcp.PeerRemoved;
51 import net.pterodactylus.fcp.PersistentGet;
52 import net.pterodactylus.fcp.ProtocolError;
53 import net.pterodactylus.fcp.RemovePeer;
54 import net.pterodactylus.fcp.SSKKeypair;
55 import net.pterodactylus.fcp.WatchGlobal;
56 import net.pterodactylus.util.thread.ObjectWrapper;
59 * High-level FCP client that hides the details of the underlying FCP
62 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
64 public class FcpClient {
66 /** Object used for synchronization. */
67 private final Object syncObject = new Object();
69 /** The name of this client. */
70 private final String name;
72 /** The underlying FCP connection. */
73 private final FcpConnection fcpConnection;
76 * Creates an FCP client with the given name.
79 * The name of the FCP client
80 * @throws UnknownHostException
81 * if the hostname “localhost” is unknown
83 public FcpClient(String name) throws UnknownHostException {
84 this(name, "localhost");
88 * Creates an FCP client.
91 * The name of the FCP client
93 * The hostname of the Freenet node
94 * @throws UnknownHostException
95 * if the given hostname can not be resolved
97 public FcpClient(String name, String hostname) throws UnknownHostException {
98 this(name, hostname, FcpConnection.DEFAULT_PORT);
102 * Creates an FCP client.
105 * The name of the FCP client
107 * The hostname of the Freenet node
109 * The Freenet node’s FCP port
110 * @throws UnknownHostException
111 * if the given hostname can not be resolved
113 public FcpClient(String name, String hostname, int port) throws UnknownHostException {
114 this(name, InetAddress.getByName(hostname), port);
118 * Creates an FCP client.
121 * The name of the FCP client
123 * The host address of the Freenet node
125 public FcpClient(String name, InetAddress host) {
126 this(name, host, FcpConnection.DEFAULT_PORT);
130 * Creates an FCP client.
133 * The name of the FCP client
135 * The host address of the Freenet node
137 * The Freenet node’s FCP port
139 public FcpClient(String name, InetAddress host, int port) {
141 fcpConnection = new FcpConnection(host, port);
149 * Connects the FCP client.
151 * @throws IOException
152 * if an I/O error occurs
153 * @throws FcpException
154 * if an FCP error occurs
156 public void connect() throws IOException, FcpException {
157 new ExtendedFcpAdapter() {
163 @SuppressWarnings("synthetic-access")
164 public void run() throws IOException {
165 fcpConnection.connect();
166 ClientHello clientHello = new ClientHello(name);
167 fcpConnection.sendMessage(clientHello);
174 public void receivedNodeHello(FcpConnection fcpConnection, NodeHello nodeHello) {
175 completionLatch.countDown();
181 * Disconnects the FCP client.
183 public void disconnect() {
184 synchronized (syncObject) {
185 fcpConnection.close();
186 syncObject.notifyAll();
195 * Returns all peers that the node has.
197 * @param withMetadata
198 * <code>true</code> to include peer metadata
199 * @param withVolatile
200 * <code>true</code> to include volatile peer data
201 * @return A set containing the node’s peers
202 * @throws IOException
203 * if an I/O error occurs
204 * @throws FcpException
205 * if an FCP error occurs
207 public Set<Peer> getPeers(final boolean withMetadata, final boolean withVolatile) throws IOException, FcpException {
208 final Set<Peer> peers = Collections.synchronizedSet(new HashSet<Peer>());
209 new ExtendedFcpAdapter() {
211 /** The ID of the “ListPeers” request. */
212 @SuppressWarnings("synthetic-access")
213 private String identifier = createIdentifier("list-peers");
219 @SuppressWarnings("synthetic-access")
220 public void run() throws IOException {
221 fcpConnection.sendMessage(new ListPeers(identifier, withMetadata, withVolatile));
228 public void receivedPeer(FcpConnection fcpConnection, Peer peer) {
229 if (peer.getIdentifier().equals(identifier)) {
238 public void receivedEndListPeers(FcpConnection fcpConnection, EndListPeers endListPeers) {
239 if (endListPeers.getIdentifier().equals(identifier)) {
240 completionLatch.countDown();
248 * Adds the given peer to the node.
252 * @throws IOException
253 * if an I/O error occurs
254 * @throws FcpException
255 * if an FCP error occurs
257 public void addPeer(Peer peer) throws IOException, FcpException {
258 addPeer(peer.getNodeRef());
262 * Adds the peer defined by the noderef to the node.
265 * The noderef that defines the new peer
266 * @throws IOException
267 * if an I/O error occurs
268 * @throws FcpException
269 * if an FCP error occurs
271 public void addPeer(NodeRef nodeRef) throws IOException, FcpException {
272 addPeer(new AddPeer(nodeRef));
276 * Adds a peer, reading the noderef from the given URL.
279 * The URL to read the noderef from
280 * @throws IOException
281 * if an I/O error occurs
282 * @throws FcpException
283 * if an FCP error occurs
285 public void addPeer(URL url) throws IOException, FcpException {
286 addPeer(new AddPeer(url));
290 * Adds a peer, reading the noderef of the peer from the given file.
291 * <strong>Note:</strong> the file to read the noderef from has to reside on
292 * the same machine as the node!
295 * The name of the file containing the peer’s noderef
296 * @throws IOException
297 * if an I/O error occurs
298 * @throws FcpException
299 * if an FCP error occurs
301 public void addPeer(String file) throws IOException, FcpException {
302 addPeer(new AddPeer(file));
306 * Sends the given {@link AddPeer} message to the node. This method should
307 * not be called directly. Use one of {@link #addPeer(Peer)},
308 * {@link #addPeer(NodeRef)}, {@link #addPeer(URL)}, or
309 * {@link #addPeer(String)} instead.
312 * The “AddPeer” message
313 * @throws IOException
314 * if an I/O error occurs
315 * @throws FcpException
316 * if an FCP error occurs
318 private void addPeer(final AddPeer addPeer) throws IOException, FcpException {
319 new ExtendedFcpAdapter() {
325 @SuppressWarnings("synthetic-access")
326 public void run() throws IOException {
327 fcpConnection.sendMessage(addPeer);
334 public void receivedPeer(FcpConnection fcpConnection, Peer peer) {
335 completionLatch.countDown();
341 * Modifies the given peer.
345 * @param allowLocalAddresses
346 * <code>true</code> to allow local address, <code>false</code>
347 * to not allow local address, <code>null</code> to not change
350 * <code>true</code> to disable the peer, <code>false</code> to
351 * enable the peer, <code>null</code> to not change the setting
353 * <code>true</code> to enable “listen only” for the peer,
354 * <code>false</code> to disable it, <code>null</code> to not
356 * @throws IOException
357 * if an I/O error occurs
358 * @throws FcpException
359 * if an FCP error occurs
361 public void modifyPeer(final Peer peer, final Boolean allowLocalAddresses, final Boolean disabled, final Boolean listenOnly) throws IOException, FcpException {
362 new ExtendedFcpAdapter() {
368 @SuppressWarnings("synthetic-access")
369 public void run() throws IOException {
370 fcpConnection.sendMessage(new ModifyPeer(peer.getIdentity(), allowLocalAddresses, disabled, listenOnly));
377 public void receivedPeer(FcpConnection fcpConnection, Peer peer) {
378 completionLatch.countDown();
384 * Removes the given peer.
388 * @throws IOException
389 * if an I/O error occurs
390 * @throws FcpException
391 * if an FCP error occurs
393 public void removePeer(final Peer peer) throws IOException, FcpException {
394 new ExtendedFcpAdapter() {
400 @SuppressWarnings("synthetic-access")
401 public void run() throws IOException {
402 fcpConnection.sendMessage(new RemovePeer(peer.getIdentity()));
409 public void receivedPeerRemoved(FcpConnection fcpConnection, PeerRemoved peerRemoved) {
410 completionLatch.countDown();
416 // PEER NOTES MANAGEMENT
420 * Returns the peer note of the given peer.
423 * The peer to get the note for
424 * @return The peer’s note
425 * @throws IOException
426 * if an I/O error occurs
427 * @throws FcpException
428 * if an FCP error occurs
430 public PeerNote getPeerNote(final Peer peer) throws IOException, FcpException {
431 final ObjectWrapper<PeerNote> objectWrapper = new ObjectWrapper<PeerNote>();
432 new ExtendedFcpAdapter() {
438 @SuppressWarnings("synthetic-access")
439 public void run() throws IOException {
440 fcpConnection.sendMessage(new ListPeerNotes(peer.getIdentity()));
447 public void receivedPeerNote(FcpConnection fcpConnection, PeerNote peerNote) {
448 if (peerNote.getNodeIdentifier().equals(peer.getIdentity())) {
449 objectWrapper.set(peerNote);
457 public void receivedEndListPeerNotes(FcpConnection fcpConnection, EndListPeerNotes endListPeerNotes) {
458 completionLatch.countDown();
461 return objectWrapper.get();
465 * Replaces the peer note for the given peer.
470 * The new base64-encoded note text
472 * The type of the note (currently only <code>1</code> is
474 * @throws IOException
475 * if an I/O error occurs
476 * @throws FcpException
477 * if an FCP error occurs
479 public void modifyPeerNote(final Peer peer, final String noteText, final int noteType) throws IOException, FcpException {
480 new ExtendedFcpAdapter() {
486 @SuppressWarnings("synthetic-access")
487 public void run() throws IOException {
488 fcpConnection.sendMessage(new ModifyPeerNote(peer.getIdentity(), noteText, noteType));
495 public void receivedPeer(FcpConnection fcpConnection, Peer receivedPeer) {
496 if (receivedPeer.getIdentity().equals(peer.getIdentity())) {
497 completionLatch.countDown();
508 * Generates a new SSK key pair.
510 * @return The generated key pair
511 * @throws IOException
512 * if an I/O error occurs
513 * @throws FcpException
514 * if an FCP error occurs
516 public SSKKeypair generateKeyPair() throws IOException, FcpException {
517 final ObjectWrapper<SSKKeypair> sskKeypairWrapper = new ObjectWrapper<SSKKeypair>();
518 new ExtendedFcpAdapter() {
524 @SuppressWarnings("synthetic-access")
525 public void run() throws IOException {
526 fcpConnection.sendMessage(new GenerateSSK());
533 public void receivedSSKKeypair(FcpConnection fcpConnection, SSKKeypair sskKeypair) {
534 sskKeypairWrapper.set(sskKeypair);
535 completionLatch.countDown();
538 return sskKeypairWrapper.get();
542 // REQUEST MANAGEMENT
546 * Returns all currently visible persistent get requests.
549 * <code>true</code> to return get requests from the global
550 * queue, <code>false</code> to only show requests from the
552 * @return All get requests
553 * @throws IOException
554 * if an I/O error occurs
555 * @throws FcpException
556 * if an FCP error occurs
558 public Set<PersistentGet> getGetRequests(final boolean global) throws IOException, FcpException {
559 final Set<PersistentGet> getRequests = Collections.synchronizedSet(new HashSet<PersistentGet>());
560 new ExtendedFcpAdapter() {
566 @SuppressWarnings("synthetic-access")
567 public void run() throws IOException {
568 fcpConnection.sendMessage(new WatchGlobal(global));
569 fcpConnection.sendMessage(new ListPersistentRequests());
576 public void receivedPersistentGet(FcpConnection fcpConnection, PersistentGet persistentGet) {
577 getRequests.add(persistentGet);
584 public void receivedEndListPersistentRequests(FcpConnection fcpConnection, EndListPersistentRequests endListPersistentRequests) {
585 completionLatch.countDown();
596 * Creates a unique request identifier.
599 * The basename of the request
600 * @return The created request identifier
602 private String createIdentifier(String basename) {
603 return basename + "-" + System.currentTimeMillis() + "-" + (int) (Math.random() * Integer.MAX_VALUE);
607 * Implementation of an {@link FcpListener} that can store an
608 * {@link FcpException} and wait for the arrival of a certain command.
610 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
612 private abstract class ExtendedFcpAdapter extends FcpAdapter {
614 /** The count down latch used to wait for completion. */
615 protected final CountDownLatch completionLatch = new CountDownLatch(1);
617 /** The FCP exception, if any. */
618 protected FcpException fcpException;
621 * Creates a new extended FCP adapter.
623 public ExtendedFcpAdapter() {
628 * Executes the FCP commands in {@link #run()}, wrapping the execution
629 * and catching exceptions.
631 * @throws IOException
632 * if an I/O error occurs
633 * @throws FcpException
634 * if an FCP error occurs
636 @SuppressWarnings("synthetic-access")
637 public void execute() throws IOException, FcpException {
638 fcpConnection.addFcpListener(this);
643 completionLatch.await();
645 } catch (InterruptedException ie1) {
646 /* ignore, we’ll loop. */
650 fcpConnection.removeFcpListener(this);
652 if (fcpException != null) {
658 * The FCP commands that actually get executed.
660 * @throws IOException
661 * if an I/O error occurs
663 public abstract void run() throws IOException;
669 public void connectionClosed(FcpConnection fcpConnection, Throwable throwable) {
670 fcpException = new FcpException("Connection closed", throwable);
671 completionLatch.countDown();
678 public void receivedCloseConnectionDuplicateClientName(FcpConnection fcpConnection, CloseConnectionDuplicateClientName closeConnectionDuplicateClientName) {
679 fcpException = new FcpException("Connection closed, duplicate client name");
680 completionLatch.countDown();
687 public void receivedProtocolError(FcpConnection fcpConnection, ProtocolError protocolError) {
688 fcpException = new FcpException("Protocol error (" + protocolError.getCode() + ", " + protocolError.getCodeDescription());
689 completionLatch.countDown();