Package com.iizix.nio

Class WorkerNIO

  • Direct Known Subclasses:
    Worker

    public class WorkerNIO
    extends java.lang.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 Summary

      Fields 
      Modifier and Type Field Description
      static int LAN_BUFFER_SIZE
      The multi-purpose buffer allocated by the worker (24KB or 16+8KB, 0x6000).
    • Constructor Summary

      Constructors 
      Constructor Description
      WorkerNIO()
      Creates an instance of the Worker queue.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void checkDisposed()
      Checks if the worker is disposed.
      boolean dispose()
      Disposes of the worker.
      boolean isDisposed()
      Checks if the worker is disposed of.
      void process()
      Starts processing the worker queue operations.
      protected boolean processOnce()
      Processes once a single write-read-execute operation.
      protected boolean processSelectorKeys​(boolean now)
      Process selector keys.
      java.nio.channels.SelectionKey register​(ISocketComm comm, java.nio.channels.SelectableChannel channel, int ops)
      Registers a SocketComm - opened - instance with the IO Selector associated with the Worker.
      boolean registerTimeoutHandler​(IWorkerTimeout handler)
      Registers a new timeout handler.
      boolean unregisterTimeoutHandler​(IWorkerTimeout handler)
      Unregisters a timeout handler.
      protected void wakeupSelector()
      Wake-up selector.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • 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:
        Constant Field Values
    • Constructor Detail

      • WorkerNIO

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

      • 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 java.nio.channels.SelectionKey register​(ISocketComm comm,
                                                       java.nio.channels.SelectableChannel channel,
                                                       int ops)
                                                throws java.nio.channels.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:
        java.nio.channels.ClosedChannelException - If the worker is disposed of or the IO Selector is closed.
      • processSelectorKeys

        protected boolean processSelectorKeys​(boolean now)
                                       throws java.io.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:
        java.io.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:
        java.lang.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.