2 * jFCPlib - FcpClient.java - Copyright © 2009 David Roden
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 package net.pterodactylus.fcp.highlevel;
21 import java.io.IOException;
22 import java.io.InputStream;
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.Map.Entry;
33 import java.util.concurrent.CountDownLatch;
35 import net.pterodactylus.fcp.AddPeer;
36 import net.pterodactylus.fcp.ClientHello;
37 import net.pterodactylus.fcp.CloseConnectionDuplicateClientName;
38 import net.pterodactylus.fcp.DataFound;
39 import net.pterodactylus.fcp.EndListPeerNotes;
40 import net.pterodactylus.fcp.EndListPeers;
41 import net.pterodactylus.fcp.EndListPersistentRequests;
42 import net.pterodactylus.fcp.FCPPluginMessage;
43 import net.pterodactylus.fcp.FCPPluginReply;
44 import net.pterodactylus.fcp.FcpAdapter;
45 import net.pterodactylus.fcp.FcpConnection;
46 import net.pterodactylus.fcp.FcpListener;
47 import net.pterodactylus.fcp.GenerateSSK;
48 import net.pterodactylus.fcp.GetFailed;
49 import net.pterodactylus.fcp.GetNode;
50 import net.pterodactylus.fcp.ListPeerNotes;
51 import net.pterodactylus.fcp.ListPeers;
52 import net.pterodactylus.fcp.ListPersistentRequests;
53 import net.pterodactylus.fcp.ModifyPeer;
54 import net.pterodactylus.fcp.ModifyPeerNote;
55 import net.pterodactylus.fcp.NodeData;
56 import net.pterodactylus.fcp.NodeHello;
57 import net.pterodactylus.fcp.NodeRef;
58 import net.pterodactylus.fcp.Peer;
59 import net.pterodactylus.fcp.PeerNote;
60 import net.pterodactylus.fcp.PeerRemoved;
61 import net.pterodactylus.fcp.PersistentGet;
62 import net.pterodactylus.fcp.PersistentPut;
63 import net.pterodactylus.fcp.ProtocolError;
64 import net.pterodactylus.fcp.RemovePeer;
65 import net.pterodactylus.fcp.SSKKeypair;
66 import net.pterodactylus.fcp.SimpleProgress;
67 import net.pterodactylus.fcp.WatchGlobal;
68 import net.pterodactylus.util.filter.Filter;
69 import net.pterodactylus.util.filter.Filters;
70 import net.pterodactylus.util.thread.ObjectWrapper;
73 * High-level FCP client that hides the details of the underlying FCP
76 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
78 public class FcpClient {
80 /** Object used for synchronization. */
81 private final Object syncObject = new Object();
83 /** Listener management. */
84 private final FcpClientListenerManager fcpClientListenerManager = new FcpClientListenerManager(this);
86 /** The name of this client. */
87 private final String name;
89 /** The underlying FCP connection. */
90 private final FcpConnection fcpConnection;
92 /** Whether the client is currently connected. */
93 private volatile boolean connected;
96 * Creates an FCP client with the given name.
99 * The name of the FCP client
100 * @throws UnknownHostException
101 * if the hostname “localhost” is unknown
103 public FcpClient(String name) throws UnknownHostException {
104 this(name, "localhost");
108 * Creates an FCP client.
111 * The name of the FCP client
113 * The hostname of the Freenet node
114 * @throws UnknownHostException
115 * if the given hostname can not be resolved
117 public FcpClient(String name, String hostname) throws UnknownHostException {
118 this(name, hostname, FcpConnection.DEFAULT_PORT);
122 * Creates an FCP client.
125 * The name of the FCP client
127 * The hostname of the Freenet node
129 * The Freenet node’s FCP port
130 * @throws UnknownHostException
131 * if the given hostname can not be resolved
133 public FcpClient(String name, String hostname, int port) throws UnknownHostException {
134 this(name, InetAddress.getByName(hostname), port);
138 * Creates an FCP client.
141 * The name of the FCP client
143 * The host address of the Freenet node
145 public FcpClient(String name, InetAddress host) {
146 this(name, host, FcpConnection.DEFAULT_PORT);
150 * Creates an FCP client.
153 * The name of the FCP client
155 * The host address of the Freenet node
157 * The Freenet node’s FCP port
159 public FcpClient(String name, InetAddress host, int port) {
161 fcpConnection = new FcpConnection(host, port);
162 fcpConnection.addFcpListener(new FcpAdapter() {
168 @SuppressWarnings("synthetic-access")
169 public void connectionClosed(FcpConnection fcpConnection, Throwable throwable) {
171 fcpClientListenerManager.fireFcpClientDisconnected();
177 // LISTENER MANAGEMENT
181 * Adds an FCP listener to the underlying connection.
184 * The FCP listener to add
186 public void addFcpListener(FcpListener fcpListener) {
187 fcpConnection.addFcpListener(fcpListener);
191 * Removes an FCP listener from the underlying connection.
194 * The FCP listener to remove
196 public void removeFcpListener(FcpListener fcpListener) {
197 fcpConnection.removeFcpListener(fcpListener);
201 * Adds an FCP client listener to the list of registered listeners.
203 * @param fcpClientListener
204 * The FCP client listener to add
206 public void addFcpClientListener(FcpClientListener fcpClientListener) {
207 fcpClientListenerManager.addListener(fcpClientListener);
211 * Removes an FCP client listener from the list of registered listeners.
213 * @param fcpClientListener
214 * The FCP client listener to remove
216 public void removeFcpClientListener(FcpClientListener fcpClientListener) {
217 fcpClientListenerManager.removeListener(fcpClientListener);
225 * Connects the FCP client.
227 * @throws IOException
228 * if an I/O error occurs
229 * @throws FcpException
230 * if an FCP error occurs
232 public void connect() throws IOException, FcpException {
233 checkConnected(false);
235 new ExtendedFcpAdapter() {
241 @SuppressWarnings("synthetic-access")
242 public void run() throws IOException {
243 fcpConnection.connect();
244 ClientHello clientHello = new ClientHello(name);
245 fcpConnection.sendMessage(clientHello);
246 WatchGlobal watchGlobal = new WatchGlobal(true);
247 fcpConnection.sendMessage(watchGlobal);
254 public void receivedNodeHello(FcpConnection fcpConnection, NodeHello nodeHello) {
255 completionLatch.countDown();
261 * Disconnects the FCP client.
263 public void disconnect() {
264 synchronized (syncObject) {
265 fcpConnection.close();
266 syncObject.notifyAll();
271 * Returns whether this client is currently connected.
273 * @return {@code true} if the client is currently connected, {@code false}
276 public boolean isConnected() {
285 * Returns all peers that the node has.
287 * @param withMetadata
288 * <code>true</code> to include peer metadata
289 * @param withVolatile
290 * <code>true</code> to include volatile peer data
291 * @return A set containing the node’s peers
292 * @throws IOException
293 * if an I/O error occurs
294 * @throws FcpException
295 * if an FCP error occurs
297 public Collection<Peer> getPeers(final boolean withMetadata, final boolean withVolatile) throws IOException, FcpException {
298 final Set<Peer> peers = Collections.synchronizedSet(new HashSet<Peer>());
299 new ExtendedFcpAdapter() {
301 /** The ID of the “ListPeers” request. */
302 @SuppressWarnings("synthetic-access")
303 private String identifier = createIdentifier("list-peers");
309 @SuppressWarnings("synthetic-access")
310 public void run() throws IOException {
311 fcpConnection.sendMessage(new ListPeers(identifier, withMetadata, withVolatile));
318 public void receivedPeer(FcpConnection fcpConnection, Peer peer) {
319 if (peer.getIdentifier().equals(identifier)) {
328 public void receivedEndListPeers(FcpConnection fcpConnection, EndListPeers endListPeers) {
329 if (endListPeers.getIdentifier().equals(identifier)) {
330 completionLatch.countDown();
338 * Returns all darknet peers.
340 * @param withMetadata
341 * <code>true</code> to include peer metadata
342 * @param withVolatile
343 * <code>true</code> to include volatile peer data
344 * @return A set containing the node’s darknet peers
345 * @throws IOException
346 * if an I/O error occurs
347 * @throws FcpException
348 * if an FCP error occurs
350 public Collection<Peer> getDarknetPeers(boolean withMetadata, boolean withVolatile) throws IOException, FcpException {
351 Collection<Peer> allPeers = getPeers(withMetadata, withVolatile);
352 Collection<Peer> darknetPeers = new HashSet<Peer>();
353 for (Peer peer : allPeers) {
354 if (!peer.isOpennet() && !peer.isSeed()) {
355 darknetPeers.add(peer);
362 * Returns all opennet peers.
364 * @param withMetadata
365 * <code>true</code> to include peer metadata
366 * @param withVolatile
367 * <code>true</code> to include volatile peer data
368 * @return A set containing the node’s opennet peers
369 * @throws IOException
370 * if an I/O error occurs
371 * @throws FcpException
372 * if an FCP error occurs
374 public Collection<Peer> getOpennetPeers(boolean withMetadata, boolean withVolatile) throws IOException, FcpException {
375 Collection<Peer> allPeers = getPeers(withMetadata, withVolatile);
376 Collection<Peer> opennetPeers = new HashSet<Peer>();
377 for (Peer peer : allPeers) {
378 if (peer.isOpennet() && !peer.isSeed()) {
379 opennetPeers.add(peer);
386 * Returns all seed peers.
388 * @param withMetadata
389 * <code>true</code> to include peer metadata
390 * @param withVolatile
391 * <code>true</code> to include volatile peer data
392 * @return A set containing the node’s seed peers
393 * @throws IOException
394 * if an I/O error occurs
395 * @throws FcpException
396 * if an FCP error occurs
398 public Collection<Peer> getSeedPeers(boolean withMetadata, boolean withVolatile) throws IOException, FcpException {
399 Collection<Peer> allPeers = getPeers(withMetadata, withVolatile);
400 Collection<Peer> seedPeers = new HashSet<Peer>();
401 for (Peer peer : allPeers) {
410 * Adds the given peer to the node.
414 * @throws IOException
415 * if an I/O error occurs
416 * @throws FcpException
417 * if an FCP error occurs
419 public void addPeer(Peer peer) throws IOException, FcpException {
420 addPeer(peer.getNodeRef());
424 * Adds the peer defined by the noderef to the node.
427 * The noderef that defines the new peer
428 * @throws IOException
429 * if an I/O error occurs
430 * @throws FcpException
431 * if an FCP error occurs
433 public void addPeer(NodeRef nodeRef) throws IOException, FcpException {
434 addPeer(new AddPeer(nodeRef));
438 * Adds a peer, reading the noderef from the given URL.
441 * The URL to read the noderef from
442 * @throws IOException
443 * if an I/O error occurs
444 * @throws FcpException
445 * if an FCP error occurs
447 public void addPeer(URL url) throws IOException, FcpException {
448 addPeer(new AddPeer(url));
452 * Adds a peer, reading the noderef of the peer from the given file.
453 * <strong>Note:</strong> the file to read the noderef from has to reside on
454 * the same machine as the node!
457 * The name of the file containing the peer’s noderef
458 * @throws IOException
459 * if an I/O error occurs
460 * @throws FcpException
461 * if an FCP error occurs
463 public void addPeer(String file) throws IOException, FcpException {
464 addPeer(new AddPeer(file));
468 * Sends the given {@link AddPeer} message to the node. This method should
469 * not be called directly. Use one of {@link #addPeer(Peer)},
470 * {@link #addPeer(NodeRef)}, {@link #addPeer(URL)}, or
471 * {@link #addPeer(String)} instead.
474 * The “AddPeer” message
475 * @throws IOException
476 * if an I/O error occurs
477 * @throws FcpException
478 * if an FCP error occurs
480 private void addPeer(final AddPeer addPeer) throws IOException, FcpException {
481 new ExtendedFcpAdapter() {
487 @SuppressWarnings("synthetic-access")
488 public void run() throws IOException {
489 fcpConnection.sendMessage(addPeer);
496 public void receivedPeer(FcpConnection fcpConnection, Peer peer) {
497 completionLatch.countDown();
503 * Modifies the given peer.
507 * @param allowLocalAddresses
508 * <code>true</code> to allow local address, <code>false</code>
509 * to not allow local address, <code>null</code> to not change
512 * <code>true</code> to disable the peer, <code>false</code> to
513 * enable the peer, <code>null</code> to not change the setting
515 * <code>true</code> to enable “listen only” for the peer,
516 * <code>false</code> to disable it, <code>null</code> to not
518 * @throws IOException
519 * if an I/O error occurs
520 * @throws FcpException
521 * if an FCP error occurs
523 public void modifyPeer(final Peer peer, final Boolean allowLocalAddresses, final Boolean disabled, final Boolean listenOnly) throws IOException, FcpException {
524 new ExtendedFcpAdapter() {
530 @SuppressWarnings("synthetic-access")
531 public void run() throws IOException {
532 fcpConnection.sendMessage(new ModifyPeer(peer.getIdentity(), allowLocalAddresses, disabled, listenOnly));
539 public void receivedPeer(FcpConnection fcpConnection, Peer peer) {
540 completionLatch.countDown();
546 * Removes the given peer.
550 * @throws IOException
551 * if an I/O error occurs
552 * @throws FcpException
553 * if an FCP error occurs
555 public void removePeer(final Peer peer) throws IOException, FcpException {
556 new ExtendedFcpAdapter() {
562 @SuppressWarnings("synthetic-access")
563 public void run() throws IOException {
564 fcpConnection.sendMessage(new RemovePeer(peer.getIdentity()));
571 public void receivedPeerRemoved(FcpConnection fcpConnection, PeerRemoved peerRemoved) {
572 completionLatch.countDown();
578 // PEER NOTES MANAGEMENT
582 * Returns the peer note of the given peer.
585 * The peer to get the note for
586 * @return The peer’s note
587 * @throws IOException
588 * if an I/O error occurs
589 * @throws FcpException
590 * if an FCP error occurs
592 public PeerNote getPeerNote(final Peer peer) throws IOException, FcpException {
593 final ObjectWrapper<PeerNote> objectWrapper = new ObjectWrapper<PeerNote>();
594 new ExtendedFcpAdapter() {
600 @SuppressWarnings("synthetic-access")
601 public void run() throws IOException {
602 fcpConnection.sendMessage(new ListPeerNotes(peer.getIdentity()));
609 public void receivedPeerNote(FcpConnection fcpConnection, PeerNote peerNote) {
610 if (peerNote.getNodeIdentifier().equals(peer.getIdentity())) {
611 objectWrapper.set(peerNote);
619 public void receivedEndListPeerNotes(FcpConnection fcpConnection, EndListPeerNotes endListPeerNotes) {
620 completionLatch.countDown();
623 return objectWrapper.get();
627 * Replaces the peer note for the given peer.
632 * The new base64-encoded note text
634 * The type of the note (currently only <code>1</code> is
636 * @throws IOException
637 * if an I/O error occurs
638 * @throws FcpException
639 * if an FCP error occurs
641 public void modifyPeerNote(final Peer peer, final String noteText, final int noteType) throws IOException, FcpException {
642 new ExtendedFcpAdapter() {
648 @SuppressWarnings("synthetic-access")
649 public void run() throws IOException {
650 fcpConnection.sendMessage(new ModifyPeerNote(peer.getIdentity(), noteText, noteType));
657 public void receivedPeer(FcpConnection fcpConnection, Peer receivedPeer) {
658 if (receivedPeer.getIdentity().equals(peer.getIdentity())) {
659 completionLatch.countDown();
670 * Generates a new SSK key pair.
672 * @return The generated key pair
673 * @throws IOException
674 * if an I/O error occurs
675 * @throws FcpException
676 * if an FCP error occurs
678 public SSKKeypair generateKeyPair() throws IOException, FcpException {
679 final ObjectWrapper<SSKKeypair> sskKeypairWrapper = new ObjectWrapper<SSKKeypair>();
680 new ExtendedFcpAdapter() {
686 @SuppressWarnings("synthetic-access")
687 public void run() throws IOException {
688 fcpConnection.sendMessage(new GenerateSSK());
695 public void receivedSSKKeypair(FcpConnection fcpConnection, SSKKeypair sskKeypair) {
696 sskKeypairWrapper.set(sskKeypair);
697 completionLatch.countDown();
700 return sskKeypairWrapper.get();
704 // REQUEST MANAGEMENT
708 * Returns all currently visible persistent get requests.
711 * <code>true</code> to return get requests from the global
712 * queue, <code>false</code> to only show requests from the
714 * @return All get requests
715 * @throws IOException
716 * if an I/O error occurs
717 * @throws FcpException
718 * if an FCP error occurs
720 public Collection<Request> getGetRequests(final boolean global) throws IOException, FcpException {
721 return Filters.filteredCollection(getRequests(global), new Filter<Request>() {
726 public boolean filterObject(Request request) {
727 return request instanceof GetRequest;
733 * Returns all currently visible persistent put requests.
736 * <code>true</code> to return put requests from the global
737 * queue, <code>false</code> to only show requests from the
739 * @return All put requests
740 * @throws IOException
741 * if an I/O error occurs
742 * @throws FcpException
743 * if an FCP error occurs
745 public Collection<Request> getPutRequests(final boolean global) throws IOException, FcpException {
746 return Filters.filteredCollection(getRequests(global), new Filter<Request>() {
751 public boolean filterObject(Request request) {
752 return request instanceof PutRequest;
758 * Returns all currently visible persistent requests.
761 * <code>true</code> to return requests from the global queue,
762 * <code>false</code> to only show requests from the client-local
764 * @return All requests
765 * @throws IOException
766 * if an I/O error occurs
767 * @throws FcpException
768 * if an FCP error occurs
770 public Collection<Request> getRequests(final boolean global) throws IOException, FcpException {
771 final Map<String, Request> requests = Collections.synchronizedMap(new HashMap<String, Request>());
772 new ExtendedFcpAdapter() {
778 @SuppressWarnings("synthetic-access")
779 public void run() throws IOException {
780 fcpConnection.sendMessage(new ListPersistentRequests());
787 public void receivedPersistentGet(FcpConnection fcpConnection, PersistentGet persistentGet) {
788 if (!persistentGet.isGlobal() || global) {
789 GetRequest getRequest = new GetRequest(persistentGet);
790 requests.put(persistentGet.getIdentifier(), getRequest);
797 * @see net.pterodactylus.fcp.FcpAdapter#receivedDataFound(net.pterodactylus.fcp.FcpConnection,
798 * net.pterodactylus.fcp.DataFound)
801 public void receivedDataFound(FcpConnection fcpConnection, DataFound dataFound) {
802 Request getRequest = requests.get(dataFound.getIdentifier());
803 if (getRequest == null) {
806 getRequest.setComplete(true);
807 getRequest.setLength(dataFound.getDataLength());
808 getRequest.setContentType(dataFound.getMetadataContentType());
814 * @see net.pterodactylus.fcp.FcpAdapter#receivedGetFailed(net.pterodactylus.fcp.FcpConnection,
815 * net.pterodactylus.fcp.GetFailed)
818 public void receivedGetFailed(FcpConnection fcpConnection, GetFailed getFailed) {
819 Request getRequest = requests.get(getFailed.getIdentifier());
820 if (getRequest == null) {
823 getRequest.setComplete(true);
824 getRequest.setFailed(true);
825 getRequest.setFatal(getFailed.isFatal());
826 getRequest.setErrorCode(getFailed.getCode());
832 * @see net.pterodactylus.fcp.FcpAdapter#receivedPersistentPut(net.pterodactylus.fcp.FcpConnection,
833 * net.pterodactylus.fcp.PersistentPut)
836 public void receivedPersistentPut(FcpConnection fcpConnection, PersistentPut persistentPut) {
837 if (!persistentPut.isGlobal() || global) {
838 PutRequest putRequest = new PutRequest(persistentPut);
839 requests.put(persistentPut.getIdentifier(), putRequest);
846 * @see net.pterodactylus.fcp.FcpAdapter#receivedSimpleProgress(net.pterodactylus.fcp.FcpConnection,
847 * net.pterodactylus.fcp.SimpleProgress)
850 public void receivedSimpleProgress(FcpConnection fcpConnection, SimpleProgress simpleProgress) {
851 Request request = requests.get(simpleProgress.getIdentifier());
852 if (request == null) {
855 request.setTotalBlocks(simpleProgress.getTotal());
856 request.setRequiredBlocks(simpleProgress.getRequired());
857 request.setFailedBlocks(simpleProgress.getFailed());
858 request.setFatallyFailedBlocks(simpleProgress.getFatallyFailed());
859 request.setSucceededBlocks(simpleProgress.getSucceeded());
860 request.setFinalizedTotal(simpleProgress.isFinalizedTotal());
867 public void receivedEndListPersistentRequests(FcpConnection fcpConnection, EndListPersistentRequests endListPersistentRequests) {
868 completionLatch.countDown();
871 return requests.values();
875 * Sends a message to a plugin and waits for the response.
878 * The name of the plugin class
880 * The parameters for the plugin
881 * @return The responses from the plugin
882 * @throws FcpException
883 * if an FCP error occurs
884 * @throws IOException
885 * if an I/O error occurs
887 public Map<String, String> sendPluginMessage(String pluginClass, Map<String, String> parameters) throws IOException, FcpException {
888 return sendPluginMessage(pluginClass, parameters, 0, null);
892 * Sends a message to a plugin and waits for the response.
895 * The name of the plugin class
897 * The parameters for the plugin
899 * The length of the optional data stream, or {@code 0} if there
900 * is no optional data stream
901 * @param dataInputStream
902 * The input stream for the payload, or {@code null} if there is
904 * @return The responses from the plugin
905 * @throws FcpException
906 * if an FCP error occurs
907 * @throws IOException
908 * if an I/O error occurs
910 public Map<String, String> sendPluginMessage(final String pluginClass, final Map<String, String> parameters, final long dataLength, final InputStream dataInputStream) throws IOException, FcpException {
911 final Map<String, String> pluginReplies = Collections.synchronizedMap(new HashMap<String, String>());
912 new ExtendedFcpAdapter() {
914 @SuppressWarnings("synthetic-access")
915 private final String identifier = createIdentifier("FCPPluginMessage");
918 @SuppressWarnings("synthetic-access")
919 public void run() throws IOException {
920 FCPPluginMessage fcpPluginMessage = new FCPPluginMessage(pluginClass);
921 for (Entry<String, String> parameter : parameters.entrySet()) {
922 fcpPluginMessage.setParameter(parameter.getKey(), parameter.getValue());
924 fcpPluginMessage.setIdentifier(identifier);
925 if ((dataLength > 0) && (dataInputStream != null)) {
926 fcpPluginMessage.setDataLength(dataLength);
927 fcpPluginMessage.setPayloadInputStream(dataInputStream);
929 fcpConnection.sendMessage(fcpPluginMessage);
936 public void receivedFCPPluginReply(FcpConnection fcpConnection, FCPPluginReply fcpPluginReply) {
937 if (!fcpPluginReply.getIdentifier().equals(identifier)) {
940 pluginReplies.putAll(fcpPluginReply.getReplies());
941 completionLatch.countDown();
945 return pluginReplies;
953 * Returns information about the node.
955 * @param giveOpennetRef
956 * Whether to return the OpenNet reference
958 * Whether to return private node data
959 * @param withVolatile
960 * Whether to return volatile node data
961 * @return Node information
962 * @throws FcpException
963 * if an FCP error occurs
964 * @throws IOException
965 * if an I/O error occurs
967 public NodeData getNodeInformation(final Boolean giveOpennetRef, final Boolean withPrivate, final Boolean withVolatile) throws IOException, FcpException {
968 final ObjectWrapper<NodeData> nodeDataWrapper = new ObjectWrapper<NodeData>();
969 new ExtendedFcpAdapter() {
972 @SuppressWarnings("synthetic-access")
973 public void run() throws IOException {
974 GetNode getNodeMessage = new GetNode(giveOpennetRef, withPrivate, withVolatile);
975 fcpConnection.sendMessage(getNodeMessage);
982 public void receivedNodeData(FcpConnection fcpConnection, NodeData nodeData) {
983 nodeDataWrapper.set(nodeData);
984 completionLatch.countDown();
987 return nodeDataWrapper.get();
995 * Creates a unique request identifier.
998 * The basename of the request
999 * @return The created request identifier
1001 private String createIdentifier(String basename) {
1002 return basename + "-" + System.currentTimeMillis() + "-" + (int) (Math.random() * Integer.MAX_VALUE);
1006 * Checks whether the connection is in the required state.
1009 * The required connection state
1010 * @throws FcpException
1011 * if the connection is not in the required state
1013 private void checkConnected(boolean connected) throws FcpException {
1014 if (this.connected != connected) {
1015 throw new FcpException("Client is " + (connected ? "not" : "already") + " connected.");
1020 * Tells the client that it is now disconnected. This method is called by
1021 * {@link ExtendedFcpAdapter} only.
1023 private void setDisconnected() {
1028 * Implementation of an {@link FcpListener} that can store an
1029 * {@link FcpException} and wait for the arrival of a certain command.
1031 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
1033 private abstract class ExtendedFcpAdapter extends FcpAdapter {
1035 /** The count down latch used to wait for completion. */
1036 protected final CountDownLatch completionLatch = new CountDownLatch(1);
1038 /** The FCP exception, if any. */
1039 protected FcpException fcpException;
1042 * Creates a new extended FCP adapter.
1044 public ExtendedFcpAdapter() {
1049 * Executes the FCP commands in {@link #run()}, wrapping the execution
1050 * and catching exceptions.
1052 * @throws IOException
1053 * if an I/O error occurs
1054 * @throws FcpException
1055 * if an FCP error occurs
1057 @SuppressWarnings("synthetic-access")
1058 public void execute() throws IOException, FcpException {
1059 checkConnected(true);
1060 fcpConnection.addFcpListener(this);
1065 completionLatch.await();
1067 } catch (InterruptedException ie1) {
1068 /* ignore, we’ll loop. */
1071 } catch (IOException ioe1) {
1075 fcpConnection.removeFcpListener(this);
1077 if (fcpException != null) {
1084 * The FCP commands that actually get executed.
1086 * @throws IOException
1087 * if an I/O error occurs
1089 public abstract void run() throws IOException;
1095 public void connectionClosed(FcpConnection fcpConnection, Throwable throwable) {
1096 fcpException = new FcpException("Connection closed", throwable);
1097 completionLatch.countDown();
1104 public void receivedCloseConnectionDuplicateClientName(FcpConnection fcpConnection, CloseConnectionDuplicateClientName closeConnectionDuplicateClientName) {
1105 fcpException = new FcpException("Connection closed, duplicate client name");
1106 completionLatch.countDown();
1113 public void receivedProtocolError(FcpConnection fcpConnection, ProtocolError protocolError) {
1114 fcpException = new FcpException("Protocol error (" + protocolError.getCode() + ", " + protocolError.getCodeDescription());
1115 completionLatch.countDown();