org.apache.mina.core.polling

## Class AbstractPollingIoProcessor<S extends AbstractIoSession>

- java.lang.Object
- - org.apache.mina.core.polling.AbstractPollingIoProcessor<S>

- Type Parameters:
    `S` - the type of the `IoSession` this processor can handle
  All Implemented Interfaces:
    IoProcessor<S>
  Direct Known Subclasses:
    AprIoProcessor, NioProcessor

      public abstract class AbstractPollingIoProcessor<S extends AbstractIoSession>
      extends Object
      implements IoProcessor<S>

  An abstract implementation of `IoProcessor` which helps transport developers to write an `IoProcessor` easily. This class is in charge of active polling a set of `IoSession` and trigger events when some I/O operation is possible.
  Author:
    Apache MINA Project

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected AtomicBoolean` | `wakeupCalled`

  - ### Constructor Summary

    Constructors
    Modifier | Constructor and Description
    --- | ---
    `protected` | `AbstractPollingIoProcessor(Executor executor)` Create an `AbstractPollingIoProcessor` with the given `Executor` for handling I/Os events.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `add(S session)` Adds the specified `session` to the I/O processor so that the I/O processor starts to perform any I/O operations related with the `session`.
    `protected abstract Iterator<S>` | `allSessions()` Get an `Iterator` for the list of `IoSession` polled by this `IoProcessor`
    `protected abstract void` | `destroy(S session)` Destroy the underlying client socket handle
    `void` | `dispose()` Releases any resources allocated by this processor.
    `protected abstract void` | `doDispose()` Dispose the resources used by this `IoProcessor` for polling the client connections.
    `void` | `flush(S session)` Flushes the internal write request queue of the specified `session`.
    `protected abstract SessionState` | `getState(S session)` Get the state of a session (preparing, open, closed)
    `protected abstract void` | `init(S session)` Initialize the polling of a session.
    `protected abstract boolean` | `isBrokenConnection()` Check that the select() has not exited immediately just because of a broken connection.
    `boolean` | `isDisposed()` Returns `true` if and if only all resources of this processor have been disposed.
    `boolean` | `isDisposing()` Returns `true` if and if only `IoProcessor.dispose()` method has been called.
    `protected abstract boolean` | `isInterestedInRead(S session)` is this session registered for reading
    `protected abstract boolean` | `isInterestedInWrite(S session)` is this session registered for writing
    `protected abstract boolean` | `isReadable(S session)` Is the session ready for reading
    `protected abstract boolean` | `isSelectorEmpty()` Say if the list of `IoSession` polled by this `IoProcessor` is empty
    `protected abstract boolean` | `isWritable(S session)` Is the session ready for writing
    `protected abstract int` | `read(S session, IoBuffer buf)` Reads a sequence of bytes from a `IoSession` into the given `IoBuffer`.
    `protected abstract void` | `registerNewSelector()` In the case we are using the java select() method, this method is used to trash the buggy selector and create a new one, registring all the sockets on it.
    `void` | `remove(S session)` Removes and closes the specified `session` from the I/O processor so that the I/O processor closes the connection associated with the `session` and releases any other related resources.
    `protected abstract int` | `select()` poll those sessions forever
    `protected abstract int` | `select(long timeout)` poll those sessions for the given timeout
    `protected abstract Iterator<S>` | `selectedSessions()` Get an `Iterator` for the list of `IoSession` found selected by the last call of {@link AbstractPollingIoProcessor#select(int)
    `protected abstract void` | `setInterestedInRead(S session, boolean isInterested)` register a session for reading
    `protected abstract void` | `setInterestedInWrite(S session, boolean isInterested)` register a session for writing
    `protected abstract int` | `transferFile(S session, FileRegion region, int length)` Write a part of a file to a `IoSession`, if the underlying API isn't supporting system calls like sendfile(), you can throw a `UnsupportedOperationException` so the file will be send using usual `write(AbstractIoSession, IoBuffer, int)` call.
    `void` | `updateTrafficControl(S session)` Controls the traffic of the specified `session` depending of the `IoSession.isReadSuspended()` and `IoSession.isWriteSuspended()` flags
    `void` | `updateTrafficMask(S session)`
    `protected abstract void` | `wakeup()` Interrupt the {@link AbstractPollingIoProcessor#select(int) call.
    `protected abstract int` | `write(S session, IoBuffer buf, int length)` Write a sequence of bytes to a `IoSession`, means to be called when a session was found ready for writing.
    `void` | `write(S session, WriteRequest writeRequest)` Writes the WriteRequest for the specified `session`.

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

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

- - ### Field Detail

    - #### wakeupCalled

          protected AtomicBoolean wakeupCalled

  - ### Constructor Detail

    - #### AbstractPollingIoProcessor

          protected AbstractPollingIoProcessor(Executor executor)

      Create an `AbstractPollingIoProcessor` with the given `Executor` for handling I/Os events.
      Parameters:
        `executor` - the `Executor` for handling I/O events

  - ### Method Detail

    - #### isDisposing

          public final boolean isDisposing()

      Returns `true` if and if only `IoProcessor.dispose()` method has been called. Please note that this method will return `true` even after all the related resources are released.
      Specified by:
        `isDisposing` in interface `IoProcessor<S extends AbstractIoSession>`

    - #### isDisposed

          public final boolean isDisposed()

      Returns `true` if and if only all resources of this processor have been disposed.
      Specified by:
        `isDisposed` in interface `IoProcessor<S extends AbstractIoSession>`

    - #### dispose

          public final void dispose()

      Releases any resources allocated by this processor. Please note that the resources might not be released as long as there are any sessions managed by this processor. Most implementations will close all sessions immediately and release the related resources.
      Specified by:
        `dispose` in interface `IoProcessor<S extends AbstractIoSession>`

    - #### doDispose

          protected abstract void doDispose()
                                     throws Exception

      Dispose the resources used by this `IoProcessor` for polling the client connections. The implementing class doDispose method will be called.
      Throws:
        `Exception` - if some low level IO error occurs

    - #### select

          protected abstract int select(long timeout)
                                 throws Exception

      poll those sessions for the given timeout
      Parameters:
        `timeout` - milliseconds before the call timeout if no event appear
      Returns:
        The number of session ready for read or for write
      Throws:
        `Exception` - if some low level IO error occurs

    - #### select

          protected abstract int select()
                                 throws Exception

      poll those sessions forever
      Returns:
        The number of session ready for read or for write
      Throws:
        `Exception` - if some low level IO error occurs

    - #### isSelectorEmpty

          protected abstract boolean isSelectorEmpty()

      Say if the list of `IoSession` polled by this `IoProcessor` is empty
      Returns:
        true if at least a session is managed by this `IoProcessor`

    - #### wakeup

          protected abstract void wakeup()

      Interrupt the {@link AbstractPollingIoProcessor#select(int) call.

    - #### allSessions

          protected abstract Iterator<S> allSessions()

      Get an `Iterator` for the list of `IoSession` polled by this `IoProcessor`
      Returns:
        `Iterator` of `IoSession`

    - #### selectedSessions

          protected abstract Iterator<S> selectedSessions()

      Get an `Iterator` for the list of `IoSession` found selected by the last call of {@link AbstractPollingIoProcessor#select(int)
      Returns:
        `Iterator` of `IoSession` read for I/Os operation

    - #### getState

          protected abstract SessionState getState(S session)

      Get the state of a session (preparing, open, closed)
      Parameters:
        `session` - the `IoSession` to inspect
      Returns:
        the state of the session

    - #### isWritable

          protected abstract boolean isWritable(S session)

      Is the session ready for writing
      Parameters:
        `session` - the session queried
      Returns:
        true is ready, false if not ready

    - #### isReadable

          protected abstract boolean isReadable(S session)

      Is the session ready for reading
      Parameters:
        `session` - the session queried
      Returns:
        true is ready, false if not ready

    - #### setInterestedInWrite

          protected abstract void setInterestedInWrite(S session,
                                  boolean isInterested)
                                                throws Exception

      register a session for writing
      Parameters:
        `session` - the session registered
        `isInterested` - true for registering, false for removing
      Throws:
        `Exception`

    - #### setInterestedInRead

          protected abstract void setInterestedInRead(S session,
                                 boolean isInterested)
                                               throws Exception

      register a session for reading
      Parameters:
        `session` - the session registered
        `isInterested` - true for registering, false for removing
      Throws:
        `Exception`

    - #### isInterestedInRead

          protected abstract boolean isInterestedInRead(S session)

      is this session registered for reading
      Parameters:
        `session` - the session queried
      Returns:
        true is registered for reading

    - #### isInterestedInWrite

          protected abstract boolean isInterestedInWrite(S session)

      is this session registered for writing
      Parameters:
        `session` - the session queried
      Returns:
        true is registered for writing

    - #### init

          protected abstract void init(S session)
                                throws Exception

      Initialize the polling of a session. Add it to the polling process.
      Parameters:
        `session` - the `IoSession` to add to the polling
      Throws:
        `Exception` - any exception thrown by the underlying system calls

    - #### destroy

          protected abstract void destroy(S session)
                                   throws Exception

      Destroy the underlying client socket handle
      Parameters:
        `session` - the `IoSession`
      Throws:
        `Exception` - any exception thrown by the underlying system calls

    - #### read

          protected abstract int read(S session,
                 IoBuffer buf)
                               throws Exception

      Reads a sequence of bytes from a `IoSession` into the given `IoBuffer`. Is called when the session was found ready for reading.
      Parameters:
        `session` - the session to read
        `buf` - the buffer to fill
      Returns:
        the number of bytes read
      Throws:
        `Exception` - any exception thrown by the underlying system calls

    - #### write

          protected abstract int write(S session,
                  IoBuffer buf,
                  int length)
                                throws Exception

      Write a sequence of bytes to a `IoSession`, means to be called when a session was found ready for writing.
      Parameters:
        `session` - the session to write
        `buf` - the buffer to write
        `length` - the number of bytes to write can be superior to the number of bytes remaining in the buffer
      Returns:
        the number of byte written
      Throws:
        `Exception` - any exception thrown by the underlying system calls

    - #### transferFile

          protected abstract int transferFile(S session,
                         FileRegion region,
                         int length)
                                       throws Exception

      Write a part of a file to a `IoSession`, if the underlying API isn't supporting system calls like sendfile(), you can throw a `UnsupportedOperationException` so the file will be send using usual `write(AbstractIoSession, IoBuffer, int)` call.
      Parameters:
        `session` - the session to write
        `region` - the file region to write
        `length` - the length of the portion to send
      Returns:
        the number of written bytes
      Throws:
        `Exception` - any exception thrown by the underlying system calls

    - #### add

          public final void add(S session)

      Adds the specified `session` to the I/O processor so that the I/O processor starts to perform any I/O operations related with the `session`.
      Specified by:
        `add` in interface `IoProcessor<S extends AbstractIoSession>`
      Parameters:
        `session` - The added session

    - #### remove

          public final void remove(S session)

      Removes and closes the specified `session` from the I/O processor so that the I/O processor closes the connection associated with the `session` and releases any other related resources.
      Specified by:
        `remove` in interface `IoProcessor<S extends AbstractIoSession>`

    - #### write

          public void write(S session,
                   WriteRequest writeRequest)

      Writes the WriteRequest for the specified `session`.
      Specified by:
        `write` in interface `IoProcessor<S extends AbstractIoSession>`
      Parameters:
        `session` - The session we want the message to be written
        `writeRequest` - the WriteRequest to write

    - #### flush

          public final void flush(S session)

      Flushes the internal write request queue of the specified `session`.
      Specified by:
        `flush` in interface `IoProcessor<S extends AbstractIoSession>`
      Parameters:
        `session` - The session we want the message to be written

    - #### updateTrafficMask

          public final void updateTrafficMask(S session)

    - #### registerNewSelector

          protected abstract void registerNewSelector()
                                               throws IOException

      In the case we are using the java select() method, this method is used to trash the buggy selector and create a new one, registring all the sockets on it.
      Throws:
        `IOException` - If we got an exception

    - #### isBrokenConnection

          protected abstract boolean isBrokenConnection()
                                                 throws IOException

      Check that the select() has not exited immediately just because of a broken connection. In this case, this is a standard case, and we just have to loop.
      Returns:
        true if a connection has been brutally closed.
      Throws:
        `IOException` - If we got an exception

    - #### updateTrafficControl

          public void updateTrafficControl(S session)

      Controls the traffic of the specified `session` depending of the `IoSession.isReadSuspended()` and `IoSession.isWriteSuspended()` flags
      Specified by:
        `updateTrafficControl` in interface `IoProcessor<S extends AbstractIoSession>`

Copyright © 2004-2012 Apache MINA Project. All Rights Reserved.