[telepathy-python/master] Fix CreateChannel to always return a new channel.

Olivier Le Thanh Duong olivier at lethanh.be
Tue Jan 19 06:27:52 PST 2010


Fix CreateChannel to always return a new channel instead of having the
same behaviour as EnsureChannel and so allow to have multiple channel of
the same type created for a given TargetHandle. Provide a existing_channel
function in ChanelManager that ConnectionManager can subclass to define
more appropriatly if there is an existing channel that match the requested
properties.
---
 src/server/channelmanager.py |   38 +++++++++++++++++++++++++++-----------
 src/server/conn.py           |    2 +-
 2 files changed, 28 insertions(+), 12 deletions(-)

diff --git a/src/server/channelmanager.py b/src/server/channelmanager.py
index aacde30..2d47604 100644
--- a/src/server/channelmanager.py
+++ b/src/server/channelmanager.py
@@ -42,9 +42,10 @@ class ChannelManager(object):
 
     def remove_channel(self, channel):
         for channel_type in self._requestable_channel_classes:
-            for handle, chan in self._channels[channel_type].items():
-                if channel == chan:
-                    del self._channels[channel_type][handle]
+            for handle, channels in self._channels[channel_type].items():
+                for chan in channels:
+                    if channel == chan:
+                        del self._channels[channel_type][handle]
 
     def _get_type_requested_handle(self, props):
         type = props[CHANNEL_INTERFACE + '.ChannelType']
@@ -56,32 +57,47 @@ class ChannelManager(object):
 
         return (type, requested, handle)
 
-    def channel_exists(self, props):
+    def existing_channel(self, props):
+        """ Return a channel corresponding to theses properties if such one exists,
+        otherwhise return None.
+        Will return the last created channel, Connection Manager should subclass this function
+        to implement more appropriate behaviour. """
+
         type, _, handle = self._get_type_requested_handle(props)
 
         if type in self._channels:
             if handle in self._channels[type]:
-                return True
+                if len(self._channels[type][handle]) > 0:
+                    return self._channels[type][handle][-1]
 
-        return False
+        return None
 
-    def channel_for_props(self, props, signal=True, **args):
+    def channel_exists(self, props):
+        return self.existing_channel(props) != None
+
+    def create_channel_for_props(self, props, signal=True, **args):
         type, _, handle = self._get_type_requested_handle(props)
 
         if type not in self._requestable_channel_classes:
             raise NotImplemented('Unknown channel type "%s"' % type)
 
-        if self.channel_exists(props):
-            return self._channels[type][handle]
-
         channel = self._requestable_channel_classes[type](
             props, **args)
 
         self._conn.add_channels([channel], signal=signal)
-        self._channels[type][handle] = channel
+        if type in self._channels:
+            if handle in self._channels[type]:
+                self._channels[type].setdefault(handle, []).append(channel)
 
         return channel
 
+    def channel_for_props(self, props, signal=True, **args):
+        channel = self.existing_channel(props)
+        if channel:
+            return channel
+        else:
+            return self.create_channel_for_props(props, signal, **args)
+
     def _implement_channel_class(self, type, make_channel, fixed, available):
         self._requestable_channel_classes[type] = make_channel
         self._channels.setdefault(type, {})
diff --git a/src/server/conn.py b/src/server/conn.py
index f976223..3c017eb 100644
--- a/src/server/conn.py
+++ b/src/server/conn.py
@@ -509,7 +509,7 @@ class ConnectionInterfaceRequests(
         self._validate_handle(request)
         props = self._alter_properties(request)
 
-        channel = self._channel_manager.channel_for_props(props, signal=False)
+        channel = self._channel_manager.create_channel_for_props(props, signal=False)
 
         # Remove mutable properties
         todel = []
-- 
1.5.6.5




More information about the telepathy-commits mailing list