[Freeswitch-svn] [commit] r9528 - freeswitch/trunk/scripts/socket/freepy

Freeswitch SVN greenlizard at freeswitch.org
Thu Sep 11 17:12:44 EDT 2008


Author: greenlizard
Date: Thu Sep 11 17:12:43 2008
New Revision: 9528

Modified:
   freeswitch/trunk/scripts/socket/freepy/fseventlistener.py

Log:
set field so the protocol object can easily get factor instance.  add ability to add observers for specific events and dispatch events to observers.  still more work needed there, since there is no relation between observers and what events are subscribed from freeswitch

Modified: freeswitch/trunk/scripts/socket/freepy/fseventlistener.py
==============================================================================
--- freeswitch/trunk/scripts/socket/freepy/fseventlistener.py	(original)
+++ freeswitch/trunk/scripts/socket/freepy/fseventlistener.py	Thu Sep 11 17:12:43 2008
@@ -140,7 +140,7 @@
 
     def eventReceived(self, event_xml_str):
         """
-        should be overridden by subclasses
+        should be overridden by subclasses.
         """
         raise Exception("This is an abstract class, should be overridden "
                         "in a subclass")
@@ -153,6 +153,9 @@
                             should be a subclass of a FreeswitchEventListener
         """
 
+        # dictionary of observers.  key: event name, value: list of observers
+        self.event2observer = {}
+        
         self.protoclass=protoclass
         
         if host:
@@ -166,12 +169,33 @@
         self.connection_deferred = None
         self.num_attempts = 0
 
+    def addobserver(self, event_name, observer):
+        """
+        @param event_name, eg "CHANNEL_ANSWER"
+        @param observer (instance of object that has an eventReceived() method
+        """
+        observers = self.event2observer.get(event_name, [])
+        observers.append(observer)
+        self.event2observer[event_name] = observers
+
+    def dispatch2observers(self, event_name, event_xml_str, event_dom):
+        """
+        called back by the underlying protocol upon receiving an
+        event from freeswitch.  Currently subclasses must explicitly
+        call this method from their eventReceived method for observers
+        to get the message.  TODO: move this call to FreeswitchEventListener
+        and use observer pattern instead of any subclassing.
+        """
+        observers = self.event2observer.get(event_name, [])
+        for observer in observers:
+            observer.eventReceived(event_name, event_xml_str, event_dom)
+        
     def reset(self):
         self.protocol = None
         self.connection_deferred = None
         
     def connect(self):
-            
+
         if self.protocol:
             # if we have a protocol object, we are connected (since we always
             # null it upon any disconnection)
@@ -191,6 +215,7 @@
 
     def conncb(self, protocol):
         self.protocol = protocol
+        self.protocol.__dict__["factory"] = self
         deferred2callback = self.connection_deferred
         self.connection_deferred = None
         deferred2callback.callback(self.protocol)



More information about the Freeswitch-svn mailing list