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).
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 the 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.
|
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.
|
int |
registerListener(EventQueueListener listener)
Registers a
listener that will receive 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.
|
void |
unregisterListener(int type)
Unregisters listener for the provided event type
|
public static EventQueue getInstance()
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 or greater than 127.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 or greater than 127.IllegalArgumentException - if the length of data buffer exceeds 24 bits.public int registerListener(EventQueueListener listener) throws IllegalStateException
listener that will receive the events of a type.
The same listener can be registered for several types.
listener - the listener to register.IllegalStateException - if there is no type left for a new listener.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.IllegalArgumentException - if listener is null.@Nullable public void setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler uncaughtExceptionHandler)
uncaughtExceptionHandler - the uncaught exception handler to set.public void unregisterListener(int type)
throws IllegalArgumentException
type - type of listener to be unregisteredIllegalArgumentException - if the given type does not correspond to any registered listener.