summaryrefslogtreecommitdiff
path: root/pjsip-apps/src/python/samples/subscribe.py
diff options
context:
space:
mode:
authorBenny Prijono <bennylp@teluu.com>2008-07-18 23:00:56 +0000
committerBenny Prijono <bennylp@teluu.com>2008-07-18 23:00:56 +0000
commita331abeec9382f40293a5b3c7e4dc2163f6ad734 (patch)
treef130641ab7dfac5b00518f1eee8eabedad817242 /pjsip-apps/src/python/samples/subscribe.py
parent63ba5ff8c3c834b685b4e5f41833f77737008193 (diff)
Implemented ticket #192 for Python: Add callback to notify application about incoming SUBSCRIBE request, and add subscription state and termination reason in buddy info
git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@2156 74dad513-b988-da41-8d7b-12977e46ad98
Diffstat (limited to 'pjsip-apps/src/python/samples/subscribe.py')
-rw-r--r--pjsip-apps/src/python/samples/subscribe.py94
1 files changed, 94 insertions, 0 deletions
diff --git a/pjsip-apps/src/python/samples/subscribe.py b/pjsip-apps/src/python/samples/subscribe.py
new file mode 100644
index 00000000..e48aeee8
--- /dev/null
+++ b/pjsip-apps/src/python/samples/subscribe.py
@@ -0,0 +1,94 @@
+# $Id$
+#
+# Authorization of incoming subscribe request
+#
+# Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
+#
+import sys
+import pjsua as pj
+
+LOG_LEVEL = 3
+
+pending_pres = None
+
+def log_cb(level, str, len):
+ print str,
+
+class MyAccountCallback(pj.AccountCallback):
+ def __init__(self, account):
+ pj.AccountCallback.__init__(self, account)
+
+ def on_incoming_subscribe(self, buddy, from_uri, pres):
+ # Allow buddy to subscribe to our presence
+ global pending_pres
+
+ if buddy:
+ return (200, None)
+ print 'Incoming SUBSCRIBE request from', from_uri
+ print 'Press "A" to accept and add, "R" to reject the request'
+ pending_pres = pres
+ return (202, None)
+
+
+lib = pj.Lib()
+
+try:
+ # Init library with default config and some customized
+ # logging config.
+ lib.init(log_cfg = pj.LogConfig(level=LOG_LEVEL, callback=log_cb))
+
+ # Create UDP transport which listens to any available port
+ transport = lib.create_transport(pj.TransportType.UDP,
+ pj.TransportConfig(0))
+ print "\nListening on", transport.info().host,
+ print "port", transport.info().port, "\n"
+
+ # Start the library
+ lib.start()
+
+ # Create local account
+ acc = lib.create_account_for_transport(transport)
+ acc.set_callback(MyAccountCallback(acc))
+
+ my_sip_uri = "sip:" + transport.info().host + \
+ ":" + str(transport.info().port)
+
+ buddy = None
+
+ # Menu loop
+ while True:
+ print "My SIP URI is", my_sip_uri
+ print "Menu: t=toggle online status, q=quit"
+
+ input = sys.stdin.readline().rstrip("\r\n")
+
+ if input == "t":
+ acc.set_basic_status(not acc.info().online_status)
+
+ elif input == "A":
+ if pending_pres:
+ acc.pres_notify(pending_pres, pj.SubscriptionState.ACTIVE)
+ pending_pres = None
+ else:
+ print "No pending request"
+
+ elif input == "R":
+ if pending_pres:
+ acc.pres_notify(pending_pres, pj.SubscriptionState.TERMINATED,
+ "rejected")
+ pending_pres = None
+ else:
+ print "No pending request"
+
+ elif input == "q":
+ break
+
+ # Shutdown the library
+ lib.destroy()
+ lib = None
+
+except pj.Error, e:
+ print "Exception: " + str(e)
+ lib.destroy()
+ lib = None
+