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

Commits to the Asterisk SCF project code repositories asterisk-scf-commits at lists.digium.com
Tue Jun 28 18:00:14 CDT 2011


branch "decorator" has been updated
       via  44084ae72a6535ca26c1ede98c8a26fb7647a309 (commit)
      from  78575f161a3277d2653ca4afbb096d3549e4f3ef (commit)

Summary of changes:
 Session_Decorator.py |  100 +++++++++++++++++---------------------------------
 1 files changed, 34 insertions(+), 66 deletions(-)


- Log -----------------------------------------------------------------
commit 44084ae72a6535ca26c1ede98c8a26fb7647a309
Author: Mark Michelson <mmichelson at digium.com>
Date:   Tue Jun 28 17:59:46 2011 -0500

    Changes to start using session decorator extension point.

diff --git a/Session_Decorator.py b/Session_Decorator.py
index d3b9b46..8174253 100755
--- a/Session_Decorator.py
+++ b/Session_Decorator.py
@@ -19,6 +19,7 @@
 import Ice, os, sys, getopt
 
 Ice.loadSlice("-I" + Ice.getSliceDir() + " -I" + os.environ["ASTSCF_HOME"] + " --all " + os.environ["ASTSCF_HOME"] + "/AsteriskSCF/SessionCommunications/SessionCommunicationsIf.ice")
+Ice.loadSlice("-I" + Ice.getSliceDir() + " -I" + os.environ["ASTSCF_HOME"] + " --all " + os.environ["ASTSCF_HOME"] + "/AsteriskSCF/SessionCommunications/SessionCommunicationsExtensionPointsIf.ice")
 
 import AsteriskSCF.SessionCommunications.V1
 
@@ -125,37 +126,13 @@ class SessionDecorator(AsteriskSCF.SessionCommunications.V1.Session):
         print "removeBridge"
         self.session.removeBridge(listener)
 
-class BridgeListener(AsteriskSCF.SessionCommunications.V1.BridgeListener):
+class SessionDecoratorHook(AsteriskSCF.SessionCommunications.ExtensionPoints.V1.SessionDecoratorHook):
     def __init__(self, adapter):
         self.adapter = adapter
-    def sessionsAdded(self, sessionBridge, sessions, current = None):
-        # When we see a session get added to the bridge, we want to replace that
-        # session with our decorator. The tricky part is that
-        # when we add the decorator to the bridge, we'll get notified of it. So we
-        # need to be sure that we don't try to do something silly like replace ourself
-        # with ourself.
-        print "Sessions added to bridge %s" % (sessionBridge.ice_getIdentity().name)
-        for s in sessions:
-            cookies = s.getCookies([SessionDecoratorCookie()])
-            if len(cookies) is 0:
-                # This is a session we did not add ourselves. We need to create our own session, add it
-                # to the bridge, and remove this session from the bridge.
-                decorator = SessionDecorator(s, self.adapter)
-                decorator.setCookies([SessionDecoratorCookie()])
-                sessionObjPrx = self.adapter.addWithUUID(decorator)
-                sessionPrx = AsteriskSCF.SessionCommunications.V1.SessionPrx.uncheckedCast(sessionObjPrx)
-                decorator.setProxy(sessionObjPrx.ice_getIdentity())
-                try:
-                    sessionBridge.replaceSession(s, [sessionPrx])
-                except Ice.Exception as ex:
-                    print "Exception while trying to do stuff with things: " + ex.what()
-    def sessionsRemoved(self, sessionBridge, sessions, current = None):
-        for s in sessions:
-            print "Session %s removed from bridge %s" % (s.ice_getIdentity().name, 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)
+    def decorateSession(self, realSession, current = None):
+        # XXX When is it appropriate to remove the decorator from the adapter?
+        decoratorObjPrx = self.adapter.addWithUUID(SessionDecorator(realSession))
+        return AsteriskSCF.SessionCommunications.V1.SessionPrx.uncheckedCast(decoratorObjPrx)
 
 class DecoratorApp(Ice.Application):
     def usage(self):
@@ -163,14 +140,18 @@ class DecoratorApp(Ice.Application):
         print "Listen to all sessions and bridges"
         print ""
         print "Options:"
-        print "-h, --help              Display help information"
-        print "-l, --locator=PROXY     Use specified proxy for Service locator"
-        print "                        Default: 'LocatorService:tcp -p 4411"
+        print "-h, --help                      Display help information"
+        print ""
+        print "-l, --locator=PROXY             Use specified proxy for Service Locator"
+        print "                                Default: 'LocatorService:tcp -p 4411'"
+        print ""
+        print "-n, --name=NAME                 Name for this session decorator. Used for location."
+        print "                                Default: 'default'"
         
     def run(self, args):
         
         try:
