Receiving dbus signals using low-level C API

John (J5) Palmieri johnp at redhat.com
Wed Jan 24 13:31:46 PST 2007


On Wed, 2007-01-24 at 13:12 -0800, Krishna R wrote:
> Hi,
> 
> I have a basic question about sending and receiving signals. Most of
> what i have learnt till now is based on the dbus low level API
> tutorial.
> 
> I am using dbus 1.0.2 and a session bus.
> 
> Sending signal: 
> I send a signal as told in the tutorial, i use
> dbus_message_append_args() to send some arguments with the message.
> 
> Rcving signal: (this is where i have some questions)
> 
> 1. I set a filter function and read the arguements of the message in
> the filter function. 
> 2. I add a match rule  to recv the signal like this
> dbus_bus_add_match(conn, "type='signal',interface='test.signal.Type'",
> &dbusError); 
> 
> 3. And instead of dbus_connection_pop_message as used in the tutorial,
> do this... 
> while (dbus_connection_read_write
> _dispatch (conn, -1) ) ;  
> 
> 4. In my filter function, i read the message using
> dbus_message_get_args() and return DBUS_HANDLER_RESULT_HANDLED.
> 
> Questions:
> 
> 1. Is this a correct way to recv signals? Do i have to do
> dbus_message_unref(message) in the filter function after handling it
> before i return? 

Yes.  Your filter function will receive all messages and signals.  You
need to unref the message when you are done with it.  This allow you to
keep it around if need be.  Remember, get_args does not return the
arguments but a pointer to the argument within the message so if you
need to keep an argument around you either need to keep the message
around or more commonly you will need to copy the argument. 

> 2. I also see that as soon as i call the match rule api, the filter
> function gets called. I further found that the message has a argument
> which a string, which is 2. Is this expected? I had assumed that the
> filter function will only be called when i send a signal and not
> before that. Why is this? 

Filter functions pick up anything sent to your app.  Chances are you are
entering your dispatch loop after you add the match and you are
receiving a NameOwnerChanged signal.  You should inspect the messages
using the DBusMessage API
(http://dbus.freedesktop.org/doc/dbus/api/html/group__DBusMessage.html)
and also read the spec to see the list of signals D-Bus may send you
(http://dbus.freedesktop.org/doc/dbus-specification.html)

> Seperate point: Also in the tutorial the send signal function closes
> the dbus connection and the dbus version 1.0.2 complains about this.
> Can someone pls fix the tutorial for this. I assume from the error
> message we dont close the connection anymore? 

No you only close private connections (dbus_connection_open_private) and
not shared ones (dbus_bus_get, dbus_connection_open).

-- 
John (J5) Palmieri <johnp at redhat.com>



More information about the dbus mailing list