Package com.iizix.nio

Class WorkerNIO

java.lang.Object
com.iizix.nio.WorkerNIO
Direct Known Subclasses:
Worker

public class WorkerNIO extends Object
The Worker is a class that handles all communication with "plain" sockets, received and sent (pending) messages to connectors, along with handling the session queue.
Author:
Christopher Mindus
  • Field Details

    • LAN_BUFFER_SIZE

      public static final int LAN_BUFFER_SIZE
      The multi-purpose buffer allocated by the worker (24KB or 16+8KB, 0x6000). The size is chosen to maximize localhost communication and large buffer transmits over 1Gb LAN's.
      See Also:
  • Constructor Details

    • WorkerNIO

      public WorkerNIO() throws IOException
      Creates an instance of the Worker queue.
      Throws:
      IOException - If an I/O error occurs.
  • Method Details

    • registerTimeoutHandler

      public boolean registerTimeoutHandler(IWorkerTimeout handler)
      Registers a new timeout handler. Calling this method more than once for the same handler does not change anything.
      Parameters:
      handler - The timeout handler.
      Returns:
      true for success, false if already registered.
    • unregisterTimeoutHandler

      public boolean unregisterTimeoutHandler(IWorkerTimeout handler)
      Unregisters a timeout handler.
      Parameters:
      handler - The timeout handler.
      Returns:
      true for success, false if not registered.
    • register

      public SelectionKey register(ISocketComm comm, SelectableChannel channel, int ops) throws ClosedChannelException
      Registers a SocketComm - opened - instance with the IO Selector associated with the Worker.
      Parameters:
      comm - The communication instance to attach to the selector.
      channel - The selectable channel (socket or server-socket channels).
      ops - The Operations the communication instance wishes to listen to.
      Returns:
      The selection key for the socket channel.
      Throws:
      ClosedChannelException - If the worker is disposed of or the IO Selector is closed.
    • processSelectorKeys

      protected boolean processSelectorKeys(boolean now) throws IOException
      Process selector keys.
      Parameters:
      now - Flag to process now or at a later stage.
      Returns:
      true if something was found to process, false for nothing.
      Throws:
      IOException - For I/O exceptions.
    • process

      public void process()
      Starts processing the worker queue operations.
    • processOnce

      protected boolean processOnce()
      Processes once a single write-read-execute operation. This is called from the process-loop and postModalWait.
      Returns:
      true for success, false for cancelled or error.
    • checkDisposed

      public void checkDisposed()
      Checks if the worker is disposed. This method does nothing if the worker is not disposed of.
      Throws:
      IllegalStateException - If the worker is disposed of.
    • dispose

      public boolean dispose()
      Disposes of the worker. All pending queued events are discarded along with all possible IO operations. Any callback functions waiting for task to complete will be informed (cancelled task).
      Returns:
      true if disposed of, false if already disposed.
    • isDisposed

      public boolean isDisposed()
      Checks if the worker is disposed of.
      Returns:
      true if worker is disposed, false otherwise.
    • wakeupSelector

      protected void wakeupSelector()
      Wake-up selector. Only called by subclasses.