mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-02-07 14:54:15 +00:00
Reformat the file.
This commit is contained in:
parent
d2e4284930
commit
0c5408e7c6
@ -21,7 +21,6 @@ import com.google.bitcoin.discovery.PeerDiscovery;
|
||||
import com.google.bitcoin.discovery.PeerDiscoveryException;
|
||||
import com.google.bitcoin.store.BlockStore;
|
||||
import com.google.bitcoin.store.BlockStoreException;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@ -29,30 +28,26 @@ import java.io.IOException;
|
||||
import java.net.ConnectException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.RejectedExecutionException;
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
/**
|
||||
* Maintain a number of connections to peers.
|
||||
*
|
||||
* <p/>
|
||||
* <p>PeerGroup tries to maintain a constant number of connections to a set of distinct peers.
|
||||
* Each peer runs a network listener in its own thread. When a connection is lost, a new peer
|
||||
* will be tried after a delay as long as the number of connections less than the maximum.
|
||||
*
|
||||
* <p/>
|
||||
* <p>Connections are made to addresses from a provided list. When that list is exhausted,
|
||||
* we start again from the head of the list.
|
||||
*
|
||||
* <p/>
|
||||
* <p>The PeerGroup can broadcast a transaction to the currently connected set of peers. It can
|
||||
* also handle download of the blockchain from peers, restarting the process when peers die.
|
||||
*
|
||||
* @author miron@google.com (Miron Cuperman a.k.a devrandom)
|
||||
*
|
||||
*/
|
||||
public class PeerGroup {
|
||||
private static final int DEFAULT_CONNECTIONS = 4;
|
||||
@ -139,18 +134,24 @@ public class PeerGroup {
|
||||
return peerPool.getMaximumPoolSize();
|
||||
}
|
||||
|
||||
/** Add an address to the list of potential peers to connect to */
|
||||
/**
|
||||
* Add an address to the list of potential peers to connect to
|
||||
*/
|
||||
public void addAddress(PeerAddress peerAddress) {
|
||||
// TODO(miron) consider deduplication
|
||||
inactives.add(peerAddress);
|
||||
}
|
||||
|
||||
/** Add addresses from a discovery source to the list of potential peers to connect to */
|
||||
/**
|
||||
* Add addresses from a discovery source to the list of potential peers to connect to
|
||||
*/
|
||||
public void addPeerDiscovery(PeerDiscovery peerDiscovery) {
|
||||
peerDiscoverers.add(peerDiscovery);
|
||||
}
|
||||
|
||||
/** Starts the background thread that makes connections. */
|
||||
/**
|
||||
* Starts the background thread that makes connections.
|
||||
*/
|
||||
public void start() {
|
||||
this.connectThread = new Thread(new PeerExecutionRunnable(), "Peer group thread");
|
||||
running = true;
|
||||
@ -159,7 +160,7 @@ public class PeerGroup {
|
||||
|
||||
/**
|
||||
* Stop this PeerGroup
|
||||
*
|
||||
* <p/>
|
||||
* <p>The peer group will be asynchronously shut down. After it is shut down
|
||||
* all peers will be disconnected and no threads will be running.
|
||||
*/
|
||||
@ -193,7 +194,7 @@ public class PeerGroup {
|
||||
/**
|
||||
* Repeatedly get the next peer address from the inactive queue
|
||||
* and try to connect.
|
||||
*
|
||||
* <p/>
|
||||
* <p>We can be terminated with Thread.interrupt. When an interrupt is received,
|
||||
* we will ask the executor to shutdown and ask each peer to disconnect. At that point
|
||||
* no threads or network connections will be active.
|
||||
@ -242,7 +243,9 @@ public class PeerGroup {
|
||||
}
|
||||
}
|
||||
|
||||
/** Try connecting to a peer. If we exceed the number of connections, delay and try again. */
|
||||
/**
|
||||
* Try connecting to a peer. If we exceed the number of connections, delay and try again.
|
||||
*/
|
||||
private void tryNextPeer() throws InterruptedException {
|
||||
final PeerAddress address = inactives.take();
|
||||
while (true) {
|
||||
@ -305,7 +308,7 @@ public class PeerGroup {
|
||||
|
||||
/**
|
||||
* Start downloading the blockchain from the first available peer.
|
||||
*
|
||||
* <p/>
|
||||
* <p>If no peers are currently connected, the download will be started
|
||||
* once a peer starts. If the peer dies, the download will resume with another peer.
|
||||
*
|
||||
@ -317,8 +320,7 @@ public class PeerGroup {
|
||||
// downloading from multiple peers and handle the case when a new peer comes along
|
||||
// with a longer chain after we thought we were done.
|
||||
synchronized (peers) {
|
||||
if (!peers.isEmpty())
|
||||
{
|
||||
if (!peers.isEmpty()) {
|
||||
startBlockChainDownloadFromPeer(peers.iterator().next());
|
||||
}
|
||||
}
|
||||
@ -326,7 +328,7 @@ public class PeerGroup {
|
||||
|
||||
/**
|
||||
* Download the blockchain from peers.<p>
|
||||
*
|
||||
* <p/>
|
||||
* This method waits until the download is complete. "Complete" is defined as downloading
|
||||
* from at least one peer all the blocks that are in that peer's inventory.
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user