Package org.bitcoinj.net
Class BlockingClient
java.lang.Object
org.bitcoinj.net.BlockingClient
- All Implemented Interfaces:
MessageWriteTarget
Creates a simple connection to a server using a StreamConnection
to process data.
Generally, using NioClient
and NioClientManager
should be preferred over BlockingClient
and BlockingClientManager
, unless you wish to connect over a proxy or use some other network settings that
cannot be set using NIO.
-
Constructor Summary
ConstructorDescriptionBlockingClient
(SocketAddress serverAddress, StreamConnection connection, int connectTimeoutMillis, SocketFactory socketFactory, Set<BlockingClient> clientSet) Creates a new client to the given server address using the givenStreamConnection
to decode the data. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Closes the connection to the server, triggering theStreamConnection.connectionClosed()
event on the network-handling thread where all callbacks occur.com.google.common.util.concurrent.ListenableFuture<SocketAddress>
Returns a future that completes once connection has occurred at the socket level or with an exception if failed to connect.static void
runReadLoop
(InputStream stream, StreamConnection connection) A blocking call that never returns, except by throwing an exception.com.google.common.util.concurrent.ListenableFuture
writeBytes
(byte[] message) Writes the given bytes to the remote server.
-
Constructor Details
-
BlockingClient
public BlockingClient(SocketAddress serverAddress, StreamConnection connection, int connectTimeoutMillis, SocketFactory socketFactory, @Nullable Set<BlockingClient> clientSet) throws IOException Creates a new client to the given server address using the given
StreamConnection
to decode the data. The given connection MUST be unique to this object. This does not block while waiting for the connection to open, but will call either theStreamConnection.connectionOpened()
orStreamConnection.connectionClosed()
callback on the created network event processing thread.- Parameters:
connectTimeoutMillis
- The connect timeout set on the connection (in milliseconds). 0 is interpreted as no timeout.socketFactory
- An object that createsSocket
objects on demand, which may be customised to control how this client connects to the internet. If not sure, use SocketFactory.getDefault()clientSet
- A set which this object will add itself to after initialization, and then remove itself from- Throws:
IOException
-
-
Method Details
-
runReadLoop
A blocking call that never returns, except by throwing an exception. It reads bytes from the input stream and feeds them to the providedStreamConnection
, for example, aPeer
.- Throws:
Exception
-
closeConnection
public void closeConnection()Closes the connection to the server, triggering theStreamConnection.connectionClosed()
event on the network-handling thread where all callbacks occur.- Specified by:
closeConnection
in interfaceMessageWriteTarget
-
writeBytes
public com.google.common.util.concurrent.ListenableFuture writeBytes(byte[] message) throws IOException Description copied from interface:MessageWriteTarget
Writes the given bytes to the remote server. The returned future will complete when all bytes have been written to the OS network buffer.- Specified by:
writeBytes
in interfaceMessageWriteTarget
- Throws:
IOException
-
getConnectFuture
Returns a future that completes once connection has occurred at the socket level or with an exception if failed to connect.
-