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.filter.Filter;
64 import net.pterodactylus.util.filter.Filters;
65 import net.pterodactylus.util.thread.ObjectWrapper;
68 * High-level FCP client that hides the details of the underlying FCP
71 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
73 public class FcpClient {
75 /** Object used for synchronization. */
76 private final Object syncObject = new Object();
78 /** The name of this client. */
79 private final String name;
81 /** The underlying FCP connection. */
82 private final FcpConnection fcpConnection;
84 /** Whether the client is currently connected. */
85 private volatile boolean connected;
88 * Creates an FCP client with the given name.
91 * The name of the FCP client
92 * @throws UnknownHostException
93 * if the hostname “localhost” is unknown
95 public FcpClient(String name) throws UnknownHostException {
96 this(name, "localhost");
100 * Creates an FCP client.
103 * The name of the FCP client
105 * The hostname of the Freenet node
106 * @throws UnknownHostException
107 * if the given hostname can not be resolved
109 public FcpClient(String name, String hostname) throws UnknownHostException {
110 this(name, hostname, FcpConnection.DEFAULT_PORT);
114 * Creates an FCP client.
117 * The name of the FCP client
119 * The hostname of the Freenet node
121 * The Freenet node’s FCP port
122 * @throws UnknownHostException
123 * if the given hostname can not be resolved
125 public FcpClient(String name, String hostname, int port) throws UnknownHostException {
126 this(name, InetAddress.getByName(hostname), port);
130 * Creates an FCP client.
133 * The name of the FCP client
135 * The host address of the Freenet node
137 public FcpClient(String name, InetAddress host) {
138 this(name, host, FcpConnection.DEFAULT_PORT);
142 * Creates an FCP client.
145 * The name of the FCP client
147 * The host address of the Freenet node
149 * The Freenet node’s FCP port
151 public FcpClient(String name, InetAddress host, int port) {
153 fcpConnection = new FcpConnection(host, port);
161 * Connects the FCP client.
163 * @throws IOException
164 * if an I/O error occurs
165 * @throws FcpException
166 * if an FCP error occurs
168 public void connect() throws IOException, FcpException {
169 checkConnected(false);
171 new ExtendedFcpAdapter() {
177 @SuppressWarnings("synthetic-access")
178 public void run() throws IOException {
179 fcpConnection.connect();
180 ClientHello clientHello = new ClientHello(name);
181 fcpConnection.sendMessage(clientHello);
182 WatchGlobal watchGlobal = new WatchGlobal(true);
183 fcpConnection.sendMessage(watchGlobal);
190 public void receivedNodeHello(FcpConnection fcpConnection, NodeHello nodeHello) {
191 completionLatch.countDown();
197 * Disconnects the FCP client.
199 public void disconnect() {
200 synchronized (syncObject) {
201 fcpConnection.close();
202 syncObject.notifyAll();
207 * Returns whether this client is currently connected.
209 * @return {@code true} if the client is currently connected, {@code false}
212 public boolean isConnected() {
221 * Returns all peers that the node has.
223 * @param withMetadata
224 * <code>true</code> to include peer metadata
225 * @param withVolatile
226 * <code>true</code> to include volatile peer data
227 * @return A set containing the node’s peers
228 * @throws IOException
229 * if an I/O error occurs
230 * @throws FcpException
231 * if an FCP error occurs
233 public Collection<Peer> getPeers(final boolean withMetadata, final boolean withVolatile) throws IOException, FcpException {
234 final Set<Peer> peers = Collections.synchronizedSet(new HashSet<Peer>());
235 new ExtendedFcpAdapter() {
237 /** The ID of the “ListPeers” request. */
238 @SuppressWarnings("synthetic-access")
239 private String identifier = createIdentifier("list-peers");
245 @SuppressWarnings("synthetic-access")
246 public void run() throws IOException {
247 fcpConnection.sendMessage(new ListPeers(identifier, withMetadata, withVolatile));
254 public void receivedPeer(FcpConnection fcpConnection, Peer peer) {
255 if (peer.getIdentifier().equals(identifier)) {
264 public void receivedEndListPeers(FcpConnection fcpConnection, EndListPeers endListPeers) {
265 if (endListPeers.getIdentifier().equals(identifier)) {
266 completionLatch.countDown();
274 * Returns all darknet peers.
276 * @param withMetadata
277 * <code>true</code> to include peer metadata
278 * @param withVolatile
279 * <code>true</code> to include volatile peer data
280 * @return A set containing the node’s darknet peers
281 * @throws IOException
282 * if an I/O error occurs
283 * @throws FcpException
284 * if an FCP error occurs
286 public Collection<Peer> getDarknetPeers(boolean withMetadata, boolean withVolatile) throws IOException, FcpException {
287 Collection<Peer> allPeers = getPeers(withMetadata, withVolatile);
288 Collection<Peer> darknetPeers = new HashSet<Peer>();
289 for (Peer peer : allPeers) {
290 if (!peer.isOpennet() && !peer.isSeed()) {
291 darknetPeers.add(peer);
298 * Returns all opennet peers.
300 * @param withMetadata
301 * <code>true</code> to include peer metadata
302 * @param withVolatile
303 * <code>true</code> to include volatile peer data
304 * @return A set containing the node’s opennet peers
305 * @throws IOException
306 * if an I/O error occurs
307 * @throws FcpException
308 * if an FCP error occurs
310 public Collection<Peer> getOpennetPeers(boolean withMetadata, boolean withVolatile) throws IOException, FcpException {
311 Collection<Peer> allPeers = getPeers(withMetadata, withVolatile);
312 Collection<Peer> opennetPeers = new HashSet<Peer>();
313 for (Peer peer : allPeers) {
314 if (peer.isOpennet() && !peer.isSeed()) {
315 opennetPeers.add(peer);
322 * Returns all seed peers.
324 * @param withMetadata
325 * <code>true</code> to include peer metadata
326 * @param withVolatile
327 * <code>true</code> to include volatile peer data
328 * @return A set containing the node’s seed peers
329 * @throws IOException
330 * if an I/O error occurs
331 * @throws FcpException
332 * if an FCP error occurs
334 public Collection<Peer> getSeedPeers(boolean withMetadata, boolean withVolatile) throws IOException, FcpException {
335 Collection<Peer> allPeers = getPeers(withMetadata, withVolatile);
336 Collection<Peer> seedPeers = new HashSet<Peer>();
337 for (Peer peer : allPeers) {
346 * Adds the given peer to the node.
350 * @throws IOException
351 * if an I/O error occurs
352 * @throws FcpException
353 * if an FCP error occurs
355 public void addPeer(Peer peer) throws IOException, FcpException {
356 addPeer(peer.getNodeRef());
360 * Adds the peer defined by the noderef to the node.
363 * The noderef that defines the new peer
364 * @throws IOException
365 * if an I/O error occurs
366 * @throws FcpException
367 * if an FCP error occurs
369 public void addPeer(NodeRef nodeRef) throws IOException, FcpException {
370 addPeer(new AddPeer(nodeRef));
374 * Adds a peer, reading the noderef from the given URL.
377 * The URL to read the noderef from
378 * @throws IOException
379 * if an I/O error occurs
380 * @throws FcpException
381 * if an FCP error occurs
383 public void addPeer(URL url) throws IOException, FcpException {
384 addPeer(new AddPeer(url));
388 * Adds a peer, reading the noderef of the peer from the given file.
389 * <strong>Note:</strong> the file to read the noderef from has to reside on
390 * the same machine as the node!
393 * The name of the file containing the peer’s noderef
394 * @throws IOException
395 * if an I/O error occurs
396 * @throws FcpException
397 * if an FCP error occurs
399 public void addPeer(String file) throws IOException, FcpException {
400 addPeer(new AddPeer(file));
404 * Sends the given {@link AddPeer} message to the node. This method should
405 * not be called directly. Use one of {@link #addPeer(Peer)},
406 * {@link #addPeer(NodeRef)}, {@link #addPeer(URL)}, or
407 * {@link #addPeer(String)} instead.
410 * The “AddPeer” message
411 * @throws IOException
412 * if an I/O error occurs
413 * @throws FcpException
414 * if an FCP error occurs
416 private void addPeer(final AddPeer addPeer) throws IOException, FcpException {
417 new ExtendedFcpAdapter() {
423 @SuppressWarnings("synthetic-access")
424 public void run() throws IOException {
425 fcpConnection.sendMessage(addPeer);
432 public void receivedPeer(FcpConnection fcpConnection, Peer peer) {
433 completionLatch.countDown();
439 * Modifies the given peer.
443 * @param allowLocalAddresses
444 * <code>true</code> to allow local address, <code>false</code>
445 * to not allow local address, <code>null</code> to not change
448 * <code>true</code> to disable the peer, <code>false</code> to
449 * enable the peer, <code>null</code> to not change the setting
451 * <code>true</code> to enable “listen only” for the peer,
452 * <code>false</code> to disable it, <code>null</code> to not
454 * @throws IOException
455 * if an I/O error occurs
456 * @throws FcpException
457 * if an FCP error occurs
459 public void modifyPeer(final Peer peer, final Boolean allowLocalAddresses, final Boolean disabled, final Boolean listenOnly) throws IOException, FcpException {
460 new ExtendedFcpAdapter() {
466 @SuppressWarnings("synthetic-access")
467 public void run() throws IOException {
468 fcpConnection.sendMessage(new ModifyPeer(peer.getIdentity(), allowLocalAddresses, disabled, listenOnly));
475 public void receivedPeer(FcpConnection fcpConnection, Peer peer) {
476 completionLatch.countDown();
482 * Removes the given peer.
486 * @throws IOException
487 * if an I/O error occurs
488 * @throws FcpException
489 * if an FCP error occurs
491 public void removePeer(final Peer peer) throws IOException, FcpException {
492 new ExtendedFcpAdapter() {
498 @SuppressWarnings("synthetic-access")
499 public void run() throws IOException {
500 fcpConnection.sendMessage(new RemovePeer(peer.getIdentity()));
507 public void receivedPeerRemoved(FcpConnection fcpConnection, PeerRemoved peerRemoved) {
508 completionLatch.countDown();
514 // PEER NOTES MANAGEMENT
518 * Returns the peer note of the given peer.
521 * The peer to get the note for
522 * @return The peer’s note
523 * @throws IOException
524 * if an I/O error occurs
525 * @throws FcpException
526 * if an FCP error occurs
528 public PeerNote getPeerNote(final Peer peer) throws IOException, FcpException {
529 final ObjectWrapper<PeerNote> objectWrapper = new ObjectWrapper<PeerNote>();
530 new ExtendedFcpAdapter() {
536 @SuppressWarnings("synthetic-access")
537 public void run() throws IOException {
538 fcpConnection.sendMessage(new ListPeerNotes(peer.getIdentity()));
545 public void receivedPeerNote(FcpConnection fcpConnection, PeerNote peerNote) {
546 if (peerNote.getNodeIdentifier().equals(peer.getIdentity())) {
547 objectWrapper.set(peerNote);
555 public void receivedEndListPeerNotes(FcpConnection fcpConnection, EndListPeerNotes endListPeerNotes) {
556 completionLatch.countDown();
559 return objectWrapper.get();
563 * Replaces the peer note for the given peer.
568 * The new base64-encoded note text
570 * The type of the note (currently only <code>1</code> is
572 * @throws IOException
573 * if an I/O error occurs
574 * @throws FcpException
575 * if an FCP error occurs
577 public void modifyPeerNote(final Peer peer, final String noteText, final int noteType) throws IOException, FcpException {
578 new ExtendedFcpAdapter() {
584 @SuppressWarnings("synthetic-access")
585 public void run() throws IOException {
586 fcpConnection.sendMessage(new ModifyPeerNote(peer.getIdentity(), noteText, noteType));
593 public void receivedPeer(FcpConnection fcpConnection, Peer receivedPeer) {
594 if (receivedPeer.getIdentity().equals(peer.getIdentity())) {
595 completionLatch.countDown();
606 * Generates a new SSK key pair.
608 * @return The generated key pair
609 * @throws IOException
610 * if an I/O error occurs
611 * @throws FcpException
612 * if an FCP error occurs
614 public SSKKeypair generateKeyPair() throws IOException, FcpException {
615 final ObjectWrapper<SSKKeypair> sskKeypairWrapper = new ObjectWrapper<SSKKeypair>();
616 new ExtendedFcpAdapter() {
622 @SuppressWarnings("synthetic-access")
623 public void run() throws IOException {
624 fcpConnection.sendMessage(new GenerateSSK());
631 public void receivedSSKKeypair(FcpConnection fcpConnection, SSKKeypair sskKeypair) {
632 sskKeypairWrapper.set(sskKeypair);
633 completionLatch.countDown();
636 return sskKeypairWrapper.get();
640 // REQUEST MANAGEMENT
644 * Returns all currently visible persistent get requests.
647 * <code>true</code> to return get requests from the global
648 * queue, <code>false</code> to only show requests from the
650 * @return All get requests
651 * @throws IOException
652 * if an I/O error occurs
653 * @throws FcpException
654 * if an FCP error occurs
656 public Collection<Request> getGetRequests(final boolean global) throws IOException, FcpException {
657 return Filters.filteredCollection(getRequests(global), new Filter<Request>() {
662 public boolean filterObject(Request request) {
663 return request instanceof GetRequest;
669 * Returns all currently visible persistent put requests.
672 * <code>true</code> to return put requests from the global
673 * queue, <code>false</code> to only show requests from the
675 * @return All put requests
676 * @throws IOException
677 * if an I/O error occurs
678 * @throws FcpException
679 * if an FCP error occurs
681 public Collection<Request> getPutRequests(final boolean global) throws IOException, FcpException {
682 return Filters.filteredCollection(getRequests(global), new Filter<Request>() {
687 public boolean filterObject(Request request) {
688 return request instanceof PutRequest;
694 * Returns all currently visible persistent requests.
697 * <code>true</code> to return requests from the global queue,
698 * <code>false</code> to only show requests from the client-local
700 * @return All requests
701 * @throws IOException
702 * if an I/O error occurs
703 * @throws FcpException
704 * if an FCP error occurs
706 public Collection<Request> getRequests(final boolean global) throws IOException, FcpException {
707 final Map<String, Request> requests = Collections.synchronizedMap(new HashMap<String, Request>());
708 new ExtendedFcpAdapter() {
714 @SuppressWarnings("synthetic-access")
715 public void run() throws IOException {
716 fcpConnection.sendMessage(new ListPersistentRequests());
723 public void receivedPersistentGet(FcpConnection fcpConnection, PersistentGet persistentGet) {
724 if (!persistentGet.isGlobal() || global) {
725 GetRequest getRequest = new GetRequest(persistentGet);
726 requests.put(persistentGet.getIdentifier(), getRequest);
733 * @see net.pterodactylus.fcp.FcpAdapter#receivedDataFound(net.pterodactylus.fcp.FcpConnection,
734 * net.pterodactylus.fcp.DataFound)
737 public void receivedDataFound(FcpConnection fcpConnection, DataFound dataFound) {
738 Request getRequest = requests.get(dataFound.getIdentifier());
739 if (getRequest == null) {
742 getRequest.setComplete(true);
743 getRequest.setLength(dataFound.getDataLength());
744 getRequest.setContentType(dataFound.getMetadataContentType());
750 * @see net.pterodactylus.fcp.FcpAdapter#receivedGetFailed(net.pterodactylus.fcp.FcpConnection,
751 * net.pterodactylus.fcp.GetFailed)
754 public void receivedGetFailed(FcpConnection fcpConnection, GetFailed getFailed) {
755 Request getRequest = requests.get(getFailed.getIdentifier());
756 if (getRequest == null) {
759 getRequest.setComplete(true);
760 getRequest.setFailed(true);
761 getRequest.setFatal(getFailed.isFatal());
762 getRequest.setErrorCode(getFailed.getCode());
768 * @see net.pterodactylus.fcp.FcpAdapter#receivedPersistentPut(net.pterodactylus.fcp.FcpConnection,
769 * net.pterodactylus.fcp.PersistentPut)
772 public void receivedPersistentPut(FcpConnection fcpConnection, PersistentPut persistentPut) {
773 if (!persistentPut.isGlobal() || global) {
774 PutRequest putRequest = new PutRequest(persistentPut);
775 requests.put(persistentPut.getIdentifier(), putRequest);
782 * @see net.pterodactylus.fcp.FcpAdapter#receivedSimpleProgress(net.pterodactylus.fcp.FcpConnection,
783 * net.pterodactylus.fcp.SimpleProgress)
786 public void receivedSimpleProgress(FcpConnection fcpConnection, SimpleProgress simpleProgress) {
787 Request request = requests.get(simpleProgress.getIdentifier());
788 if (request == null) {
791 request.setTotalBlocks(simpleProgress.getTotal());
792 request.setRequiredBlocks(simpleProgress.getRequired());
793 request.setFailedBlocks(simpleProgress.getFailed());
794 request.setFatallyFailedBlocks(simpleProgress.getFatallyFailed());
795 request.setSucceededBlocks(simpleProgress.getSucceeded());
796 request.setFinalizedTotal(simpleProgress.isFinalizedTotal());
803 public void receivedEndListPersistentRequests(FcpConnection fcpConnection, EndListPersistentRequests endListPersistentRequests) {
804 completionLatch.countDown();
807 return requests.values();
815 * Creates a unique request identifier.
818 * The basename of the request
819 * @return The created request identifier
821 private String createIdentifier(String basename) {
822 return basename + "-" + System.currentTimeMillis() + "-" + (int) (Math.random() * Integer.MAX_VALUE);
826 * Checks whether the connection is in the required state.
829 * The required connection state
830 * @throws FcpException
831 * if the connection is not in the required state
833 private void checkConnected(boolean connected) throws FcpException {
834 if (this.connected != connected) {
835 throw new FcpException("Client is " + (connected ? "not" : "already") + " connected.");
840 * Tells the client that it is now disconnected. This method is called by
841 * {@link ExtendedFcpAdapter} only.
843 private void setDisconnected() {
848 * Implementation of an {@link FcpListener} that can store an
849 * {@link FcpException} and wait for the arrival of a certain command.
851 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
853 private abstract class ExtendedFcpAdapter extends FcpAdapter {
855 /** The count down latch used to wait for completion. */
856 protected final CountDownLatch completionLatch = new CountDownLatch(1);
858 /** The FCP exception, if any. */
859 protected FcpException fcpException;
862 * Creates a new extended FCP adapter.
864 public ExtendedFcpAdapter() {
869 * Executes the FCP commands in {@link #run()}, wrapping the execution
870 * and catching exceptions.
872 * @throws IOException
873 * if an I/O error occurs
874 * @throws FcpException
875 * if an FCP error occurs
877 @SuppressWarnings("synthetic-access")
878 public void execute() throws IOException, FcpException {
879 checkConnected(true);
880 fcpConnection.addFcpListener(this);
885 completionLatch.await();
887 } catch (InterruptedException ie1) {
888 /* ignore, we’ll loop. */
891 } catch (IOException ioe1) {
895 fcpConnection.removeFcpListener(this);
897 if (fcpException != null) {
904 * The FCP commands that actually get executed.
906 * @throws IOException
907 * if an I/O error occurs
909 public abstract void run() throws IOException;
915 public void connectionClosed(FcpConnection fcpConnection, Throwable throwable) {
916 fcpException = new FcpException("Connection closed", throwable);
917 completionLatch.countDown();
924 public void receivedCloseConnectionDuplicateClientName(FcpConnection fcpConnection, CloseConnectionDuplicateClientName closeConnectionDuplicateClientName) {
925 fcpException = new FcpException("Connection closed, duplicate client name");
926 completionLatch.countDown();
933 public void receivedProtocolError(FcpConnection fcpConnection, ProtocolError protocolError) {
934 fcpException = new FcpException("Protocol error (" + protocolError.getCode() + ", " + protocolError.getCodeDescription());
935 completionLatch.countDown();