Package com.iizix

Class Worker


public final class Worker extends WorkerNIO
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
  • Constructor Details

    • Worker

      public Worker(SessionInfo sessionInfo) throws IOException
      Creates an instance of the Worker queue.
      Parameters:
      sessionInfo - The SessionInfo for an EndPoint, null for server or (single) main worker (for Eclipse case).
      Throws:
      IOException - If an I/O error occurs.
  • Method Details

    • isVerbose

      public boolean isVerbose()
      Gets the verbose flag.
      Returns:
      The verbose output flag on console.
    • setVerbose

      public void setVerbose(boolean isVerbose)
      Sets the verbose flag, also used for new clients.
      Parameters:
      isVerbose - The verbose output flag on console.
    • getSessionInfo

      public SessionInfo getSessionInfo()
      Get the SessionInfo from the Worker.
      Returns:
      The SessionInfo for an EndPoint, null for server or (single) main worker (for Eclipse case).
    • processSelectorKeys

      protected boolean processSelectorKeys(boolean now) throws IOException
      Process selector keys.
      Overrides:
      processSelectorKeys in class WorkerNIO
      Parameters:
      now - Checking of selectors now or when queue is empty.
      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.
      Overrides:
      process in class WorkerNIO
    • isWorkerThread

      public boolean isWorkerThread()
      Checks if the calling thread is the worker thread or not.
      Returns:
      true, same calling thread as the worker thread, false otherwise.
    • processOnce

      protected boolean processOnce()
      Processes once a single write-read-execute operation. This is called from the process-loop and postModalWait.
      Overrides:
      processOnce in class WorkerNIO
      Returns:
      true for success, false for cancelled or error.
    • 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).
      Overrides:
      dispose in class WorkerNIO
      Returns:
      true if disposed of, false if already disposed.
    • post

      public boolean post(Runnable runnable)
      Helper method to post a runnable for execution.
      Parameters:
      runnable - The runnable.
      Returns:
      true if the task has been queue successfully, false if the worker has been disposed of, or task couldn't be queue, because e.g. no processing thread is assigned to the worker.
    • post

      public boolean post(WorkerTask<?,?,?> task)
      Posts a task for execution at a later stage.
      Parameters:
      task - The task to post, non-null.
      Returns:
      true if the task has been queue successfully, false if the worker has been disposed of, or task couldn't be queue, because e.g. no processing thread is assigned to the worker.
      Throws:
      IllegalStateException - If the task already has been posted to a worker.
    • postAndWait

      public boolean postAndWait(WorkerTask<?,?,?> task) throws InterruptedException
      Posts a task for execution at a later stage and waits for it's completion. If this method is called from the worker thread (that is not allowed to post-and-wait), the task is just executed directly as if it was called with the execute method.
      Parameters:
      task - The task to post, non-null.
      Returns:
      true if the task has been queue successfully, false if the worker has been disposed of, or task couldn't be queued.
      Throws:
      InterruptedException - If the task was interrupted.
      IllegalStateException - If the task already has been posted to a worker.
    • runNowOrPost

      public boolean runNowOrPost(Runnable runnable)
      Helper method to perform the execution of the runnable now IF the current thread is the worker thread, otherwise a post is done.
      Parameters:
      runnable - The runnable.
      Returns:
      true If the task has been executed, false if queued.
      Throws:
      IllegalStateException - If the worker is disposed of.
    • postModalWait

      public boolean postModalWait(WorkerTask<?,?,?> task) throws InterruptedException
      Posts a task for execution at a later stage and waits for it's completion when a call to relaseModal(modalID,result) is called.
      Parameters:
      task - The task to post, non-null.
      Returns:
      true if the task has been queue successfully and completed (including the modal release), false if the worker has been disposed of, or task couldn't be queued.
      Throws:
      InterruptedException - If the task was interrupted.
      IllegalStateException - If the task already has been posted to a worker.
    • releaseModal

      public boolean releaseModal(int modalID, Object result)
      Releases a modal waiting task and sets its result.
      Parameters:
      modalID - The modal ID of the task.
      result - The result to set for the task.
      Returns:
      true for success, false for failure.
      Throws:
      IllegalStateException - If the task is already released.