Package com.iizix
Class Worker
java.lang.Object
com.iizix.nio.WorkerNIO
com.iizix.Worker
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_SIZEConstructor Summary
ConstructorsMethod Summary
Modifier and TypeMethodDescriptionbooleandispose()Disposes of the worker.Get the SessionInfo from the Worker.booleanGets the verbose flag.booleanChecks if the calling thread is the worker thread or not.booleanpost(WorkerTask<?, ?, ?> task) Posts a task for execution at a later stage.booleanHelper 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 torelaseModal(modalID,result)is called.voidprocess()Starts processing the worker queue operations.protected booleanProcesses once a single write-read-execute operation.protected booleanprocessSelectorKeys(boolean now) Process selector keys.booleanreleaseModal(int modalID, Object result) Releases a modal waiting task and sets its result.booleanrunNowOrPost(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 com.iizix.nio.WorkerNIO
checkDisposed, isDisposed, register, registerTimeoutHandler, unregisterTimeoutHandler, wakeupSelector
Constructor Details
Worker
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
Get the SessionInfo from the Worker.- Returns:
- The SessionInfo for an EndPoint, null for server or (single) main worker (for Eclipse case).
processSelectorKeys
Process selector keys.- Overrides:
processSelectorKeysin classWorkerNIO- 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.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:
processOncein 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
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
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
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 theexecutemethod.- 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
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
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:
InterruptedException- If the task was interrupted.IllegalStateException- If the task already has been posted to a worker.
releaseModal
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.