3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-07 14:54:15 +00:00

Add numPeers() and getPeers() accessors to PeerGroup. Resolves issue 106.

This commit is contained in:
Mike Hearn 2011-12-27 12:04:49 +00:00
parent b9a141a96b
commit 234090e5a6
3 changed files with 29 additions and 9 deletions

View File

@ -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;
@ -157,6 +155,24 @@ public class PeerGroup {
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<Peer> getPeers() {
ArrayList<Peer> result = new ArrayList<Peer>(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
*/

View File

@ -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();
@ -111,6 +109,12 @@ public class PeerGroupTest extends TestWithNetworkConnections {
peerGroup.addPeer(p1);
peerGroup.addPeer(p2);
// Check the peer accessors.
assertEquals(2, peerGroup.numPeers());
List<Peer> 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.
Block b1 = TestUtils.createFakeBlock(params, blockStore).block;