com.google.bitcoin.core
Class Peer

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

public class Peer
extends Object

A Peer handles the high level communication with a BitCoin node. It requires a NetworkConnection to be set up for it. After that it takes ownership of the connection, creates and manages its own thread used for communication with the network. All these threads synchronize on the block chain.


Constructor Summary
Peer(NetworkParameters params, NetworkConnection conn, BlockChain blockChain)
          Construct a peer that handles the given network connection and reads/writes from the given block chain.
 
Method Summary
 void disconnect()
          Terminates the network connection and stops the background thread.
 Future<Block> getBlock(byte[] blockHash)
          Asks the connected peer for the block of the given hash, and returns a Future representing the answer.
 void start()
          Starts the background thread that processes messages.
 CountDownLatch startBlockChainDownload()
          Starts an asynchronous download of the block chain.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Peer

public Peer(NetworkParameters params,
            NetworkConnection conn,
            BlockChain blockChain)
Construct a peer that handles the given network connection and reads/writes from the given block chain. Note that communication won't occur until you call start().

Method Detail

start

public void start()
Starts the background thread that processes messages.


getBlock

public Future<Block> getBlock(byte[] blockHash)
                       throws IOException
Asks the connected peer for the block of the given hash, and returns a Future representing the answer. If you want the block right away and don't mind waiting for it, just call .get() on the result. Your thread will block until the peer answers. You can also use the Future object to wait with a timeout, or just check whether it's done later.

Parameters:
blockHash - Hash of the block you wareare requesting.
Throws:
IOException

startBlockChainDownload

public CountDownLatch startBlockChainDownload()
                                       throws IOException
Starts an asynchronous download of the block chain. The chain download is deemed to be complete once we've downloaded the same number of blocks that the peer advertised having in its version handshake message.

Returns:
a CountDownLatch that can be used to track progress and wait for completion.
Throws:
IOException

disconnect

public void disconnect()
Terminates the network connection and stops the background thread.



Copyright © 2011. All Rights Reserved.