com.h3c.nbio

## Class SelectLoop

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

- All Implemented Interfaces:
    Runnable
  Direct Known Subclasses:
    AcceptLoop, IOLoop

      public abstract class SelectLoop
      extends Object
      implements Runnable

  Base abstraction of an I/O loop.

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static long` | `DEFAULT_TIMEOUT` Default select timeout of 500L milliseconds.
    `protected org.slf4j.Logger` | `log` Shared logger.
    `protected Selector` | `selector` Shared selector used to drive the loop operation.
    `protected boolean` | `stopped` When true, implementations are expected to break their loop.
    `protected long` | `timeout` Select timeout in milliseconds.

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `SelectLoop()` Creates a select loop with default timeout.
    `SelectLoop(long timeout)` Creates a select loop with the specified timeout.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `cease()` Gracefully stops the loop.
    `Throwable` | `getError()` Gets the prior I/O error, if one occurred.
    `protected abstract void` | `loop()` I/O event loop.
    `protected Selector` | `openSelector()` Opens a new selector for the use by the loop.
    `void` | `run()`
    `protected void` | `signalStart()` Signals all observers that loop has started.
    `boolean` | `waitForFinish(long timeout)` Waits for the loop execution to stop.
    `boolean` | `waitForStart(long timeout)` Waits for the loop execution to start.

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

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

- - ### Field Detail

    - #### log

          protected org.slf4j.Logger log

      Shared logger.

    - #### DEFAULT_TIMEOUT

          public static final long DEFAULT_TIMEOUT

      Default select timeout of 500L milliseconds.
      See Also:
        Constant Field Values

    - #### selector

          protected Selector selector

      Shared selector used to drive the loop operation.

    - #### stopped

          protected volatile boolean stopped

      When true, implementations are expected to break their loop.

    - #### timeout

          protected long timeout

      Select timeout in milliseconds.

  - ### Constructor Detail

    - #### SelectLoop

          public SelectLoop()
                     throws IOException

      Creates a select loop with default timeout.
      Throws:
        `IOException` - if unable to open selector

    - #### SelectLoop

          public SelectLoop(long timeout)
                     throws IOException

      Creates a select loop with the specified timeout.
      Parameters:
        `timeout` - select timeout in milliseconds
      Throws:
        `IOException` - if unable to open selector

  - ### Method Detail

    - #### openSelector

          protected Selector openSelector()
                                   throws IOException

      Opens a new selector for the use by the loop.
      Returns:
        newly open selector
      Throws:
        `IOException` - if unable to open selector

    - #### cease

          public void cease()

      Gracefully stops the loop.

    - #### loop

          protected abstract void loop()
                                throws IOException

      I/O event loop. Implementations are expected to break when `stopped` becomes `true` via `cease()` invocation. Also, implementations are expected to `signalStart()` when ready.
      Throws:
        `IOException` - if unable to properly select channels or an I/O error is propagated by an event handler.

    - #### run

          public void run()

      This implementation first resets the stopped state to false, then allocates the selector and finally invokes the `loop()` method.

      Specified by:
        `run` in interface `Runnable`

    - #### signalStart

          protected void signalStart()

      Signals all observers that loop has started.

      Implementations are expected to invoke this method from their `loop()` method when ready to start processing requests.

    - #### waitForStart

          public final boolean waitForStart(long timeout)

      Waits for the loop execution to start.
      Parameters:
        `timeout` - number of milliseconds to wait
      Returns:
        true if loop started in time

    - #### waitForFinish

          public final boolean waitForFinish(long timeout)

      Waits for the loop execution to stop.
      Parameters:
        `timeout` - number of milliseconds to wait
      Returns:
        true if loop finished in time

    - #### getError

          public Throwable getError()

      Gets the prior I/O error, if one occurred.
      Returns:
        flush error; null if none occurred

Copyright © 2014. All Rights Reserved.