com.h3c.nbio

## Class AcceptLoop

- java.lang.Object
- - com.h3c.nbio.SelectLoop
  - - com.h3c.nbio.AcceptLoop

- All Implemented Interfaces:
    Runnable

      public abstract class AcceptLoop
      extends SelectLoop

  Abstraction of an I/O loop for accepting in-bound socket connections.

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected SSLContext` | `sslContext` SSL context for secure connections.

    - ### Fields inherited from class com.h3c.nbio.SelectLoop

      `DEFAULT_TIMEOUT, log, selector, stopped, timeout`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `AcceptLoop(long timeout, SocketAddress... addresses)` Creates an accept loop bound to the specified socket address(es) with the given select timeout.
    `AcceptLoop(SocketAddress... addresses)` Creates an accept loop bound to the specified socket address(es) with default select timeout.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected abstract void` | `accept(ServerSocketChannel ssc)` Processes the accept event pending on the specified socket channel.
    `void` | `cease()` Gracefully stops the loop.
    `protected void` | `closeChannels()` Closes the server socket channel(s).
    `protected ServerSocketChannel` | `configureChannel(ServerSocketChannel ssc, SocketAddress address)` Configures the given server socket channel to be in an unblocking mode, registers it and binds it to the specified socket address.
    `protected void` | `createChannels()` Allocates the server socket channel(s) in non-blocking mode, registers it (them) with the selector and then binds to the configured listen address(es).
    `protected void` | `loop()` I/O event loop.
    `void` | `setSslContext(SSLContext context)` Sets the SSL context for secure connections.
    `SSLContext` | `sslContext()` Returns the SSL context used for secure connections.

    - ### Methods inherited from class com.h3c.nbio.SelectLoop

      `getError, openSelector, run, signalStart, waitForFinish, waitForStart`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### sslContext

          protected SSLContext sslContext

      SSL context for secure connections.

  - ### Constructor Detail

    - #### AcceptLoop

          public AcceptLoop(SocketAddress... addresses)
                     throws IOException

      Creates an accept loop bound to the specified socket address(es) with default select timeout.
      Parameters:
        `addresses` - listen socket address(es)
      Throws:
        `IllegalArgumentException` - if no addresses are specified
        `IOException` - if unable to open selector

    - #### AcceptLoop

          public AcceptLoop(long timeout,
                    SocketAddress... addresses)
                     throws IOException

      Creates an accept loop bound to the specified socket address(es) with the given select timeout.
      Parameters:
        `timeout` - select timeout in milliseconds
        `addresses` - listen socket address(es)
      Throws:
        `IllegalArgumentException` - if no addresses are specified
        `IOException` - if unable to open selector

  - ### Method Detail

    - #### setSslContext

          public void setSslContext(SSLContext context)

      Sets the SSL context for secure connections.
      Parameters:
        `context` - the SSL context

    - #### sslContext

          public SSLContext sslContext()

      Returns the SSL context used for secure connections.
      Returns:
        the SSL context

    - #### accept

          protected abstract void accept(ServerSocketChannel ssc)
                                  throws IOException

      Processes the accept event pending on the specified socket channel.
      Parameters:
        `ssc` - server socket channel with pending accept operation
      Throws:
        `IOException` - if unable to accept the incoming socket connection

    - #### createChannels

          protected void createChannels()
                                 throws IOException

      Allocates the server socket channel(s) in non-blocking mode, registers it (them) with the selector and then binds to the configured listen address(es).
      Throws:
        `IOException` - if channel could not be created or bound to address

    - #### configureChannel

          protected ServerSocketChannel configureChannel(ServerSocketChannel ssc,
                                             SocketAddress address)
                                                  throws IOException

      Configures the given server socket channel to be in an unblocking mode, registers it and binds it to the specified socket address.
      Parameters:
        `ssc` - the server socket channel to configure
        `address` - the address to bind to
      Returns:
        the channel, for chaining
      Throws:
        `IOException` - if the channel could not be configured

    - #### closeChannels

          protected void closeChannels()
                                throws IOException

      Closes the server socket channel(s).
      Throws:
        `IOException` - if any channel could not be closed

    - #### cease

          public void cease()

      Gracefully stops the loop.

      This implementation first closes the channels, then terminates the loop.

      Overrides:
        `cease` in class `SelectLoop`

    - #### loop

          protected void loop()
                       throws IOException

      I/O event loop. Implementations are expected to break when `SelectLoop.stopped` becomes `true` via `SelectLoop.cease()` invocation. Also, implementations are expected to `SelectLoop.signalStart()` when ready.

      This implementation first allocates server socket channels via `createChannels()` before starting the loop, which simply accepts any in-bound connections via `accept(java.nio.channels.ServerSocketChannel)` method. After breaking out of the loop, the channels are closed.

      Specified by:
        `loop` in class `SelectLoop`
      Throws:
        `IOException` - if unable to properly select channels or an I/O error is propagated by an event handler.

Copyright © 2014. All Rights Reserved.