From 0b2d16cc31bec2f55070c8ed41f550819b131f34 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Tue, 10 Apr 2012 14:35:46 +0100 Subject: [PATCH 3/3] Install remaining Python examples --- examples/client/python/Makefile.am | 6 +- examples/client/python/stream-tube-accepter.py | 73 ++++++++++++++ examples/client/python/stream-tube-offerer.py | 104 ++++++++++++++++++++ examples/client/python/stream-tubes.py/accepter.py | 73 -------------- examples/client/python/stream-tubes.py/offerer.py | 104 -------------------- 5 files changed, 181 insertions(+), 179 deletions(-) create mode 100755 examples/client/python/stream-tube-accepter.py create mode 100755 examples/client/python/stream-tube-offerer.py delete mode 100755 examples/client/python/stream-tubes.py/accepter.py delete mode 100755 examples/client/python/stream-tubes.py/offerer.py diff --git a/examples/client/python/Makefile.am b/examples/client/python/Makefile.am index 0377190..939cb49 100644 --- a/examples/client/python/Makefile.am +++ b/examples/client/python/Makefile.am @@ -6,8 +6,8 @@ EXTRA_DIST = \ text-handler.py \ file-transfer.py \ ft-handler.py \ - stream-tubes.py/offerer.py \ - stream-tubes.py/accepter.py + stream-tube-offerer.py \ + stream-tube-accepter.py installable_scripts = \ telepathy-example-python-dialler \ @@ -17,6 +17,8 @@ installable_scripts = \ telepathy-example-python-ft-handler \ telepathy-example-python-inspect-cm \ telepathy-example-python-text-handler \ + telepathy-example-python-stream-tube-accepter \ + telepathy-example-python-stream-tube-offerer \ $(NULL) telepathy-example-python-%: %.py diff --git a/examples/client/python/stream-tube-accepter.py b/examples/client/python/stream-tube-accepter.py new file mode 100755 index 0000000..0720ae7 --- /dev/null +++ b/examples/client/python/stream-tube-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() diff --git a/examples/client/python/stream-tube-offerer.py b/examples/client/python/stream-tube-offerer.py new file mode 100755 index 0000000..1f4309d --- /dev/null +++ b/examples/client/python/stream-tube-offerer.py @@ -0,0 +1,104 @@ +#!/usr/bin/env python + +import sys +import os + +from gi.repository import GObject, Gio +from gi.repository import TelepathyGLib as Tp + +def usage(): + print "%s ACCOUNT CONTACT" % sys.argv[0] + print "ACCOUNT is a Telepathy account name, use 'mc-tool list' to list all your accounts" + print "CONTACT is a contact id such as badger@nyan.cat" + + sys.exit(1) + +def offer_channel_cb(tube, result, loop): + try: + tube.offer_finish(result) + print "tube offered" + + except GObject.GError, e: + print "Failed to offer tube: %s" % e + sys.exit(1) + +def tube_conn_closed(tube, error): + print "Tube connection has been closed", error.message + +def channel_close_cb(tube, result, loop): + try: + tube.close_finish(result) + print "tube channel closed" + + except GObject.GError, e: + print "Failed to close tube channel: %s" % e + sys.exit(1) + +def tube_incoming_cb(tube, tube_conn, loop): + tube_conn.connect('closed', tube_conn_closed) + + contact = tube_conn.get_contact(); + + print "Got IOStream from", 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() + + buf, len = in_stream.read_line_utf8(None) + print "Received:", buf + + print "Sending: Pong" + out_stream.write("Pong\n", None) + + tube.close_async(channel_close_cb, contact) + +def tube_invalidated_cb(tube, domain, code, message, loop): + print "tube has been invalidated:", message + loop.quit() + +def create_channel_cb(request, result, loop): + try: + (chan, context) = request.create_and_handle_channel_finish(result) + + chan.connect('incoming', tube_incoming_cb, loop) + chan.connect('invalidated', tube_invalidated_cb, loop) + + chan.offer_async({}, offer_channel_cb, loop) + + except GObject.GError, e: + print "Failed to create channel: %s" % e + sys.exit(1) + +if __name__ == '__main__': + Tp.debug_set_flags(os.getenv('EXAMPLE_DEBUG', '')) + + if len(sys.argv) != 3: + usage() + + _, account_id, contact_id = sys.argv + + account_manager = Tp.AccountManager.dup() + account = account_manager.ensure_account("%s%s" % + (Tp.ACCOUNT_OBJECT_PATH_BASE, account_id)) + + request_dict = { + Tp.PROP_CHANNEL_CHANNEL_TYPE: + Tp.IFACE_CHANNEL_TYPE_STREAM_TUBE, + Tp.PROP_CHANNEL_TARGET_HANDLE_TYPE: + int(Tp.HandleType.CONTACT), + Tp.PROP_CHANNEL_TARGET_ID: + contact_id, + + Tp.PROP_CHANNEL_TYPE_STREAM_TUBE_SERVICE: "ExampleService", + } + + request = Tp.AccountChannelRequest.new(account, request_dict, 0) + + loop = GObject.MainLoop() + request.create_and_handle_channel_async(None, create_channel_cb, loop) + + loop.run() diff --git a/examples/client/python/stream-tubes.py/accepter.py b/examples/client/python/stream-tubes.py/accepter.py deleted file mode 100755 index 0720ae7..0000000 --- a/examples/client/python/stream-tubes.py/accepter.py +++ /dev/null @@ -1,73 +0,0 @@ -#!/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() diff --git a/examples/client/python/stream-tubes.py/offerer.py b/examples/client/python/stream-tubes.py/offerer.py deleted file mode 100755 index 1f4309d..0000000 --- a/examples/client/python/stream-tubes.py/offerer.py +++ /dev/null @@ -1,104 +0,0 @@ -#!/usr/bin/env python - -import sys -import os - -from gi.repository import GObject, Gio -from gi.repository import TelepathyGLib as Tp - -def usage(): - print "%s ACCOUNT CONTACT" % sys.argv[0] - print "ACCOUNT is a Telepathy account name, use 'mc-tool list' to list all your accounts" - print "CONTACT is a contact id such as badger@nyan.cat" - - sys.exit(1) - -def offer_channel_cb(tube, result, loop): - try: - tube.offer_finish(result) - print "tube offered" - - except GObject.GError, e: - print "Failed to offer tube: %s" % e - sys.exit(1) - -def tube_conn_closed(tube, error): - print "Tube connection has been closed", error.message - -def channel_close_cb(tube, result, loop): - try: - tube.close_finish(result) - print "tube channel closed" - - except GObject.GError, e: - print "Failed to close tube channel: %s" % e - sys.exit(1) - -def tube_incoming_cb(tube, tube_conn, loop): - tube_conn.connect('closed', tube_conn_closed) - - contact = tube_conn.get_contact(); - - print "Got IOStream from", 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() - - buf, len = in_stream.read_line_utf8(None) - print "Received:", buf - - print "Sending: Pong" - out_stream.write("Pong\n", None) - - tube.close_async(channel_close_cb, contact) - -def tube_invalidated_cb(tube, domain, code, message, loop): - print "tube has been invalidated:", message - loop.quit() - -def create_channel_cb(request, result, loop): - try: - (chan, context) = request.create_and_handle_channel_finish(result) - - chan.connect('incoming', tube_incoming_cb, loop) - chan.connect('invalidated', tube_invalidated_cb, loop) - - chan.offer_async({}, offer_channel_cb, loop) - - except GObject.GError, e: - print "Failed to create channel: %s" % e - sys.exit(1) - -if __name__ == '__main__': - Tp.debug_set_flags(os.getenv('EXAMPLE_DEBUG', '')) - - if len(sys.argv) != 3: - usage() - - _, account_id, contact_id = sys.argv - - account_manager = Tp.AccountManager.dup() - account = account_manager.ensure_account("%s%s" % - (Tp.ACCOUNT_OBJECT_PATH_BASE, account_id)) - - request_dict = { - Tp.PROP_CHANNEL_CHANNEL_TYPE: - Tp.IFACE_CHANNEL_TYPE_STREAM_TUBE, - Tp.PROP_CHANNEL_TARGET_HANDLE_TYPE: - int(Tp.HandleType.CONTACT), - Tp.PROP_CHANNEL_TARGET_ID: - contact_id, - - Tp.PROP_CHANNEL_TYPE_STREAM_TUBE_SERVICE: "ExampleService", - } - - request = Tp.AccountChannelRequest.new(account, request_dict, 0) - - loop = GObject.MainLoop() - request.create_and_handle_channel_async(None, create_channel_cb, loop) - - loop.run() -- 1.7.9.5