From 5c1982a295acc36cadc189c2046ded8ea8ae177c Mon Sep 17 00:00:00 2001 From: Traun Leyden Date: Thu, 11 Sep 2008 21:12:43 +0000 Subject: [PATCH] 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 git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@9528 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- scripts/socket/freepy/fseventlistener.py | 29 ++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/scripts/socket/freepy/fseventlistener.py b/scripts/socket/freepy/fseventlistener.py index 80cd4e0410..8e1f118528 100644 --- a/scripts/socket/freepy/fseventlistener.py +++ b/scripts/socket/freepy/fseventlistener.py @@ -140,7 +140,7 @@ class FreeswitchEventListener(LineReceiver): 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 @@ class FreeswitchEventListenerFactory(ClientFactory): 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 @@ class FreeswitchEventListenerFactory(ClientFactory): 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 @@ class FreeswitchEventListenerFactory(ClientFactory): def conncb(self, protocol): self.protocol = protocol + self.protocol.__dict__["factory"] = self deferred2callback = self.connection_deferred self.connection_deferred = None deferred2callback.callback(self.protocol)