|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
public interface IEmxDispatcher
Specifies the event dispatcher.
A dispatcher serves as the core event loop for an event driven program. The dispatcher exports methods to register interest in events and methods to drive the dispatcher event pump. Threads drive program execution by registering interest in events and driving the event pump. Upon invocation, the event pump waits for the registered events to get triggered at which point the triggered events are dispatched to associated event handlers. The event pump returns control to the caller when atleast one registered event was triggered and dispatched. Typically, unless the program execution is to be terminated, the caller almost immediately reinvokes the event pump to continue to dispatch triggered events. The repeated invocation of the event pump constitutes the program event loop.
A multi-threaded event driven program operates with multiple concurrently executing dispatchers. As noted in the threading section below, a dispatcher can be owned and operated by a single (owner) thread at a time. Therefore, in a multi-threaded program, at any point in time, each executing thread owns and operates its own dispatcher concurrently with the other executing threads. It is permissible to change thread-dispatcher ownerships during the lifecycle of the program. However, in a well designed multi-threaded event driven program based on the dispatcher, this rebalancing of thread-dispatcher ownnership should occur infrequently. In such a program architecture, inter thread communication is facilitated through the use of user events.
The registration of interest in an event is performed through the act of 'scheduling' an event. A user registers interest in an event by instantiating an event type specific event object object and handing it to the dispatcher for scheduling. The act of scheduling involves (1) waiting for the event specific condition to be triggered and (2) dispatching the provided event object to a specified event handler when the condition is triggered. From an event object lifecycle perspective, a registered event object waits in the dispatcher wait queue until the event condition is triggered. On trigger, the event object is moved to the dispatcher ready queue and dispatched in priority order. The return value from the event handler indicates whether the event continues to be scheduled by the dispatcher or not. A return value of true indicates that the user wishes for the event to be scheduled again. In this case, the event object is moved back to the wait queue. Otherwise, the event is discarded by the dispatcher. The event object also contains a flag to indicate whether repeat scheduling is permissible or not. In case the flag indicates that repeat scheduling is not possible, the return value from the event handler is ignored and the event always discarded after it is dispatched.
Since an event object is retained internally by the dispatcher in its wait queues, the event handler should not retain a reference to the event unless it is certain that the dispatcher has discarded its reference to the event (i.e. by returning false from the event handler and/or ensuring that the 'permit reschedule' flag is false in the event object).
The dispatcher supports three types of events. They are
- Network Ready Events
- Alarm Events
- User Events
There are four different types of network ready events. They are the 'connect
ready' event, the 'accept ready' event, the 'read ready' event and the
'write ready' event. A separate scheduling/unscheduling method pair exists
for each of these event types.
The scheduling of an 'accept ready' event means that the user wants to
be notified when a specified EMX link server endpoint is ready to accept
an incoming connection request. The user schedules such an event by
instantiating an IEmxNwAcceptReadyEvent
object and invoking the
schedNwAcceptReadyEv(com.neeve.emx.IEmxNwAcceptReadyEvent)
method. The instantiated event object contains
the EMX link server endpoint of interest. A successful scheduling of the
network event causes the dispatcher to place the event in the dispatcher wait
queue where the event waits until there is an incoming link connection
request waiting to be accepted via the provided server endpoint. The event
is then moved from the wait queue to the ready queue from where it is
dispatched to the user in priority order. The user can use the
unschedNwAcceptReadyEv(com.neeve.emx.IEmxNwAcceptReadyEvent)
method to unschedule a previously scheduled
'accept ready' event i.e. remove the event from the dispatcher's scheduling
queues before it is dispatched to the user. 'Accept ready' event scheduling
and unscheduling can only be done by the dispatcher owner thread.
The scheduling of a 'connect ready' event means that the user wants to
be notified when a specified EMX link client endpoint is ready to complete
a previously initiated connection request. The user schedules such an event
by instantiating an IEmxNwConnectReadyEvent
object and invoking the
schedNwConnectReadyEv(com.neeve.emx.IEmxNwConnectReadyEvent)
method. The instantiated event object contains
the EMX link client endpoint of interest. A successful scheduling of the
network event causes the dispatcher to place the event in the dispatcher wait
queue where the event waits until the connection request is ready to be
completed. The event is then moved from the wait queue to the ready queue
from where it is dispatched to the user in priority order. The user can use
the unschedNwConnectReadyEv(com.neeve.emx.IEmxNwConnectReadyEvent)
method to unschedule a previously
scheduled 'connect ready' event i.e. remove the event from the dispatcher's
scheduling queues before it is dispatched to the user. 'Connect ready' event
scheduling and unscheduling can only be done by the dispatcher owner thread.
The scheduling of a 'read ready' event means that the user wants to be
notified when there is data ready to be read through a specified EMX link
peer endpoint. The user schedules such an event by instantiating an
IEmxNwReadReadyEvent
object and invoking the schedNwReadReadyEv(com.neeve.emx.IEmxNwReadReadyEvent)
method. The instantiated event object contains the EMX link peer endpoint of
interest. A successful scheduling of the network event causes the dispatcher
to place the event in the dispatcher wait queue where the event waits until
the data is ready to be read through the client endpoint. The event is then
moved from the wait queue to the ready queue from where it is dispatched to
the user in priority order. The user can use the unschedNwReadReadyEv(com.neeve.emx.IEmxNwReadReadyEvent)
method to unschedule a previously scheduled 'read ready' event i.e. remove
the event from the dispatcher's scheduling queues before it is dispatched to
the user. 'Read ready' event scheduling and unscheduling can only be done
by the dispatcher owner thread.
The scheduling of a 'write ready' event means that the user wants to be
notified when a specified EMX link peer endpoint is ready for data to be
written through it. The user schedules such an event by instantiating an
IEmxNwWriteReadyEvent
object and invoking the schedNwWriteReadyEv(com.neeve.emx.IEmxNwWriteReadyEvent)
method. The instantiated event object contains the EMX link peer endpoint of
interest. A successful scheduling of the network event causes the dispatcher
to place the event in the dispatcher wait queue where the event waits until
the the client endpoint is ready for data to be written through it. The event
is then moved from the wait queue to the ready queue from where it is dispatched
to the user in priority order. The user can use the unschedNwWriteReadyEv(com.neeve.emx.IEmxNwWriteReadyEvent)
method to unschedule a previously scheduled 'write ready' event i.e. remove
the event from the dispatcher's scheduling queues before it is dispatched to
the user. 'Write ready' event scheduling and unscheduling can only be done
by the dispatcher owner thread.
The request to schedule an alarm event means that the user want to be
notified after a specified time interval. The user schedules an alarm
event by instantiating an IEmxAlarmEvent
and invoking the
schedAlarmEv(com.neeve.emx.IEmxAlarmEvent)
method. The instantiate event object contains the
timeout interval upon expiry of which the user needs to be notified. A
successful scheduling of an alarm event causes the event to be placed in
the wait queue where the event waits for the specified time interval. After
expiry of the interval, the event is moved to the ready queue from where it
is dispatched in priority order. The user can use the unschedAlarmEv(com.neeve.emx.IEmxAlarmEvent)
method to cancel an alarm i.e. remove the alarm event from the scheduler
queues. The resetAlarmEv(com.neeve.emx.IEmxAlarmEvent, long)
is used to reset the alarm base time (the
base time + interval = dispatch time). The scheduling and unscheduling of
alarm events can only be done by the dispatcher owner thread.
The request to schedule a user event means that the user just wants to place the event in the dispatcher ready queue. User events, as opposed to alarm and network events, do not have a triggering condition. User events are ready for dispatch immediately when scheduled and, once again opposed to alarm and network events, can be scheduled by a thread different from the owner thread of the dispatcher in which the event is being scheduled. Events scheduled by the owner thread of the dispatcher in which they are scheduled is a technique typically used to defer the processing of a task (since the act of scheduling an event puts the event behind all events of higher or equal priority in the dispatchers ready queue). Scheduling events in dispatchers owned by a different thread is a mechanism used for inter thread communication.
setOwner(int)
method at any point during the lifetime of the dispatcher.
However, the dispatcher needs to be in a particular state for ownership to
be transferred. Refer to setOwner(int)
for more information on
permissible states in which thread ownership can be changed. Most dispatcher
methods apart from the method used to schedule user events permit invocation
only by the owner thread. Refer to the method documentation for more
information on which threads are permitted to invoke which methods.Nested Class Summary | |
---|---|
static class |
IEmxDispatcher.Params
Specifies dispatcher operating parameters. |
Field Summary | |
---|---|
static int |
FLAG_TAG_TO_OWNER
Instruct a dispatcher to tag the owner thread with the owned dispatcher. |
Method Summary | |
---|---|
void |
close(boolean flush)
Close the dispatcher. |
void |
flush()
Flush the dispatcher ready queue. |
Object |
getAttachment()
Get the object currently attached to the dispatcher. |
String |
getName()
Get the dispatcher name. |
Thread |
getOwner()
Get the current owner of the dispatcher. |
IEmxDispatcher.Params |
getParams()
Get the dispatcher parameters object. |
int |
getReadyEvCount()
Get the number of events in the dispatcher ready queue. |
IEmxDispatcherStats |
getStats()
Get the dispatcher statistics object. |
boolean |
getThreaded()
Get whether the dispatcher is operating in a multi-threaded mode. |
int |
getWaitEvCount()
Get the number of events in the dispatcher wait queue. |
void |
resetAlarmEv(IEmxAlarmEvent event,
long lastDispatchTime)
Reset a scheduled alarm event. |
int |
run(int timeout)
The dispatcher event pump. |
Object |
run(int timeout,
IEmxDispatcherRunCompletionChecker completionChecker)
Drives the dispatcher until user deems complete or timeout. |
void |
schedAlarmEv(IEmxAlarmEvent event)
Schedule an alarm event for dispatch. |
void |
schedEv(IEmxEvent event)
Schedule an event for dispatch. |
void |
schedNwAcceptReadyEv(IEmxNwAcceptReadyEvent event)
Schedule an 'accept ready' event for dispatch. |
void |
schedNwConnectReadyEv(IEmxNwConnectReadyEvent event)
Schedule a 'connect ready' event for dispatch. |
void |
schedNwReadReadyEv(IEmxNwReadReadyEvent event)
Schedule an 'read ready' event for dispatch. |
void |
schedNwWriteReadyEv(IEmxNwWriteReadyEvent event)
Schedule an 'write ready' event for dispatch. |
void |
schedUserEv(IEmxUserEvent event)
Schedule a user event for dispatch. |
void |
setAttachment(Object object)
Attach an opaque object to the dispatcher. |
void |
setOwner()
Acquire ownership of the dispatcher. |
void |
setOwner(int flags)
Acquire ownership of the dispatcher. |
void |
unschedAlarmEv(IEmxAlarmEvent event)
Cancel a scheduled alarm event. |
void |
unschedEv(IEmxEvent event)
Cancel a scheduled event. |
void |
unschedNwAcceptReadyEv(IEmxNwAcceptReadyEvent event)
Cancel a scheduled 'accept ready' event. |
void |
unschedNwConnectReadyEv(IEmxNwConnectReadyEvent event)
Cancel a scheduled 'connect ready' event. |
void |
unschedNwReadReadyEv(IEmxNwReadReadyEvent event)
Cancel a scheduled 'read ready' event. |
void |
unschedNwWriteReadyEv(IEmxNwWriteReadyEvent event)
Cancel a scheduled 'write ready' event. |
Field Detail |
---|
static final int FLAG_TAG_TO_OWNER
This flag is used by the setOwner(int)
method to determine
whether to set owned dispatcher in the dispatcher thread local variable.
This enables getting the dispatcher from a thread from anywhere in a
program.
Method Detail |
---|
String getName()
A dispatcher name is used to to identify a dispatcher during tracing and debugging. The dispatcher name is srt during dispatcher creation and cannot be changed during the lifetime of the dispatcher.
void setOwner(int flags)
flags
- Flags used to qualify the ownership acquisition operation.
The following flags are permissible for use here:FLAG_TAG_TO_OWNER
This method transfers ownership of the dispatcher to the calling thread.
Optionally, if the FLAG_TAG_TO_OWNER
flag is
specified, the method also sets the EmxThread.dispatcher
thread
local variable with the owned dispatcher.
void setOwner()
This method invokes the following:
setOwner(FLAG_TAG_TO_OWNER)
Thread getOwner()
boolean getThreaded()
getParams()
method.IEmxDispatcher.Params getParams()
IEmxDispatcherStats getStats()
void setAttachment(Object object)
This method replaces the current attachment with the provided object.
Object getAttachment()
int getWaitEvCount() throws EEmxNotOwnerException
EEmxNotOwnerException
- Thrown in case this method is invoked
by a thread that is not the dispatcher owner.
This method returns the number of events in the dispatcher wait queue. Events in the wait queue are awaiting for an appropriate event to be triggered before they are moved to the dispatcher ready queue for dispatch.
int getReadyEvCount() throws EEmxNotOwnerException
EEmxNotOwnerException
- Thrown in case this method is invoked
by a thread that is not the dispatcher owner.
This method returns the number of events in the dispatcher ready queue pending dispatch.
void schedUserEv(IEmxUserEvent event) throws EEmxAlreadyScheduledException
event
- The user event to be scheduled
EEmxAlreadyScheduledException
- Thrown in case the event is
currently scheduled with a dispatcher.
This method schedules a user event for dispatch. The event is placed directly in the dispatcher ready queue and is ready for dispatch.
void schedAlarmEv(IEmxAlarmEvent event) throws EEmxNotOwnerException, EEmxAlreadyScheduledException
event
- The alarm event to be scheduled.
EEmxNotOwnerException
- Thrown in case this method is invoked
by a thread that is not the dispatcher owner.
EEmxAlreadyScheduledException
- Thrown in case the event is
currently scheduled with a dispatcher.
This method schedules an alarm event for dispatch. The event is placed into the dispatcher wait queue and is moved to ready queue for dispatch upon expiry of the specified interval.
void resetAlarmEv(IEmxAlarmEvent event, long lastDispatchTime) throws EEmxNotOwnerException, EEmxNotScheduledException
event
- The alarm event to be reset.lastDispatchTime
- Specifies the time to which to reset the
alarm event's bsae time (base time + interval = dispatch time). A
value of 0 causes the alarm to be reset using the current time.
EEmxNotOwnerException
- Thrown in case this method is invoked
by a thread that is not the dispatcher owner.
EEmxNotScheduledException
- Thrown in case the event is not
currently scheduled with the dispatcher.
This method resets a scheduled alarm event. Resetting an alarm event causes the dispatcher to set the alarm event's base time to the specified time i.e. the event will now be dispatched at a time equal to the specified last dispatch time plus the alarm interval.
void unschedAlarmEv(IEmxAlarmEvent event) throws EEmxNotOwnerException, EEmxNotScheduledException
event
- The alarm event to be cancelled.
EEmxNotOwnerException
- Thrown in case this method is invoked
by a thread that is not the dispatcher owner.
EEmxNotScheduledException
- Thrown in case the event is not
currently scheduled with the dispatcher.
This method removes a previously scheduled alarm event from the dispatcher's scheduling queues. A successful return from this method guarantees that the dispatcher releases all its references to the cancelled alarm event and the event will not be subsequently dispatched to the user.
void schedNwAcceptReadyEv(IEmxNwAcceptReadyEvent event) throws EEmxNotOwnerException, EEmxAlreadyScheduledException
event
- The 'accept ready' event to be scheduled.
EEmxNotOwnerException
- Thrown in case this method is invoked
by a thread that is not the dispatcher owner.
EEmxAlreadyScheduledException
- Thrown in case the event is
currently scheduled with a dispatcher.
This method schedules an 'accept ready' event for dispatch. The event is placed into the dispatcher wait queue and is moved to ready queue for dispatch when the EMX link server endpoint contained in the event object is ready to accept an incoming connection request.
void unschedNwAcceptReadyEv(IEmxNwAcceptReadyEvent event) throws EEmxNotOwnerException, EEmxNotScheduledException
event
- The 'accept ready' event to be cancelled.
EEmxNotOwnerException
- Thrown in case this method is invoked
by a thread that is not the dispatcher owner.
EEmxNotScheduledException
- Thrown in case the event is not
currently scheduled with the dispatcher.
This method removes a previously scheduled 'accept ready' event from the dispatcher's scheduling queues. A successful return from this method guarantees that the dispatcher releases all its references to the the cancelled network event and the event will not be subsequently dispatched to the user.
void schedNwConnectReadyEv(IEmxNwConnectReadyEvent event) throws EEmxNotOwnerException, EEmxAlreadyScheduledException
event
- The 'connect ready' event to be scheduled.
EEmxNotOwnerException
- Thrown in case this method is invoked
by a thread that is not the dispatcher owner.
EEmxAlreadyScheduledException
- Thrown in case the event is
currently scheduled with a dispatcher.
This method schedules a 'connect ready' event for dispatch. The event is placed into the dispatcher wait queue and is moved to ready queue for dispatch when the EMX link client endpoint contained in the event object is ready to complete the connect operation.
void unschedNwConnectReadyEv(IEmxNwConnectReadyEvent event) throws EEmxNotOwnerException, EEmxNotScheduledException
event
- The 'connect ready' event to be cancelled.
EEmxNotOwnerException
- Thrown in case this method is invoked
by a thread that is not the dispatcher owner.
EEmxNotScheduledException
- Thrown in case the event is not
currently scheduled with the dispatcher.
This method removes a previously scheduled 'connect ready' event from the dispatcher's scheduling queues. A successful return from this method guarantees that the dispatcher releases all its references to the the cancelled network event and the event will not be subsequently dispatched to the user.
void schedNwReadReadyEv(IEmxNwReadReadyEvent event) throws EEmxNotOwnerException, EEmxAlreadyScheduledException
event
- The 'read ready' event to be scheduled.
EEmxNotOwnerException
- Thrown in case this method is invoked
by a thread that is not the dispatcher owner.
EEmxAlreadyScheduledException
- Thrown in case the event is
currently scheduled with a dispatcher.
This method schedules an 'read ready' event for dispatch. The event is placed into the dispatcher wait queue and is moved to ready queue for dispatch when the EMX link peer endpoint contained in the event object is ready to read an incoming data.
void unschedNwReadReadyEv(IEmxNwReadReadyEvent event) throws EEmxNotOwnerException, EEmxNotScheduledException
event
- The 'read ready' event to be cancelled.
EEmxNotOwnerException
- Thrown in case this method is invoked
by a thread that is not the dispatcher owner.
EEmxNotScheduledException
- Thrown in case the event is not
currently scheduled with the dispatcher.
This method removes a previously scheduled 'read ready' event from the dispatcher's scheduling queues. A successful return from this method guarantees that the dispatcher releases all its references to the the cancelled network event and the event will not be subsequently dispatched to the user.
void schedNwWriteReadyEv(IEmxNwWriteReadyEvent event) throws EEmxNotOwnerException, EEmxAlreadyScheduledException
event
- The 'write ready' event to be scheduled.
EEmxNotOwnerException
- Thrown in case this method is invoked
by a thread that is not the dispatcher owner.
EEmxAlreadyScheduledException
- Thrown in case the event is
currently scheduled with a dispatcher.
This method schedules an 'write ready' event for dispatch. The event is placed into the dispatcher wait queue and is moved to ready queue for dispatch when the EMX link peer endpoint contained in the event object is ready to write outgoing data.
void unschedNwWriteReadyEv(IEmxNwWriteReadyEvent event) throws EEmxNotOwnerException, EEmxNotScheduledException
event
- The 'write ready' event to be cancelled.
EEmxNotOwnerException
- Thrown in case this method is invoked
by a thread that is not the dispatcher owner.
EEmxNotScheduledException
- Thrown in case the event is not
currently scheduled with the dispatcher.
This method removes a previously scheduled 'write ready' event from the dispatcher's scheduling queues. A successful return from this method guarantees that the dispatcher releases all its references to the the cancelled network event and the event will not be subsequently dispatched to the user.
void schedEv(IEmxEvent event) throws EEmxNotOwnerException, EEmxAlreadyScheduledException
event
- The event to be scheduled.
EEmxNotOwnerException
- Thrown in case this method is invoked
by a thread that is not the dispatcher owner.
EEmxAlreadyScheduledException
- Thrown in case the event is
currently scheduled with a dispatcher.
IllegalArgumentException
- Thrown in case the event type is
not recognized by the dispatcher.
This method is a convenience method to schedule an event for dispatch. The method checks the event type and incokes the type specific schedule method.
void unschedEv(IEmxEvent event) throws EEmxNotOwnerException, EEmxNotScheduledException
event
- The event to be cancelled.
EEmxNotOwnerException
- Thrown in case this method is invoked
by a thread that is not the dispatcher owner.
EEmxNotScheduledException
- Thrown in case the event is not
currently scheduled with the dispatcher.
IllegalArgumentException
- Thrown in case the event type is
not recognized by the dispatcher.
This method is a convenience method to cancel a scheduled event. The method checks the event type and incokes the type specific unschedule method.
int run(int timeout) throws EEmxNotOwnerException
timeout
- Specifies the maximum time in milliseconds to
wait for events to arrive for dispatch. A value < 0 indicates an
indefinite wait.
EEmxNotOwnerException
- Thrown in case this method is invoked
by a thread that is not the dispatcher owner.
This method dispatches events present in the dispatcher ready queue. In case the ready queue is empty, the method sleeps and wait for the specified timeout for an event to arrive in the ready queue. The method will return when atleast one event is dispatched by the method or the timeout expired before any event arrived in the ready queue.
Object run(int timeout, IEmxDispatcherRunCompletionChecker completionChecker) throws EEmxNotOwnerException, EEmxTimeoutException, EEmxException
timeout
- Specifies the maximum time in milliseconds to run the
dispatcher. A value < 0 indicates an indefinite run.completionChecker
- The object that this method will consult to
determine whether the run has completed or not. This parameter
can be null in which case the method will return only on timeout or
error.
EEmxNotOwnerException
- Thrown in case this method is invoked
by a thread that is not the dispatcher owner.
EEmxTimeoutException
- Thrown if the timeout occurred before
the completion checker indicated completion.
ELnkException
- Thrown in case an error occurs during the running
of the dispatcher or the completion checker threw an exception on
completion.
This method runs the dispatcher dispatching events until the specified
completion checker reports that the run has completed, the timeout
specified expires or an error occurs. Essentially, this method invokes
run(int)
using a diminishing timeout and querying the completion
checker for completion after each invocation to run(int)
EEmxException
void flush() throws EEmxNotOwnerException
EEmxNotOwnerException
- Thrown in case this method is invoked
by a thread that is not the dispatcher owner.
This method dispatches any events pending dispatch in the dispatcher ready queue.
void close(boolean flush) throws EEmxNotOwnerException, EEmxException
EEmxNotOwnerException
- Thrown in case this method is invoked
by a thread that is not the dispatcher owner and a flush is requested.
The method can be closed by the non-owner thread in case a flush is
not requested.
EEmxException
- Thrown if an error occurred while closing internal
dispatcher resources.
This method closes the dispatcher. This method should be invoked by a
user when it is done working with the dispatcher. This this method will
invoke flush()
(if requested) to dispatch any remaining events
in the ready queue, automatically cancel any events pending in the wait
queues and then release all internal dispatcher resources.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |