Unset release flag, and start NEWS for 0.82.5
[dbus-python-phuang.git] / examples / example-async-client.py
blob7a8214a7bb392e93d1f39328d0d773d48fbeeff5
1 #!/usr/bin/env python
3 usage = """Usage:
4 python example-service.py &
5 python example-async-client.py
6 python example-client.py --exit-service
7 """
9 # Copyright (C) 2004-2006 Red Hat Inc. <http://www.redhat.com/>
10 # Copyright (C) 2005-2007 Collabora Ltd. <http://www.collabora.co.uk/>
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:
20 # The above copyright notice and this permission notice shall be
21 # included in all copies or substantial portions of the Software.
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.
32 import sys
33 import traceback
35 import gobject
37 import dbus
38 import dbus.mainloop.glib
40 # Callbacks for asynchronous calls
42 def handle_hello_reply(r):
43 global hello_replied
44 hello_replied = True
46 print str(r)
48 if hello_replied and raise_replied:
49 loop.quit()
51 def handle_hello_error(e):
52 global failed
53 global hello_replied
54 hello_replied = True
55 failed = True
57 print "HelloWorld raised an exception! That's not meant to happen..."
58 print "\t", str(e)
60 if hello_replied and raise_replied:
61 loop.quit()
63 def handle_hello_error(e):
64 global failed
65 global hello_replied
66 hello_replied = True
67 failed = True
69 print "HelloWorld raised an exception! That's not meant to happen..."
70 print "\t", str(e)
72 if hello_replied and raise_replied:
73 loop.quit()
75 def handle_raise_reply():
76 global failed
77 global raise_replied
78 raise_replied = True
79 failed = True
81 print "RaiseException returned normally! That's not meant to happen..."
83 if hello_replied and raise_replied:
84 loop.quit()
86 def handle_raise_error(e):
87 global raise_replied
88 raise_replied = True
90 print "RaiseException raised an exception as expected:"
91 print "\t", str(e)
93 if hello_replied and raise_replied:
94 loop.quit()
96 def make_calls():
97 # To make an async call, use the reply_handler and error_handler kwargs
98 remote_object.HelloWorld("Hello from example-async-client.py!",
99 dbus_interface='com.example.SampleInterface',
100 reply_handler=handle_hello_reply,
101 error_handler=handle_hello_error)
103 # Interface objects also support async calls
104 iface = dbus.Interface(remote_object, 'com.example.SampleInterface')
106 iface.RaiseException(reply_handler=handle_raise_reply,
107 error_handler=handle_raise_error)
109 return False
111 if __name__ == '__main__':
112 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
114 bus = dbus.SessionBus()
115 try:
116 remote_object = bus.get_object("com.example.SampleService","/SomeObject")
117 except dbus.DBusException:
118 traceback.print_exc()
119 print usage
120 sys.exit(1)
122 # Make the method call after a short delay
123 gobject.timeout_add(1000, make_calls)
125 failed = False
126 hello_replied = False
127 raise_replied = False
129 loop = gobject.MainLoop()
130 loop.run()
131 if failed:
132 raise SystemExit("Example async client failed!")