mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-02-12 02:05:53 +00:00
Add numPeers() and getPeers() accessors to PeerGroup. Resolves issue 106.
This commit is contained in:
parent
b9a141a96b
commit
234090e5a6
@ -382,7 +382,7 @@ public class Peer {
|
|||||||
// TODO: Block locators should be abstracted out rather than special cased here.
|
// TODO: Block locators should be abstracted out rather than special cased here.
|
||||||
List<Sha256Hash> blockLocator = new LinkedList<Sha256Hash>();
|
List<Sha256Hash> blockLocator = new LinkedList<Sha256Hash>();
|
||||||
// For now we don't do the exponential thinning as suggested here:
|
// 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
|
// 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
|
// 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.
|
// chain that turned out not to be the longest. This happened roughly once a week.
|
||||||
|
@ -28,9 +28,7 @@ import java.io.IOException;
|
|||||||
import java.net.ConnectException;
|
import java.net.ConnectException;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.net.SocketTimeoutException;
|
import java.net.SocketTimeoutException;
|
||||||
import java.util.Collections;
|
import java.util.*;
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.Set;
|
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
@ -157,6 +155,24 @@ public class PeerGroup {
|
|||||||
return peerPool.getMaximumPoolSize();
|
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
|
* Add an address to the list of potential peers to connect to
|
||||||
*/
|
*/
|
||||||
|
@ -16,23 +16,21 @@
|
|||||||
|
|
||||||
package com.google.bitcoin.core;
|
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.PeerDiscovery;
|
||||||
import com.google.bitcoin.discovery.PeerDiscoveryException;
|
import com.google.bitcoin.discovery.PeerDiscoveryException;
|
||||||
import com.google.bitcoin.store.MemoryBlockStore;
|
import com.google.bitcoin.store.MemoryBlockStore;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
|
import java.util.List;
|
||||||
import java.util.concurrent.BlockingQueue;
|
import java.util.concurrent.BlockingQueue;
|
||||||
import java.util.concurrent.LinkedBlockingQueue;
|
import java.util.concurrent.LinkedBlockingQueue;
|
||||||
import java.util.concurrent.Semaphore;
|
import java.util.concurrent.Semaphore;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
public class PeerGroupTest extends TestWithNetworkConnections {
|
public class PeerGroupTest extends TestWithNetworkConnections {
|
||||||
static final NetworkParameters params = NetworkParameters.unitTests();
|
static final NetworkParameters params = NetworkParameters.unitTests();
|
||||||
|
|
||||||
@ -111,6 +109,12 @@ public class PeerGroupTest extends TestWithNetworkConnections {
|
|||||||
peerGroup.addPeer(p1);
|
peerGroup.addPeer(p1);
|
||||||
peerGroup.addPeer(p2);
|
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
|
// 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.
|
// are downloading the chain.
|
||||||
Block b1 = TestUtils.createFakeBlock(params, blockStore).block;
|
Block b1 = TestUtils.createFakeBlock(params, blockStore).block;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user