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;
96 * Creates an FCP client with the given name.
98 * @throws UnknownHostException
99 * if the hostname “localhost” is unknown
101 public FcpClient() throws UnknownHostException {
106 * Creates an FCP client.
109 * The hostname of the Freenet node
110 * @throws UnknownHostException
111 * if the given hostname can not be resolved
113 public FcpClient(String hostname) throws UnknownHostException {
114 this(hostname, FcpConnection.DEFAULT_PORT);
118 * Creates an FCP client.
121 * The hostname of the Freenet node
123 * The Freenet node’s FCP port
124 * @throws UnknownHostException
125 * if the given hostname can not be resolved
127 public FcpClient(String hostname, int port) throws UnknownHostException {
128 this(InetAddress.getByName(hostname), port);
132 * Creates an FCP client.
135 * The host address of the Freenet node
137 public FcpClient(InetAddress host) {
138 this(host, FcpConnection.DEFAULT_PORT);
142 * Creates an FCP client.
145 * The host address of the Freenet node
147 * The Freenet node’s FCP port
149 public FcpClient(InetAddress host, int port) {
150 this(new FcpConnection(host, port));
154 * Creates a new high-level FCP client that will use the given connection.
155 * This constructor will assume that the FCP connection is already
158 * @param fcpConnection
159 * The FCP connection to use
161 public FcpClient(FcpConnection fcpConnection) {
162 this(fcpConnection, true);
166 * Creates a new high-level FCP client that will use the given connection.
168 * @param fcpConnection
169 * The FCP connection to use
171 * The initial status of the FCP connection
173 public FcpClient(FcpConnection fcpConnection, boolean connected) {
174 this.fcpConnection = fcpConnection;
175 this.connected = connected;
176 fcpConnection.addFcpListener(new FcpAdapter() {
182 @SuppressWarnings("synthetic-access")
183 public void connectionClosed(FcpConnection fcpConnection, Throwable throwable) {
184 FcpClient.this.connected = false;
185 fcpClientListenerManager.fireFcpClientDisconnected();
191 // LISTENER MANAGEMENT
195 * Adds an FCP listener to the underlying connection.
198 * The FCP listener to add
200 public void addFcpListener(FcpListener fcpListener) {
201 fcpConnection.addFcpListener(fcpListener);
205 * Removes an FCP listener from the underlying connection.
208 * The FCP listener to remove
210 public void removeFcpListener(FcpListener fcpListener) {
211 fcpConnection.removeFcpListener(fcpListener);
215 * Adds an FCP client listener to the list of registered listeners.
217 * @param fcpClientListener
218 * The FCP client listener to add
220 public void addFcpClientListener(FcpClientListener fcpClientListener) {
221 fcpClientListenerManager.addListener(fcpClientListener);
225 * Removes an FCP client listener from the list of registered listeners.
227 * @param fcpClientListener
228 * The FCP client listener to remove
230 public void removeFcpClientListener(FcpClientListener fcpClientListener) {
231 fcpClientListenerManager.removeListener(fcpClientListener);
239 * Returns the {@link NodeHello} object that the node returned when
242 * @return The {@code NodeHello} data container
244 public NodeHello getNodeHello() {
249 * Returns the underlying FCP connection.
251 * @return The underlying FCP connection
253 public FcpConnection getConnection() {
254 return fcpConnection;
262 * Connects the FCP client.
265 * The name of the client
266 * @throws IOException
267 * if an I/O error occurs
268 * @throws FcpException
269 * if an FCP error occurs
271 public void connect(final String name) throws IOException, FcpException {
272 checkConnected(false);
274 new ExtendedFcpAdapter() {
280 @SuppressWarnings("synthetic-access")
281 public void run() throws IOException {
282 fcpConnection.connect();
283 ClientHello clientHello = new ClientHello(name);
284 fcpConnection.sendMessage(clientHello);
285 WatchGlobal watchGlobal = new WatchGlobal(true);
286 fcpConnection.sendMessage(watchGlobal);
293 @SuppressWarnings("synthetic-access")
294 public void receivedNodeHello(FcpConnection fcpConnection, NodeHello nodeHello) {
295 FcpClient.this.nodeHello = nodeHello;
296 completionLatch.countDown();
302 * Disconnects the FCP client.
304 public void disconnect() {
305 synchronized (syncObject) {
306 fcpConnection.close();
307 syncObject.notifyAll();
312 * Returns whether this client is currently connected.
314 * @return {@code true} if the client is currently connected, {@code false}
317 public boolean isConnected() {
326 * Returns all peers that the node has.
328 * @param withMetadata
329 * <code>true</code> to include peer metadata
330 * @param withVolatile
331 * <code>true</code> to include volatile peer data
332 * @return A set containing the node’s peers
333 * @throws IOException
334 * if an I/O error occurs
335 * @throws FcpException
336 * if an FCP error occurs
338 public Collection<Peer> getPeers(final boolean withMetadata, final boolean withVolatile) throws IOException, FcpException {
339 final Set<Peer> peers = Collections.synchronizedSet(new HashSet<Peer>());
340 new ExtendedFcpAdapter() {
342 /** The ID of the “ListPeers” request. */
343 @SuppressWarnings("synthetic-access")
344 private String identifier = createIdentifier("list-peers");
350 @SuppressWarnings("synthetic-access")
351 public void run() throws IOException {
352 fcpConnection.sendMessage(new ListPeers(identifier, withMetadata, withVolatile));
359 public void receivedPeer(FcpConnection fcpConnection, Peer peer) {
360 if (peer.getIdentifier().equals(identifier)) {
369 public void receivedEndListPeers(FcpConnection fcpConnection, EndListPeers endListPeers) {
370 if (endListPeers.getIdentifier().equals(identifier)) {
371 completionLatch.countDown();
379 * Returns all darknet peers.
381 * @param withMetadata
382 * <code>true</code> to include peer metadata
383 * @param withVolatile
384 * <code>true</code> to include volatile peer data
385 * @return A set containing the node’s darknet peers
386 * @throws IOException
387 * if an I/O error occurs
388 * @throws FcpException
389 * if an FCP error occurs
391 public Collection<Peer> getDarknetPeers(boolean withMetadata, boolean withVolatile) throws IOException, FcpException {
392 Collection<Peer> allPeers = getPeers(withMetadata, withVolatile);
393 Collection<Peer> darknetPeers = new HashSet<Peer>();
394 for (Peer peer : allPeers) {
395 if (!peer.isOpennet() && !peer.isSeed()) {
396 darknetPeers.add(peer);
403 * Returns all opennet peers.
405 * @param withMetadata
406 * <code>true</code> to include peer metadata
407 * @param withVolatile
408 * <code>true</code> to include volatile peer data
409 * @return A set containing the node’s opennet peers
410 * @throws IOException
411 * if an I/O error occurs
412 * @throws FcpException
413 * if an FCP error occurs
415 public Collection<Peer> getOpennetPeers(boolean withMetadata, boolean withVolatile) throws IOException, FcpException {
416 Collection<Peer> allPeers = getPeers(withMetadata, withVolatile);
417 Collection<Peer> opennetPeers = new HashSet<Peer>();
418 for (Peer peer : allPeers) {
419 if (peer.isOpennet() && !peer.isSeed()) {
420 opennetPeers.add(peer);
427 * Returns all seed peers.
429 * @param withMetadata
430 * <code>true</code> to include peer metadata
431 * @param withVolatile
432 * <code>true</code> to include volatile peer data
433 * @return A set containing the node’s seed peers
434 * @throws IOException
435 * if an I/O error occurs
436 * @throws FcpException
437 * if an FCP error occurs
439 public Collection<Peer> getSeedPeers(boolean withMetadata, boolean withVolatile) throws IOException, FcpException {
440 Collection<Peer> allPeers = getPeers(withMetadata, withVolatile);
441 Collection<Peer> seedPeers = new HashSet<Peer>();
442 for (Peer peer : allPeers) {
451 * Adds the given peer to the node.
455 * @throws IOException
456 * if an I/O error occurs
457 * @throws FcpException
458 * if an FCP error occurs
460 public void addPeer(Peer peer) throws IOException, FcpException {
461 addPeer(peer.getNodeRef());
465 * Adds the peer defined by the noderef to the node.
468 * The noderef that defines the new peer
469 * @throws IOException
470 * if an I/O error occurs
471 * @throws FcpException
472 * if an FCP error occurs
474 public void addPeer(NodeRef nodeRef) throws IOException, FcpException {
475 addPeer(new AddPeer(nodeRef));
479 * Adds a peer, reading the noderef from the given URL.
482 * The URL to read the noderef from
483 * @throws IOException
484 * if an I/O error occurs
485 * @throws FcpException
486 * if an FCP error occurs
488 public void addPeer(URL url) throws IOException, FcpException {
489 addPeer(new AddPeer(url));
493 * Adds a peer, reading the noderef of the peer from the given file.
494 * <strong>Note:</strong> the file to read the noderef from has to reside on
495 * the same machine as the node!
498 * The name of the file containing the peer’s noderef
499 * @throws IOException
500 * if an I/O error occurs
501 * @throws FcpException
502 * if an FCP error occurs
504 public void addPeer(String file) throws IOException, FcpException {
505 addPeer(new AddPeer(file));
509 * Sends the given {@link AddPeer} message to the node. This method should
510 * not be called directly. Use one of {@link #addPeer(Peer)},
511 * {@link #addPeer(NodeRef)}, {@link #addPeer(URL)}, or
512 * {@link #addPeer(String)} instead.
515 * The “AddPeer” message
516 * @throws IOException
517 * if an I/O error occurs
518 * @throws FcpException
519 * if an FCP error occurs
521 private void addPeer(final AddPeer addPeer) throws IOException, FcpException {
522 new ExtendedFcpAdapter() {
528 @SuppressWarnings("synthetic-access")
529 public void run() throws IOException {
530 fcpConnection.sendMessage(addPeer);
537 public void receivedPeer(FcpConnection fcpConnection, Peer peer) {
538 completionLatch.countDown();
544 * Modifies the given peer.
548 * @param allowLocalAddresses
549 * <code>true</code> to allow local address, <code>false</code>
550 * to not allow local address, <code>null</code> to not change
553 * <code>true</code> to disable the peer, <code>false</code> to
554 * enable the peer, <code>null</code> to not change the setting
556 * <code>true</code> to enable “listen only” for the peer,
557 * <code>false</code> to disable it, <code>null</code> to not
559 * @throws IOException
560 * if an I/O error occurs
561 * @throws FcpException
562 * if an FCP error occurs
564 public void modifyPeer(final Peer peer, final Boolean allowLocalAddresses, final Boolean disabled, final Boolean listenOnly) throws IOException, FcpException {
565 new ExtendedFcpAdapter() {
571 @SuppressWarnings("synthetic-access")
572 public void run() throws IOException {
573 fcpConnection.sendMessage(new ModifyPeer(peer.getIdentity(), allowLocalAddresses, disabled, listenOnly));
580 public void receivedPeer(FcpConnection fcpConnection, Peer peer) {
581 completionLatch.countDown();
587 * Removes the given peer.
591 * @throws IOException
592 * if an I/O error occurs
593 * @throws FcpException
594 * if an FCP error occurs
596 public void removePeer(final Peer peer) throws IOException, FcpException {
597 new ExtendedFcpAdapter() {
603 @SuppressWarnings("synthetic-access")
604 public void run() throws IOException {
605 fcpConnection.sendMessage(new RemovePeer(peer.getIdentity()));
612 public void receivedPeerRemoved(FcpConnection fcpConnection, PeerRemoved peerRemoved) {
613 completionLatch.countDown();
619 // PEER NOTES MANAGEMENT
623 * Returns the peer note of the given peer.
626 * The peer to get the note for
627 * @return The peer’s note
628 * @throws IOException
629 * if an I/O error occurs
630 * @throws FcpException
631 * if an FCP error occurs
633 public PeerNote getPeerNote(final Peer peer) throws IOException, FcpException {
634 final ObjectWrapper<PeerNote> objectWrapper = new ObjectWrapper<PeerNote>();
635 new ExtendedFcpAdapter() {
641 @SuppressWarnings("synthetic-access")
642 public void run() throws IOException {
643 fcpConnection.sendMessage(new ListPeerNotes(peer.getIdentity()));
650 public void receivedPeerNote(FcpConnection fcpConnection, PeerNote peerNote) {
651 if (peerNote.getNodeIdentifier().equals(peer.getIdentity())) {
652 objectWrapper.set(peerNote);
660 public void receivedEndListPeerNotes(FcpConnection fcpConnection, EndListPeerNotes endListPeerNotes) {
661 completionLatch.countDown();
664 return objectWrapper.get();
668 * Replaces the peer note for the given peer.
673 * The new base64-encoded note text
675 * The type of the note (currently only <code>1</code> is
677 * @throws IOException
678 * if an I/O error occurs
679 * @throws FcpException
680 * if an FCP error occurs
682 public void modifyPeerNote(final Peer peer, final String noteText, final int noteType) throws IOException, FcpException {
683 new ExtendedFcpAdapter() {
689 @SuppressWarnings("synthetic-access")
690 public void run() throws IOException {
691 fcpConnection.sendMessage(new ModifyPeerNote(peer.getIdentity(), noteText, noteType));
698 public void receivedPeer(FcpConnection fcpConnection, Peer receivedPeer) {
699 if (receivedPeer.getIdentity().equals(peer.getIdentity())) {
700 completionLatch.countDown();
711 * Generates a new SSK key pair.
713 * @return The generated key pair
714 * @throws IOException
715 * if an I/O error occurs
716 * @throws FcpException
717 * if an FCP error occurs
719 public SSKKeypair generateKeyPair() throws IOException, FcpException {
720 final ObjectWrapper<SSKKeypair> sskKeypairWrapper = new ObjectWrapper<SSKKeypair>();
721 new ExtendedFcpAdapter() {
727 @SuppressWarnings("synthetic-access")
728 public void run() throws IOException {
729 fcpConnection.sendMessage(new GenerateSSK());
736 public void receivedSSKKeypair(FcpConnection fcpConnection, SSKKeypair sskKeypair) {
737 sskKeypairWrapper.set(sskKeypair);
738 completionLatch.countDown();
741 return sskKeypairWrapper.get();
745 // REQUEST MANAGEMENT
749 * Returns all currently visible persistent get requests.
752 * <code>true</code> to return get requests from the global
753 * queue, <code>false</code> to only show requests from the
755 * @return All get requests
756 * @throws IOException
757 * if an I/O error occurs
758 * @throws FcpException
759 * if an FCP error occurs
761 public Collection<Request> getGetRequests(final boolean global) throws IOException, FcpException {
762 return Filters.filteredCollection(getRequests(global), new Filter<Request>() {
767 public boolean filterObject(Request request) {
768 return request instanceof GetRequest;
774 * Returns all currently visible persistent put requests.
777 * <code>true</code> to return put requests from the global
778 * queue, <code>false</code> to only show requests from the
780 * @return All put requests
781 * @throws IOException
782 * if an I/O error occurs
783 * @throws FcpException
784 * if an FCP error occurs
786 public Collection<Request> getPutRequests(final boolean global) throws IOException, FcpException {
787 return Filters.filteredCollection(getRequests(global), new Filter<Request>() {
792 public boolean filterObject(Request request) {
793 return request instanceof PutRequest;
799 * Returns all currently visible persistent requests.
802 * <code>true</code> to return requests from the global queue,
803 * <code>false</code> to only show requests from the client-local
805 * @return All requests
806 * @throws IOException
807 * if an I/O error occurs
808 * @throws FcpException
809 * if an FCP error occurs
811 public Collection<Request> getRequests(final boolean global) throws IOException, FcpException {
812 final Map<String, Request> requests = Collections.synchronizedMap(new HashMap<String, Request>());
813 new ExtendedFcpAdapter() {
819 @SuppressWarnings("synthetic-access")
820 public void run() throws IOException {
821 fcpConnection.sendMessage(new ListPersistentRequests());
828 public void receivedPersistentGet(FcpConnection fcpConnection, PersistentGet persistentGet) {
829 if (!persistentGet.isGlobal() || global) {
830 GetRequest getRequest = new GetRequest(persistentGet);
831 requests.put(persistentGet.getIdentifier(), getRequest);
838 * @see net.pterodactylus.fcp.FcpAdapter#receivedDataFound(net.pterodactylus.fcp.FcpConnection,
839 * net.pterodactylus.fcp.DataFound)
842 public void receivedDataFound(FcpConnection fcpConnection, DataFound dataFound) {
843 Request getRequest = requests.get(dataFound.getIdentifier());
844 if (getRequest == null) {
847 getRequest.setComplete(true);
848 getRequest.setLength(dataFound.getDataLength());
849 getRequest.setContentType(dataFound.getMetadataContentType());
855 * @see net.pterodactylus.fcp.FcpAdapter#receivedGetFailed(net.pterodactylus.fcp.FcpConnection,
856 * net.pterodactylus.fcp.GetFailed)
859 public void receivedGetFailed(FcpConnection fcpConnection, GetFailed getFailed) {
860 Request getRequest = requests.get(getFailed.getIdentifier());
861 if (getRequest == null) {
864 getRequest.setComplete(true);
865 getRequest.setFailed(true);
866 getRequest.setFatal(getFailed.isFatal());
867 getRequest.setErrorCode(getFailed.getCode());
873 * @see net.pterodactylus.fcp.FcpAdapter#receivedPersistentPut(net.pterodactylus.fcp.FcpConnection,
874 * net.pterodactylus.fcp.PersistentPut)
877 public void receivedPersistentPut(FcpConnection fcpConnection, PersistentPut persistentPut) {
878 if (!persistentPut.isGlobal() || global) {
879 PutRequest putRequest = new PutRequest(persistentPut);
880 requests.put(persistentPut.getIdentifier(), putRequest);
887 * @see net.pterodactylus.fcp.FcpAdapter#receivedSimpleProgress(net.pterodactylus.fcp.FcpConnection,
888 * net.pterodactylus.fcp.SimpleProgress)
891 public void receivedSimpleProgress(FcpConnection fcpConnection, SimpleProgress simpleProgress) {
892 Request request = requests.get(simpleProgress.getIdentifier());
893 if (request == null) {
896 request.setTotalBlocks(simpleProgress.getTotal());
897 request.setRequiredBlocks(simpleProgress.getRequired());
898 request.setFailedBlocks(simpleProgress.getFailed());
899 request.setFatallyFailedBlocks(simpleProgress.getFatallyFailed());
900 request.setSucceededBlocks(simpleProgress.getSucceeded());
901 request.setFinalizedTotal(simpleProgress.isFinalizedTotal());
908 public void receivedEndListPersistentRequests(FcpConnection fcpConnection, EndListPersistentRequests endListPersistentRequests) {
909 completionLatch.countDown();
912 return requests.values();
916 * Sends a message to a plugin and waits for the response.
919 * The name of the plugin class
921 * The parameters for the plugin
922 * @return The responses from the plugin
923 * @throws FcpException
924 * if an FCP error occurs
925 * @throws IOException
926 * if an I/O error occurs
928 public Map<String, String> sendPluginMessage(String pluginClass, Map<String, String> parameters) throws IOException, FcpException {
929 return sendPluginMessage(pluginClass, parameters, 0, null);
933 * Sends a message to a plugin and waits for the response.
936 * The name of the plugin class
938 * The parameters for the plugin
940 * The length of the optional data stream, or {@code 0} if there
941 * is no optional data stream
942 * @param dataInputStream
943 * The input stream for the payload, or {@code null} if there is
945 * @return The responses from the plugin
946 * @throws FcpException
947 * if an FCP error occurs
948 * @throws IOException
949 * if an I/O error occurs
951 public Map<String, String> sendPluginMessage(final String pluginClass, final Map<String, String> parameters, final long dataLength, final InputStream dataInputStream) throws IOException, FcpException {
952 final Map<String, String> pluginReplies = Collections.synchronizedMap(new HashMap<String, String>());
953 new ExtendedFcpAdapter() {
955 @SuppressWarnings("synthetic-access")
956 private final String identifier = createIdentifier("FCPPluginMessage");
959 @SuppressWarnings("synthetic-access")
960 public void run() throws IOException {
961 FCPPluginMessage fcpPluginMessage = new FCPPluginMessage(pluginClass);
962 for (Entry<String, String> parameter : parameters.entrySet()) {
963 fcpPluginMessage.setParameter(parameter.getKey(), parameter.getValue());
965 fcpPluginMessage.setIdentifier(identifier);
966 if ((dataLength > 0) && (dataInputStream != null)) {
967 fcpPluginMessage.setDataLength(dataLength);
968 fcpPluginMessage.setPayloadInputStream(dataInputStream);
970 fcpConnection.sendMessage(fcpPluginMessage);
977 public void receivedFCPPluginReply(FcpConnection fcpConnection, FCPPluginReply fcpPluginReply) {
978 if (!fcpPluginReply.getIdentifier().equals(identifier)) {
981 pluginReplies.putAll(fcpPluginReply.getReplies());
982 completionLatch.countDown();
986 return pluginReplies;
994 * Returns information about the node.
996 * @param giveOpennetRef
997 * Whether to return the OpenNet reference
999 * Whether to return private node data
1000 * @param withVolatile
1001 * Whether to return volatile node data
1002 * @return Node information
1003 * @throws FcpException
1004 * if an FCP error occurs
1005 * @throws IOException
1006 * if an I/O error occurs
1008 public NodeData getNodeInformation(final Boolean giveOpennetRef, final Boolean withPrivate, final Boolean withVolatile) throws IOException, FcpException {
1009 final ObjectWrapper<NodeData> nodeDataWrapper = new ObjectWrapper<NodeData>();
1010 new ExtendedFcpAdapter() {
1013 @SuppressWarnings("synthetic-access")
1014 public void run() throws IOException {
1015 GetNode getNodeMessage = new GetNode(giveOpennetRef, withPrivate, withVolatile);
1016 fcpConnection.sendMessage(getNodeMessage);
1023 public void receivedNodeData(FcpConnection fcpConnection, NodeData nodeData) {
1024 nodeDataWrapper.set(nodeData);
1025 completionLatch.countDown();
1028 return nodeDataWrapper.get();
1036 * Creates a unique request identifier.
1039 * The basename of the request
1040 * @return The created request identifier
1042 private String createIdentifier(String basename) {
1043 return basename + "-" + System.currentTimeMillis() + "-" + (int) (Math.random() * Integer.MAX_VALUE);
1047 * Checks whether the connection is in the required state.
1050 * The required connection state
1051 * @throws FcpException
1052 * if the connection is not in the required state
1054 private void checkConnected(boolean connected) throws FcpException {
1055 if (this.connected != connected) {
1056 throw new FcpException("Client is " + (connected ? "not" : "already") + " connected.");
1061 * Tells the client that it is now disconnected. This method is called by
1062 * {@link ExtendedFcpAdapter} only.
1064 private void setDisconnected() {
1069 * Implementation of an {@link FcpListener} that can store an
1070 * {@link FcpException} and wait for the arrival of a certain command.
1072 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
1074 private abstract class ExtendedFcpAdapter extends FcpAdapter {
1076 /** The count down latch used to wait for completion. */
1077 protected final CountDownLatch completionLatch = new CountDownLatch(1);
1079 /** The FCP exception, if any. */
1080 protected FcpException fcpException;
1083 * Creates a new extended FCP adapter.
1085 public ExtendedFcpAdapter() {
1090 * Executes the FCP commands in {@link #run()}, wrapping the execution
1091 * and catching exceptions.
1093 * @throws IOException
1094 * if an I/O error occurs
1095 * @throws FcpException
1096 * if an FCP error occurs
1098 @SuppressWarnings("synthetic-access")
1099 public void execute() throws IOException, FcpException {
1100 checkConnected(true);
1101 fcpConnection.addFcpListener(this);
1106 completionLatch.await();
1108 } catch (InterruptedException ie1) {
1109 /* ignore, we’ll loop. */
1112 } catch (IOException ioe1) {
1116 fcpConnection.removeFcpListener(this);
1118 if (fcpException != null) {
1125 * The FCP commands that actually get executed.
1127 * @throws IOException
1128 * if an I/O error occurs
1130 public abstract void run() throws IOException;
1136 public void connectionClosed(FcpConnection fcpConnection, Throwable throwable) {
1137 fcpException = new FcpException("Connection closed", throwable);
1138 completionLatch.countDown();
1145 public void receivedCloseConnectionDuplicateClientName(FcpConnection fcpConnection, CloseConnectionDuplicateClientName closeConnectionDuplicateClientName) {
1146 fcpException = new FcpException("Connection closed, duplicate client name");
1147 completionLatch.countDown();
1154 public void receivedProtocolError(FcpConnection fcpConnection, ProtocolError protocolError) {
1155 fcpException = new FcpException("Protocol error (" + protocolError.getCode() + ", " + protocolError.getCodeDescription());
1156 completionLatch.countDown();