3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-01-31 07:12:17 +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

@ -382,7 +382,7 @@ public class Peer {
// TODO: Block locators should be abstracted out rather than special cased here.
List<Sha256Hash> blockLocator = new LinkedList<Sha256Hash>();
// 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.

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;
@ -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<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();
@ -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<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.