Class NioSocketConnector

    • Constructor Detail

      • NioSocketConnector

        public NioSocketConnector()
        Constructor for NioSocketConnector with default configuration (multiple thread model).
      • NioSocketConnector

        public NioSocketConnector​(int processorCount)
        Constructor for NioSocketConnector with default configuration, and given number of NioProcessor for multithreading I/O operations
        Parameters:
        processorCount - the number of processor to create and place in a SimpleIoProcessorPool
      • NioSocketConnector

        public NioSocketConnector​(IoProcessor<NioSession> processor)
        Constructor for NioSocketConnector with default configuration but a specific IoProcessor, useful for sharing the same processor over multiple IoService of the same type.
        Parameters:
        processor - the processor to use for managing I/O events
      • NioSocketConnector

        public NioSocketConnector​(java.util.concurrent.Executor executor,
                                  IoProcessor<NioSession> processor)
        Constructor for NioSocketConnector with a given Executor for handling connection events and a given IoProcessor for handling I/O events, useful for sharing the same processor and executor over multiple IoService of the same type.
        Parameters:
        executor - the executor for connection
        processor - the processor for I/O operations
      • NioSocketConnector

        public NioSocketConnector​(java.lang.Class<? extends IoProcessor<NioSession>> processorClass,
                                  int processorCount)
        Constructor for NioSocketConnector with default configuration which will use a built-in thread pool executor to manage the given number of processor instances. The processor class must have a constructor that accepts ExecutorService or Executor as its single argument, or, failing that, a no-arg constructor.
        Parameters:
        processorClass - the processor class.
        processorCount - the number of processors to instantiate.
        Since:
        2.0.0-M4
        See Also:
        SimpleIoProcessorPool(Class, Executor, int, java.nio.channels.spi.SelectorProvider)
      • NioSocketConnector

        public NioSocketConnector​(java.lang.Class<? extends IoProcessor<NioSession>> processorClass)
        Constructor for NioSocketConnector with default configuration with default configuration which will use a built-in thread pool executor to manage the default number of processor instances. The processor class must have a constructor that accepts ExecutorService or Executor as its single argument, or, failing that, a no-arg constructor. The default number of instances is equal to the number of processor cores in the system, plus one.
        Parameters:
        processorClass - the processor class.
        Since:
        2.0.0-M4
        See Also:
        SimpleIoProcessorPool(Class, Executor, int, java.nio.channels.spi.SelectorProvider)
    • Method Detail

      • init

        protected void init()
                     throws java.lang.Exception
        Initialize the polling system, will be called at construction time.
        Specified by:
        init in class AbstractPollingIoConnector<NioSession,​java.nio.channels.SocketChannel>
        Throws:
        java.lang.Exception - any exception thrown by the underlying system calls
      • destroy

        protected void destroy()
                        throws java.lang.Exception
        Destroy the polling system, will be called when this IoConnector implementation will be disposed.
        Specified by:
        destroy in class AbstractPollingIoConnector<NioSession,​java.nio.channels.SocketChannel>
        Throws:
        java.lang.Exception - any exception thrown by the underlying systems calls
      • allHandles

        protected java.util.Iterator<java.nio.channels.SocketChannel> allHandles()
        Iterator for all the client sockets polled for connection.
        Specified by:
        allHandles in class AbstractPollingIoConnector<NioSession,​java.nio.channels.SocketChannel>
        Returns:
        the list of client sockets currently polled for connection
      • connect

        protected boolean connect​(java.nio.channels.SocketChannel handle,
                                  java.net.SocketAddress remoteAddress)
                           throws java.lang.Exception
        Connect a newly created client socket handle to a remote SocketAddress. This operation is non-blocking, so at end of the call the socket can be still in connection process.
        Specified by:
        connect in class AbstractPollingIoConnector<NioSession,​java.nio.channels.SocketChannel>
        Parameters:
        handle - the client socket handle
        remoteAddress - the remote address where to connect
        Returns:
        true if a connection was established, false if this client socket is in non-blocking mode and the connection operation is in progress
        Throws:
        java.lang.Exception - If the connect failed
      • close

        protected void close​(java.nio.channels.SocketChannel handle)
                      throws java.lang.Exception
        Close a client socket.
        Specified by:
        close in class AbstractPollingIoConnector<NioSession,​java.nio.channels.SocketChannel>
        Parameters:
        handle - the client socket
        Throws:
        java.lang.Exception - any exception thrown by the underlying systems calls
      • finishConnect

        protected boolean finishConnect​(java.nio.channels.SocketChannel handle)
                                 throws java.lang.Exception
        Finish the connection process of a client socket after it was marked as ready to process by the AbstractPollingIoConnector.select(int) call. The socket will be connected or reported as connection failed.
        Specified by:
        finishConnect in class AbstractPollingIoConnector<NioSession,​java.nio.channels.SocketChannel>
        Parameters:
        handle - the client socket handle to finish to connect
        Returns:
        true if the socket is connected
        Throws:
        java.lang.Exception - any exception thrown by the underlying systems calls
      • newHandle

        protected java.nio.channels.SocketChannel newHandle​(java.net.SocketAddress localAddress)
                                                     throws java.lang.Exception
        Create a new client socket handle from a local SocketAddress
        Specified by:
        newHandle in class AbstractPollingIoConnector<NioSession,​java.nio.channels.SocketChannel>
        Parameters:
        localAddress - the socket address for binding the new client socket
        Returns:
        a new client socket handle
        Throws:
        java.lang.Exception - any exception thrown by the underlying systems calls
      • select

        protected int select​(int timeout)
                      throws java.lang.Exception
        Check for connected sockets, interrupt when at least a connection is processed (connected or failed to connect). All the client socket descriptors processed need to be returned by AbstractPollingIoConnector.selectedHandles()
        Specified by:
        select in class AbstractPollingIoConnector<NioSession,​java.nio.channels.SocketChannel>
        Parameters:
        timeout - The timeout for the select() method
        Returns:
        The number of socket having received some data
        Throws:
        java.lang.Exception - any exception thrown by the underlying systems calls