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

Commits to the Asterisk SCF project code repositories asterisk-scf-commits at lists.digium.com
Fri Sep 30 09:12:22 CDT 2011


branch "master" has been updated
       via  12c24dc571f7063d7cafa87ce4850d51352ef626 (commit)
       via  bf8e37f7ec3d65c664913546b0eec8faaf58ed81 (commit)
      from  a4585180c529c953d773cf7eab4ffc356643f158 (commit)

Summary of changes:
 ExamplePartyIdHooks.py |  188 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 188 insertions(+), 0 deletions(-)
 create mode 100644 ExamplePartyIdHooks.py


- Log -----------------------------------------------------------------
commit 12c24dc571f7063d7cafa87ce4850d51352ef626
Merge: bf8e37f a458518
Author: Ken Hunt <ken.hunt at digium.com>
Date:   Fri Sep 30 09:12:54 2011 -0500

    Merge branch 'master' of git.asterisk.org:asterisk-scf/examples


commit bf8e37f7ec3d65c664913546b0eec8faaf58ed81
Author: Ken Hunt <ken.hunt at digium.com>
Date:   Fri Sep 30 09:12:11 2011 -0500

    Example party id hooks.

diff --git a/ExamplePartyIdHooks.py b/ExamplePartyIdHooks.py
new file mode 100644
index 0000000..6b84688
--- /dev/null
+++ b/ExamplePartyIdHooks.py
@@ -0,0 +1,188 @@
+#!/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.
+#
+
+#  Party Id sample hooks
+
+import sys, threading, Ice, os, getopt
+
+Ice.loadSlice("--underscore -I\"" + os.environ["ASTSCF_HOME"] + "\" -I\"" + Ice.getSliceDir() + "\" --all \"" + os.environ["ASTSCF_HOME"] + "/AsteriskSCF/Core/Discovery/ServiceLocatorIf.ice\"")
+Ice.loadSlice("--underscore -I\"" + os.environ["ASTSCF_HOME"] + "\" -I\"" + Ice.getSliceDir() + "\" --all \"" + os.environ["ASTSCF_HOME"] + "/AsteriskSCF/SessionCommunications/SessionCommunicationsExtensionPointsIf.ice\"")
+Ice.loadSlice("--underscore -I\"" + os.environ["ASTSCF_HOME"] + "\" -I\"" + Ice.getSliceDir() + "\" --all \"" + os.environ["ASTSCF_HOME"] + "/AsteriskSCF/SessionCommunications/PartyIdentificationIf.ice\"")
+
+
+from AsteriskSCF.Core.Discovery.V1 import *
+from AsteriskSCF.SessionCommunications.ExtensionPoints.V1 import *
+from AsteriskSCF.SessionCommunications.PartyIdentification.V1 import *
+from AsteriskSCF.System.Hook.V1 import *
+
+# 
+# This hook is applied whenever a SessionController sends a ConnectedLine that is received by 
+# the bridge's corresponding SessionController. The bridge caches the 
+# ConnectedLine information for each session and this hook alters what gets cached
+# by the bridge for future use. So this is a handy place to do things like blocking 
+# the ConnectedLine data altogether. 
+#
+# The downside of this hook is that it is invoked when you don't know who the receiver
+# might eventually be. 
+#
+class ExampleReceivedConnectedLineHook(ReceivedConnectedLinePartyIdHook):
+    def modifyReceivedConnectedLine(self, sendingSession, receivedConnectedLine, replacementConnectedLine):
+        result = HookResult()
+        result.status = HookStatus.Succeeded
+
+        # Foo-ify the party names. 
+	ids = receivedConnectedLine.ids[:]  # Copy the ids
+	for id in ids:
+            id.partyName = 'foo.' + id.partyName
+        
+        replacementConnectedLine = AsteriskSCF.SessionCommunications.PartyIdentification.V1.ConnectedLine()
+	replacementConnectedLine = ids
+
+        return (result, replacementConnectedLine)
+
+#
+# This hook is applied when the bridge is about to send the ConnectedLine information to 
+# members of the bridge other than the original sender. This hook can customize the 
+# ConnectedLine information specifically to a given destination. 
+#
+class ExampleForwardingConnectedLineHook(ForwardingConnectedLinePartyIdHook):
+    def modifyForwardingConnectedLine(self, sendingSession, destinationSession, receivedConnectedLine, replacementConnectedLine):
+        result = HookResult()
+        result.status = HookStatus.Succeeded
+
+        # Bar-ify the party names. (We don't really make use of the destinationSession here, 
+	# but we could.) 
+	ids = receivedConnectedLine.ids[:]  # Copy the ids
+	for id in ids:
+            id.partyName = id.partyName + '.bar'
+        
+        replacementConnectedLine = AsteriskSCF.SessionCommunications.PartyIdentification.V1.ConnectedLine()
+	replacementConnectedLine = ids
+
+        return (result, replacementConnectedLine)
+
+#
+# This hook is applied when the bridge is about to forward the Redirection information to 
+# members of the bridge other than the original sender. This hook can customize the 
+# Redirected information specifically to a given destination. 
+#
+class ExampleForwardingRedirectionsHook(ForwardingRedirectionsPartyIdHook):
+    def modifyForwardingRedirection(self, sendingSession, destinationSession, receivedRedirection, replacementRedirection):
+        result = HookResult()
+        result.status = HookStatus.Succeeded
+
+        # Let's just provide a made-up redirect. 
+        
+        replacementRedirection = AsteriskSCF.SessionCommunications.PartyIdentification.V1.Redirection()
+        redirection = AsteriskSCF.SessionCommunications.PartyIdentification.V1.Redirection()
+
+        fromName = AsteriskSCF.SessionCommunications.PartyIdentification.V1.Name('Linda')
+	fromNumber = AsteriskSCF.SessionCommunications.PartyIdentification.V1.Number('800.GET.LOST')
+	fromId = AsteriskSCF.SessionCommunications.PartyIdentification.V1.Id(fromName, fromNumber)
+	redirection.fromId = fromId
+
+        toName = AsteriskSCF.SessionCommunications.PartyIdentification.V1.Name('Jenny')
+	toNumber = AsteriskSCF.SessionCommunications.PartyIdentification.V1.Number('867.5309')
+	toId = AsteriskSCF.SessionCommunications.PartyIdentification.V1.Id(toName, toNumber)
+	redirection.toId = toId 
+
+        # Adding a single redirection record here. But you could add more.
+	replacementRedirections.redirects[0] = redirection
+
+        return (result, replacementRedirections)
+
+class ExamplePartyIdHookApp(Ice.Application):
+    def usage(self):
+        """Print usage information for the example Pary Identification hooks"""
+
+        print "Usage: " + self.appName() + " [--locator=stringified service locator proxy]"
+        print "Hook into a Bridge component and alter the party id traffic."
+        print ""
+        print "Mandatory arguments to long options are mandatory for short options too."
+        print "-h, --help              display help information"
+        print "-l, --locator=PROXY     use specified proxy for service locator"
+
+    def run(self, args):
+        try:
+            opts, arguments = getopt.getopt(args[1:], "hl:", ["help", "locator="])
+        except getopt.GetoptError, err:
+            print str(err)
+            self.usage()
+            return 1
+
+        serviceLocator = None
+        extensionPoint = None
+        serviceLocatorString = "LocatorService:tcp -p 4411"
+
+        for o, a in opts:
+            if o in ("-h", "--help"):
+                self.usage()
+                return 1
+
+            elif o in ("-l", "--locator"):
+                serviceLocatorString = a
+
+        serviceLocator = ServiceLocatorPrx.checkedCast(self.communicator().stringToProxy(serviceLocatorString))
+
+        if serviceLocator == None:
+            print >> sys.stderr, "No service locator proxy specified. For usage details please run " + sys.argv[0] + " --help"
+            return -1
+
+        serviceLocatorParams = ServiceLocatorParams()
+        serviceLocatorParams.category = PartyIdentificationHookLocatorCategory
+	serviceLocatorParams.service = 'default'
+
+        try:
+            service = serviceLocator.locate(serviceLocatorParams)
+        except AsteriskSCF.Core.Discovery.V1.ServiceNotFound:
+            print >> sys.stderr, "Bridge component's Party Id extension point could not be found in service locator."
+            return -1
+
+        extensionPoint = PartyIdentificationExtensionPointPrx.checkedCast(service)
+
+        adapter = self.communicator().createObjectAdapterWithEndpoints("ExamplePartyIdHookApp", "default");
+        adapter.activate();
+
+        receiveConnectedLineHook = ExampleReceivedConnectedLineHook()
+        receiveConnectedLineHookPrx = ReceivedConnectedLinePartyIdHookPrx.uncheckedCast(adapter.addWithUUID(receiveConnectedLineHook))
+
+        forwardConnectedLineHook = ExampleForwardingConnectedLineHook()
+        forwardConnectedLineHookPrx = ForwardingConnectedLinePartyIdHookPrx.uncheckedCast(adapter.addWithUUID(forwardConnectedLineHook))
+
+	forwardRedirectionHook = ExampleForwardingRedirectionsHook()
+        forwardRedirectionHookPrx = ForwardingRedirectionsPartyIdHookPrx.uncheckedCast(adapter.addWithUUID(forwardRedirectionHook))
+
+        print "Hooking into bridge service extension point..."
+        extensionPoint.addReceivedConnectedLinePartyIdHook(receiveConnectedLineHookPrx)
+        extensionPoint.addForwardingConnectedLinePartyIdHook(forwardConnectedLineHookPrx)
+        extensionPoint.addForwardingRedirectionsPartyIdHook(forwardRedirectionHookPrx)
+        print "Hooks added."
+
+        print "Processing until shutdown" 
+        self.communicator().waitForShutdown()
+
+        print "Attempting to remove our hooks from the bridge service."
+        extensionPoint.removeReceivedConnectedLinePartyIdHook(receiveConnectedLineHookPrx)
+        extensionPoint.removeForwardingConnectedLinePartyIdHook(forwardConnectedLineHookPrx)
+        extensionPoint.removeForwardingRedirectionsPartyIdHook(forwardRedirectionHookPrx)
+
+        return 0
+
+app = ExamplePartyIdHookApp()
+sys.exit(app.main(sys.argv))
+

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


-- 
asterisk-scf/examples.git



More information about the asterisk-scf-commits mailing list