Class NettyTcpTransport

  • All Implemented Interfaces:
    Transport
    Direct Known Subclasses:
    NettyWsTransport

    public class NettyTcpTransport
    extends Object
    implements Transport
    TCP based transport that uses Netty as the underlying IO layer.
    • Constructor Detail

      • NettyTcpTransport

        public NettyTcpTransport​(URI remoteLocation,
                                 TransportOptions options,
                                 boolean secure)
        Create a new transport instance
        Parameters:
        remoteLocation - the URI that defines the remote resource to connect to.
        options - the transport options used to configure the socket connection.
        secure - should the transport enable an SSL layer.
      • NettyTcpTransport

        public NettyTcpTransport​(TransportListener listener,
                                 URI remoteLocation,
                                 TransportOptions options,
                                 boolean secure)
        Create a new transport instance
        Parameters:
        listener - the TransportListener that will receive events from this Transport.
        remoteLocation - the URI that defines the remote resource to connect to.
        options - the transport options used to configure the socket connection.
        secure - should the transport enable an SSL layer.
    • Method Detail

      • connect

        public ScheduledExecutorService connect​(Runnable initRoutine,
                                                SSLContext sslContextOverride)
                                         throws 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.
        Specified by:
        connect in interface Transport
        Parameters:
        initRoutine - a runnable initialization method that is executed in the context of the transport's IO thread to allow thread safe setup of resources that will be run from the transport executor service.
        sslContextOverride - a user-provided SSLContext to use if establishing a secure connection, overrides applicable URI configuration
        Returns:
        A ScheduledThreadPoolExecutor that can run work on the Transport IO thread.
        Throws:
        IOException - if an error occurs while attempting the connect.
      • 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).
      • close

        public void close()
                   throws IOException
        Description copied from interface: Transport
        Close the Transport, no additional send operations are accepted.
        Specified by:
        close in interface Transport
        Throws:
        IOException - if an error occurs while closing the connection.
      • allocateSendBuffer

        public io.netty.buffer.ByteBuf allocateSendBuffer​(int size)
                                                   throws IOException
        Description copied from interface: Transport
        Request that the Transport provide an output buffer sized for the given value.
        Specified by:
        allocateSendBuffer in interface Transport
        Parameters:
        size - the size necessary to hold the outgoing bytes.
        Returns:
        a new ByteBuf allocated for sends operations.
        Throws:
        IOException - if an error occurs while allocating the send buffer.
      • write

        public void write​(io.netty.buffer.ByteBuf output)
                   throws 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.
        Throws:
        IOException - if an error occurs during the write operation.
      • writeAndFlush

        public void writeAndFlush​(io.netty.buffer.ByteBuf output)
                           throws 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.
        Throws:
        IOException - if an error occurs during the write operation.
      • flush

        public void flush()
                   throws IOException
        Description copied from interface: Transport
        Request a flush of all pending writes to the underlying connection.
        Specified by:
        flush in interface Transport
        Throws:
        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.
      • setTransportListener

        public void setTransportListener​(TransportListener listener)
        Description copied from interface: Transport
        Sets the Transport Listener instance that will be notified of incoming data or error events.
        Specified by:
        setTransportListener in interface Transport
        Parameters:
        listener - The new TransportListener instance to use (cannot be null).
      • getRemoteLocation

        public URI getRemoteLocation()
        Specified by:
        getRemoteLocation in interface Transport
        Returns:
        the URI of the remote peer that this Transport connects to.
      • getLocalPrincipal

        public Principal getLocalPrincipal()
        Specified by:
        getLocalPrincipal in interface Transport
        Returns:
        the local principal for a Transport that is using a secure connection.
      • setMaxFrameSize

        public void setMaxFrameSize​(int maxFrameSize)
        Description copied from interface: Transport
        Sets the Maximum Frame Size the transport should accept from the remote. This option is not applicable to all transport types, those that support validating the incoming frame size should apply the configured value.
        Specified by:
        setMaxFrameSize in interface Transport
        Parameters:
        maxFrameSize - The maximum frame size to accept from the remote.
      • getMaxFrameSize

        public int getMaxFrameSize()
        Description copied from interface: Transport
        Returns the currently configured maximum frame size setting.
        Specified by:
        getMaxFrameSize in interface Transport
        Returns:
        the current max frame size setting for this transport.
      • getRemoteHost

        protected String getRemoteHost()
      • getRemotePort

        protected int getRemotePort()
      • addAdditionalHandlers

        protected void addAdditionalHandlers​(io.netty.channel.ChannelPipeline pipeline)
      • createChannelHandler

        protected io.netty.channel.ChannelInboundHandlerAdapter createChannelHandler()
      • handleConnected

        protected void handleConnected​(io.netty.channel.Channel channel)
                                throws Exception
        Throws:
        Exception
      • handleChannelInactive

        protected void handleChannelInactive​(io.netty.channel.Channel channel)
                                      throws Exception
        Throws:
        Exception
      • handleException

        protected void handleException​(io.netty.channel.Channel channel,
                                       Throwable cause)