3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-07 06:44:16 +00:00

Allow user to provide NetworkConnection with a socket timeout. Resolves issue 17. Patch from John Sample.

This commit is contained in:
Mike Hearn 2011-05-02 11:58:51 +00:00
parent e43ad1f754
commit 71604505ad
3 changed files with 11 additions and 6 deletions

View File

@ -21,8 +21,8 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;
import java.util.Date;
import static com.google.bitcoin.core.Utils.*;
@ -65,14 +65,19 @@ public class NetworkConnection {
* @param remoteIp IP address to connect to. IPv6 is not currently supported by BitCoin.
* @param params Defines which network to connect to and details of the protocol.
* @param bestHeight How many blocks are in our best chain
* @param connectTimeout Timeout in milliseconds when initially connecting to peer
* @throws IOException if there is a network related failure.
* @throws ProtocolException if the version negotiation failed.
*/
public NetworkConnection(InetAddress remoteIp, NetworkParameters params, int bestHeight)
public NetworkConnection(InetAddress remoteIp, NetworkParameters params, int bestHeight, int connectTimeout)
throws IOException, ProtocolException {
this.params = params;
this.remoteIp = remoteIp;
socket = new Socket(remoteIp, params.port);
InetSocketAddress address = new InetSocketAddress(remoteIp, params.port);
socket = new Socket();
socket.connect(address, connectTimeout);
out = socket.getOutputStream();
in = socket.getInputStream();

View File

@ -52,10 +52,10 @@ public class PingService {
System.out.println("Reading block store from disk");
BlockStore blockStore = new DiskBlockStore(params, new File(filePrefix + ".blockchain"));
// Connect to the localhost node.
// Connect to the localhost node. One minute timeout since we won't try any other peers
System.out.println("Connecting ...");
NetworkConnection conn = new NetworkConnection(InetAddress.getLocalHost(), params,
blockStore.getChainHead().getHeight());
blockStore.getChainHead().getHeight(), 60000);
BlockChain chain = new BlockChain(params, wallet, blockStore);
final Peer peer = new Peer(params, conn, chain);
peer.start();

View File

@ -46,7 +46,7 @@ public class PrivateKeys {
wallet.addKey(key);
// Find the transactions that involve those coins.
NetworkConnection conn = new NetworkConnection(InetAddress.getLocalHost(), params, 0);
NetworkConnection conn = new NetworkConnection(InetAddress.getLocalHost(), params, 0, 60000);
BlockChain chain = new BlockChain(params, wallet, new MemoryBlockStore(params));
Peer peer = new Peer(params, conn, chain);
peer.start();