dbus
Communicating with our application using python and dbus
Submitted by flaper87 on Wed, 06/04/2008 - 10:32.
Hi!!
I've been programming the httpServer and the dbusServer for mouseTrap and I wanted to share how simple is communicate with our applications using dbus and its python bindings.
The piece of code we are interested in is this:
import dbus
import gobject
import dbus.service
from dbus.mainloop.glib import DBusGMainLoop
main_loop = DBusGMainLoop()
bus = dbus.SessionBus(mainloop=main_loop)
DBUS_NAME = "org.myApp"
DBUS_PATH = "/org/myApp"
class myAppdBus(dbus.service.Object):
"""
Our dbus Class
"""
def __init__( self ):
"""
Starting the dbus service.
"""
global bus
bus_name = dbus.service.BusName(DBUS_NAME, bus=bus)
dbus.service.Object.__init__(self, bus_name, DBUS_PATH)
@dbus.service.method(DBUS_NAME)
def do( self ):
"""
Function to execute using dbus service
"""
print "do function has been called using dbus service"
class myApp:
def __init__(self):
self.loop = gobject.MainLoop()
d = myAppdBus()
def start(self):
self.loop.run()
if __name__ == '__main__':
app = myApp()
app.start()
It is a simple script with 2 classes. The main class called myApp will call the dbus class (myAppdBus) and will start the applications main loop. The second class (myAppdBus) is the dbus class, it starts the service and register the methods that can be called using dbus.
The script can be executed like this (in my case the name of the script is dbus_script.py):
$ python dbus_script.py
After running the script it is possible to execute the do function like this:
$ dbus-send --reply-timeout=30000 --print-reply --dest=org.myApp /org/myApp org.myApp.do
This last command will show us the information related to the call we just executed. The output is something like:
flaper87@r4-p17:~$ dbus-send --reply-timeout=30000 --print-reply --dest=org.myApp /org/myApp org.myApp.do method return sender=:1.6 -> dest=:1.7 reply_serial=2
Ass you can the classes are really simple and show an easy way to communicate with our applications using dbus. There are a lot of options a tweaks that can be implemented so it's all in you hands now.
Good Luck.
P.S: Quick post, isn't it? :P
