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 Summary

      Constructors 
      ConstructorDescription
      Worker​(SessionInfo sessionInfo)
      Creates an instance of the Worker queue.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and TypeMethodDescription
      booleandispose()
      Disposes of the worker.
      SessionInfogetSessionInfo()
      Get the SessionInfo from the Worker.
      booleanisVerbose()
      Gets the verbose flag.
      booleanisWorkerThread()
      Checks if the calling thread is the worker thread or not.
      booleanpost​(WorkerTask<?,​?,​?> task)
      Posts a task for execution at a later stage.
      booleanpost​(java.lang.Runnable runnable)
      Helper method to post a runnable for execution.
      booleanpostAndWait​(WorkerTask<?,​?,​?> task)
      Posts a task for execution at a later stage and waits for it's completion.
      booleanpostModalWait​(WorkerTask<?,​?,​?> task)
      Posts a task for execution at a later stage and waits for it's completion when a call to relaseModal(modalID,result) is called.
      voidprocess()
      Starts processing the worker queue operations.
      protected booleanprocessOnce()
      Processes once a single write-read-execute operation.
      protected booleanprocessSelectorKeys​(boolean now)
      Process selector keys.
      booleanreleaseModal​(int modalID, java.lang.Object result)
      Releases a modal waiting task and sets its result.
      booleanrunNowOrPost​(java.lang.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.
      voidsetVerbose​(boolean isVerbose)
      Sets the verbose flag, also used for new clients.
      • Methods inherited from class java.lang.Object

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

      • Worker

        public Worker​(SessionInfo sessionInfo)
               throws java.io.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:
        java.io.IOException - If an I/O error occurs.
    • Method Detail

      • 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 java.io.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:
        java.io.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​(java.lang.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:
        java.lang.IllegalStateException - If the task already has been posted to a worker.
      • postAndWait

        public boolean postAndWait​(WorkerTask<?,​?,​?> task)
                            throws java.lang.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:
        java.lang.InterruptedException - If the task was interrupted.
        java.lang.IllegalStateException - If the task already has been posted to a worker.
      • runNowOrPost

        public boolean runNowOrPost​(java.lang.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:
        java.lang.IllegalStateException - If the worker is disposed of.
      • postModalWait

        public boolean postModalWait​(WorkerTask<?,​?,​?> task)
                              throws java.lang.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:
        java.lang.InterruptedException - If the task was interrupted.
        java.lang.IllegalStateException - If the task already has been posted to a worker.
      • releaseModal

        public boolean releaseModal​(int modalID,
                                    java.lang.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:
        java.lang.IllegalStateException - If the task is already released.