Is it possible that a service object gets a notification when a client closes dbus connection?

John (J5) Palmieri johnp at redhat.com
Mon Mar 31 07:52:03 PDT 2008


On Mon, 2008-03-31 at 22:15 +0800, Huang Peng wrote:
> Hi Simon,
> 
> Thanks. I write some code to test your idea. It works well. :)
> But I still have a little problem. You said we can use argyment matching
> to avoid unnecessay wakeups. How to get the client's Name (arg0)? Can I
> get the name of the caller in any functions of my service? Or I must
> provide a function in my service and let client call it with the
> client's name as a argument?
> 
> BTW, I am using dbus-python.

The pattern goes like this:

client calls server
service gets the call, requests the unique name of the caller
(e.g. :1.4) 
service checks against a table (in python just keep a dict around) to
see if it added the match yet
if not add the match listening for NameOwnerChanged with the first
argument being the unique name match, and add the name to the table
when a NameOwnerChanged signal comes over remove the match, remove the
name from the table, call any function for dealing with clients going
off the bus

You might want to add a register method to the service so you don't have
to run the check for every method or you could 'inherit' from the
dbus.service.Method decorator but that would involve some knowledge of
the internals.

Assuming a register method in python it would look like:

@dbus.service.Method(sender_keyword="sender")
def Register(self, sender):
   if not self.clients_table.haskey(sender):
     self.client_match_table[sender] =
       self.session_connection.add_signal_receiver(
            handler_function = self.handle_name_owner_changed_cb,
            signal_name = "NameOwnerChanged",
            dbus_interface = dbus.BUS_DAEMON_IFACE,
            bus_name = dbus.BUS_DAEMON_NAME,
            path = dbus.BUS_DAEMON_PATH,
            arg0 = sender)


to remove simply call self.client_match_table[sender].remove()

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



More information about the dbus mailing list