#!/usr/bin/env python

usage = """Usage:
python dbus-hang.py &

one console:
dbus-send --dest=com.example.SampleService --print-reply /SomeObject com.example.SampleInterface.HelloWorld string:moo

other console:
dbus-send --dest=com.example.SampleService --type=signal /SomeObject com.example.SampleInterface.Poke

Signals sent while the daemon is idle will arrive. But signals sent while a
HelloWorld() call is pending and the priviate main loop is running never
arrived, until Control-C is hit.
"""

import gobject

import dbus
import dbus.service
import dbus.mainloop.glib

class SomeObject(dbus.service.Object):

    def poke_handler(self):
        print 'poke_handler: quitting private main loop'
        self.my_loop.quit()

    @dbus.service.method("com.example.SampleInterface",
                         in_signature='s', out_signature='as')
    def HelloWorld(self, hello_message):
        print 'HelloWorld(%s) called; waiting for Poke signal to continue' % hello_message

        self.my_loop.run()

        print 'HelloWorld(): returning'
        return ["Hello", " from example-service.py", "with unique name",
                session_bus.get_unique_name()]

if __name__ == '__main__':
    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)

    session_bus = dbus.SessionBus()
    name = dbus.service.BusName("com.example.SampleService", session_bus)
    object = SomeObject(session_bus, '/SomeObject')
    object.my_loop = gobject.MainLoop()

    # connect Poke signal to poke_handler
    session_bus.add_signal_receiver(object.poke_handler, dbus_interface =
        "com.example.SampleInterface", signal_name = "Poke")

    mainloop = gobject.MainLoop()
    print usage
    mainloop.run()
