From 26bee5876c5048ab5673d3076b8e614028c5cee0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 19 Mar 2009 00:48:00 +0100 Subject: [PATCH] Add method getPeers(). --- src/net/pterodactylus/fcp/highlevel/FcpClient.java | 53 ++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/src/net/pterodactylus/fcp/highlevel/FcpClient.java b/src/net/pterodactylus/fcp/highlevel/FcpClient.java index 21dc647..ecbd62d 100644 --- a/src/net/pterodactylus/fcp/highlevel/FcpClient.java +++ b/src/net/pterodactylus/fcp/highlevel/FcpClient.java @@ -22,14 +22,18 @@ 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.NodeHello; +import net.pterodactylus.fcp.Peer; import net.pterodactylus.fcp.ProtocolError; /** @@ -172,6 +176,55 @@ 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 getPeers() throws IOException, FcpException { + final Set peers = new HashSet(); + 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); + try { + while (true) { + try { + fcpAdapter.complete(); + break; + } catch (InterruptedException e) { + /* ignore, we’ll loop. */ + } + } + } finally { + fcpConnection.removeFcpListener(fcpAdapter); + } + return peers; + } + /** * Implementation of an {@link FcpListener} that can store an * {@link FcpException} and wait for the arrival of a certain command. -- 2.7.4