Package org.bitcoinj.net
Class BlockingClient
- java.lang.Object
-
- org.bitcoinj.net.BlockingClient
-
- All Implemented Interfaces:
MessageWriteTarget
public class BlockingClient extends java.lang.Object implements MessageWriteTarget
Creates a simple connection to a server using a
StreamConnectionto process data.Generally, using
NioClientandNioClientManagershould be preferred overBlockingClientandBlockingClientManager, unless you wish to connect over a proxy or use some other network settings that cannot be set using NIO.
-
-
Constructor Summary
Constructors Constructor Description BlockingClient(java.net.SocketAddress serverAddress, StreamConnection connection, java.time.Duration connectTimeout, javax.net.SocketFactory socketFactory, java.util.Set<BlockingClient> clientSet)Creates a new client to the given server address using the givenStreamConnectionto decode the data.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidcloseConnection()Closes the connection to the server, triggering theStreamConnection.connectionClosed()event on the network-handling thread where all callbacks occur.ListenableCompletableFuture<java.net.SocketAddress>getConnectFuture()Returns a future that completes once connection has occurred at the socket level or with an exception if failed to connect.static voidrunReadLoop(java.io.InputStream stream, StreamConnection connection)A blocking call that never returns, except by throwing an exception.ListenableCompletableFuture<java.lang.Void>writeBytes(byte[] message)Writes the given bytes to the remote server.
-
-
-
Constructor Detail
-
BlockingClient
public BlockingClient(java.net.SocketAddress serverAddress, StreamConnection connection, java.time.Duration connectTimeout, javax.net.SocketFactory socketFactory, @Nullable java.util.Set<BlockingClient> clientSet) throws java.io.IOExceptionCreates a new client to the given server address using the given
StreamConnectionto 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:
connectTimeout- The connect timeout set on the connection. ZERO is interpreted as no timeout.socketFactory- An object that createsSocketobjects 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:
java.io.IOException
-
-
Method Detail
-
runReadLoop
public static void runReadLoop(java.io.InputStream stream, StreamConnection connection) throws java.lang.ExceptionA 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:
java.lang.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:
closeConnectionin interfaceMessageWriteTarget
-
writeBytes
public ListenableCompletableFuture<java.lang.Void> writeBytes(byte[] message) throws java.io.IOException
Description copied from interface:MessageWriteTargetWrites 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:
writeBytesin interfaceMessageWriteTarget- Throws:
java.io.IOException
-
getConnectFuture
public ListenableCompletableFuture<java.net.SocketAddress> getConnectFuture()
Returns a future that completes once connection has occurred at the socket level or with an exception if failed to connect.
-
-