diff options
author | Guillaume Desmottes <guillaume.desmottes@collabora.co.uk> | 2012-04-03 15:54:12 +0200 |
---|---|---|
committer | Guillaume Desmottes <guillaume.desmottes@collabora.co.uk> | 2012-04-04 09:03:17 +0200 |
commit | 08f4a05e33f8a3fa2fe0e326014dbaccc7c55be6 (patch) | |
tree | 60d28a0e379f30fc310172e9d8c18a5a48969bc3 | |
parent | 8c31202992b37f0e387f67ca0100df2e1643c889 (diff) | |
download | telepathy-glib-08f4a05e33f8a3fa2fe0e326014dbaccc7c55be6.tar.gz |
add stream-tubes.py/accepter.py
-rw-r--r-- | examples/client/python/Makefile.am | 3 | ||||
-rwxr-xr-x | examples/client/python/stream-tubes.py/accepter.py | 73 |
2 files changed, 75 insertions, 1 deletions
diff --git a/examples/client/python/Makefile.am b/examples/client/python/Makefile.am index 696a66634..1d359797b 100644 --- a/examples/client/python/Makefile.am +++ b/examples/client/python/Makefile.am @@ -5,4 +5,5 @@ EXTRA_DIST = \ text-handler.py \ file-transfer.py \ ft-handler.py \ - stream-tubes.py/offerer.py + stream-tubes.py/offerer.py \ + stream-tubes.py/accepter.py diff --git a/examples/client/python/stream-tubes.py/accepter.py b/examples/client/python/stream-tubes.py/accepter.py new file mode 100755 index 000000000..0720ae7be --- /dev/null +++ b/examples/client/python/stream-tubes.py/accepter.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python + +import os + +from gi.repository import GObject, Gio +from gi.repository import TelepathyGLib as Tp + +def tube_conn_closed(tube, error): + print "Tube connection has been closed", error.message + +def tube_accept_cb(tube, result, loop): + try: + tube_conn = tube.accept_finish(result) + except GObject.GError, e: + print "Failed to accept tube: %s" % e + sys.exit(1) + + tube_conn.connect('closed', tube_conn_closed) + + contact = tube_conn.get_contact(); + + print "Got IOStream to", contact.get_identifier() + + conn = tube_conn.get_socket_connection(); + + # g_input_stream_read() can't be used from Python so we use the more + # binding friendly GDataInputStream + in_stream = Gio.DataInputStream (base_stream=conn.get_input_stream()) + out_stream = conn.get_output_stream() + + print "Sending: Ping" + out_stream.write("Ping\n", None) + + buf, len = in_stream.read_line_utf8(None) + print "Received:", buf + +def tube_invalidated_cb(tube, domain, code, message, loop): + print "tube has been invalidated:", message + loop.quit() + +def handle_channels_cb(handler, account, connection, channels, requests, + user_action_time, context, loop): + for channel in channels: + if not isinstance(channel, Tp.StreamTubeChannel): + continue + + print "Accepting tube" + + channel.connect('invalidated', tube_invalidated_cb, loop) + + channel.accept_async(tube_accept_cb, loop) + + context.accept() + +if __name__ == '__main__': + Tp.debug_set_flags(os.getenv('EXAMPLE_DEBUG', '')) + + loop = GObject.MainLoop() + + account_manager = Tp.AccountManager.dup() + handler = Tp.SimpleHandler.new_with_am(account_manager, False, False, + 'ExampleServiceHandler', False, handle_channels_cb, loop) + + handler.add_handler_filter({ + Tp.PROP_CHANNEL_CHANNEL_TYPE: Tp.IFACE_CHANNEL_TYPE_STREAM_TUBE, + Tp.PROP_CHANNEL_TARGET_HANDLE_TYPE: int(Tp.HandleType.CONTACT), + Tp.PROP_CHANNEL_TYPE_STREAM_TUBE_SERVICE: "ExampleService", + }) + + handler.register() + + print "Waiting for tube offer" + loop.run() |