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.Collection;
27 import java.util.Collections;
28 import java.util.HashMap;
29 import java.util.HashSet;
32 import java.util.concurrent.CountDownLatch;
34 import net.pterodactylus.fcp.AddPeer;
35 import net.pterodactylus.fcp.ClientHello;
36 import net.pterodactylus.fcp.CloseConnectionDuplicateClientName;
37 import net.pterodactylus.fcp.DataFound;
38 import net.pterodactylus.fcp.EndListPeerNotes;
39 import net.pterodactylus.fcp.EndListPeers;
40 import net.pterodactylus.fcp.EndListPersistentRequests;
41 import net.pterodactylus.fcp.FcpAdapter;
42 import net.pterodactylus.fcp.FcpConnection;
43 import net.pterodactylus.fcp.FcpListener;
44 import net.pterodactylus.fcp.GenerateSSK;
45 import net.pterodactylus.fcp.GetFailed;
46 import net.pterodactylus.fcp.ListPeerNotes;
47 import net.pterodactylus.fcp.ListPeers;
48 import net.pterodactylus.fcp.ListPersistentRequests;
49 import net.pterodactylus.fcp.ModifyPeer;
50 import net.pterodactylus.fcp.ModifyPeerNote;
51 import net.pterodactylus.fcp.NodeHello;
52 import net.pterodactylus.fcp.NodeRef;
53 import net.pterodactylus.fcp.Peer;
54 import net.pterodactylus.fcp.PeerNote;
55 import net.pterodactylus.fcp.PeerRemoved;
56 import net.pterodactylus.fcp.PersistentGet;
57 import net.pterodactylus.fcp.PersistentPut;
58 import net.pterodactylus.fcp.ProtocolError;
59 import net.pterodactylus.fcp.RemovePeer;
60 import net.pterodactylus.fcp.SSKKeypair;
61 import net.pterodactylus.fcp.SimpleProgress;
62 import net.pterodactylus.fcp.WatchGlobal;
63 import net.pterodactylus.util.thread.ObjectWrapper;
66 * High-level FCP client that hides the details of the underlying FCP
69 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
71 public class FcpClient {
73 /** Object used for synchronization. */
74 private final Object syncObject = new Object();
76 /** The name of this client. */
77 private final String name;
79 /** The underlying FCP connection. */
80 private final FcpConnection fcpConnection;
83 * Creates an FCP client with the given name.
86 * The name of the FCP client
87 * @throws UnknownHostException
88 * if the hostname “localhost” is unknown
90 public FcpClient(String name) throws UnknownHostException {
91 this(name, "localhost");
95 * Creates an FCP client.
98 * The name of the FCP client
100 * The hostname of the Freenet node
101 * @throws UnknownHostException
102 * if the given hostname can not be resolved
104 public FcpClient(String name, String hostname) throws UnknownHostException {
105 this(name, hostname, FcpConnection.DEFAULT_PORT);
109 * Creates an FCP client.
112 * The name of the FCP client
114 * The hostname of the Freenet node
116 * The Freenet node’s FCP port
117 * @throws UnknownHostException
118 * if the given hostname can not be resolved
120 public FcpClient(String name, String hostname, int port) throws UnknownHostException {
121 this(name, InetAddress.getByName(hostname), port);
125 * Creates an FCP client.
128 * The name of the FCP client
130 * The host address of the Freenet node
132 public FcpClient(String name, InetAddress host) {
133 this(name, host, FcpConnection.DEFAULT_PORT);
137 * Creates an FCP client.
140 * The name of the FCP client
142 * The host address of the Freenet node
144 * The Freenet node’s FCP port
146 public FcpClient(String name, InetAddress host, int port) {
148 fcpConnection = new FcpConnection(host, port);
156 * Connects the FCP client.
158 * @throws IOException
159 * if an I/O error occurs
160 * @throws FcpException
161 * if an FCP error occurs
163 public void connect() throws IOException, FcpException {
164 new ExtendedFcpAdapter() {
170 @SuppressWarnings("synthetic-access")
171 public void run() throws IOException {
172 fcpConnection.connect();
173 ClientHello clientHello = new ClientHello(name);
174 fcpConnection.sendMessage(clientHello);
175 WatchGlobal watchGlobal = new WatchGlobal(true);
176 fcpConnection.sendMessage(watchGlobal);
183 public void receivedNodeHello(FcpConnection fcpConnection, NodeHello nodeHello) {
184 completionLatch.countDown();
190 * Disconnects the FCP client.
192 public void disconnect() {
193 synchronized (syncObject) {
194 fcpConnection.close();
195 syncObject.notifyAll();
204 * Returns all peers that the node has.
206 * @param withMetadata
207 * <code>true</code> to include peer metadata
208 * @param withVolatile
209 * <code>true</code> to include volatile peer data
210 * @return A set containing the node’s peers
211 * @throws IOException
212 * if an I/O error occurs
213 * @throws FcpException
214 * if an FCP error occurs
216 public Collection<Peer> getPeers(final boolean withMetadata, final boolean withVolatile) throws IOException, FcpException {
217 final Set<Peer> peers = Collections.synchronizedSet(new HashSet<Peer>());
218 new ExtendedFcpAdapter() {
220 /** The ID of the “ListPeers” request. */
221 @SuppressWarnings("synthetic-access")
222 private String identifier = createIdentifier("list-peers");
228 @SuppressWarnings("synthetic-access")
229 public void run() throws IOException {
230 fcpConnection.sendMessage(new ListPeers(identifier, withMetadata, withVolatile));
237 public void receivedPeer(FcpConnection fcpConnection, Peer peer) {
238 if (peer.getIdentifier().equals(identifier)) {
247 public void receivedEndListPeers(FcpConnection fcpConnection, EndListPeers endListPeers) {
248 if (endListPeers.getIdentifier().equals(identifier)) {
249 completionLatch.countDown();
257 * Adds the given peer to the node.
261 * @throws IOException
262 * if an I/O error occurs
263 * @throws FcpException
264 * if an FCP error occurs
266 public void addPeer(Peer peer) throws IOException, FcpException {
267 addPeer(peer.getNodeRef());
271 * Adds the peer defined by the noderef to the node.
274 * The noderef that defines the new peer
275 * @throws IOException
276 * if an I/O error occurs
277 * @throws FcpException
278 * if an FCP error occurs
280 public void addPeer(NodeRef nodeRef) throws IOException, FcpException {
281 addPeer(new AddPeer(nodeRef));
285 * Adds a peer, reading the noderef from the given URL.
288 * The URL to read the noderef from
289 * @throws IOException
290 * if an I/O error occurs
291 * @throws FcpException
292 * if an FCP error occurs
294 public void addPeer(URL url) throws IOException, FcpException {
295 addPeer(new AddPeer(url));
299 * Adds a peer, reading the noderef of the peer from the given file.
300 * <strong>Note:</strong> the file to read the noderef from has to reside on
301 * the same machine as the node!
304 * The name of the file containing the peer’s noderef
305 * @throws IOException
306 * if an I/O error occurs
307 * @throws FcpException
308 * if an FCP error occurs
310 public void addPeer(String file) throws IOException, FcpException {
311 addPeer(new AddPeer(file));
315 * Sends the given {@link AddPeer} message to the node. This method should
316 * not be called directly. Use one of {@link #addPeer(Peer)},
317 * {@link #addPeer(NodeRef)}, {@link #addPeer(URL)}, or
318 * {@link #addPeer(String)} instead.
321 * The “AddPeer” message
322 * @throws IOException
323 * if an I/O error occurs
324 * @throws FcpException
325 * if an FCP error occurs
327 private void addPeer(final AddPeer addPeer) throws IOException, FcpException {
328 new ExtendedFcpAdapter() {
334 @SuppressWarnings("synthetic-access")
335 public void run() throws IOException {
336 fcpConnection.sendMessage(addPeer);
343 public void receivedPeer(FcpConnection fcpConnection, Peer peer) {
344 completionLatch.countDown();
350 * Modifies the given peer.
354 * @param allowLocalAddresses
355 * <code>true</code> to allow local address, <code>false</code>
356 * to not allow local address, <code>null</code> to not change
359 * <code>true</code> to disable the peer, <code>false</code> to
360 * enable the peer, <code>null</code> to not change the setting
362 * <code>true</code> to enable “listen only” for the peer,
363 * <code>false</code> to disable it, <code>null</code> to not
365 * @throws IOException
366 * if an I/O error occurs
367 * @throws FcpException
368 * if an FCP error occurs
370 public void modifyPeer(final Peer peer, final Boolean allowLocalAddresses, final Boolean disabled, final Boolean listenOnly) throws IOException, FcpException {
371 new ExtendedFcpAdapter() {
377 @SuppressWarnings("synthetic-access")
378 public void run() throws IOException {
379 fcpConnection.sendMessage(new ModifyPeer(peer.getIdentity(), allowLocalAddresses, disabled, listenOnly));
386 public void receivedPeer(FcpConnection fcpConnection, Peer peer) {
387 completionLatch.countDown();
393 * Removes the given peer.
397 * @throws IOException
398 * if an I/O error occurs
399 * @throws FcpException
400 * if an FCP error occurs
402 public void removePeer(final Peer peer) throws IOException, FcpException {
403 new ExtendedFcpAdapter() {
409 @SuppressWarnings("synthetic-access")
410 public void run() throws IOException {
411 fcpConnection.sendMessage(new RemovePeer(peer.getIdentity()));
418 public void receivedPeerRemoved(FcpConnection fcpConnection, PeerRemoved peerRemoved) {
419 completionLatch.countDown();
425 // PEER NOTES MANAGEMENT
429 * Returns the peer note of the given peer.
432 * The peer to get the note for
433 * @return The peer’s note
434 * @throws IOException
435 * if an I/O error occurs
436 * @throws FcpException
437 * if an FCP error occurs
439 public PeerNote getPeerNote(final Peer peer) throws IOException, FcpException {
440 final ObjectWrapper<PeerNote> objectWrapper = new ObjectWrapper<PeerNote>();
441 new ExtendedFcpAdapter() {
447 @SuppressWarnings("synthetic-access")
448 public void run() throws IOException {
449 fcpConnection.sendMessage(new ListPeerNotes(peer.getIdentity()));
456 public void receivedPeerNote(FcpConnection fcpConnection, PeerNote peerNote) {
457 if (peerNote.getNodeIdentifier().equals(peer.getIdentity())) {
458 objectWrapper.set(peerNote);
466 public void receivedEndListPeerNotes(FcpConnection fcpConnection, EndListPeerNotes endListPeerNotes) {
467 completionLatch.countDown();
470 return objectWrapper.get();
474 * Replaces the peer note for the given peer.
479 * The new base64-encoded note text
481 * The type of the note (currently only <code>1</code> is
483 * @throws IOException
484 * if an I/O error occurs
485 * @throws FcpException
486 * if an FCP error occurs
488 public void modifyPeerNote(final Peer peer, final String noteText, final int noteType) throws IOException, FcpException {
489 new ExtendedFcpAdapter() {
495 @SuppressWarnings("synthetic-access")
496 public void run() throws IOException {
497 fcpConnection.sendMessage(new ModifyPeerNote(peer.getIdentity(), noteText, noteType));
504 public void receivedPeer(FcpConnection fcpConnection, Peer receivedPeer) {
505 if (receivedPeer.getIdentity().equals(peer.getIdentity())) {
506 completionLatch.countDown();
517 * Generates a new SSK key pair.
519 * @return The generated key pair
520 * @throws IOException
521 * if an I/O error occurs
522 * @throws FcpException
523 * if an FCP error occurs
525 public SSKKeypair generateKeyPair() throws IOException, FcpException {
526 final ObjectWrapper<SSKKeypair> sskKeypairWrapper = new ObjectWrapper<SSKKeypair>();
527 new ExtendedFcpAdapter() {
533 @SuppressWarnings("synthetic-access")
534 public void run() throws IOException {
535 fcpConnection.sendMessage(new GenerateSSK());
542 public void receivedSSKKeypair(FcpConnection fcpConnection, SSKKeypair sskKeypair) {
543 sskKeypairWrapper.set(sskKeypair);
544 completionLatch.countDown();
547 return sskKeypairWrapper.get();
551 // REQUEST MANAGEMENT
555 * Returns all currently visible persistent get requests.
558 * <code>true</code> to return get requests from the global
559 * queue, <code>false</code> to only show requests from the
561 * @return All get requests
562 * @throws IOException
563 * if an I/O error occurs
564 * @throws FcpException
565 * if an FCP error occurs
567 public Collection<Request> getGetRequests(final boolean global) throws IOException, FcpException {
568 return getRequests(global);
571 public Collection<Request> getRequests(final boolean global) throws IOException, FcpException {
572 final Map<String, Request> requests = Collections.synchronizedMap(new HashMap<String, Request>());
573 new ExtendedFcpAdapter() {
579 @SuppressWarnings("synthetic-access")
580 public void run() throws IOException {
581 fcpConnection.sendMessage(new ListPersistentRequests());
588 public void receivedPersistentGet(FcpConnection fcpConnection, PersistentGet persistentGet) {
589 if (!persistentGet.isGlobal() || global) {
590 GetRequest getRequest = new GetRequest(persistentGet);
591 requests.put(persistentGet.getIdentifier(), getRequest);
598 * @see net.pterodactylus.fcp.FcpAdapter#receivedDataFound(net.pterodactylus.fcp.FcpConnection,
599 * net.pterodactylus.fcp.DataFound)
602 public void receivedDataFound(FcpConnection fcpConnection, DataFound dataFound) {
603 Request getRequest = requests.get(dataFound.getIdentifier());
604 if (getRequest == null) {
607 getRequest.setComplete(true);
608 getRequest.setLength(dataFound.getDataLength());
609 getRequest.setContentType(dataFound.getMetadataContentType());
615 * @see net.pterodactylus.fcp.FcpAdapter#receivedGetFailed(net.pterodactylus.fcp.FcpConnection,
616 * net.pterodactylus.fcp.GetFailed)
619 public void receivedGetFailed(FcpConnection fcpConnection, GetFailed getFailed) {
620 Request getRequest = requests.get(getFailed.getIdentifier());
621 if (getRequest == null) {
624 getRequest.setComplete(true);
625 getRequest.setFailed(true);
626 getRequest.setFatal(getFailed.isFatal());
627 getRequest.setErrorCode(getFailed.getCode());
633 * @see net.pterodactylus.fcp.FcpAdapter#receivedPersistentPut(net.pterodactylus.fcp.FcpConnection,
634 * net.pterodactylus.fcp.PersistentPut)
637 public void receivedPersistentPut(FcpConnection fcpConnection, PersistentPut persistentPut) {
638 if (!persistentPut.isGlobal() || global) {
639 PutRequest putRequest = new PutRequest(persistentPut);
640 requests.put(persistentPut.getIdentifier(), putRequest);
647 * @see net.pterodactylus.fcp.FcpAdapter#receivedSimpleProgress(net.pterodactylus.fcp.FcpConnection,
648 * net.pterodactylus.fcp.SimpleProgress)
651 public void receivedSimpleProgress(FcpConnection fcpConnection, SimpleProgress simpleProgress) {
652 Request request = requests.get(simpleProgress.getIdentifier());
653 if (request == null) {
656 request.setTotalBlocks(simpleProgress.getTotal());
657 request.setRequiredBlocks(simpleProgress.getRequired());
658 request.setFailedBlocks(simpleProgress.getFailed());
659 request.setFatallyFailedBlocks(simpleProgress.getFatallyFailed());
660 request.setSucceededBlocks(simpleProgress.getSucceeded());
661 request.setFinalizedTotal(simpleProgress.isFinalizedTotal());
668 public void receivedEndListPersistentRequests(FcpConnection fcpConnection, EndListPersistentRequests endListPersistentRequests) {
669 completionLatch.countDown();
672 return requests.values();
680 * Creates a unique request identifier.
683 * The basename of the request
684 * @return The created request identifier
686 private String createIdentifier(String basename) {
687 return basename + "-" + System.currentTimeMillis() + "-" + (int) (Math.random() * Integer.MAX_VALUE);
691 * Implementation of an {@link FcpListener} that can store an
692 * {@link FcpException} and wait for the arrival of a certain command.
694 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
696 private abstract class ExtendedFcpAdapter extends FcpAdapter {
698 /** The count down latch used to wait for completion. */
699 protected final CountDownLatch completionLatch = new CountDownLatch(1);
701 /** The FCP exception, if any. */
702 protected FcpException fcpException;
705 * Creates a new extended FCP adapter.
707 public ExtendedFcpAdapter() {
712 * Executes the FCP commands in {@link #run()}, wrapping the execution
713 * and catching exceptions.
715 * @throws IOException
716 * if an I/O error occurs
717 * @throws FcpException
718 * if an FCP error occurs
720 @SuppressWarnings("synthetic-access")
721 public void execute() throws IOException, FcpException {
722 fcpConnection.addFcpListener(this);
727 completionLatch.await();
729 } catch (InterruptedException ie1) {
730 /* ignore, we’ll loop. */
734 fcpConnection.removeFcpListener(this);
736 if (fcpException != null) {
742 * The FCP commands that actually get executed.
744 * @throws IOException
745 * if an I/O error occurs
747 public abstract void run() throws IOException;
753 public void connectionClosed(FcpConnection fcpConnection, Throwable throwable) {
754 fcpException = new FcpException("Connection closed", throwable);
755 completionLatch.countDown();
762 public void receivedCloseConnectionDuplicateClientName(FcpConnection fcpConnection, CloseConnectionDuplicateClientName closeConnectionDuplicateClientName) {
763 fcpException = new FcpException("Connection closed, duplicate client name");
764 completionLatch.countDown();
771 public void receivedProtocolError(FcpConnection fcpConnection, ProtocolError protocolError) {
772 fcpException = new FcpException("Protocol error (" + protocolError.getCode() + ", " + protocolError.getCodeDescription());
773 completionLatch.countDown();