Class TcpTransport
- java.lang.Object
-
- org.apache.qpid.protonj2.client.transport.TcpTransport
-
- All Implemented Interfaces:
Transport
- Direct Known Subclasses:
WebSocketTransport
public class TcpTransport extends java.lang.Object implements Transport
TCP based transport that uses Netty as the underlying IO layer.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected class
TcpTransport.NettyDefaultHandler<E>
protected class
TcpTransport.NettyTcpTransportHandler
-
Field Summary
Fields Modifier and Type Field Description protected io.netty.bootstrap.Bootstrap
bootstrap
protected io.netty.channel.Channel
channel
protected java.util.concurrent.atomic.AtomicBoolean
closed
protected java.util.concurrent.atomic.AtomicBoolean
connected
protected java.util.concurrent.CountDownLatch
connectedLatch
protected java.io.IOException
failureCause
protected java.lang.String
host
protected TransportListener
listener
protected TransportOptions
options
protected int
port
protected SslOptions
sslOptions
-
Constructor Summary
Constructors Constructor Description TcpTransport(io.netty.bootstrap.Bootstrap bootstrap, TransportOptions options, SslOptions sslOptions)
Create a newTcpTransport
instance with the given configuration.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
addAdditionalHandlers(io.netty.channel.ChannelPipeline pipeline)
void
awaitConnect()
Waits interruptibly for theTransport
to connect to the remote that was indicated in theTransport.connect(String, int, TransportListener)
call.protected void
checkConnected()
void
close()
Close the Transport, no additional send operations are accepted.TcpTransport
connect(java.lang.String host, int port, TransportListener listener)
Performs the connect operation for the implemented Transport type such as a TCP socket connection, SSL/TLS handshake etc.protected io.netty.channel.ChannelInboundHandlerAdapter
createChannelHandler()
TcpTransport
flush()
Request a flush of all pending writes to the underlying connection.ProtonBufferAllocator
getBufferAllocator()
Gets a buffer allocator that can produceProtonBuffer
instance that may be optimized for use with the underlying transport implementation.java.lang.String
getHost()
java.security.Principal
getLocalPrincipal()
int
getPort()
java.net.URI
getRemoteURI()
Returns a URI that contains some meaningful information about the remote connection such as a scheme that reflects the transport type and the remote host and port that the connection was instructed to connect to.protected java.lang.String
getScheme()
SslOptions
getSslOptions()
TransportListener
getTransportListener()
Gets the currently set TransportListener instanceTransportOptions
getTransportOptions()
protected void
handleConnected(io.netty.channel.Channel connectedChannel)
protected void
handleTransportFailure(io.netty.channel.Channel failedChannel, java.lang.Throwable cause)
boolean
isConnected()
boolean
isSecure()
protected io.netty.buffer.ByteBuf
toOutputBuffer(ProtonBuffer output)
TcpTransport
write(ProtonBuffer output)
Writes a chunk of data over the Transport connection without performing an explicit flush on the transport.TcpTransport
write(ProtonBuffer output, java.lang.Runnable onComplete)
Writes a chunk of data over the Transport connection without performing an explicit flush on the transport.TcpTransport
writeAndFlush(ProtonBuffer output)
Writes a chunk of data over the Transport connection and requests a flush of all pending queued write operationsTcpTransport
writeAndFlush(ProtonBuffer output, java.lang.Runnable onComplete)
Writes a chunk of data over the Transport connection and requests a flush of all pending queued write operations
-
-
-
Field Detail
-
connected
protected final java.util.concurrent.atomic.AtomicBoolean connected
-
closed
protected final java.util.concurrent.atomic.AtomicBoolean closed
-
connectedLatch
protected final java.util.concurrent.CountDownLatch connectedLatch
-
options
protected final TransportOptions options
-
sslOptions
protected final SslOptions sslOptions
-
bootstrap
protected final io.netty.bootstrap.Bootstrap bootstrap
-
channel
protected io.netty.channel.Channel channel
-
failureCause
protected volatile java.io.IOException failureCause
-
host
protected java.lang.String host
-
port
protected int port
-
listener
protected TransportListener listener
-
-
Constructor Detail
-
TcpTransport
public TcpTransport(io.netty.bootstrap.Bootstrap bootstrap, TransportOptions options, SslOptions sslOptions)
Create a newTcpTransport
instance with the given configuration.- Parameters:
bootstrap
- the NettyBootstrap
that this transport's IO layer is bound to.options
- theTransportOptions
used to configure the socket connection.sslOptions
- theSslOptions
to use if the options indicate SSL is enabled.
-
-
Method Detail
-
connect
public TcpTransport connect(java.lang.String host, int port, TransportListener listener) throws java.io.IOException
Description copied from interface:Transport
Performs the connect operation for the implemented Transport type such as a TCP socket connection, SSL/TLS handshake etc. The connection operation itself will be performed as an asynchronous operation with the success or failure being communicated to the event pointTransportListener.transportError(Throwable)
. If the users wishes to perform a block on connect outcome theTransport.awaitConnect()
method will wait for and or throw an error based on the connect outcome.- Specified by:
connect
in interfaceTransport
- Parameters:
host
- The remote host that thisTransport
should attempt to connect to.port
- The port on the remote host that thisTransport
should attempt to bind to.listener
- TheTransportListener
that will handleTransport
events.- Returns:
- this
Transport
instance. - Throws:
java.io.IOException
- if an error occurs while attempting the connect.
-
awaitConnect
public void awaitConnect() throws java.lang.InterruptedException, java.io.IOException
Description copied from interface:Transport
Waits interruptibly for theTransport
to connect to the remote that was indicated in theTransport.connect(String, int, TransportListener)
call.- Specified by:
awaitConnect
in interfaceTransport
- Throws:
java.lang.InterruptedException
- If the wait mechanism was interrupted while waiting for a successful connect.java.io.IOException
- If theTransport
failed to connect or was closed before connected.
-
isConnected
public boolean isConnected()
- Specified by:
isConnected
in interfaceTransport
- Returns:
- true if transport is connected or false if the connection is down.
-
isSecure
public boolean isSecure()
-
getHost
public java.lang.String getHost()
-
getPort
public int getPort()
-
close
public void close() throws java.io.IOException
Description copied from interface:Transport
Close the Transport, no additional send operations are accepted.
-
getBufferAllocator
public ProtonBufferAllocator getBufferAllocator()
Description copied from interface:Transport
Gets a buffer allocator that can produceProtonBuffer
instance that may be optimized for use with the underlying transport implementation.- Specified by:
getBufferAllocator
in interfaceTransport
- Returns:
- a
ProtonBufferAllocator
that creates transport friendly buffers.
-
write
public TcpTransport write(ProtonBuffer output) throws java.io.IOException
Description copied from interface:Transport
Writes a chunk of data over the Transport connection without performing an explicit flush on the transport.
-
write
public TcpTransport write(ProtonBuffer output, java.lang.Runnable onComplete) throws java.io.IOException
Description copied from interface:Transport
Writes a chunk of data over the Transport connection without performing an explicit flush on the transport. This method allows for a completion callback that is signaled when the actual low level IO operation is completed which could be after this method has returned.- Specified by:
write
in interfaceTransport
- Parameters:
output
- The buffer of data that is to be transmitted.onComplete
- ARunnable
that is invoked when the IO operation completes successfully.- Returns:
- this
Transport
instance. - Throws:
java.io.IOException
- if an error occurs during the write operation.
-
writeAndFlush
public TcpTransport writeAndFlush(ProtonBuffer output) throws java.io.IOException
Description copied from interface:Transport
Writes a chunk of data over the Transport connection and requests a flush of all pending queued write operations- Specified by:
writeAndFlush
in interfaceTransport
- Parameters:
output
- The buffer of data that is to be transmitted.- Returns:
- this
Transport
instance. - Throws:
java.io.IOException
- if an error occurs during the write operation.
-
writeAndFlush
public TcpTransport writeAndFlush(ProtonBuffer output, java.lang.Runnable onComplete) throws java.io.IOException
Description copied from interface:Transport
Writes a chunk of data over the Transport connection and requests a flush of all pending queued write operations- Specified by:
writeAndFlush
in interfaceTransport
- Parameters:
output
- The buffer of data that is to be transmitted.onComplete
- ARunnable
that is invoked when the IO operation completes successfully.- Returns:
- this
Transport
instance. - Throws:
java.io.IOException
- if an error occurs during the write operation.
-
flush
public TcpTransport flush() throws java.io.IOException
Description copied from interface:Transport
Request a flush of all pending writes to the underlying connection.
-
getTransportListener
public TransportListener getTransportListener()
Description copied from interface:Transport
Gets the currently set TransportListener instance- Specified by:
getTransportListener
in interfaceTransport
- Returns:
- the current TransportListener or null if none set.
-
getTransportOptions
public TransportOptions getTransportOptions()
- Specified by:
getTransportOptions
in interfaceTransport
- Returns:
- a
TransportOptions
instance copied from the immutable options given at create time..
-
getSslOptions
public SslOptions getSslOptions()
- Specified by:
getSslOptions
in interfaceTransport
- Returns:
- a
SslOptions
instance copied from the immutable options given at create time..
-
getLocalPrincipal
public java.security.Principal getLocalPrincipal()
- Specified by:
getLocalPrincipal
in interfaceTransport
- Returns:
- the local principal for a Transport that is using a secure connection.
-
toOutputBuffer
protected final io.netty.buffer.ByteBuf toOutputBuffer(ProtonBuffer output) throws java.io.IOException
- Throws:
java.io.IOException
-
addAdditionalHandlers
protected void addAdditionalHandlers(io.netty.channel.ChannelPipeline pipeline)
-
createChannelHandler
protected io.netty.channel.ChannelInboundHandlerAdapter createChannelHandler()
-
handleConnected
protected void handleConnected(io.netty.channel.Channel connectedChannel) throws java.lang.Exception
- Throws:
java.lang.Exception
-
handleTransportFailure
protected void handleTransportFailure(io.netty.channel.Channel failedChannel, java.lang.Throwable cause)
-
checkConnected
protected final void checkConnected() throws java.io.IOException
- Throws:
java.io.IOException
-
getRemoteURI
public java.net.URI getRemoteURI()
Description copied from interface:Transport
Returns a URI that contains some meaningful information about the remote connection such as a scheme that reflects the transport type and the remote host and port that the connection was instructed to connect to. If called before theTransport.connect(String, int, TransportListener)
method this method returnsnull
.- Specified by:
getRemoteURI
in interfaceTransport
- Returns:
- a URI that reflects a meaningful view of the
Transport
remote connection details.
-
getScheme
protected java.lang.String getScheme()
-
-