|
Packit |
130fc8 |
#!/usr/bin/env python
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
usage = """Usage:
|
|
Packit |
130fc8 |
python example-signal-emitter.py &
|
|
Packit |
130fc8 |
python example-signal-recipient.py
|
|
Packit |
130fc8 |
python example-signal-recipient.py --exit-service
|
|
Packit |
130fc8 |
"""
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
# Copyright (C) 2004-2006 Red Hat Inc. <http://www.redhat.com/>
|
|
Packit |
130fc8 |
# Copyright (C) 2005-2007 Collabora Ltd. <http://www.collabora.co.uk/>
|
|
Packit |
130fc8 |
#
|
|
Packit |
130fc8 |
# Permission is hereby granted, free of charge, to any person
|
|
Packit |
130fc8 |
# obtaining a copy of this software and associated documentation
|
|
Packit |
130fc8 |
# files (the "Software"), to deal in the Software without
|
|
Packit |
130fc8 |
# restriction, including without limitation the rights to use, copy,
|
|
Packit |
130fc8 |
# modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
Packit |
130fc8 |
# of the Software, and to permit persons to whom the Software is
|
|
Packit |
130fc8 |
# furnished to do so, subject to the following conditions:
|
|
Packit |
130fc8 |
#
|
|
Packit |
130fc8 |
# The above copyright notice and this permission notice shall be
|
|
Packit |
130fc8 |
# included in all copies or substantial portions of the Software.
|
|
Packit |
130fc8 |
#
|
|
Packit |
130fc8 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
Packit |
130fc8 |
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
Packit |
130fc8 |
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
Packit |
130fc8 |
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
Packit |
130fc8 |
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
Packit |
130fc8 |
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
Packit |
130fc8 |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
Packit |
130fc8 |
# DEALINGS IN THE SOFTWARE.
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
import sys
|
|
Packit |
130fc8 |
import traceback
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
from gi.repository import GLib
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
import dbus
|
|
Packit |
130fc8 |
import dbus.mainloop.glib
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
def handle_reply(msg):
|
|
Packit |
130fc8 |
print msg
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
def handle_error(e):
|
|
Packit |
130fc8 |
print str(e)
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
def emit_signal():
|
|
Packit |
130fc8 |
#call the emitHelloSignal method
|
|
Packit |
130fc8 |
object.emitHelloSignal(dbus_interface="com.example.TestService")
|
|
Packit |
130fc8 |
#reply_handler = handle_reply, error_handler = handle_error)
|
|
Packit |
130fc8 |
# exit after waiting a short time for the signal
|
|
Packit |
130fc8 |
GLib.timeout_add(2000, loop.quit)
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
if sys.argv[1:] == ['--exit-service']:
|
|
Packit |
130fc8 |
object.Exit(dbus_interface='com.example.TestService')
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
return False
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
def hello_signal_handler(hello_string):
|
|
Packit |
130fc8 |
print ("Received signal (by connecting using remote object) and it says: "
|
|
Packit |
130fc8 |
+ hello_string)
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
def catchall_signal_handler(*args, **kwargs):
|
|
Packit |
130fc8 |
print ("Caught signal (in catchall handler) "
|
|
Packit |
130fc8 |
+ kwargs['dbus_interface'] + "." + kwargs['member'])
|
|
Packit |
130fc8 |
for arg in args:
|
|
Packit |
130fc8 |
print " " + str(arg)
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
def catchall_hello_signals_handler(hello_string):
|
|
Packit |
130fc8 |
print "Received a hello signal and it says " + hello_string
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
def catchall_testservice_interface_handler(hello_string, dbus_message):
|
|
Packit |
130fc8 |
print "com.example.TestService interface says " + hello_string + " when it sent signal " + dbus_message.get_member()
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
if __name__ == '__main__':
|
|
Packit |
130fc8 |
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
bus = dbus.SessionBus()
|
|
Packit |
130fc8 |
try:
|
|
Packit |
130fc8 |
object = bus.get_object("com.example.TestService","/com/example/TestService/object")
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
object.connect_to_signal("HelloSignal", hello_signal_handler, dbus_interface="com.example.TestService", arg0="Hello")
|
|
Packit |
130fc8 |
except dbus.DBusException:
|
|
Packit |
130fc8 |
traceback.print_exc()
|
|
Packit |
130fc8 |
print usage
|
|
Packit |
130fc8 |
sys.exit(1)
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
#lets make a catchall
|
|
Packit |
130fc8 |
bus.add_signal_receiver(catchall_signal_handler, interface_keyword='dbus_interface', member_keyword='member')
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
bus.add_signal_receiver(catchall_hello_signals_handler, dbus_interface = "com.example.TestService", signal_name = "HelloSignal")
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
bus.add_signal_receiver(catchall_testservice_interface_handler, dbus_interface = "com.example.TestService", message_keyword='dbus_message')
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
# Tell the remote object to emit the signal after a short delay
|
|
Packit |
130fc8 |
GLib.timeout_add(2000, emit_signal)
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
loop = GLib.MainLoop()
|
|
Packit |
130fc8 |
loop.run()
|