From 234090e5a6aa95f5a47b9a4b6bf9abad5b688c2d Mon Sep 17 00:00:00 2001 From: Mike Hearn Date: Tue, 27 Dec 2011 12:04:49 +0000 Subject: [PATCH] Add numPeers() and getPeers() accessors to PeerGroup. Resolves issue 106. --- src/com/google/bitcoin/core/Peer.java | 2 +- src/com/google/bitcoin/core/PeerGroup.java | 22 ++++++++++++++++--- .../google/bitcoin/core/PeerGroupTest.java | 14 +++++++----- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/com/google/bitcoin/core/Peer.java b/src/com/google/bitcoin/core/Peer.java index bbd8eddc..5c880868 100644 --- a/src/com/google/bitcoin/core/Peer.java +++ b/src/com/google/bitcoin/core/Peer.java @@ -382,7 +382,7 @@ public class Peer { // TODO: Block locators should be abstracted out rather than special cased here. List blockLocator = new LinkedList(); // For now we don't do the exponential thinning as suggested here: - // https://en.bitcoin.it/wiki/Protocol_specification#getblocks + // https://en.bitcoin.it/wiki/Protocol_specification#getblocks // However, this should be taken seriously going forward. The old implementation only added the hash of the // genesis block and the current chain head, which randomly led us to halt block fetching when ending on a // chain that turned out not to be the longest. This happened roughly once a week. diff --git a/src/com/google/bitcoin/core/PeerGroup.java b/src/com/google/bitcoin/core/PeerGroup.java index a53fb025..18500f4d 100644 --- a/src/com/google/bitcoin/core/PeerGroup.java +++ b/src/com/google/bitcoin/core/PeerGroup.java @@ -28,9 +28,7 @@ import java.io.IOException; import java.net.ConnectException; import java.net.InetSocketAddress; import java.net.SocketTimeoutException; -import java.util.Collections; -import java.util.HashSet; -import java.util.Set; +import java.util.*; import java.util.concurrent.*; import java.util.concurrent.atomic.AtomicInteger; @@ -156,6 +154,24 @@ public class PeerGroup { public synchronized int getMaxConnections() { return peerPool.getMaximumPoolSize(); } + + /** + * Returns a newly allocated list containing the currently connected peers. If all you care about is the count, + * use numPeers(). + */ + public synchronized List getPeers() { + ArrayList result = new ArrayList(peers.size()); + result.addAll(peers); + return result; + } + + /** + * Returns the number of currently connected peers. To be informed when this count changes, register a + * {@link PeerEventListener} and use the onPeerConnected/onPeerDisconnected methods. + */ + public synchronized int numPeers() { + return peers.size(); + } /** * Add an address to the list of potential peers to connect to diff --git a/tests/com/google/bitcoin/core/PeerGroupTest.java b/tests/com/google/bitcoin/core/PeerGroupTest.java index cf4fb546..dd25aa7b 100644 --- a/tests/com/google/bitcoin/core/PeerGroupTest.java +++ b/tests/com/google/bitcoin/core/PeerGroupTest.java @@ -16,23 +16,21 @@ package com.google.bitcoin.core; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - import com.google.bitcoin.discovery.PeerDiscovery; import com.google.bitcoin.discovery.PeerDiscoveryException; import com.google.bitcoin.store.MemoryBlockStore; - import org.junit.Before; import org.junit.Test; import java.io.IOException; import java.net.InetSocketAddress; +import java.util.List; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.Semaphore; +import static org.junit.Assert.*; + public class PeerGroupTest extends TestWithNetworkConnections { static final NetworkParameters params = NetworkParameters.unitTests(); @@ -110,6 +108,12 @@ public class PeerGroupTest extends TestWithNetworkConnections { peerGroup.start(); peerGroup.addPeer(p1); peerGroup.addPeer(p2); + + // Check the peer accessors. + assertEquals(2, peerGroup.numPeers()); + List tmp = peerGroup.getPeers(); + assertEquals(p1, tmp.get(0)); + assertEquals(p2, tmp.get(1)); // Set up a little block chain. We heard about b1 but not b2 (it is pending download). b3 is solved whilst we // are downloading the chain.