Those days I had to make a D-Bus server and client in python and I thought to share with you guys some things about the D-Bus.
First of all, what is D-Bus? Well, D-Bus is a system that offers a communication channel for multiple programs.
Someone must also distinguish between: session and system bus.
The session bus is specific for each user, while the system bus is specially designed for the state of the whole system and can change this state (like adding a new storage device, the internet connection suffers some modification).
“Talk is cheap, show me the code” Linus Torvalds
One example of a session bus server is the following#!/usr/bin/env python3
from gi.repository import GLib
import dbus
import dbus.service
from dbus.mainloop.glib import DBusGMainLoop
class Session_DBus(dbus.service.Object):
def __init__(self):
bus_name=dbus.service.BusName('org.me.test_session',bus=dbus.SessionBus())
dbus.service.Object.__init__(self,bus_name,'/org/me/test_session')
@dbus.service.method('org.me.test1')
def session_bus_message1(sefl):
return "Session Bus 1"
@dbus.service.method('org.me.test2')
def session_bus_message2(self):
return "Session Bus 2"
@dbus.service.method('org.me.test2')
def session_bus_strings(self,string1,string2):
return string1+" "+string2
DBusGMainLoop(set_as_default=True)
dbus_service=Session_DBus()
try:
GLib.MainLoop().run()
except KeyboardInterrupt:
print("\nThe MainLoop will close...")
GLib.MainLoop().quit()
Version 1:
#!/usr/bin/env python3
import dbus
bus = dbus.SessionBus()
session = bus.get_object("org.me.test_session", "/org/me/test_session")
method_message1 = session.get_dbus_method('session_bus_message1', 'org.me.test1')
method_message2 = session.get_dbus_method('session_bus_message2', 'org.me.test2')
method_message3 = session.get_dbus_method('session_bus_strings', 'org.me.test2')
print(method_message1())
#interface1 = dbus.Interface(session, "org.me.test1")
#print(interface1.session_bus_message1())
print(method_message2())
print(method_message3("Hello", "World"))
#!/usr/bin/env python3
import dbus
bus = dbus.SessionBus()
session = bus.get_object("org.me.test_session", "/org/me/test_session")
interface1 = dbus.Interface(session, "org.me.test1")
interface2 = dbus.Interface(session, "org.me.test2")
print(interface1.session_bus_message1())
print(interface2.session_bus_message2())
print(interface2.session_bus_strings("hello", "world"))
[D-BUS Service]
Name=org.me.test_session
Exec="/home/charles/my-test-session.py"
we can verify this service using dbus tools:
$ dbus-send --session --type=method_call --print-reply --dest="org.me.test_session" /org/me/test_session org.freedesktop.DBus.Introspectable.Introspect
method return time=1491700435.911482 sender=:1.133 -> destination=:1.134 serial=3 reply_serial=2
string "
"
dbus-send --session --type=method_call --print-reply --dest="org.me.test_session" /org/me/test_session org.me.test2.session_bus_strings string:"hello" string:"world"
method return time=1491700611.464318 sender=:1.133 -> destination=:1.137 serial=4 reply_serial=2
string "hello world"
https://georgemuraruc.wordpress.com/2015/07/16/d-bus-tutorial-for-python/