Remove from EXTRA_DIST files we'd already be distributing
[dbus-python-phuang.git] / examples / example-async-client.py
blob37a3ce5045bc60d16bc20579cb97fa042218c15b
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 import sys
10 import traceback
12 import gobject
14 import dbus
15 import dbus.mainloop.glib
17 # Callbacks for asynchronous calls
19 def handle_hello_reply(r):
20 global hello_replied
21 hello_replied = True
23 print str(r)
25 if hello_replied and raise_replied:
26 loop.quit()
28 def handle_hello_error(e):
29 global failed
30 global hello_replied
31 hello_replied = True
32 failed = True
34 print "HelloWorld raised an exception! That's not meant to happen..."
35 print "\t", str(e)
37 if hello_replied and raise_replied:
38 loop.quit()
40 def handle_hello_error(e):
41 global failed
42 global hello_replied
43 hello_replied = True
44 failed = True
46 print "HelloWorld raised an exception! That's not meant to happen..."
47 print "\t", str(e)
49 if hello_replied and raise_replied:
50 loop.quit()
52 def handle_raise_reply():
53 global failed
54 global raise_replied
55 raise_replied = True
56 failed = True
58 print "RaiseException returned normally! That's not meant to happen..."
60 if hello_replied and raise_replied:
61 loop.quit()
63 def handle_raise_error(e):
64 global raise_replied
65 raise_replied = True
67 print "RaiseException raised an exception as expected:"
68 print "\t", str(e)
70 if hello_replied and raise_replied:
71 loop.quit()
73 def make_calls():
74 # To make an async call, use the reply_handler and error_handler kwargs
75 remote_object.HelloWorld("Hello from example-async-client.py!",
76 dbus_interface='com.example.SampleInterface',
77 reply_handler=handle_hello_reply,
78 error_handler=handle_hello_error)
80 # Interface objects also support async calls
81 iface = dbus.Interface(remote_object, 'com.example.SampleInterface')
83 iface.RaiseException(reply_handler=handle_raise_reply,
84 error_handler=handle_raise_error)
86 return False
88 if __name__ == '__main__':
89 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
91 bus = dbus.SessionBus()
92 try:
93 remote_object = bus.get_object("com.example.SampleService","/SomeObject")
94 except dbus.DBusException:
95 traceback.print_exc()
96 print usage
97 sys.exit(1)
99 # Make the method call after a short delay
100 gobject.timeout_add(1000, make_calls)
102 failed = False
103 hello_replied = False
104 raise_replied = False
106 loop = gobject.MainLoop()
107 loop.run()
108 if failed:
109 raise SystemExit("Example async client failed!")