Use distinct identifier for every “ListPeers” request.
[jFCPlib.git] / src / net / pterodactylus / fcp / highlevel / FcpClient.java
index 5a3ec09..71b118e 100644 (file)
@@ -23,6 +23,7 @@ import java.io.IOException;
 import java.net.InetAddress;
 import java.net.URL;
 import java.net.UnknownHostException;
+import java.util.Collections;
 import java.util.HashSet;
 import java.util.Set;
 import java.util.concurrent.CountDownLatch;
@@ -30,16 +31,22 @@ import java.util.concurrent.CountDownLatch;
 import net.pterodactylus.fcp.AddPeer;
 import net.pterodactylus.fcp.ClientHello;
 import net.pterodactylus.fcp.CloseConnectionDuplicateClientName;
+import net.pterodactylus.fcp.EndListPeerNotes;
 import net.pterodactylus.fcp.EndListPeers;
 import net.pterodactylus.fcp.FcpAdapter;
 import net.pterodactylus.fcp.FcpConnection;
 import net.pterodactylus.fcp.FcpListener;
+import net.pterodactylus.fcp.ListPeerNotes;
 import net.pterodactylus.fcp.ListPeers;
 import net.pterodactylus.fcp.ModifyPeer;
 import net.pterodactylus.fcp.NodeHello;
 import net.pterodactylus.fcp.NodeRef;
 import net.pterodactylus.fcp.Peer;
+import net.pterodactylus.fcp.PeerNote;
+import net.pterodactylus.fcp.PeerRemoved;
 import net.pterodactylus.fcp.ProtocolError;
+import net.pterodactylus.fcp.RemovePeer;
+import net.pterodactylus.util.thread.ObjectWrapper;
 
 /**
  * High-level FCP client that hides the details of the underlying FCP
@@ -140,7 +147,18 @@ public class FcpClient {
         *             if an FCP error occurs
         */
        public void connect() throws IOException, FcpException {
-               ExtendedFcpAdapter fcpListener = new ExtendedFcpAdapter() {
+               new ExtendedFcpAdapter() {
+
+                       /**
+                        * {@inheritDoc}
+                        */
+                       @Override
+                       @SuppressWarnings("synthetic-access")
+                       public void run() throws IOException {
+                               fcpConnection.connect();
+                               ClientHello clientHello = new ClientHello(name);
+                               fcpConnection.sendMessage(clientHello);
+                       }
 
                        /**
                         * {@inheritDoc}
@@ -149,26 +167,7 @@ public class FcpClient {
                        public void receivedNodeHello(FcpConnection fcpConnection, NodeHello nodeHello) {
                                completionLatch.countDown();
                        }
-               };
-               fcpConnection.addFcpListener(fcpListener);
-               try {
-                       fcpConnection.connect();
-                       ClientHello clientHello = new ClientHello(name);
-                       fcpConnection.sendMessage(clientHello);
-                       while (true) {
-                               try {
-                                       fcpListener.complete();
-                                       break;
-                               } catch (InterruptedException e) {
-                                       /* ignore, we’ll loop. */
-                               }
-                       }
-               } finally {
-                       fcpConnection.removeFcpListener(fcpListener);
-               }
-               if (fcpListener.getFcpException() != null) {
-                       throw fcpListener.getFcpException();
-               }
+               }.execute();
        }
 
        /**
@@ -188,22 +187,40 @@ public class FcpClient {
        /**
         * Returns all peers that the node has.
         *
+        * @param withMetadata
+        *            <code>true</code> to include peer metadata
+        * @param withVolatile
+        *            <code>true</code> to include volatile peer data
         * @return A set containing the node’s peers
         * @throws IOException
         *             if an I/O error occurs
         * @throws FcpException
         *             if an FCP error occurs
         */
-       public Set<Peer> getPeers() throws IOException, FcpException {
-               final Set<Peer> peers = new HashSet<Peer>();
-               ExtendedFcpAdapter fcpListener = new ExtendedFcpAdapter() {
+       public Set<Peer> getPeers(final boolean withMetadata, final boolean withVolatile) throws IOException, FcpException {
+               final Set<Peer> peers = Collections.synchronizedSet(new HashSet<Peer>());
+               new ExtendedFcpAdapter() {
+
+                       /** The ID of the “ListPeers” request. */
+                       private String identifier = "list-peers-" + System.currentTimeMillis();
+
+                       /**
+                        * {@inheritDoc}
+                        */
+                       @Override
+                       @SuppressWarnings("synthetic-access")
+                       public void run() throws IOException {
+                               fcpConnection.sendMessage(new ListPeers(identifier, withMetadata, withVolatile));
+                       }
 
                        /**
                         * {@inheritDoc}
                         */
                        @Override
                        public void receivedPeer(FcpConnection fcpConnection, Peer peer) {
-                               peers.add(peer);
+                               if (peer.getIdentifier().equals(identifier)) {
+                                       peers.add(peer);
+                               }
                        }
 
                        /**
@@ -211,26 +228,11 @@ public class FcpClient {
                         */
                        @Override
                        public void receivedEndListPeers(FcpConnection fcpConnection, EndListPeers endListPeers) {
-                               completionLatch.countDown();
-                       }
-               };
-               fcpConnection.addFcpListener(fcpListener);
-               fcpConnection.sendMessage(new ListPeers("list-peers"));
-               try {
-                       while (true) {
-                               try {
-                                       fcpListener.complete();
-                                       break;
-                               } catch (InterruptedException e) {
-                                       /* ignore, we’ll loop. */
+                               if (endListPeers.getIdentifier().equals(identifier)) {
+                                       completionLatch.countDown();
                                }
                        }
-               } finally {
-                       fcpConnection.removeFcpListener(fcpListener);
-               }
-               if (fcpListener.getFcpException() != null) {
-                       throw fcpListener.getFcpException();
-               }
+               }.execute();
                return peers;
        }
 
@@ -305,8 +307,17 @@ public class FcpClient {
         * @throws FcpException
         *             if an FCP error occurs
         */
-       private void addPeer(AddPeer addPeer) throws IOException, FcpException {
-               ExtendedFcpAdapter fcpListener = new ExtendedFcpAdapter() {
+       private void addPeer(final AddPeer addPeer) throws IOException, FcpException {
+               new ExtendedFcpAdapter() {
+
+                       /**
+                        * {@inheritDoc}
+                        */
+                       @Override
+                       @SuppressWarnings("synthetic-access")
+                       public void run() throws IOException {
+                               fcpConnection.sendMessage(addPeer);
+                       }
 
                        /**
                         * {@inheritDoc}
@@ -315,24 +326,7 @@ public class FcpClient {
                        public void receivedPeer(FcpConnection fcpConnection, Peer peer) {
                                completionLatch.countDown();
                        }
-               };
-               fcpConnection.addFcpListener(fcpListener);
-               try {
-                       fcpConnection.sendMessage(addPeer);
-                       while (true) {
-                               try {
-                                       fcpListener.complete();
-                                       break;
-                               } catch (InterruptedException ie1) {
-                                       /* ignore, we’ll loop. */
-                               }
-                       }
-               } finally {
-                       fcpConnection.removeFcpListener(fcpListener);
-               }
-               if (fcpListener.getFcpException() != null) {
-                       throw fcpListener.getFcpException();
-               }
+               }.execute();
        }
 
        /**
@@ -356,8 +350,17 @@ public class FcpClient {
         * @throws FcpException
         *             if an FCP error occurs
         */
-       public void modifyPeer(Peer peer, Boolean allowLocalAddresses, Boolean disabled, Boolean listenOnly) throws IOException, FcpException {
-               ExtendedFcpAdapter fcpListener = new ExtendedFcpAdapter() {
+       public void modifyPeer(final Peer peer, final Boolean allowLocalAddresses, final Boolean disabled, final Boolean listenOnly) throws IOException, FcpException {
+               new ExtendedFcpAdapter() {
+
+                       /**
+                        * {@inheritDoc}
+                        */
+                       @Override
+                       @SuppressWarnings("synthetic-access")
+                       public void run() throws IOException {
+                               fcpConnection.sendMessage(new ModifyPeer(peer.getIdentity(), allowLocalAddresses, disabled, listenOnly));
+                       }
 
                        /**
                         * {@inheritDoc}
@@ -366,16 +369,88 @@ public class FcpClient {
                        public void receivedPeer(FcpConnection fcpConnection, Peer peer) {
                                completionLatch.countDown();
                        }
-               };
-               fcpConnection.addFcpListener(fcpListener);
-               try {
-                       fcpConnection.sendMessage(new ModifyPeer(peer.getIdentity(), allowLocalAddresses, disabled, listenOnly));
-               } finally {
-                       fcpConnection.removeFcpListener(fcpListener);
-               }
-               if (fcpListener.getFcpException() != null) {
-                       throw fcpListener.getFcpException();
-               }
+               }.execute();
+       }
+
+       /**
+        * Removes the given peer.
+        *
+        * @param peer
+        *            The peer to remove
+        * @throws IOException
+        *             if an I/O error occurs
+        * @throws FcpException
+        *             if an FCP error occurs
+        */
+       public void removePeer(final Peer peer) throws IOException, FcpException {
+               new ExtendedFcpAdapter() {
+
+                       /**
+                        * {@inheritDoc}
+                        */
+                       @Override
+                       @SuppressWarnings("synthetic-access")
+                       public void run() throws IOException {
+                               fcpConnection.sendMessage(new RemovePeer(peer.getIdentity()));
+                       }
+
+                       /**
+                        * {@inheritDoc}
+                        */
+                       @Override
+                       public void receivedPeerRemoved(FcpConnection fcpConnection, PeerRemoved peerRemoved) {
+                               completionLatch.countDown();
+                       }
+               }.execute();
+       }
+
+       //
+       // PEER NOTES MANAGEMENT
+       //
+
+       /**
+        * Returns the peer note of the given peer.
+        *
+        * @param peer
+        *            The peer to get the note for
+        * @return The peer’s note
+        * @throws IOException
+        *             if an I/O error occurs
+        * @throws FcpException
+        *             if an FCP error occurs
+        */
+       public PeerNote getPeerNote(final Peer peer) throws IOException, FcpException {
+               final ObjectWrapper<PeerNote> objectWrapper = new ObjectWrapper<PeerNote>();
+               new ExtendedFcpAdapter() {
+
+                       /**
+                        * {@inheritDoc}
+                        */
+                       @Override
+                       @SuppressWarnings("synthetic-access")
+                       public void run() throws IOException {
+                               fcpConnection.sendMessage(new ListPeerNotes(peer.getIdentity()));
+                       }
+
+                       /**
+                        * {@inheritDoc}
+                        */
+                       @Override
+                       public void receivedPeerNote(FcpConnection fcpConnection, PeerNote peerNote) {
+                               if (peerNote.getNodeIdentifier().equals(peer.getIdentity())) {
+                                       objectWrapper.set(peerNote);
+                               }
+                       }
+
+                       /**
+                        * {@inheritDoc}
+                        */
+                       @Override
+                       public void receivedEndListPeerNotes(FcpConnection fcpConnection, EndListPeerNotes endListPeerNotes) {
+                               completionLatch.countDown();
+                       }
+               }.execute();
+               return objectWrapper.get();
        }
 
        /**
@@ -384,7 +459,7 @@ public class FcpClient {
         *
         * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
         */
-       private static class ExtendedFcpAdapter extends FcpAdapter {
+       private abstract class ExtendedFcpAdapter extends FcpAdapter {
 
                /** The count down latch used to wait for completion. */
                protected final CountDownLatch completionLatch = new CountDownLatch(1);
@@ -400,24 +475,42 @@ public class FcpClient {
                }
 
                /**
-                * Returns the FCP exception that occured. If no FCP exception occured,
-                * <code>null</code> is returned.
+                * Executes the FCP commands in {@link #run()}, wrapping the execution
+                * and catching exceptions.
                 *
-                * @return The FCP exception that occured, or <code>null</code>
+                * @throws IOException
+                *             if an I/O error occurs
+                * @throws FcpException
+                *             if an FCP error occurs
                 */
-               public FcpException getFcpException() {
-                       return fcpException;
+               @SuppressWarnings("synthetic-access")
+               public void execute() throws IOException, FcpException {
+                       fcpConnection.addFcpListener(this);
+                       try {
+                               run();
+                               while (true) {
+                                       try {
+                                               completionLatch.await();
+                                               break;
+                                       } catch (InterruptedException ie1) {
+                                               /* ignore, we’ll loop. */
+                                       }
+                               }
+                       } finally {
+                               fcpConnection.removeFcpListener(this);
+                       }
+                       if (fcpException != null) {
+                               throw fcpException;
+                       }
                }
 
                /**
-                * Waits for the completion of the command.
+                * The FCP commands that actually get executed.
                 *
-                * @throws InterruptedException
-                *             if {@link CountDownLatch#await()} is interrupted
+                * @throws IOException
+                *             if an I/O error occurs
                 */
-               public void complete() throws InterruptedException {
-                       completionLatch.await();
-               }
+               public abstract void run() throws IOException;
 
                /**
                 * {@inheritDoc}