Damien LEFEVRE
Damien LEFEVRE

Reputation: 170

D-Bus python service example

I'm having trouble running the simplest D-Bus service. Here's the code I try t use

#!/usr/bin/python3
from gi.repository import GLib
import dbus
import dbus.service
from dbus.mainloop.glib import DBusGMainLoop

class Example(dbus.service.Object):
    def __init__(self, object_path):
        dbus.service.Object.__init__(self, dbus.SessionBus(), object_path)
        self._last_input = None

    @dbus.service.method(dbus_interface='com.example.Sample',
                         in_signature='v', out_signature='s')
    def StringifyVariant(self, var):
        self.LastInputChanged(var)      # emits the signal
        return str(var)

    @dbus.service.signal(dbus_interface='com.example.Sample',
                         signature='v')
    def LastInputChanged(self, var):
        # run just before the signal is actually emitted
        # just put "pass" if nothing should happen
        self._last_input = var

    @dbus.service.method(dbus_interface='com.example.Sample',
                         in_signature='', out_signature='v')
    def GetLastInput(self):
        return self._last_input

if __name__ == '__main__':
    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)

    session_bus = dbus.SessionBus()
    name = dbus.service.BusName("com.example.SampleService", session_bus)
    object = Example('/com/example/Sample')

    mainloop = GLib.MainLoop()
    print ("Running sample service.")
    mainloop.run()

Then I added /usr/share/dbus-1/services/com.example.Sample.service

[D-BUS Service]
Name=com.example.Sample
Exec=/home/me/dbus_test/service.py

Permissions are correct:

~/dbus_test $ ll
-rwxrwxr-x  1 me me 1345 Jun  3 19:52 service.py*

But I'm not able to connect to the service

$ dbus-send --session  --dest="com.example.Sample" --type="method_call" --print-reply "/com/example/Sample" "com.example.Sample.GetLastInput"

Error org.freedesktop.DBus.Error.NoReply: Did not receive a reply. Possible causes include: the remote application did not send a reply, the message bus security policy blocked the reply, the reply timeout expired, or the network connection was broken.

I just get timeouts.

What am I missing?

Thanks

Upvotes: 0

Views: 1668

Answers (1)

Jussi Kukkonen
Jussi Kukkonen

Reputation: 14617

Your service name is com.example.SampleService but your test client uses com.example.Sample as destination (and the service file has the same mistake).

I recommend using d-feet to 'debug' D-Bus: mistakes like this are easier to spot there.

Upvotes: 3

Related Questions