Class TcpTransport

  • All Implemented Interfaces:
    ITransport<java.nio.channels.SocketChannel>

    public class TcpTransport
    extends java.lang.Object
    implements ITransport<java.nio.channels.SocketChannel>
    The selector thread waits for NIO events and issues the appropriate actions for asynchronous sending and receiving as data becomes available.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected ITransportHandler<java.nio.channels.SocketChannel> handler
      The transport handler, e.g. for delivering received messages.
      protected int maxmsgsize
      Maximum size a message is allowed to have (including header).
      static int PRIORITY
      Priority of transport.
      protected boolean running
      Flag indicating the thread should be running (set to false for shutdown).
      protected java.nio.channels.Selector selector
      The NIO selector.
      protected boolean shutdown
      Flag indicating the transport has been shut down..
      protected java.util.List<java.lang.Runnable> tasks
      The tasks enqueued from external threads.
      protected IDaemonThreadPoolService tps
      Daemon thread pool.
      protected java.util.Map<java.nio.channels.SocketChannel,​java.util.List<Tuple2<java.nio.ByteBuffer,​Future<java.lang.Integer>>>> writetasks
      The write tasks of data waiting to be written to a connection.
    • Constructor Summary

      Constructors 
      Constructor Description
      TcpTransport​(int maxmsgsize)
      Creates the transport
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected void closeConnection​(java.nio.channels.SelectionKey key, java.lang.Exception e, boolean remove)
      Perform close operations on a channel as identified by the given key.
      void closeConnection​(java.nio.channels.SocketChannel sc)
      Perform close operations on a connection.
      IFuture<java.nio.channels.SocketChannel> createConnection​(java.lang.String address, IComponentIdentifier target)
      Create a connection to a given address.
      java.lang.String getProtocolName()
      Get the protocol name.
      protected void handleAccept​(java.nio.channels.SelectionKey key)
      Accept a connection request from a client.
      protected void handleConnect​(java.nio.channels.SelectionKey key)
      Complete a connection request to a server.
      protected void handleRead​(java.nio.channels.SelectionKey key)
      Read data from a connection.
      protected void handleWrite​(java.nio.channels.SelectionKey key)
      Write data to a connection.
      void init​(ITransportHandler<java.nio.channels.SocketChannel> handler)
      Initialize the transport.
      IFuture<java.lang.Integer> openPort​(int port)
      Open a server socket.
      protected void schedule​(java.lang.Runnable task)
      Schedule a task on the thread.
      IFuture<java.lang.Integer> sendMessage​(java.nio.channels.SocketChannel sc, byte[] header, byte[] body)
      Send bytes using the given connection.
      void shutdown()
      Set the running flag to false to gracefully terminate the thread.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • handler

        protected ITransportHandler<java.nio.channels.SocketChannel> handler
        The transport handler, e.g. for delivering received messages.
      • running

        protected boolean running
        Flag indicating the thread should be running (set to false for shutdown).
      • shutdown

        protected boolean shutdown
        Flag indicating the transport has been shut down..
      • maxmsgsize

        protected int maxmsgsize
        Maximum size a message is allowed to have (including header).
      • selector

        protected java.nio.channels.Selector selector
        The NIO selector.
      • tasks

        protected java.util.List<java.lang.Runnable> tasks
        The tasks enqueued from external threads.
      • writetasks

        protected java.util.Map<java.nio.channels.SocketChannel,​java.util.List<Tuple2<java.nio.ByteBuffer,​Future<java.lang.Integer>>>> writetasks
        The write tasks of data waiting to be written to a connection.
    • Constructor Detail

      • TcpTransport

        public TcpTransport​(int maxmsgsize)
        Creates the transport
        Parameters:
        maxmsgsize - Maximum size a message is allowed to have (including header).
    • Method Detail

      • init

        public void init​(ITransportHandler<java.nio.channels.SocketChannel> handler)
        Initialize the transport. To be called once, before any other method.
        Specified by:
        init in interface ITransport<java.nio.channels.SocketChannel>
        Parameters:
        handler - The transport handler with callback methods.
      • shutdown

        public void shutdown()
        Set the running flag to false to gracefully terminate the thread.
        Specified by:
        shutdown in interface ITransport<java.nio.channels.SocketChannel>
      • getProtocolName

        public java.lang.String getProtocolName()
        Get the protocol name.
        Specified by:
        getProtocolName in interface ITransport<java.nio.channels.SocketChannel>
      • openPort

        public IFuture<java.lang.Integer> openPort​(int port)
        Open a server socket. Must not be called while the thread is running.
        Specified by:
        openPort in interface ITransport<java.nio.channels.SocketChannel>
        Parameters:
        port - The (positive) port or 0 for any port.
        Returns:
        A future holding the actual port, once the server is running.
      • createConnection

        public IFuture<java.nio.channels.SocketChannel> createConnection​(java.lang.String address,
                                                                         IComponentIdentifier target)
        Create a connection to a given address.
        Specified by:
        createConnection in interface ITransport<java.nio.channels.SocketChannel>
        Parameters:
        address - The target platform's address.
        target - The target identifier to maybe perform authentication of the connection.
        Returns:
        A future containing the connection when succeeded.
      • closeConnection

        public void closeConnection​(java.nio.channels.SocketChannel sc)
        Perform close operations on a connection. Potentially cleans up key attachments as well.
        Specified by:
        closeConnection in interface ITransport<java.nio.channels.SocketChannel>
      • sendMessage

        public IFuture<java.lang.Integer> sendMessage​(java.nio.channels.SocketChannel sc,
                                                      byte[] header,
                                                      byte[] body)
        Send bytes using the given connection.
        Specified by:
        sendMessage in interface ITransport<java.nio.channels.SocketChannel>
        Parameters:
        sc - The connection.
        header - The message header.
        body - The message body.
        Returns:
        A future indicating success.
      • closeConnection

        protected void closeConnection​(java.nio.channels.SelectionKey key,
                                       java.lang.Exception e,
                                       boolean remove)
        Perform close operations on a channel as identified by the given key. Potentially cleans up key attachments as well.
        Parameters:
        e - The exception, if any.
        remove - Notify the handler to remove the connection (should only be called for connections known to the handler).
      • schedule

        protected void schedule​(java.lang.Runnable task)
        Schedule a task on the thread. Can be called on arbitrary threads.
      • handleAccept

        protected void handleAccept​(java.nio.channels.SelectionKey key)
        Accept a connection request from a client.
      • handleConnect

        protected void handleConnect​(java.nio.channels.SelectionKey key)
        Complete a connection request to a server.
      • handleRead

        protected void handleRead​(java.nio.channels.SelectionKey key)
        Read data from a connection.
      • handleWrite

        protected void handleWrite​(java.nio.channels.SelectionKey key)
        Write data to a connection.