[Vala] Proposal for asynchronous DBus calls.



= intro =
Vala has embedded DBus calls through its dynamic method mechanism. 
It is nice, but this is not yet the complete story of DBus.

Aside from the sync calls, DBus can also make async calls. In an async
call, the caller starts a DBus call on a DBusGProxy by
dbus_g_proxy_begin_call, giving a callback to be invoked when the call
is finished. In the callback, dbus_g_proxy_end_call is called to collect
the 'out' args.

Here is a tutorial on how to make asynchrous dbus calls.
http://maemo.org/maemo_training_material/maemo4.x/html/maemo_Platform_Development/Chapter_05_Asynchronous_GLibDBus.html

= current solution =
In current vala, the related functions are exposed in dbus-glib-1.vapi,
and in principle one can always write explicit code with these api to
achieve an async call. lambda functions for DBusGProxyCallNotify can be
used to to mitigate the discontinuity problem of async calls.


= proposed solution =
What if vala can also embed async DBus calls the same way as sync DBus
calls? 

I have a vague, inchoate idea in mind. The newly introduced keyword is
'async', to describe an async function call. 

async Object . Method(method paramters) += callback;

where callback is either a class/namespace method or a lambda function.

Here is an example 

dynamic DBus.Object something;
int int_param = 1;
string string_param = "hello";
async something.Method(string_param, int_param) += (rt1, rt2) => {
    message("async call returns: rt1 = " + rt1.to_string() + "rt2 = " +
rt2.to_string());
}

At the implementation level, valaccodedynamicmethodbinding has to be
modified to generate appropriate wrappers for the callback function by
invoking dbus_g_proxy_end_call.

= Problems =
Note: current vala doesn't support type signatures on parameters of
lambda functions, but we can use a class member function(which has
explicit parameter types) instead. Bug 511879 is related to this.

The implication of asynchronous calls is a running MainLoop. this
implication is not a new one, because we already need a mainloop for
dynamic signals anyways.

= Benifits =

Embedding the support of async calls into Vala makes vala better
suitable for writing async programs which do not block the UI for long
time DBus calls.

The 'async' keyword makes it extremely easy to modify existed sync
program to async programs. In some cases if the return value is not
interested, the modification is merely prefixing the 'async' keyword.

= further =
This can also be extended to non-dynamical methods, by GLib's g_idle
mechanism. 


Yu






[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]