Class AbstractPollingIoAcceptor<S extends AbstractIoSession, H>

Type Parameters:
S - The type of IoSession
H - The type of IoHandler
All Implemented Interfaces:
IoAcceptor, IoService
Direct Known Subclasses:
AprSocketAcceptor, NioSocketAcceptor

public abstract class AbstractPollingIoAcceptor<S extends AbstractIoSession, H> extends AbstractIoAcceptor
A base class for implementing 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, accepting and disposing the server sockets. An Executor will be used for running client accepting and an AbstractPollingIoProcessor will be used for processing client I/O operations like reading, writing and closing. All the low level methods for binding, accepting, closing need to be provided by the subclassing implementation.
See Also:
  • Field Details

  • Constructor Details

  • Method Details

    • init

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

      protected abstract void init(SelectorProvider selectorProvider) throws Exception
      Initialize the polling system, will be called at construction time.
      Parameters:
      selectorProvider - The Selector Provider that will be used by this polling acceptor
      Throws:
      Exception - any exception thrown by the underlying system calls
    • destroy

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

      protected abstract int select() throws Exception
      Check for acceptable connections, interrupt when at least a server is ready for accepting. All the ready server socket descriptors need to be returned by selectedHandles()
      Returns:
      The number of sockets having got incoming client
      Throws:
      Exception - any exception thrown by the underlying systems calls
    • wakeup

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

      protected abstract Iterator<H> selectedHandles()
      Iterator for the set of server sockets found with acceptable incoming connections during the last select() call.
      Returns:
      the list of server handles ready
    • open

      protected abstract H open(SocketAddress localAddress) throws Exception
      Open a server socket for a given local address.
      Parameters:
      localAddress - the associated local address
      Returns:
      the opened server socket
      Throws:
      Exception - any exception thrown by the underlying systems calls
    • localAddress

      protected abstract SocketAddress localAddress(H handle) throws Exception
      Get the local address associated with a given server socket
      Parameters:
      handle - the server socket
      Returns:
      the local SocketAddress associated with this handle
      Throws:
      Exception - any exception thrown by the underlying systems calls
    • accept

      protected abstract S accept(IoProcessor<S> processor, H handle) throws Exception
      Accept a client connection for a server socket and return a new IoSession associated with the given IoProcessor
      Parameters:
      processor - the IoProcessor to associate with the IoSession
      handle - the server handle
      Returns:
      the created IoSession
      Throws:
      Exception - any exception thrown by the underlying systems calls
    • close

      protected abstract void close(H handle) throws Exception
      Close a server socket.
      Parameters:
      handle - the server socket
      Throws:
      Exception - any exception thrown by the underlying systems calls
    • dispose0

      protected void dispose0() throws 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:
      Exception - If the dispose failed
    • bindRequestAdded

      protected void bindRequestAdded()
      Invoked when a bind request has been registered for processing. The default implementation does nothing.
    • bindInternal

      protected final Set<SocketAddress> bindInternal(List<? extends SocketAddress> localAddresses) throws Exception
      Starts the acceptor, and register the given addresses
      Specified by:
      bindInternal in class AbstractIoAcceptor
      Parameters:
      localAddresses - The address to bind to
      Returns:
      the Set of the local addresses which is bound actually
      Throws:
      Exception - If the bind failed
    • startupAcceptor

      private void startupAcceptor() throws InterruptedException
      This method is called by the doBind() and doUnbind() methods. If the acceptor is null, the acceptor object will be created and kicked off by the executor. If the acceptor object is null, probably already created and this class is now working, then nothing will happen and the method will just return.
      Throws:
      InterruptedException
    • unbind0

      protected final void unbind0(List<? extends SocketAddress> localAddresses) throws Exception
      Implement this method to perform the actual unbind operation.
      Specified by:
      unbind0 in class AbstractIoAcceptor
      Parameters:
      localAddresses - The address to unbind from
      Throws:
      Exception - If the unbind failed
    • processHandles

      protected void processHandles(Iterator<H> handles) throws Exception
      Handles new incoming connections by accepting the connections and creating new sessions for them.
      Parameters:
      handles - the connection handles to accept and create new sessions for
      Throws:
      Exception - on errors
    • hasUnbindings

      protected boolean hasUnbindings()
      Tells whether there are pending unbindings.
      Returns:
      true if there are any unbindings pending; false otherwise
    • handleUnbound

      protected void handleUnbound(Collection<AbstractIoAcceptor.AcceptorOperationFuture> unboundFutures) throws Exception
      Processes the futures for executed unbindings, marking all futures as done.
      Parameters:
      unboundFutures - describing the unbindings
      Throws:
      Exception - on errors
    • newSession

      public final IoSession newSession(SocketAddress remoteAddress, SocketAddress localAddress)
      (Optional) Returns an IoSession that is bound to the specified localAddress and the specified remoteAddress which reuses the local address that is already bound by this service.

      This operation is optional. Please throw UnsupportedOperationException if the transport type doesn't support this operation. This operation is usually implemented for connectionless transport types.

      Parameters:
      remoteAddress - The remote address bound to the service
      localAddress - The local address the session will be bound to
      Returns:
      The session bound to the the given localAddress and remote address
    • getBacklog

      public int getBacklog()
      Returns:
      the backLog
    • setBacklog

      public void setBacklog(int backlog)
      Sets the Backlog parameter
      Parameters:
      backlog - the backlog variable
    • isReuseAddress

      public boolean isReuseAddress()
      Returns:
      the flag that sets the reuseAddress information
    • setReuseAddress

      public void setReuseAddress(boolean reuseAddress)
      Set the Reuse Address flag
      Parameters:
      reuseAddress - The flag to set
    • getSessionConfig

      public SocketSessionConfig getSessionConfig()
      Returns:
      the default configuration of the new IoSessions created by this service.