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 underlying FCP connection. */
87 private final FcpConnection fcpConnection;
89 /** The {@link NodeHello} data sent by the node on connection. */
90 private volatile NodeHello nodeHello;
92 /** Whether the client is currently connected. */
93 private volatile boolean connected;
95 /** The listener for “connection closed” events. */
96 private FcpListener connectionClosedListener;
99 * Creates an FCP client with the given name.
101 * @throws UnknownHostException
102 * if the hostname “localhost” is unknown
104 public FcpClient() throws UnknownHostException {
109 * Creates an FCP client.
112 * The hostname of the Freenet node
113 * @throws UnknownHostException
114 * if the given hostname can not be resolved
116 public FcpClient(String hostname) throws UnknownHostException {
117 this(hostname, FcpConnection.DEFAULT_PORT);
121 * Creates an FCP client.
124 * The hostname of the Freenet node
126 * The Freenet node’s FCP port
127 * @throws UnknownHostException
128 * if the given hostname can not be resolved
130 public FcpClient(String hostname, int port) throws UnknownHostException {
131 this(InetAddress.getByName(hostname), port);
135 * Creates an FCP client.
138 * The host address of the Freenet node
140 public FcpClient(InetAddress host) {
141 this(host, FcpConnection.DEFAULT_PORT);
145 * Creates an FCP client.
148 * The host address of the Freenet node
150 * The Freenet node’s FCP port
152 public FcpClient(InetAddress host, int port) {
153 this(new FcpConnection(host, port), false);
157 * Creates a new high-level FCP client that will use the given connection.
158 * This constructor will assume that the FCP connection is already
161 * @param fcpConnection
162 * The FCP connection to use
164 public FcpClient(FcpConnection fcpConnection) {
165 this(fcpConnection, true);
169 * Creates a new high-level FCP client that will use the given connection.
171 * @param fcpConnection
172 * The FCP connection to use
174 * The initial status of the FCP connection
176 public FcpClient(FcpConnection fcpConnection, boolean connected) {
177 this.fcpConnection = fcpConnection;
178 this.connected = connected;
179 connectionClosedListener = new FcpAdapter() {
185 @SuppressWarnings("synthetic-access")
186 public void connectionClosed(FcpConnection fcpConnection, Throwable throwable) {
187 FcpClient.this.connected = false;
188 fcpClientListenerManager.fireFcpClientDisconnected();
191 fcpConnection.addFcpListener(connectionClosedListener);
195 // LISTENER MANAGEMENT
199 * Adds an FCP listener to the underlying connection.
202 * The FCP listener to add
204 public void addFcpListener(FcpListener fcpListener) {
205 fcpConnection.addFcpListener(fcpListener);
209 * Removes an FCP listener from the underlying connection.
212 * The FCP listener to remove
214 public void removeFcpListener(FcpListener fcpListener) {
215 fcpConnection.removeFcpListener(fcpListener);
219 * Adds an FCP client listener to the list of registered listeners.
221 * @param fcpClientListener
222 * The FCP client listener to add
224 public void addFcpClientListener(FcpClientListener fcpClientListener) {
225 fcpClientListenerManager.addListener(fcpClientListener);
229 * Removes an FCP client listener from the list of registered listeners.
231 * @param fcpClientListener
232 * The FCP client listener to remove
234 public void removeFcpClientListener(FcpClientListener fcpClientListener) {
235 fcpClientListenerManager.removeListener(fcpClientListener);
243 * Returns the {@link NodeHello} object that the node returned when
246 * @return The {@code NodeHello} data container
248 public NodeHello getNodeHello() {
253 * Returns the underlying FCP connection.
255 * @return The underlying FCP connection
257 public FcpConnection getConnection() {
258 return fcpConnection;
266 * Connects the FCP client.
269 * The name of the client
270 * @throws IOException
271 * if an I/O error occurs
272 * @throws FcpException
273 * if an FCP error occurs
275 public void connect(final String name) throws IOException, FcpException {
276 checkConnected(false);
278 new ExtendedFcpAdapter() {
284 @SuppressWarnings("synthetic-access")
285 public void run() throws IOException {
286 fcpConnection.connect();
287 ClientHello clientHello = new ClientHello(name);
288 fcpConnection.sendMessage(clientHello);
289 WatchGlobal watchGlobal = new WatchGlobal(true);
290 fcpConnection.sendMessage(watchGlobal);
297 @SuppressWarnings("synthetic-access")
298 public void receivedNodeHello(FcpConnection fcpConnection, NodeHello nodeHello) {
299 FcpClient.this.nodeHello = nodeHello;
300 completionLatch.countDown();
306 * Disconnects the FCP client.
308 public void disconnect() {
309 synchronized (syncObject) {
310 fcpConnection.close();
311 syncObject.notifyAll();
316 * Returns whether this client is currently connected.
318 * @return {@code true} if the client is currently connected, {@code false}
321 public boolean isConnected() {
326 * Detaches this client from its underlying FCP connection.
328 public void detach() {
329 fcpConnection.removeFcpListener(connectionClosedListener);
337 * Returns all peers that the node has.
339 * @param withMetadata
340 * <code>true</code> to include peer metadata
341 * @param withVolatile
342 * <code>true</code> to include volatile peer data
343 * @return A set containing the node’s peers
344 * @throws IOException
345 * if an I/O error occurs
346 * @throws FcpException
347 * if an FCP error occurs
349 public Collection<Peer> getPeers(final boolean withMetadata, final boolean withVolatile) throws IOException, FcpException {
350 final Set<Peer> peers = Collections.synchronizedSet(new HashSet<Peer>());
351 new ExtendedFcpAdapter() {
353 /** The ID of the “ListPeers” request. */
354 @SuppressWarnings("synthetic-access")
355 private String identifier = createIdentifier("list-peers");
361 @SuppressWarnings("synthetic-access")
362 public void run() throws IOException {
363 fcpConnection.sendMessage(new ListPeers(identifier, withMetadata, withVolatile));
370 public void receivedPeer(FcpConnection fcpConnection, Peer peer) {
371 if (peer.getIdentifier().equals(identifier)) {
380 public void receivedEndListPeers(FcpConnection fcpConnection, EndListPeers endListPeers) {
381 if (endListPeers.getIdentifier().equals(identifier)) {
382 completionLatch.countDown();
390 * Returns all darknet peers.
392 * @param withMetadata
393 * <code>true</code> to include peer metadata
394 * @param withVolatile
395 * <code>true</code> to include volatile peer data
396 * @return A set containing the node’s darknet peers
397 * @throws IOException
398 * if an I/O error occurs
399 * @throws FcpException
400 * if an FCP error occurs
402 public Collection<Peer> getDarknetPeers(boolean withMetadata, boolean withVolatile) throws IOException, FcpException {
403 Collection<Peer> allPeers = getPeers(withMetadata, withVolatile);
404 Collection<Peer> darknetPeers = new HashSet<Peer>();
405 for (Peer peer : allPeers) {
406 if (!peer.isOpennet() && !peer.isSeed()) {
407 darknetPeers.add(peer);
414 * Returns all opennet peers.
416 * @param withMetadata
417 * <code>true</code> to include peer metadata
418 * @param withVolatile
419 * <code>true</code> to include volatile peer data
420 * @return A set containing the node’s opennet peers
421 * @throws IOException
422 * if an I/O error occurs
423 * @throws FcpException
424 * if an FCP error occurs
426 public Collection<Peer> getOpennetPeers(boolean withMetadata, boolean withVolatile) throws IOException, FcpException {
427 Collection<Peer> allPeers = getPeers(withMetadata, withVolatile);
428 Collection<Peer> opennetPeers = new HashSet<Peer>();
429 for (Peer peer : allPeers) {
430 if (peer.isOpennet() && !peer.isSeed()) {
431 opennetPeers.add(peer);
438 * Returns all seed peers.
440 * @param withMetadata
441 * <code>true</code> to include peer metadata
442 * @param withVolatile
443 * <code>true</code> to include volatile peer data
444 * @return A set containing the node’s seed peers
445 * @throws IOException
446 * if an I/O error occurs
447 * @throws FcpException
448 * if an FCP error occurs
450 public Collection<Peer> getSeedPeers(boolean withMetadata, boolean withVolatile) throws IOException, FcpException {
451 Collection<Peer> allPeers = getPeers(withMetadata, withVolatile);
452 Collection<Peer> seedPeers = new HashSet<Peer>();
453 for (Peer peer : allPeers) {
462 * Adds the given peer to the node.
466 * @throws IOException
467 * if an I/O error occurs
468 * @throws FcpException
469 * if an FCP error occurs
471 public void addPeer(Peer peer) throws IOException, FcpException {
472 addPeer(peer.getNodeRef());
476 * Adds the peer defined by the noderef to the node.
479 * The noderef that defines the new peer
480 * @throws IOException
481 * if an I/O error occurs
482 * @throws FcpException
483 * if an FCP error occurs
485 public void addPeer(NodeRef nodeRef) throws IOException, FcpException {
486 addPeer(new AddPeer(nodeRef));
490 * Adds a peer, reading the noderef from the given URL.
493 * The URL to read the noderef from
494 * @throws IOException
495 * if an I/O error occurs
496 * @throws FcpException
497 * if an FCP error occurs
499 public void addPeer(URL url) throws IOException, FcpException {
500 addPeer(new AddPeer(url));
504 * Adds a peer, reading the noderef of the peer from the given file.
505 * <strong>Note:</strong> the file to read the noderef from has to reside on
506 * the same machine as the node!
509 * The name of the file containing the peer’s noderef
510 * @throws IOException
511 * if an I/O error occurs
512 * @throws FcpException
513 * if an FCP error occurs
515 public void addPeer(String file) throws IOException, FcpException {
516 addPeer(new AddPeer(file));
520 * Sends the given {@link AddPeer} message to the node. This method should
521 * not be called directly. Use one of {@link #addPeer(Peer)},
522 * {@link #addPeer(NodeRef)}, {@link #addPeer(URL)}, or
523 * {@link #addPeer(String)} instead.
526 * The “AddPeer” message
527 * @throws IOException
528 * if an I/O error occurs
529 * @throws FcpException
530 * if an FCP error occurs
532 private void addPeer(final AddPeer addPeer) throws IOException, FcpException {
533 new ExtendedFcpAdapter() {
539 @SuppressWarnings("synthetic-access")
540 public void run() throws IOException {
541 fcpConnection.sendMessage(addPeer);
548 public void receivedPeer(FcpConnection fcpConnection, Peer peer) {
549 completionLatch.countDown();
555 * Modifies the given peer.
559 * @param allowLocalAddresses
560 * <code>true</code> to allow local address, <code>false</code>
561 * to not allow local address, <code>null</code> to not change
564 * <code>true</code> to disable the peer, <code>false</code> to
565 * enable the peer, <code>null</code> to not change the setting
567 * <code>true</code> to enable “listen only” for the peer,
568 * <code>false</code> to disable it, <code>null</code> to not
570 * @throws IOException
571 * if an I/O error occurs
572 * @throws FcpException
573 * if an FCP error occurs
575 public void modifyPeer(final Peer peer, final Boolean allowLocalAddresses, final Boolean disabled, final Boolean listenOnly) throws IOException, FcpException {
576 new ExtendedFcpAdapter() {
582 @SuppressWarnings("synthetic-access")
583 public void run() throws IOException {
584 fcpConnection.sendMessage(new ModifyPeer(peer.getIdentity(), allowLocalAddresses, disabled, listenOnly));
591 public void receivedPeer(FcpConnection fcpConnection, Peer peer) {
592 completionLatch.countDown();
598 * Removes the given peer.
602 * @throws IOException
603 * if an I/O error occurs
604 * @throws FcpException
605 * if an FCP error occurs
607 public void removePeer(final Peer peer) throws IOException, FcpException {
608 new ExtendedFcpAdapter() {
614 @SuppressWarnings("synthetic-access")
615 public void run() throws IOException {
616 fcpConnection.sendMessage(new RemovePeer(peer.getIdentity()));
623 public void receivedPeerRemoved(FcpConnection fcpConnection, PeerRemoved peerRemoved) {
624 completionLatch.countDown();
630 // PEER NOTES MANAGEMENT
634 * Returns the peer note of the given peer.
637 * The peer to get the note for
638 * @return The peer’s note
639 * @throws IOException
640 * if an I/O error occurs
641 * @throws FcpException
642 * if an FCP error occurs
644 public PeerNote getPeerNote(final Peer peer) throws IOException, FcpException {
645 final ObjectWrapper<PeerNote> objectWrapper = new ObjectWrapper<PeerNote>();
646 new ExtendedFcpAdapter() {
652 @SuppressWarnings("synthetic-access")
653 public void run() throws IOException {
654 fcpConnection.sendMessage(new ListPeerNotes(peer.getIdentity()));
661 public void receivedPeerNote(FcpConnection fcpConnection, PeerNote peerNote) {
662 if (peerNote.getNodeIdentifier().equals(peer.getIdentity())) {
663 objectWrapper.set(peerNote);
671 public void receivedEndListPeerNotes(FcpConnection fcpConnection, EndListPeerNotes endListPeerNotes) {
672 completionLatch.countDown();
675 return objectWrapper.get();
679 * Replaces the peer note for the given peer.
684 * The new base64-encoded note text
686 * The type of the note (currently only <code>1</code> is
688 * @throws IOException
689 * if an I/O error occurs
690 * @throws FcpException
691 * if an FCP error occurs
693 public void modifyPeerNote(final Peer peer, final String noteText, final int noteType) throws IOException, FcpException {
694 new ExtendedFcpAdapter() {
700 @SuppressWarnings("synthetic-access")
701 public void run() throws IOException {
702 fcpConnection.sendMessage(new ModifyPeerNote(peer.getIdentity(), noteText, noteType));
709 public void receivedPeer(FcpConnection fcpConnection, Peer receivedPeer) {
710 if (receivedPeer.getIdentity().equals(peer.getIdentity())) {
711 completionLatch.countDown();
722 * Generates a new SSK key pair.
724 * @return The generated key pair
725 * @throws IOException
726 * if an I/O error occurs
727 * @throws FcpException
728 * if an FCP error occurs
730 public SSKKeypair generateKeyPair() throws IOException, FcpException {
731 final ObjectWrapper<SSKKeypair> sskKeypairWrapper = new ObjectWrapper<SSKKeypair>();
732 new ExtendedFcpAdapter() {
738 @SuppressWarnings("synthetic-access")
739 public void run() throws IOException {
740 fcpConnection.sendMessage(new GenerateSSK());
747 public void receivedSSKKeypair(FcpConnection fcpConnection, SSKKeypair sskKeypair) {
748 sskKeypairWrapper.set(sskKeypair);
749 completionLatch.countDown();
752 return sskKeypairWrapper.get();
756 // REQUEST MANAGEMENT
760 * Returns all currently visible persistent get requests.
763 * <code>true</code> to return get requests from the global
764 * queue, <code>false</code> to only show requests from the
766 * @return All get requests
767 * @throws IOException
768 * if an I/O error occurs
769 * @throws FcpException
770 * if an FCP error occurs
772 public Collection<Request> getGetRequests(final boolean global) throws IOException, FcpException {
773 return Filters.filteredCollection(getRequests(global), new Filter<Request>() {
778 public boolean filterObject(Request request) {
779 return request instanceof GetRequest;
785 * Returns all currently visible persistent put requests.
788 * <code>true</code> to return put requests from the global
789 * queue, <code>false</code> to only show requests from the
791 * @return All put requests
792 * @throws IOException
793 * if an I/O error occurs
794 * @throws FcpException
795 * if an FCP error occurs
797 public Collection<Request> getPutRequests(final boolean global) throws IOException, FcpException {
798 return Filters.filteredCollection(getRequests(global), new Filter<Request>() {
803 public boolean filterObject(Request request) {
804 return request instanceof PutRequest;
810 * Returns all currently visible persistent requests.
813 * <code>true</code> to return requests from the global queue,
814 * <code>false</code> to only show requests from the client-local
816 * @return All requests
817 * @throws IOException
818 * if an I/O error occurs
819 * @throws FcpException
820 * if an FCP error occurs
822 public Collection<Request> getRequests(final boolean global) throws IOException, FcpException {
823 final Map<String, Request> requests = Collections.synchronizedMap(new HashMap<String, Request>());
824 new ExtendedFcpAdapter() {
830 @SuppressWarnings("synthetic-access")
831 public void run() throws IOException {
832 fcpConnection.sendMessage(new ListPersistentRequests());
839 public void receivedPersistentGet(FcpConnection fcpConnection, PersistentGet persistentGet) {
840 if (!persistentGet.isGlobal() || global) {
841 GetRequest getRequest = new GetRequest(persistentGet);
842 requests.put(persistentGet.getIdentifier(), getRequest);
849 * @see net.pterodactylus.fcp.FcpAdapter#receivedDataFound(net.pterodactylus.fcp.FcpConnection,
850 * net.pterodactylus.fcp.DataFound)
853 public void receivedDataFound(FcpConnection fcpConnection, DataFound dataFound) {
854 Request getRequest = requests.get(dataFound.getIdentifier());
855 if (getRequest == null) {
858 getRequest.setComplete(true);
859 getRequest.setLength(dataFound.getDataLength());
860 getRequest.setContentType(dataFound.getMetadataContentType());
866 * @see net.pterodactylus.fcp.FcpAdapter#receivedGetFailed(net.pterodactylus.fcp.FcpConnection,
867 * net.pterodactylus.fcp.GetFailed)
870 public void receivedGetFailed(FcpConnection fcpConnection, GetFailed getFailed) {
871 Request getRequest = requests.get(getFailed.getIdentifier());
872 if (getRequest == null) {
875 getRequest.setComplete(true);
876 getRequest.setFailed(true);
877 getRequest.setFatal(getFailed.isFatal());
878 getRequest.setErrorCode(getFailed.getCode());
884 * @see net.pterodactylus.fcp.FcpAdapter#receivedPersistentPut(net.pterodactylus.fcp.FcpConnection,
885 * net.pterodactylus.fcp.PersistentPut)
888 public void receivedPersistentPut(FcpConnection fcpConnection, PersistentPut persistentPut) {
889 if (!persistentPut.isGlobal() || global) {
890 PutRequest putRequest = new PutRequest(persistentPut);
891 requests.put(persistentPut.getIdentifier(), putRequest);
898 * @see net.pterodactylus.fcp.FcpAdapter#receivedSimpleProgress(net.pterodactylus.fcp.FcpConnection,
899 * net.pterodactylus.fcp.SimpleProgress)
902 public void receivedSimpleProgress(FcpConnection fcpConnection, SimpleProgress simpleProgress) {
903 Request request = requests.get(simpleProgress.getIdentifier());
904 if (request == null) {
907 request.setTotalBlocks(simpleProgress.getTotal());
908 request.setRequiredBlocks(simpleProgress.getRequired());
909 request.setFailedBlocks(simpleProgress.getFailed());
910 request.setFatallyFailedBlocks(simpleProgress.getFatallyFailed());
911 request.setSucceededBlocks(simpleProgress.getSucceeded());
912 request.setFinalizedTotal(simpleProgress.isFinalizedTotal());
919 public void receivedEndListPersistentRequests(FcpConnection fcpConnection, EndListPersistentRequests endListPersistentRequests) {
920 completionLatch.countDown();
923 return requests.values();
927 * Sends a message to a plugin and waits for the response.
930 * The name of the plugin class
932 * The parameters for the plugin
933 * @return The responses from the plugin
934 * @throws FcpException
935 * if an FCP error occurs
936 * @throws IOException
937 * if an I/O error occurs
939 public Map<String, String> sendPluginMessage(String pluginClass, Map<String, String> parameters) throws IOException, FcpException {
940 return sendPluginMessage(pluginClass, parameters, 0, null);
944 * Sends a message to a plugin and waits for the response.
947 * The name of the plugin class
949 * The parameters for the plugin
951 * The length of the optional data stream, or {@code 0} if there
952 * is no optional data stream
953 * @param dataInputStream
954 * The input stream for the payload, or {@code null} if there is
956 * @return The responses from the plugin
957 * @throws FcpException
958 * if an FCP error occurs
959 * @throws IOException
960 * if an I/O error occurs
962 public Map<String, String> sendPluginMessage(final String pluginClass, final Map<String, String> parameters, final long dataLength, final InputStream dataInputStream) throws IOException, FcpException {
963 final Map<String, String> pluginReplies = Collections.synchronizedMap(new HashMap<String, String>());
964 new ExtendedFcpAdapter() {
966 @SuppressWarnings("synthetic-access")
967 private final String identifier = createIdentifier("FCPPluginMessage");
970 @SuppressWarnings("synthetic-access")
971 public void run() throws IOException {
972 FCPPluginMessage fcpPluginMessage = new FCPPluginMessage(pluginClass);
973 for (Entry<String, String> parameter : parameters.entrySet()) {
974 fcpPluginMessage.setParameter(parameter.getKey(), parameter.getValue());
976 fcpPluginMessage.setIdentifier(identifier);
977 if ((dataLength > 0) && (dataInputStream != null)) {
978 fcpPluginMessage.setDataLength(dataLength);
979 fcpPluginMessage.setPayloadInputStream(dataInputStream);
981 fcpConnection.sendMessage(fcpPluginMessage);
988 public void receivedFCPPluginReply(FcpConnection fcpConnection, FCPPluginReply fcpPluginReply) {
989 if (!fcpPluginReply.getIdentifier().equals(identifier)) {
992 pluginReplies.putAll(fcpPluginReply.getReplies());
993 completionLatch.countDown();
997 return pluginReplies;
1005 * Returns information about the node.
1007 * @param giveOpennetRef
1008 * Whether to return the OpenNet reference
1009 * @param withPrivate
1010 * Whether to return private node data
1011 * @param withVolatile
1012 * Whether to return volatile node data
1013 * @return Node information
1014 * @throws FcpException
1015 * if an FCP error occurs
1016 * @throws IOException
1017 * if an I/O error occurs
1019 public NodeData getNodeInformation(final Boolean giveOpennetRef, final Boolean withPrivate, final Boolean withVolatile) throws IOException, FcpException {
1020 final ObjectWrapper<NodeData> nodeDataWrapper = new ObjectWrapper<NodeData>();
1021 new ExtendedFcpAdapter() {
1024 @SuppressWarnings("synthetic-access")
1025 public void run() throws IOException {
1026 GetNode getNodeMessage = new GetNode(giveOpennetRef, withPrivate, withVolatile);
1027 fcpConnection.sendMessage(getNodeMessage);
1034 public void receivedNodeData(FcpConnection fcpConnection, NodeData nodeData) {
1035 nodeDataWrapper.set(nodeData);
1036 completionLatch.countDown();
1039 return nodeDataWrapper.get();
1047 * Creates a unique request identifier.
1050 * The basename of the request
1051 * @return The created request identifier
1053 private String createIdentifier(String basename) {
1054 return basename + "-" + System.currentTimeMillis() + "-" + (int) (Math.random() * Integer.MAX_VALUE);
1058 * Checks whether the connection is in the required state.
1061 * The required connection state
1062 * @throws FcpException
1063 * if the connection is not in the required state
1065 private void checkConnected(boolean connected) throws FcpException {
1066 if (this.connected != connected) {
1067 throw new FcpException("Client is " + (connected ? "not" : "already") + " connected.");
1072 * Tells the client that it is now disconnected. This method is called by
1073 * {@link ExtendedFcpAdapter} only.
1075 private void setDisconnected() {
1080 * Implementation of an {@link FcpListener} that can store an
1081 * {@link FcpException} and wait for the arrival of a certain command.
1083 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
1085 private abstract class ExtendedFcpAdapter extends FcpAdapter {
1087 /** The count down latch used to wait for completion. */
1088 protected final CountDownLatch completionLatch = new CountDownLatch(1);
1090 /** The FCP exception, if any. */
1091 protected FcpException fcpException;
1094 * Creates a new extended FCP adapter.
1096 public ExtendedFcpAdapter() {
1101 * Executes the FCP commands in {@link #run()}, wrapping the execution
1102 * and catching exceptions.
1104 * @throws IOException
1105 * if an I/O error occurs
1106 * @throws FcpException
1107 * if an FCP error occurs
1109 @SuppressWarnings("synthetic-access")
1110 public void execute() throws IOException, FcpException {
1111 checkConnected(true);
1112 fcpConnection.addFcpListener(this);
1117 completionLatch.await();
1119 } catch (InterruptedException ie1) {
1120 /* ignore, we’ll loop. */
1123 } catch (IOException ioe1) {
1127 fcpConnection.removeFcpListener(this);
1129 if (fcpException != null) {
1136 * The FCP commands that actually get executed.
1138 * @throws IOException
1139 * if an I/O error occurs
1141 public abstract void run() throws IOException;
1147 public void connectionClosed(FcpConnection fcpConnection, Throwable throwable) {
1148 fcpException = new FcpException("Connection closed", throwable);
1149 completionLatch.countDown();
1156 public void receivedCloseConnectionDuplicateClientName(FcpConnection fcpConnection, CloseConnectionDuplicateClientName closeConnectionDuplicateClientName) {
1157 fcpException = new FcpException("Connection closed, duplicate client name");
1158 completionLatch.countDown();
1165 public void receivedProtocolError(FcpConnection fcpConnection, ProtocolError protocolError) {
1166 fcpException = new FcpException("Protocol error (" + protocolError.getCode() + ", " + protocolError.getCodeDescription());
1167 completionLatch.countDown();