summaryrefslogtreecommitdiff
path: root/examples/client/python/stream-tube-offerer.py
diff options
context:
space:
mode:
authorSimon McVittie <simon.mcvittie@collabora.co.uk>2012-04-10 14:35:46 +0100
committerSimon McVittie <simon.mcvittie@collabora.co.uk>2012-04-12 16:08:32 +0100
commit2d2991d46fa150f38ac2a5090c45d64b5346391d (patch)
tree7a502ad9a868296efbf4ac7ef8f6d53f61d738bd /examples/client/python/stream-tube-offerer.py
parente5f8a8211107a57d617865a1a74605430ad63ecb (diff)
Install remaining Python examples
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=48504 Reviewed-by: Jonny Lamb <jonny.lamb@collabora.co.uk>
Diffstat (limited to 'examples/client/python/stream-tube-offerer.py')
-rwxr-xr-xexamples/client/python/stream-tube-offerer.py104
1 files changed, 104 insertions, 0 deletions
diff --git a/examples/client/python/stream-tube-offerer.py b/examples/client/python/stream-tube-offerer.py
new file mode 100755
index 000000000..1f4309d51
--- /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()