Class 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.
    • 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
      • 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
    • Constructor Detail

      • TcpTransport

        public TcpTransport​(io.netty.bootstrap.Bootstrap bootstrap,
                            TransportOptions options,
                            SslOptions sslOptions)
        Create a new TcpTransport instance with the given configuration.
        Parameters:
        bootstrap - the Netty Bootstrap that this transport's IO layer is bound to.
        options - the TransportOptions used to configure the socket connection.
        sslOptions - the SslOptions 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 point TransportListener.transportError(Throwable). If the users wishes to perform a block on connect outcome the Transport.awaitConnect() method will wait for and or throw an error based on the connect outcome.
        Specified by:
        connect in interface Transport
        Parameters:
        host - The remote host that this Transport should attempt to connect to.
        port - The port on the remote host that this Transport should attempt to bind to.
        listener - The TransportListener that will handle Transport 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 the Transport to connect to the remote that was indicated in the Transport.connect(String, int, TransportListener) call.
        Specified by:
        awaitConnect in interface Transport
        Throws:
        java.lang.InterruptedException - If the wait mechanism was interrupted while waiting for a successful connect.
        java.io.IOException - If the Transport failed to connect or was closed before connected.
      • isConnected

        public boolean isConnected()
        Specified by:
        isConnected in interface Transport
        Returns:
        true if transport is connected or false if the connection is down.
      • isSecure

        public boolean isSecure()
        Specified by:
        isSecure in interface Transport
        Returns:
        true if transport is connected using a secured channel (SSL).
      • getHost

        public java.lang.String getHost()
        Specified by:
        getHost in interface Transport
        Returns:
        the host name or IP address that the transport connects to.
      • getPort

        public int getPort()
        Specified by:
        getPort in interface Transport
        Returns:
        the port that the transport connects to.
      • close

        public void close()
                   throws java.io.IOException
        Description copied from interface: Transport
        Close the Transport, no additional send operations are accepted.
        Specified by:
        close in interface Transport
        Throws:
        java.io.IOException - if an error occurs while closing the connection.
      • 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.
        Specified by:
        write in interface Transport
        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.
      • 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 interface Transport
        Parameters:
        output - The buffer of data that is to be transmitted.
        onComplete - A Runnable 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 interface Transport
        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 interface Transport
        Parameters:
        output - The buffer of data that is to be transmitted.
        onComplete - A Runnable 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.
        Specified by:
        flush in interface Transport
        Returns:
        this Transport instance.
        Throws:
        java.io.IOException - if an error occurs during the flush operation.
      • getTransportListener

        public TransportListener getTransportListener()
        Description copied from interface: Transport
        Gets the currently set TransportListener instance
        Specified by:
        getTransportListener in interface Transport
        Returns:
        the current TransportListener or null if none set.
      • getLocalPrincipal

        public java.security.Principal getLocalPrincipal()
        Specified by:
        getLocalPrincipal in interface Transport
        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 the Transport.connect(String, int, TransportListener) method this method returns null.
        Specified by:
        getRemoteURI in interface Transport
        Returns:
        a URI that reflects a meaningful view of the Transport remote connection details.
      • getScheme

        protected java.lang.String getScheme()