Initial import package dbus-python: D-Bus Python Bindings
[profile/ivi/dbus-python.git] / examples / example-signal-emitter.py
1 #!/usr/bin/env python
2
3 usage = """Usage:
4 python example-signal-emitter.py &
5 python example-signal-recipient.py
6 python example-signal-recipient.py --exit-service
7 """
8
9 # Copyright (C) 2004-2006 Red Hat Inc. <http://www.redhat.com/>
10 # Copyright (C) 2005-2007 Collabora Ltd. <http://www.collabora.co.uk/>
11 #
12 # Permission is hereby granted, free of charge, to any person
13 # obtaining a copy of this software and associated documentation
14 # files (the "Software"), to deal in the Software without
15 # restriction, including without limitation the rights to use, copy,
16 # modify, merge, publish, distribute, sublicense, and/or sell copies
17 # of the Software, and to permit persons to whom the Software is
18 # furnished to do so, subject to the following conditions:
19 #
20 # The above copyright notice and this permission notice shall be
21 # included in all copies or substantial portions of the Software.
22 #
23 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
27 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
28 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30 # DEALINGS IN THE SOFTWARE.
31
32 import gobject
33
34 import dbus
35 import dbus.service
36 import dbus.mainloop.glib
37
38 class TestObject(dbus.service.Object):
39     def __init__(self, conn, object_path='/com/example/TestService/object'):
40         dbus.service.Object.__init__(self, conn, object_path)
41
42     @dbus.service.signal('com.example.TestService')
43     def HelloSignal(self, message):
44         # The signal is emitted when this method exits
45         # You can have code here if you wish
46         pass
47
48     @dbus.service.method('com.example.TestService')
49     def emitHelloSignal(self):
50         #you emit signals by calling the signal's skeleton method
51         self.HelloSignal('Hello')
52         return 'Signal emitted'
53
54     @dbus.service.method("com.example.TestService",
55                          in_signature='', out_signature='')
56     def Exit(self):
57         loop.quit()
58
59 if __name__ == '__main__':
60     dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
61
62     session_bus = dbus.SessionBus()
63     name = dbus.service.BusName('com.example.TestService', session_bus)
64     object = TestObject(session_bus)
65
66     loop = gobject.MainLoop()
67     print "Running example signal emitter service."
68     print usage
69     loop.run()