com.google.bitcoin.core
Class NetworkConnection

java.lang.Object
  extended by com.google.bitcoin.core.NetworkConnection

public class NetworkConnection
extends Object

A NetworkConnection handles talking to a remote BitCoin peer at a low level. It understands how to read and write messages off the network, but doesn't asynchronously communicate with the peer or handle the higher level details of the protocol. After constructing a NetworkConnection, use a Peer to hand off communication to a background thread. Construction is blocking whilst the protocol version is negotiated.


Constructor Summary
NetworkConnection(InetAddress remoteIp, NetworkParameters params, int bestHeight, int connectTimeout)
          Connect to the given IP address using the port specified as part of the network parameters.
 
Method Summary
 VersionMessage getVersionMessage()
          Returns the version message received from the other end of the connection during the handshake.
 void ping()
          Sends a "ping" message to the remote node.
 Message readMessage()
          Reads a network message from the wire, blocking until the message is fully received.
 void shutdown()
          Shuts down the network socket.
 String toString()
           
 void writeMessage(Message message)
          Writes the given message out over the network using the protocol tag.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

NetworkConnection

public NetworkConnection(InetAddress remoteIp,
                         NetworkParameters params,
                         int bestHeight,
                         int connectTimeout)
                  throws IOException,
                         ProtocolException
Connect to the given IP address using the port specified as part of the network parameters. Once construction is complete a functioning network channel is set up and running.

Parameters:
remoteIp - IP address to connect to. IPv6 is not currently supported by BitCoin.
params - Defines which network to connect to and details of the protocol.
bestHeight - How many blocks are in our best chain
connectTimeout - Timeout in milliseconds when initially connecting to peer
Throws:
IOException - if there is a network related failure.
ProtocolException - if the version negotiation failed.
Method Detail

ping

public void ping()
          throws IOException
Sends a "ping" message to the remote node. The protocol doesn't presently use this feature much.

Throws:
IOException

shutdown

public void shutdown()
              throws IOException
Shuts down the network socket. Note that there's no way to wait for a socket to be fully flushed out to the wire, so if you call this immediately after sending a message it might not get sent.

Throws:
IOException

toString

public String toString()
Overrides:
toString in class Object

readMessage

public Message readMessage()
                    throws IOException,
                           ProtocolException
Reads a network message from the wire, blocking until the message is fully received.

Returns:
An instance of a Message subclass
Throws:
ProtocolException - if the message is badly formatted, failed checksum or there was a TCP failure.
IOException

writeMessage

public void writeMessage(Message message)
                  throws IOException
Writes the given message out over the network using the protocol tag. For a Transaction this should be "tx" for example. It's safe to call this from multiple threads simultaneously, the actual writing will be serialized.

Throws:
IOException

getVersionMessage

public VersionMessage getVersionMessage()
Returns the version message received from the other end of the connection during the handshake.



Copyright © 2011. All Rights Reserved.