-            opts, arguments = getopt.getopt(args[1:], "hl:", ["help", "locator="])
+            opts, arguments = getopt.getopt(args[1:], "hl:n:", ["help", "locator=", "name="])
         except getopt.GetoptError, err:
             print str(err)
             self.usage()
@@ -179,6 +160,7 @@ class DecoratorApp(Ice.Application):
         # The default value for the service locator is to assume it's running
         # on the same machine on port 4411
         serviceLocatorString = "LocatorService:tcp -p 4411"
+        decoratorName = "default"
 
         for o, a in opts:
             if o in ("-h", "--help"):
@@ -186,53 +168,39 @@ class DecoratorApp(Ice.Application):
                 return -1
             if o in ("-l", "--locator"):
                 serviceLocatorString = a
+            if o in ("-n", "--name"):
+                decoratorName = a
 
         print "Starting " + self.appName()
 
         # First thing we're going to do is create an object adapter.
         adapter = self.communicator().createObjectAdapterWithEndpoints("ExampleDecorator", "default")
 
-        bridgeListenerObjPrx = adapter.addWithUUID(BridgeListener(adapter))
-        print "Added BridgeListener to our object adapter"
+        hookObjPrx = adapter.addWithUUID(SessionDecoratorHook(adapter))
+        self.hookPrx = AsteriskSCF.SessionCommunications.ExtensionPoints.V1.SessionDecoratorHookPrx.uncheckedCast(hookObjPrx)
+        print "Added decorator hook to our object adapter"
 
         adapter.activate()
         print "Activated our object adapter"
 
-        # 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(serviceLocatorString))
-        
-        if not serviceLocator:
+        locatorObj = self.communicator().stringToProxy(serviceLocatorString)
+        locator = AsteriskSCF.Core.Discovery.V1.ServiceLocatorManagementPrx.checkedCast(locatorObj)
+
+        if not locator:
             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
+        # XXX Once changes have been merged for Service discovery to use names without registering a custom
+        # comparator, we'll actually use the passed-in decoratorName here.
+        params = AsteriskSCF.Core.Discovery.V1.ServiceLocatorParams(AsteriskSCF.SessionCommunications.ExtensionPoints.V1.SessionDecoratorLocatorCategory)
+        extensionPointObj = locator.locate(params)
+        self.extensionPoint = AsteriskSCF.SessionCommunications.ExtensionPoints.V1.SessionDecoratorExtensionPointPrx.checkedCast(extensionPointObj)
 
-        # 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)
-
-        if not self.bridgeManager:
-            print "Couldn't get the bridge manager"
+        if not self.extensionPoint:
+            print "Couldn't get the session decorator extension point interface"
             return -1
 
-        print "Acquired proxy to the BridgeManager"
-
-        # We add ourself as a default bridge listener. This means that we will automatically
-        # have ourself added as a listener to any bridge that gets created.
-        self.bridgeListenerPrx = AsteriskSCF.SessionCommunications.V1.BridgeListenerPrx.uncheckedCast(bridgeListenerObjPrx)
-        try:
-            self.bridgeManager.addDefaultBridgeListener(self.bridgeListenerPrx)
-        except Ice.Exception as ex:
-            print "Exception while attempting to add ourself as a DefaultBridgeListener: " + ex.what()
-
-        print "Added ourself as a default BridgeListener"
+        self.extensionPoint.addDecorator(self.hookPrx)
 
         # We want to remove ourself as listeners when we terminate, so we need to hook into the
         # termination signal. Ice.Application provides a nice way to do this.
@@ -252,9 +220,9 @@ class DecoratorApp(Ice.Application):
         # as a default bridge listener and a bridge manager listener.
         print "Removing ourself as a default bridge listener"
         try:
-            self.bridgeManager.removeDefaultBridgeListener(self.bridgeListenerPrx)
+            self.extensionPoint.removeDecorator(self.hookPrx)
         except Ice.Exception as ex:
-            print "Error trying to remove ourself as a default bridge listener: " + ex.what()
+            print "Error trying to remove decorator: " + ex.what()
 
         Ice.Application._destroyOnInterruptCallback(sig)
 

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


-- 
asterisk-scf/examples.git



More information about the asterisk-scf-commits mailing list