I am currently writing a Java bridge to a eyetracking library written in C. The whole thing works very well, but building an actual application with it is hard, since eyetracking is done by polling for new data.
Polling becomes a problem as soon as you have a graphical user interface, as a poll-loop blocks the main event loop, freezing the gui. This also makes it impossible to stop the polling loop through user input.
My goal is to build a polling service that is generic enough to be used for a lot of different applications. It has to be asynchronous and has to have some kind of callback mechanism to use any relevant data.
This is my idea (pseudocode):
PollService {
// The filterFunction decides if a eyetracking event is relevant or can be ignored
setFilterFunction(filterFunction);
// The callbackFunction is executed if a relevant eyetracking event is polled
setCallbackFunction(callbackFunction);
start();
stop();
}
The PollService
itself only manages the asynchronous polling process. It is supplied a couple of functions to perform the two basic steps:
- Filter if a eyetracking event is relevant, skip event if not
- Perform some kind of operation on any event that is relevant
Possible callback operations might be:
- Perform a long running computation with the eyetracking event data
- Print something to the screen
- Store something to a database
- Communicate the event to another thread (using a messaging queue for example)
- Update a GUI (using
Platform.runLater()
for example)
The filterFunction
and callbackFunction
would be implemented as functional interfaces, so Java 8 users can easily supply these functions using lambdas or method references.
What do you think about this design? Are there any obvious flaws i am not seeing?