-
-
Save bcho/4461173 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding: utf-8 | |
import sys | |
import gobject | |
import dbus | |
import dbus.mainloop.glib | |
from service import service_name, object_path | |
def send_signal(): | |
c.send_signal(dbus_interface=service_name) | |
gobject.timeout_add(2000, loop.quit) | |
if sys.argv[1:] == ['--exit']: | |
c.exit(dbus_interface=service_name) | |
def test_signal_handler(s): | |
print 'in handler %s ' % s | |
def catch_signal(s): | |
print 'catched signal %s' % s | |
if __name__ == '__main__': | |
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) | |
bus = dbus.SessionBus() | |
c = bus.get_object(service_name, object_path) | |
#: callback handler? | |
c.connect_to_signal('hello_signal', test_signal_handler, | |
dbus_interface=service_name) | |
#: subscribe to the signal | |
bus.add_signal_receiver(catch_signal, dbus_interface=service_name, | |
signal_name='hello_signal') | |
#: fire the signal | |
gobject.timeout_add(2000, send_signal) | |
loop = gobject.MainLoop() | |
loop.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding: utf-8 | |
import gobject | |
import dbus | |
import dbus.service | |
import dbus.mainloop.glib | |
service_name = 'com.example.TestService' | |
object_path = '/'.join([''] + service_name.split('.')) | |
class Test(dbus.service.Object): | |
def __init__(self, conn, object_path): | |
dbus.service.Object.__init__(self, conn, object_path) | |
@dbus.service.signal(service_name) | |
def hello_signal(self, message): | |
print message | |
@dbus.service.method(service_name) | |
def send_signal(self): | |
self.hello_signal('hello world x2') | |
return 'Signal sent' | |
@dbus.service.method(service_name, | |
in_signature='', out_signature='') | |
def exit(self): | |
loop.quit() | |
if __name__ == '__main__': | |
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) | |
session_bus = dbus.SessionBus() | |
name = dbus.service.BusName(service_name, session_bus) | |
o = Test(session_bus, object_path) | |
loop = gobject.MainLoop() | |
loop.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment