Device server with user defined event loop

Intended audience: developers, Programming language: c++

Sometimes, it could be usefull to write your own process event handling loop. For instance, this feature can be used in a device server process where the ORB is only one of several components that must perform event handling. A device server with a graphical user interface must allow the GUI to handle windowing events in addition to allowing the ORB to handle incoming requests. These types of device server therefore perform non-blocking event handling. They turn the main thread of control over each of the vvarious event-handling sub-systems while not allowing any of them to block for significants period of time. The Tango::Util class has a method called server_set_event_loop() to deal with such a case. This method has only one argument which is a function pointer. This function does not receive any argument and returns a boolean. If this boolean is true, the device server process exits. The device server core will call this function in a loop without any sleeping time between the call. It is the user responsability to implement in this function some kind of sleeping mechanism in order not to make this loop too CPU consuming. The code of this function is executed by the device server main thread. The following piece of code is an example of how you can use this feature.

 1  bool my_event_loop()
 2  {
 3     bool ret;
 4
 5     some_sleeping_time();
 6
 7     ret = handle_gui_events();
 8
 9     return ret;
10  }
11
12  int main(int argc,char *argv[])
13  {
14     Tango::Util *tg;
15     try
16     {
17        // Initialise the device server
18        //----------------------------------------
19        tg = Tango::Util::init(argc,argv);
20
21        tg->set_polling_threads_pool_size(5);
22
23        // Create the device server singleton
24        //        which will create everything
25        //----------------------------------------
26        tg->server_init(false);
27
28        tg->server_set_event_loop(my_event_loop);
29
30        // Run the endless loop
31        //----------------------------------------
32        cout << "Ready to accept request" << endl;
33        tg->server_run();
34     }
35     catch (bad_alloc)
36     {
37     ...

The device server main event loop is set at line 29 before the call to the Util::server_run() method. The function used as server loop is defined between lines 2 and 11.