[asterisk-scf-commits] asterisk-scf/examples.git branch "master" created.

Commits to the Asterisk SCF project code repositories asterisk-scf-commits at lists.digium.com
Fri Jun 24 11:09:10 CDT 2011


branch "master" has been created
        at  2cf857cc2de0a12b192c09ae73ee1aa9d63578a3 (commit)

- Log -----------------------------------------------------------------
commit 2cf857cc2de0a12b192c09ae73ee1aa9d63578a3
Author: Mark Michelson <mmichelson at digium.com>
Date:   Fri Jun 24 10:58:10 2011 -0500

    Add a script that listens to sessions and bridges and prints info when stuff happens.

diff --git a/bslistener.py b/bslistener.py
new file mode 100755
index 0000000..a5a4af6
--- /dev/null
+++ b/bslistener.py
@@ -0,0 +1,165 @@
+#!/usr/bin/env python
+
+#
+# Asterisk SCF -- An open-source communications framework.
+#
+# Copyright (C) 2011, Digium, Inc.
+#
+# See http://www.asterisk.org for more information about
+# the Asterisk SCF project. Please do not directly contact
+# any of the maintainers of this project for assistance;
+# the project provides a web site, mailing lists and IRC
+# channels for your use.
+#
+# This program is free software, distributed under the terms of
+# the GNU General Public License Version 2. See the LICENSE.txt file
+# at the top of the source tree.
+#
+
+import Ice, os, sys
+
+Ice.loadSlice("-I" + Ice.getSliceDir() + " -I" + os.environ["ASTSCF_HOME"] + " --all " + os.environ["ASTSCF_HOME"] + "/AsteriskSCF/SessionCommunications/SessionCommunicationsIf.ice")
+
+import AsteriskSCF.SessionCommunications.V1
+
+SListenerID = "SListener"
+
+class SListener(AsteriskSCF.SessionCommunications.V1.SessionListener):
+    def __init__(self, adapter):
+        self.adapter = adapter
+    def indicated(self, source, event, cookies, current = None):
+        if isinstance(event, AsteriskSCF.SessionCommunications.V1.ConnectedIndication):
+            print "Session %s received Connected indication" % (source.ice_getIdentity().name)
+        if isinstance(event, AsteriskSCF.SessionCommunications.V1.FlashedIndication):
+            print "Session %s received Flashed indication" % (source.ice_getIdentity().name)
+        if isinstance(event, AsteriskSCF.SessionCommunications.V1.HeldIndication):
+            print "Session %s received Held indication" % (source.ice_getIdentity().name)
+        if isinstance(event, AsteriskSCF.SessionCommunications.V1.ProgressingIndication):
+            print "Session %s received Progressing indication %d" % (source.ice_getIdentity().name, event.response.isdnCode)
+        if isinstance(event, AsteriskSCF.SessionCommunications.V1.RingingIndication):
+            print "Session %s received Ringing indication" % (source.ice_getIdentity().name)
+        if isinstance(event, AsteriskSCF.SessionCommunications.V1.StoppedIndication):
+            print "Session %s received Stopped indication: %d" % (source.ice_getIdentity().name, event.response.isdnCode)
+        if isinstance(event, AsteriskSCF.SessionCommunications.V1.UnheldIndication):
+            print "Session %s received Unheld indication" % (source.ice_getIdentity().name)
+        if isinstance(event, AsteriskSCF.SessionCommunications.V1.ConnectIndication):
+            print "Session %s received Connect indication" % (source.ice_getIdentity().name)
+        if isinstance(event, AsteriskSCF.SessionCommunications.V1.FlashIndication):
+            print "Session %s received Flash indication" % (source.ice_getIdentity().name)
+        if isinstance(event, AsteriskSCF.SessionCommunications.V1.HoldIndication):
+            print "Session %s received Hold indication" % (source.ice_getIdentity().name)
+        if isinstance(event, AsteriskSCF.SessionCommunications.V1.UnholdIndication):
+            print "Session %s received Unhold indication" % (source.ice_getIdentity().name)
+        if isinstance(event, AsteriskSCF.SessionCommunications.V1.RingIndication):
+            print "Session %s received Ring indication" % (source.ice_getIdentity().name)
+        if isinstance(event, AsteriskSCF.SessionCommunications.V1.ProgressIndication):
+            print "Session %s received Progress indication: %d" % (source.ice_getIdentity().name, event.response.isdnCode)
+
+BListenerID = "BListener"
+
+class BListener(AsteriskSCF.SessionCommunications.V1.BridgeListener):
+    def __init__(self, adapter):
+        self.adapter = adapter
+    def sessionsAdded(self, sessionBridge, sessions, current = None):
+        print "Sessions added to bridge %s" % (sessionBridge.ice_getIdentity().name)
+        for s in sessions:
+            s.addListener(AsteriskSCF.SessionCommunications.V1.SessionListenerPrx.uncheckedCast(self.adapter.createDirectProxy(self.adapter.getCommunicator().stringToIdentity(SListenerID))))
+            print "Added ourselves as a listener to session %s with endpoint %s" % (s.ice_getIdentity().name, s.getEndpoint().getId())
+    def sessionsRemoved(self, sessionBridge, sessions, current = None):
+        print "Sessions removed from bridge %s" % (sessionBridge.ice_getIdentity().name)
+    def stopped(self, sessionBridge, current = None):
+        print "Bridge %s stopped" % (sessionBridge.ice_getIdentity().name)
+    def stopping(self, sessionBridge, current = None):
+        print "Bridge %s stopping" % (sessionBridge.ice_getIdentity().name)
+
+BMListenerID = "BMListener"
+
+class BMListener(AsteriskSCF.SessionCommunications.V1.BridgeManagerListener):
+    def __init__(self, adapter):
+        self.adapter = adapter
+    def bridgeCreated(self, manager, newBridge, current = None):
+        print "Detected creation of new bridge %s" % (newBridge.ice_getIdentity().name)
+    def stopped(self, manager, current = None):
+        print "Bridge Manager has stopped"
+    def stopping(self, manager, current = None):
+        print "Bridge Manager is stopping"
+        self.adapter.remove(self.communicator().stringToIdentity(BMListenerId))
+
+class ListenerApp(Ice.Application):
+    def run(self, args):
+
+        # First thing we're going to do is create an object adapter.
+        adapter = self.communicator().createObjectAdapterWithEndpoints("ExampleListener", "default")
+
+        # Now we add instances of our listeners to the object adapter. This will allow
+        # us to create proxies to the objects when we want to add ourselves as a listener
+        # to sessions, bridges, etc.
+        adapter.add(BMListener(adapter), self.communicator().stringToIdentity(BMListenerID))
+        adapter.add(BListener(adapter), self.communicator().stringToIdentity(BListenerID))
+        adapter.add(SListener(adapter), self.communicator().stringToIdentity(SListenerID))
+
+        adapter.activate()
+
+        # Now we need to find the bridge manager. We use the service locator for this.
+        # First, we get a proxy to the service locator.
+        serviceLocator = AsteriskSCF.Core.Discovery.V1.ServiceLocatorPrx.checkedCast(self.communicator().stringToProxy("LocatorService:tcp -p 4411"))
+        
+        if not serviceLocator:
+            print "Couldn't get the service locator"
+            return -1
+
+        # Next, we need to  populate the parameters for the service locator to be able
+        # to find the bridge manager.
+        serviceLocatorParams = AsteriskSCF.Core.Discovery.V1.ServiceLocatorParams()
+        serviceLocatorParams.category = AsteriskSCF.SessionCommunications.V1.BridgeServiceDiscoveryCategory
+
+        # Now we locate the bridge manager
+        try:
+            service = serviceLocator.locate(serviceLocatorParams)
+        except Ice.Exception as ex:
+            print "Exception while attempting to locate BridgeManager: " + ex.what()
+        self.bridgeManager = AsteriskSCF.SessionCommunications.V1.BridgeManagerPrx.checkedCast(service)
+
+        # We add ourselves as a listener to the bridge manager.
+        self.BMListenerPrx = AsteriskSCF.SessionCommunications.V1.BridgeManagerListenerPrx.uncheckedCast(adapter.createDirectProxy(self.communicator().stringToIdentity(BMListenerID)))
+        try:
+            self.bridgeManager.addListener(self.BMListenerPrx)
+        except Ice.Exception as ex:
+            print "Exception while attempting to add ourselves as a BridgeManagerListener: " + ex.what()
+
+        # We also add ourselves as a default bridge listener. This means that we will automatically
+        # have ourselves added as a listener to any bridge that gets created.
+        self.BListenerPrx = AsteriskSCF.SessionCommunications.V1.BridgeListenerPrx.uncheckedCast(adapter.createDirectProxy(self.communicator().stringToIdentity(BListenerID)))
+        try:
+            self.bridgeManager.addDefaultBridgeListener(self.BListenerPrx)
+        except Ice.Exception as ex:
+            print "Exception while attempting to add ourselves as a DefaultBridgeListener: " + ex.what()
+
+        # We want to remove ourselves as listeners when we terminate, so we need to hook into the
+        # termination signal. Ice.Application provides a nice way to do this.
+        self.callbackOnInterrupt()
+        # And now we wait...
+        self.communicator().waitForShutdown()
+
+        if self.interrupted():
+            print self.appName() + ": terminating"
+
+        return 0
+
+    def interruptCallback(self, sig):
+        print "Removing ourself as a default bridge listener"
+        try:
+            self.bridgeManager.removeDefaultBridgeListener(self.BListenerPrx)
+        except Ice.Exception as ex:
+            print "Error trying to remove ourselves as a default bridge listener: " + ex.what()
+
+        print "Removing ourself as a bridge manager listener"
+        try:
+            self.bridgeManager.removeListener(self.BMListenerPrx)
+        except Ice.Exception as ex:
+            print "Error tryint to remove ourselves as a bridge manager listener: " + ex.what()
+
+        Ice.Application._destroyOnInterruptCallback(sig)
+
+app = ListenerApp()
+sys.exit(app.main(sys.argv))

-----------------------------------------------------------------------


-- 
asterisk-scf/examples.git



More information about the asterisk-scf-commits mailing list