Package com.iizix
Class Worker
- java.lang.Object
- com.iizix.nio.WorkerNIO
- com.iizix.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
Field Summary
Fields inherited from class com.iizix.nio.WorkerNIO
LAN_BUFFER_SIZE
Constructor Summary
Constructors Constructor Description Worker(SessionInfo sessionInfo)
Creates an instance of the Worker queue.
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
dispose()
Disposes of the worker.SessionInfo
getSessionInfo()
Get the SessionInfo from the Worker.boolean
isVerbose()
Gets the verbose flag.boolean
isWorkerThread()
Checks if the calling thread is the worker thread or not.boolean
post(WorkerTask<?,?,?> task)
Posts a task for execution at a later stage.boolean
post(java.lang.Runnable runnable)
Helper method to post a runnable for execution.boolean
postAndWait(WorkerTask<?,?,?> task)
Posts a task for execution at a later stage and waits for it's completion.boolean
postModalWait(WorkerTask<?,?,?> task)
Posts a task for execution at a later stage and waits for it's completion when a call torelaseModal(modalID,result)
is called.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.boolean
releaseModal(int modalID, java.lang.Object result)
Releases a modal waiting task and sets its result.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.void
setVerbose(boolean isVerbose)
Sets the verbose flag, also used for new clients.Methods inherited from class com.iizix.nio.WorkerNIO
checkDisposed, isDisposed, register, registerTimeoutHandler, unregisterTimeoutHandler, wakeupSelector
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 classWorkerNIO
- 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.
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 classWorkerNIO
- 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).
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 theexecute
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 torelaseModal(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.