Class AbstractPollingIoConnector<S extends AbstractIoSession,​H>

  • Type Parameters:
    H - The type of IoHandler
    S - The type of IoSession
    All Implemented Interfaces:
    IoConnector, IoService
    Direct Known Subclasses:
    AprSocketConnector, NioDatagramConnector, NioSocketConnector

    public abstract class AbstractPollingIoConnector<S extends AbstractIoSession,​H>
    extends AbstractIoConnector
    A base class for implementing client transport using a polling strategy. The underlying sockets will be checked in an active loop and woke up when an socket needed to be processed. This class handle the logic behind binding, connecting and disposing the client sockets. A Executor will be used for running client connection, and an AbstractPollingIoProcessor will be used for processing connected client I/O operations like reading, writing and closing. All the low level methods for binding, connecting, closing need to be provided by the subclassing implementation.
    Author:
    Apache MINA Project
    See Also:
    for a example of implementation
    • Method Detail

      • init

        protected abstract void init()
                              throws java.lang.Exception
        Initialize the polling system, will be called at construction time.
        Throws:
        java.lang.Exception - any exception thrown by the underlying system calls
      • destroy

        protected abstract void destroy()
                                 throws java.lang.Exception
        Destroy the polling system, will be called when this IoConnector implementation will be disposed.
        Throws:
        java.lang.Exception - any exception thrown by the underlying systems calls
      • newHandle

        protected abstract H newHandle​(java.net.SocketAddress localAddress)
                                throws java.lang.Exception
        Create a new client socket handle from a local SocketAddress
        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
      • connect

        protected abstract boolean connect​(H 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.
        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
      • finishConnect

        protected abstract boolean finishConnect​(H handle)
                                          throws java.lang.Exception
        Finish the connection process of a client socket after it was marked as ready to process by the select(int) call. The socket will be connected or reported as connection failed.
        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
      • newSession

        protected abstract S newSession​(IoProcessor<S> processor,
                                        H handle)
                                 throws java.lang.Exception
        Create a new IoSession from a connected socket client handle. Will assign the created IoSession to the given IoProcessor for managing future I/O events.
        Parameters:
        processor - the processor in charge of this session
        handle - the newly connected client socket handle
        Returns:
        a new IoSession
        Throws:
        java.lang.Exception - any exception thrown by the underlying systems calls
      • close

        protected abstract void close​(H handle)
                               throws java.lang.Exception
        Close a client socket.
        Parameters:
        handle - the client socket
        Throws:
        java.lang.Exception - any exception thrown by the underlying systems calls
      • wakeup

        protected abstract void wakeup()
        Interrupt the select(int) method. Used when the poll set need to be modified.
      • select

        protected abstract 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 selectedHandles()
        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
      • selectedHandles

        protected abstract java.util.Iterator<H> selectedHandles()
        Iterator for the set of client sockets found connected or failed to connect during the last select(int) call.
        Returns:
        the list of client socket handles to process
      • allHandles

        protected abstract java.util.Iterator<H> allHandles()
        Iterator for all the client sockets polled for connection.
        Returns:
        the list of client sockets currently polled for connection
      • dispose0

        protected final void dispose0()
                               throws java.lang.Exception
        Implement this method to release any acquired resources. This method is invoked only once by AbstractIoService.dispose().
        Specified by:
        dispose0 in class AbstractIoService
        Throws:
        java.lang.Exception - If the dispose failed
      • connect0

        protected final ConnectFuture connect0​(java.net.SocketAddress remoteAddress,
                                               java.net.SocketAddress localAddress,
                                               IoSessionInitializer<? extends ConnectFuture> sessionInitializer)
        Implement this method to perform the actual connect operation.
        Specified by:
        connect0 in class AbstractIoConnector
        Parameters:
        remoteAddress - The remote address to connect from
        localAddress - null if no local address is specified
        sessionInitializer - The IoSessionInitializer to use when the connection s successful
        Returns:
        The ConnectFuture associated with this asynchronous operation