public class EventQueue extends Object
A FIFO mechanism is implemented on the native side and is system specific. The user can offer events to this FIFO.
Event notifications are handled using event listeners (Observer design pattern). The application code has to register
EventQueueListener
to be notified when new events are coming. To do so, the user can call
registerListener(EventQueueListener, int)
.
Then the pump automatically retrieves new events pushed in the FIFO and notifies the application via the
EventQueueListener
.
EventQueue runs on a dedicated Java thread to forward and process events. Application event listener's calls are done
in he context of this thread. This thread is started when method getInstance()
is called.
Events reading operations are done using the SNI mechanism. The thread is suspended if the events FIFO is empty. The thread resume is done by the native part when a new event is sent if the events FIFO was previously empty.
Event format
An event is composed of a type and some data. The type identifies the listener that will handle the event. The data is application specific and passed to the listener. The items stored in the FIFO buffer are integers (4 bytes). There are two kinds of events that can be sent over the Event Queue:
+--------------+----------+---------------------------------------------------+ | Extended (1) | Type (7) | Data (if Extended==0), Length (if Extended==1) (24) | +--------------+----------+---------------------------------------------------+ ... +-----------------------------------------------------------------------------+ | Extended Data for extended events (32) | (x integers) +-----------------------------------------------------------------------------+Format explanation:
For more information, please have a look at our Event Queue documentation.
Modifier and Type | Method and Description |
---|---|
static EventQueue |
getInstance()
Gets an instance of a started EventQueue.
|
int |
getNewType()
Gets an unique Id to register a Listener.
|
void |
offerEvent(int type,
int data)
Inserts an event to the FIFO.
|
void |
offerExtendedEvent(int type,
byte[] data)
Inserts an extended event to the FIFO.
|
void |
registerListener(EventQueueListener listener,
int type)
Registers a
listener that will received the events of a type . |
void |
setDefaultListener(EventQueueListener listener)
Sets the default
listener . |
void |
setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler uncaughtExceptionHandler)
Sets a handler for the exceptions that occur during event reading or execution.
|
public static EventQueue getInstance()
public int getNewType()
IllegalStateException
- if the maximum number of types (127
) has been reached.public void offerEvent(int type, int data)
The data
must not exceed 24 bits. Otherwise use offerExtendedEvent(int, byte[])
.
type
- the type of the event.data
- the data of the event.IllegalStateException
- if the FIFO is full.IllegalArgumentException
- if the given type
is lower than 0
.IllegalArgumentException
- if the data
exceeds 24 bits.public void offerExtendedEvent(int type, byte[] data)
When the data exceeds 24 bits, it can be split into several integers. These integers are pushed consecutively to the FIFO. The listener will receive all these integers at once and will be able to rebuild a complex data.
type
- the type of the events.data
- the data of the events.IllegalStateException
- if the FIFO is full.IllegalArgumentException
- if the given type
is lower than 0
.IllegalArgumentException
- if the length of data
buffer exceeds 24 bits.public void registerListener(EventQueueListener listener, int type)
listener
that will received the events of a type
.
The same listener can be registered for several types.
listener
- the listener to register.type
- the type handled by the listener
.IllegalArgumentException
- if the given type
is lower than 0
or higher than 127
.IllegalArgumentException
- if there is already a listener for the event type
.public void setDefaultListener(EventQueueListener listener)
listener
. It will receive all the events whose type is not handled by any registered
listener.listener
- the listener to set.public void setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler uncaughtExceptionHandler)
uncaughtExceptionHandler
- the uncaught exception handler to set.