Don’t forget to throw the FCP exception.
[jFCPlib.git] / src / net / pterodactylus / fcp / highlevel / FcpClient.java
index bcf529b..f9445c9 100644 (file)
@@ -22,14 +22,19 @@ package net.pterodactylus.fcp.highlevel;
 import java.io.IOException;
 import java.net.InetAddress;
 import java.net.UnknownHostException;
+import java.util.HashSet;
+import java.util.Set;
 import java.util.concurrent.CountDownLatch;
 
 import net.pterodactylus.fcp.ClientHello;
 import net.pterodactylus.fcp.CloseConnectionDuplicateClientName;
+import net.pterodactylus.fcp.EndListPeers;
 import net.pterodactylus.fcp.FcpAdapter;
 import net.pterodactylus.fcp.FcpConnection;
 import net.pterodactylus.fcp.FcpListener;
+import net.pterodactylus.fcp.ListPeers;
 import net.pterodactylus.fcp.NodeHello;
+import net.pterodactylus.fcp.Peer;
 import net.pterodactylus.fcp.ProtocolError;
 
 /**
@@ -142,18 +147,21 @@ public class FcpClient {
                        }
                };
                fcpConnection.addFcpListener(fcpListener);
-               fcpConnection.connect();
-               ClientHello clientHello = new ClientHello(name);
-               fcpConnection.sendMessage(clientHello);
-               while (true) {
-                       try {
-                               fcpListener.complete();
-                               break;
-                       } catch (InterruptedException e) {
-                               /* ignore, we’ll loop. */
+               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);
                }
-               fcpConnection.removeFcpListener(fcpListener);
                if (fcpListener.getFcpException() != null) {
                        throw fcpListener.getFcpException();
                }
@@ -169,6 +177,59 @@ public class FcpClient {
                }
        }
 
+       //
+       // PEER MANAGEMENT
+       //
+
+       /**
+        * Returns all peers that the node has.
+        *
+        * @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 fcpAdapter = new ExtendedFcpAdapter() {
+
+                       /**
+                        * {@inheritDoc}
+                        */
+                       @Override
+                       public void receivedPeer(FcpConnection fcpConnection, Peer peer) {
+                               peers.add(peer);
+                       }
+
+                       /**
+                        * {@inheritDoc}
+                        */
+                       @Override
+                       public void receivedEndListPeers(FcpConnection fcpConnection, EndListPeers endListPeers) {
+                               completionLatch.countDown();
+                       }
+               };
+               fcpConnection.addFcpListener(fcpAdapter);
+               fcpConnection.sendMessage(new ListPeers("list-peers"));
+               try {
+                       while (true) {
+                               try {
+                                       fcpAdapter.complete();
+                                       break;
+                               } catch (InterruptedException e) {
+                                       /* ignore, we’ll loop. */
+                               }
+                       }
+               } finally {
+                       fcpConnection.removeFcpListener(fcpAdapter);
+               }
+               if (fcpListener.getFcpException() != null) {
+                       throw fcpListener.getFcpException();
+               }
+               return peers;
+       }
+
        /**
         * Implementation of an {@link FcpListener} that can store an
         * {@link FcpException} and wait for the arrival of a certain command.