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.io.InputStream;
24 import java.net.InetAddress;
26 import java.net.UnknownHostException;
27 import java.util.Collection;
28 import java.util.Collections;
29 import java.util.HashMap;
30 import java.util.HashSet;
33 import java.util.Map.Entry;
34 import java.util.concurrent.CountDownLatch;
36 import net.pterodactylus.fcp.AddPeer;
37 import net.pterodactylus.fcp.ClientHello;
38 import net.pterodactylus.fcp.CloseConnectionDuplicateClientName;
39 import net.pterodactylus.fcp.DataFound;
40 import net.pterodactylus.fcp.EndListPeerNotes;
41 import net.pterodactylus.fcp.EndListPeers;
42 import net.pterodactylus.fcp.EndListPersistentRequests;
43 import net.pterodactylus.fcp.FCPPluginMessage;
44 import net.pterodactylus.fcp.FCPPluginReply;
45 import net.pterodactylus.fcp.FcpAdapter;
46 import net.pterodactylus.fcp.FcpConnection;
47 import net.pterodactylus.fcp.FcpListener;
48 import net.pterodactylus.fcp.GenerateSSK;
49 import net.pterodactylus.fcp.GetFailed;
50 import net.pterodactylus.fcp.GetNode;
51 import net.pterodactylus.fcp.ListPeerNotes;
52 import net.pterodactylus.fcp.ListPeers;
53 import net.pterodactylus.fcp.ListPersistentRequests;
54 import net.pterodactylus.fcp.ModifyPeer;
55 import net.pterodactylus.fcp.ModifyPeerNote;
56 import net.pterodactylus.fcp.NodeData;
57 import net.pterodactylus.fcp.NodeHello;
58 import net.pterodactylus.fcp.NodeRef;
59 import net.pterodactylus.fcp.Peer;
60 import net.pterodactylus.fcp.PeerNote;
61 import net.pterodactylus.fcp.PeerRemoved;
62 import net.pterodactylus.fcp.PersistentGet;
63 import net.pterodactylus.fcp.PersistentPut;
64 import net.pterodactylus.fcp.ProtocolError;
65 import net.pterodactylus.fcp.RemovePeer;
66 import net.pterodactylus.fcp.SSKKeypair;
67 import net.pterodactylus.fcp.SimpleProgress;
68 import net.pterodactylus.fcp.WatchGlobal;
69 import net.pterodactylus.util.filter.Filter;
70 import net.pterodactylus.util.filter.Filters;
71 import net.pterodactylus.util.thread.ObjectWrapper;
74 * High-level FCP client that hides the details of the underlying FCP
77 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
79 public class FcpClient {
81 /** Object used for synchronization. */
82 private final Object syncObject = new Object();
84 /** Listener management. */
85 private final FcpClientListenerManager fcpClientListenerManager = new FcpClientListenerManager(this);
87 /** The name of this client. */
88 private final String name;
90 /** The underlying FCP connection. */
91 private final FcpConnection fcpConnection;
93 /** Whether the client is currently connected. */
94 private volatile boolean connected;
97 * Creates an FCP client with the given name.
100 * The name of the FCP client
101 * @throws UnknownHostException
102 * if the hostname “localhost” is unknown
104 public FcpClient(String name) throws UnknownHostException {
105 this(name, "localhost");
109 * Creates an FCP client.
112 * The name of the FCP client
114 * The hostname of the Freenet node
115 * @throws UnknownHostException
116 * if the given hostname can not be resolved
118 public FcpClient(String name, String hostname) throws UnknownHostException {
119 this(name, hostname, FcpConnection.DEFAULT_PORT);
123 * Creates an FCP client.
126 * The name of the FCP client
128 * The hostname of the Freenet node
130 * The Freenet node’s FCP port
131 * @throws UnknownHostException
132 * if the given hostname can not be resolved
134 public FcpClient(String name, String hostname, int port) throws UnknownHostException {
135 this(name, InetAddress.getByName(hostname), port);
139 * Creates an FCP client.
142 * The name of the FCP client
144 * The host address of the Freenet node
146 public FcpClient(String name, InetAddress host) {
147 this(name, host, FcpConnection.DEFAULT_PORT);
151 * Creates an FCP client.
154 * The name of the FCP client
156 * The host address of the Freenet node
158 * The Freenet node’s FCP port
160 public FcpClient(String name, InetAddress host, int port) {
162 fcpConnection = new FcpConnection(host, port);
163 fcpConnection.addFcpListener(new FcpAdapter() {
169 @SuppressWarnings("synthetic-access")
170 public void connectionClosed(FcpConnection fcpConnection, Throwable throwable) {
172 fcpClientListenerManager.fireFcpClientDisconnected();
178 // LISTENER MANAGEMENT
182 * Adds an FCP listener to the underlying connection.
185 * The FCP listener to add
187 public void addFcpListener(FcpListener fcpListener) {
188 fcpConnection.addFcpListener(fcpListener);
192 * Removes an FCP listener from the underlying connection.
195 * The FCP listener to remove
197 public void removeFcpListener(FcpListener fcpListener) {
198 fcpConnection.removeFcpListener(fcpListener);
202 * Adds an FCP client listener to the list of registered listeners.
204 * @param fcpClientListener
205 * The FCP client listener to add
207 public void addFcpClientListener(FcpClientListener fcpClientListener) {
208 fcpClientListenerManager.addListener(fcpClientListener);
212 * Removes an FCP client listener from the list of registered listeners.
214 * @param fcpClientListener
215 * The FCP client listener to remove
217 public void removeFcpClientListener(FcpClientListener fcpClientListener) {
218 fcpClientListenerManager.removeListener(fcpClientListener);
226 * Connects the FCP client.
228 * @throws IOException
229 * if an I/O error occurs
230 * @throws FcpException
231 * if an FCP error occurs
233 public void connect() throws IOException, FcpException {
234 checkConnected(false);
236 new ExtendedFcpAdapter() {
242 @SuppressWarnings("synthetic-access")
243 public void run() throws IOException {
244 fcpConnection.connect();
245 ClientHello clientHello = new ClientHello(name);
246 fcpConnection.sendMessage(clientHello);
247 WatchGlobal watchGlobal = new WatchGlobal(true);
248 fcpConnection.sendMessage(watchGlobal);
255 public void receivedNodeHello(FcpConnection fcpConnection, NodeHello nodeHello) {
256 completionLatch.countDown();
262 * Disconnects the FCP client.
264 public void disconnect() {
265 synchronized (syncObject) {
266 fcpConnection.close();
267 syncObject.notifyAll();
272 * Returns whether this client is currently connected.
274 * @return {@code true} if the client is currently connected, {@code false}
277 public boolean isConnected() {
286 * Returns all peers that the node has.
288 * @param withMetadata
289 * <code>true</code> to include peer metadata
290 * @param withVolatile
291 * <code>true</code> to include volatile peer data
292 * @return A set containing the node’s peers
293 * @throws IOException
294 * if an I/O error occurs
295 * @throws FcpException
296 * if an FCP error occurs
298 public Collection<Peer> getPeers(final boolean withMetadata, final boolean withVolatile) throws IOException, FcpException {
299 final Set<Peer> peers = Collections.synchronizedSet(new HashSet<Peer>());
300 new ExtendedFcpAdapter() {
302 /** The ID of the “ListPeers” request. */
303 @SuppressWarnings("synthetic-access")
304 private String identifier = createIdentifier("list-peers");
310 @SuppressWarnings("synthetic-access")
311 public void run() throws IOException {
312 fcpConnection.sendMessage(new ListPeers(identifier, withMetadata, withVolatile));
319 public void receivedPeer(FcpConnection fcpConnection, Peer peer) {
320 if (peer.getIdentifier().equals(identifier)) {
329 public void receivedEndListPeers(FcpConnection fcpConnection, EndListPeers endListPeers) {
330 if (endListPeers.getIdentifier().equals(identifier)) {
331 completionLatch.countDown();
339 * Returns all darknet peers.
341 * @param withMetadata
342 * <code>true</code> to include peer metadata
343 * @param withVolatile
344 * <code>true</code> to include volatile peer data
345 * @return A set containing the node’s darknet peers
346 * @throws IOException
347 * if an I/O error occurs
348 * @throws FcpException
349 * if an FCP error occurs
351 public Collection<Peer> getDarknetPeers(boolean withMetadata, boolean withVolatile) throws IOException, FcpException {
352 Collection<Peer> allPeers = getPeers(withMetadata, withVolatile);
353 Collection<Peer> darknetPeers = new HashSet<Peer>();
354 for (Peer peer : allPeers) {
355 if (!peer.isOpennet() && !peer.isSeed()) {
356 darknetPeers.add(peer);
363 * Returns all opennet peers.
365 * @param withMetadata
366 * <code>true</code> to include peer metadata
367 * @param withVolatile
368 * <code>true</code> to include volatile peer data
369 * @return A set containing the node’s opennet peers
370 * @throws IOException
371 * if an I/O error occurs
372 * @throws FcpException
373 * if an FCP error occurs
375 public Collection<Peer> getOpennetPeers(boolean withMetadata, boolean withVolatile) throws IOException, FcpException {
376 Collection<Peer> allPeers = getPeers(withMetadata, withVolatile);
377 Collection<Peer> opennetPeers = new HashSet<Peer>();
378 for (Peer peer : allPeers) {
379 if (peer.isOpennet() && !peer.isSeed()) {
380 opennetPeers.add(peer);
387 * Returns all seed peers.
389 * @param withMetadata
390 * <code>true</code> to include peer metadata
391 * @param withVolatile
392 * <code>true</code> to include volatile peer data
393 * @return A set containing the node’s seed peers
394 * @throws IOException
395 * if an I/O error occurs
396 * @throws FcpException
397 * if an FCP error occurs
399 public Collection<Peer> getSeedPeers(boolean withMetadata, boolean withVolatile) throws IOException, FcpException {
400 Collection<Peer> allPeers = getPeers(withMetadata, withVolatile);
401 Collection<Peer> seedPeers = new HashSet<Peer>();
402 for (Peer peer : allPeers) {
411 * Adds the given peer to the node.
415 * @throws IOException
416 * if an I/O error occurs
417 * @throws FcpException
418 * if an FCP error occurs
420 public void addPeer(Peer peer) throws IOException, FcpException {
421 addPeer(peer.getNodeRef());
425 * Adds the peer defined by the noderef to the node.
428 * The noderef that defines the new peer
429 * @throws IOException
430 * if an I/O error occurs
431 * @throws FcpException
432 * if an FCP error occurs
434 public void addPeer(NodeRef nodeRef) throws IOException, FcpException {
435 addPeer(new AddPeer(nodeRef));
439 * Adds a peer, reading the noderef from the given URL.
442 * The URL to read the noderef from
443 * @throws IOException
444 * if an I/O error occurs
445 * @throws FcpException
446 * if an FCP error occurs
448 public void addPeer(URL url) throws IOException, FcpException {
449 addPeer(new AddPeer(url));
453 * Adds a peer, reading the noderef of the peer from the given file.
454 * <strong>Note:</strong> the file to read the noderef from has to reside on
455 * the same machine as the node!
458 * The name of the file containing the peer’s noderef
459 * @throws IOException
460 * if an I/O error occurs
461 * @throws FcpException
462 * if an FCP error occurs
464 public void addPeer(String file) throws IOException, FcpException {
465 addPeer(new AddPeer(file));
469 * Sends the given {@link AddPeer} message to the node. This method should
470 * not be called directly. Use one of {@link #addPeer(Peer)},
471 * {@link #addPeer(NodeRef)}, {@link #addPeer(URL)}, or
472 * {@link #addPeer(String)} instead.
475 * The “AddPeer” message
476 * @throws IOException
477 * if an I/O error occurs
478 * @throws FcpException
479 * if an FCP error occurs
481 private void addPeer(final AddPeer addPeer) throws IOException, FcpException {
482 new ExtendedFcpAdapter() {
488 @SuppressWarnings("synthetic-access")
489 public void run() throws IOException {
490 fcpConnection.sendMessage(addPeer);
497 public void receivedPeer(FcpConnection fcpConnection, Peer peer) {
498 completionLatch.countDown();
504 * Modifies the given peer.
508 * @param allowLocalAddresses
509 * <code>true</code> to allow local address, <code>false</code>
510 * to not allow local address, <code>null</code> to not change
513 * <code>true</code> to disable the peer, <code>false</code> to
514 * enable the peer, <code>null</code> to not change the setting
516 * <code>true</code> to enable “listen only” for the peer,
517 * <code>false</code> to disable it, <code>null</code> to not
519 * @throws IOException
520 * if an I/O error occurs
521 * @throws FcpException
522 * if an FCP error occurs
524 public void modifyPeer(final Peer peer, final Boolean allowLocalAddresses, final Boolean disabled, final Boolean listenOnly) throws IOException, FcpException {
525 new ExtendedFcpAdapter() {
531 @SuppressWarnings("synthetic-access")
532 public void run() throws IOException {
533 fcpConnection.sendMessage(new ModifyPeer(peer.getIdentity(), allowLocalAddresses, disabled, listenOnly));
540 public void receivedPeer(FcpConnection fcpConnection, Peer peer) {
541 completionLatch.countDown();
547 * Removes the given peer.
551 * @throws IOException
552 * if an I/O error occurs
553 * @throws FcpException
554 * if an FCP error occurs
556 public void removePeer(final Peer peer) throws IOException, FcpException {
557 new ExtendedFcpAdapter() {
563 @SuppressWarnings("synthetic-access")
564 public void run() throws IOException {
565 fcpConnection.sendMessage(new RemovePeer(peer.getIdentity()));
572 public void receivedPeerRemoved(FcpConnection fcpConnection, PeerRemoved peerRemoved) {
573 completionLatch.countDown();
579 // PEER NOTES MANAGEMENT
583 * Returns the peer note of the given peer.
586 * The peer to get the note for
587 * @return The peer’s note
588 * @throws IOException
589 * if an I/O error occurs
590 * @throws FcpException
591 * if an FCP error occurs
593 public PeerNote getPeerNote(final Peer peer) throws IOException, FcpException {
594 final ObjectWrapper<PeerNote> objectWrapper = new ObjectWrapper<PeerNote>();
595 new ExtendedFcpAdapter() {
601 @SuppressWarnings("synthetic-access")
602 public void run() throws IOException {
603 fcpConnection.sendMessage(new ListPeerNotes(peer.getIdentity()));
610 public void receivedPeerNote(FcpConnection fcpConnection, PeerNote peerNote) {
611 if (peerNote.getNodeIdentifier().equals(peer.getIdentity())) {
612 objectWrapper.set(peerNote);
620 public void receivedEndListPeerNotes(FcpConnection fcpConnection, EndListPeerNotes endListPeerNotes) {
621 completionLatch.countDown();
624 return objectWrapper.get();
628 * Replaces the peer note for the given peer.
633 * The new base64-encoded note text
635 * The type of the note (currently only <code>1</code> is
637 * @throws IOException
638 * if an I/O error occurs
639 * @throws FcpException
640 * if an FCP error occurs
642 public void modifyPeerNote(final Peer peer, final String noteText, final int noteType) throws IOException, FcpException {
643 new ExtendedFcpAdapter() {
649 @SuppressWarnings("synthetic-access")
650 public void run() throws IOException {
651 fcpConnection.sendMessage(new ModifyPeerNote(peer.getIdentity(), noteText, noteType));
658 public void receivedPeer(FcpConnection fcpConnection, Peer receivedPeer) {
659 if (receivedPeer.getIdentity().equals(peer.getIdentity())) {
660 completionLatch.countDown();
671 * Generates a new SSK key pair.
673 * @return The generated key pair
674 * @throws IOException
675 * if an I/O error occurs
676 * @throws FcpException
677 * if an FCP error occurs
679 public SSKKeypair generateKeyPair() throws IOException, FcpException {
680 final ObjectWrapper<SSKKeypair> sskKeypairWrapper = new ObjectWrapper<SSKKeypair>();
681 new ExtendedFcpAdapter() {
687 @SuppressWarnings("synthetic-access")
688 public void run() throws IOException {
689 fcpConnection.sendMessage(new GenerateSSK());
696 public void receivedSSKKeypair(FcpConnection fcpConnection, SSKKeypair sskKeypair) {
697 sskKeypairWrapper.set(sskKeypair);
698 completionLatch.countDown();
701 return sskKeypairWrapper.get();
705 // REQUEST MANAGEMENT
709 * Returns all currently visible persistent get requests.
712 * <code>true</code> to return get requests from the global
713 * queue, <code>false</code> to only show requests from the
715 * @return All get requests
716 * @throws IOException
717 * if an I/O error occurs
718 * @throws FcpException
719 * if an FCP error occurs
721 public Collection<Request> getGetRequests(final boolean global) throws IOException, FcpException {
722 return Filters.filteredCollection(getRequests(global), new Filter<Request>() {
727 public boolean filterObject(Request request) {
728 return request instanceof GetRequest;
734 * Returns all currently visible persistent put requests.
737 * <code>true</code> to return put requests from the global
738 * queue, <code>false</code> to only show requests from the
740 * @return All put requests
741 * @throws IOException
742 * if an I/O error occurs
743 * @throws FcpException
744 * if an FCP error occurs
746 public Collection<Request> getPutRequests(final boolean global) throws IOException, FcpException {
747 return Filters.filteredCollection(getRequests(global), new Filter<Request>() {
752 public boolean filterObject(Request request) {
753 return request instanceof PutRequest;
759 * Returns all currently visible persistent requests.
762 * <code>true</code> to return requests from the global queue,
763 * <code>false</code> to only show requests from the client-local
765 * @return All requests
766 * @throws IOException
767 * if an I/O error occurs
768 * @throws FcpException
769 * if an FCP error occurs
771 public Collection<Request> getRequests(final boolean global) throws IOException, FcpException {
772 final Map<String, Request> requests = Collections.synchronizedMap(new HashMap<String, Request>());
773 new ExtendedFcpAdapter() {
779 @SuppressWarnings("synthetic-access")
780 public void run() throws IOException {
781 fcpConnection.sendMessage(new ListPersistentRequests());
788 public void receivedPersistentGet(FcpConnection fcpConnection, PersistentGet persistentGet) {
789 if (!persistentGet.isGlobal() || global) {
790 GetRequest getRequest = new GetRequest(persistentGet);
791 requests.put(persistentGet.getIdentifier(), getRequest);
798 * @see net.pterodactylus.fcp.FcpAdapter#receivedDataFound(net.pterodactylus.fcp.FcpConnection,
799 * net.pterodactylus.fcp.DataFound)
802 public void receivedDataFound(FcpConnection fcpConnection, DataFound dataFound) {
803 Request getRequest = requests.get(dataFound.getIdentifier());
804 if (getRequest == null) {
807 getRequest.setComplete(true);
808 getRequest.setLength(dataFound.getDataLength());
809 getRequest.setContentType(dataFound.getMetadataContentType());
815 * @see net.pterodactylus.fcp.FcpAdapter#receivedGetFailed(net.pterodactylus.fcp.FcpConnection,
816 * net.pterodactylus.fcp.GetFailed)
819 public void receivedGetFailed(FcpConnection fcpConnection, GetFailed getFailed) {
820 Request getRequest = requests.get(getFailed.getIdentifier());
821 if (getRequest == null) {
824 getRequest.setComplete(true);
825 getRequest.setFailed(true);
826 getRequest.setFatal(getFailed.isFatal());
827 getRequest.setErrorCode(getFailed.getCode());
833 * @see net.pterodactylus.fcp.FcpAdapter#receivedPersistentPut(net.pterodactylus.fcp.FcpConnection,
834 * net.pterodactylus.fcp.PersistentPut)
837 public void receivedPersistentPut(FcpConnection fcpConnection, PersistentPut persistentPut) {
838 if (!persistentPut.isGlobal() || global) {
839 PutRequest putRequest = new PutRequest(persistentPut);
840 requests.put(persistentPut.getIdentifier(), putRequest);
847 * @see net.pterodactylus.fcp.FcpAdapter#receivedSimpleProgress(net.pterodactylus.fcp.FcpConnection,
848 * net.pterodactylus.fcp.SimpleProgress)
851 public void receivedSimpleProgress(FcpConnection fcpConnection, SimpleProgress simpleProgress) {
852 Request request = requests.get(simpleProgress.getIdentifier());
853 if (request == null) {
856 request.setTotalBlocks(simpleProgress.getTotal());
857 request.setRequiredBlocks(simpleProgress.getRequired());
858 request.setFailedBlocks(simpleProgress.getFailed());
859 request.setFatallyFailedBlocks(simpleProgress.getFatallyFailed());
860 request.setSucceededBlocks(simpleProgress.getSucceeded());
861 request.setFinalizedTotal(simpleProgress.isFinalizedTotal());
868 public void receivedEndListPersistentRequests(FcpConnection fcpConnection, EndListPersistentRequests endListPersistentRequests) {
869 completionLatch.countDown();
872 return requests.values();
876 * Sends a message to a plugin and waits for the response.
879 * The name of the plugin class
881 * The parameters for the plugin
882 * @return The responses from the plugin
883 * @throws FcpException
884 * if an FCP error occurs
885 * @throws IOException
886 * if an I/O error occurs
888 public Map<String, String> sendPluginMessage(String pluginClass, Map<String, String> parameters) throws IOException, FcpException {
889 return sendPluginMessage(pluginClass, parameters, 0, null);
893 * Sends a message to a plugin and waits for the response.
896 * The name of the plugin class
898 * The parameters for the plugin
900 * The length of the optional data stream, or {@code 0} if there
901 * is no optional data stream
902 * @param dataInputStream
903 * The input stream for the payload, or {@code null} if there is
905 * @return The responses from the plugin
906 * @throws FcpException
907 * if an FCP error occurs
908 * @throws IOException
909 * if an I/O error occurs
911 public Map<String, String> sendPluginMessage(final String pluginClass, final Map<String, String> parameters, final long dataLength, final InputStream dataInputStream) throws IOException, FcpException {
912 final Map<String, String> pluginReplies = Collections.synchronizedMap(new HashMap<String, String>());
913 new ExtendedFcpAdapter() {
915 @SuppressWarnings("synthetic-access")
916 private final String identifier = createIdentifier("FCPPluginMessage");
919 @SuppressWarnings("synthetic-access")
920 public void run() throws IOException {
921 FCPPluginMessage fcpPluginMessage = new FCPPluginMessage(pluginClass);
922 for (Entry<String, String> parameter : parameters.entrySet()) {
923 fcpPluginMessage.setParameter(parameter.getKey(), parameter.getValue());
925 fcpPluginMessage.setIdentifier(identifier);
926 if ((dataLength > 0) && (dataInputStream != null)) {
927 fcpPluginMessage.setDataLength(dataLength);
928 fcpPluginMessage.setPayloadInputStream(dataInputStream);
930 fcpConnection.sendMessage(fcpPluginMessage);
937 public void receivedFCPPluginReply(FcpConnection fcpConnection, FCPPluginReply fcpPluginReply) {
938 if (!fcpPluginReply.getIdentifier().equals(identifier)) {
941 pluginReplies.putAll(fcpPluginReply.getReplies());
942 completionLatch.countDown();
946 return pluginReplies;
954 * Returns information about the node.
956 * @param giveOpennetRef
957 * Whether to return the OpenNet reference
959 * Whether to return private node data
960 * @param withVolatile
961 * Whether to return volatile node data
962 * @return Node information
963 * @throws FcpException
964 * if an FCP error occurs
965 * @throws IOException
966 * if an I/O error occurs
968 public NodeData getNodeInformation(final Boolean giveOpennetRef, final Boolean withPrivate, final Boolean withVolatile) throws IOException, FcpException {
969 final ObjectWrapper<NodeData> nodeDataWrapper = new ObjectWrapper<NodeData>();
970 new ExtendedFcpAdapter() {
973 @SuppressWarnings("synthetic-access")
974 public void run() throws IOException {
975 GetNode getNodeMessage = new GetNode(giveOpennetRef, withPrivate, withVolatile);
976 fcpConnection.sendMessage(getNodeMessage);
983 public void receivedNodeData(FcpConnection fcpConnection, NodeData nodeData) {
984 nodeDataWrapper.set(nodeData);
985 completionLatch.countDown();
988 return nodeDataWrapper.get();
996 * Creates a unique request identifier.
999 * The basename of the request
1000 * @return The created request identifier
1002 private String createIdentifier(String basename) {
1003 return basename + "-" + System.currentTimeMillis() + "-" + (int) (Math.random() * Integer.MAX_VALUE);
1007 * Checks whether the connection is in the required state.
1010 * The required connection state
1011 * @throws FcpException
1012 * if the connection is not in the required state
1014 private void checkConnected(boolean connected) throws FcpException {
1015 if (this.connected != connected) {
1016 throw new FcpException("Client is " + (connected ? "not" : "already") + " connected.");
1021 * Tells the client that it is now disconnected. This method is called by
1022 * {@link ExtendedFcpAdapter} only.
1024 private void setDisconnected() {
1029 * Implementation of an {@link FcpListener} that can store an
1030 * {@link FcpException} and wait for the arrival of a certain command.
1032 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
1034 private abstract class ExtendedFcpAdapter extends FcpAdapter {
1036 /** The count down latch used to wait for completion. */
1037 protected final CountDownLatch completionLatch = new CountDownLatch(1);
1039 /** The FCP exception, if any. */
1040 protected FcpException fcpException;
1043 * Creates a new extended FCP adapter.
1045 public ExtendedFcpAdapter() {
1050 * Executes the FCP commands in {@link #run()}, wrapping the execution
1051 * and catching exceptions.
1053 * @throws IOException
1054 * if an I/O error occurs
1055 * @throws FcpException
1056 * if an FCP error occurs
1058 @SuppressWarnings("synthetic-access")
1059 public void execute() throws IOException, FcpException {
1060 checkConnected(true);
1061 fcpConnection.addFcpListener(this);
1066 completionLatch.await();
1068 } catch (InterruptedException ie1) {
1069 /* ignore, we’ll loop. */
1072 } catch (IOException ioe1) {
1076 fcpConnection.removeFcpListener(this);
1078 if (fcpException != null) {
1085 * The FCP commands that actually get executed.
1087 * @throws IOException
1088 * if an I/O error occurs
1090 public abstract void run() throws IOException;
1096 public void connectionClosed(FcpConnection fcpConnection, Throwable throwable) {
1097 fcpException = new FcpException("Connection closed", throwable);
1098 completionLatch.countDown();
1105 public void receivedCloseConnectionDuplicateClientName(FcpConnection fcpConnection, CloseConnectionDuplicateClientName closeConnectionDuplicateClientName) {
1106 fcpException = new FcpException("Connection closed, duplicate client name");
1107 completionLatch.countDown();
1114 public void receivedProtocolError(FcpConnection fcpConnection, ProtocolError protocolError) {
1115 fcpException = new FcpException("Protocol error (" + protocolError.getCode() + ", " + protocolError.getCodeDescription());
1116 completionLatch.countDown();