Ignore:
Timestamp:
Nov 1, 2008, 7:18:50 PM (13 years ago)
Author:
Ralph Meijer <ralphm@…>
Branch:
default
Convert:
svn:b33ecbfc-034c-dc11-8662-000475d9059e/trunk@134
Message:

Bring subprotocols code in line with Twisted trunk.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wokkel/subprotocols.py

    r22 r45  
    2424
    2525class XMPPHandler(object):
     26    """
     27    XMPP protocol handler.
     28
     29    Classes derived from this class implement (part of) one or more XMPP
     30    extension protocols, and are referred to as a subprotocol implementation.
     31    """
     32
    2633    implements(IXMPPHandler)
     34
     35    def __init__(self):
     36        self.parent = None
     37        self.xmlstream = None
     38
    2739
    2840    def setHandlerParent(self, parent):
     
    3042        self.parent.addHandler(self)
    3143
     44
    3245    def disownHandlerParent(self, parent):
    3346        self.parent.removeHandler(self)
    3447        self.parent = None
    3548
     49
    3650    def makeConnection(self, xs):
    3751        self.xmlstream = xs
    3852        self.connectionMade()
    3953
     54
    4055    def connectionMade(self):
    41         pass
     56        """
     57        Called after a connection has been established.
     58
     59        Can be overridden to perform work before stream initialization.
     60        """
     61
    4262
    4363    def connectionInitialized(self):
    44         pass
     64        """
     65        The XML stream has been initialized.
     66
     67        Can be overridden to perform work after stream initialization, e.g. to
     68        set up observers and start exchanging XML stanzas.
     69        """
     70
    4571
    4672    def connectionLost(self, reason):
     73        """
     74        The XML stream has been closed.
     75
     76        This method can be extended to inspect the C{reason} argument and
     77        act on it.
     78        """
    4779        self.xmlstream = None
     80
    4881
    4982    def send(self, obj):
     
    6598
    6699
     100
    67101class XMPPHandlerCollection(object):
    68102    """
     
    72106    L{XMPPHandler} itself, so this is not recursive.
    73107
    74     @ivar xmlstream: Currently managed XML stream.
    75     @type xmlstream: L{XmlStream}
    76108    @ivar handlers: List of protocol handlers.
    77109    @type handlers: L{list} of objects providing
     
    83115    def __init__(self):
    84116        self.handlers = []
    85         self.xmlstream = None
    86         self._initialized = False
     117
    87118
    88119    def __iter__(self):
     
    92123        return iter(self.handlers)
    93124
     125
    94126    def addHandler(self, handler):
    95127        """
     
    97129
    98130        Protocol handlers are expected to provide L{IXMPPHandler}.
    99 
    100         When an XML stream has already been established, the handler's
    101         C{connectionInitialized} will be called to get it up to speed.
    102         """
    103 
     131        """
    104132        self.handlers.append(handler)
    105133
    106         # get protocol handler up to speed when a connection has already
    107         # been established
    108         if self.xmlstream and self._initialized:
    109             handler.makeConnection(self.xmlstream)
    110             handler.connectionInitialized()
    111134
    112135    def removeHandler(self, handler):
     
    114137        Remove protocol handler.
    115138        """
    116 
    117139        self.handlers.remove(handler)
     140
     141
    118142
    119143class StreamManager(XMPPHandlerCollection):
     
    126150    using L{addHandler}.
    127151
     152    @ivar xmlstream: currently managed XML stream
     153    @type xmlstream: L{XmlStream}
    128154    @ivar logTraffic: if true, log all traffic.
    129155    @type logTraffic: L{bool}
     156    @ivar _initialized: Whether the stream represented by L{xmlstream} has
     157                        been initialized. This is used when caching outgoing
     158                        stanzas.
     159    @type _initialized: C{bool}
    130160    @ivar _packetQueue: internal buffer of unsent data. See L{send} for details.
    131161    @type _packetQueue: L{list}
     
    135165
    136166    def __init__(self, factory):
    137         self.handlers = []
     167        XMPPHandlerCollection.__init__(self)
    138168        self.xmlstream = None
    139169        self._packetQueue = []
     
    147177        self.factory = factory
    148178
     179
     180    def addHandler(self, handler):
     181        """
     182        Add protocol handler.
     183
     184        When an XML stream has already been established, the handler's
     185        C{connectionInitialized} will be called to get it up to speed.
     186        """
     187        XMPPHandlerCollection.addHandler(self, handler)
     188
     189        # get protocol handler up to speed when a connection has already
     190        # been established
     191        if self.xmlstream and self._initialized:
     192            handler.makeConnection(self.xmlstream)
     193            handler.connectionInitialized()
     194
     195
    149196    def _connected(self, xs):
     197        """
     198        Called when the transport connection has been established.
     199
     200        Here we optionally set up traffic logging (depending on L{logTraffic})
     201        and call each handler's C{makeConnection} method with the L{XmlStream}
     202        instance.
     203        """
    150204        def logDataIn(buf):
    151205            log.msg("RECV: %r" % buf)
     
    163217            e.makeConnection(xs)
    164218
     219
    165220    def _authd(self, xs):
     221        """
     222        Called when the stream has been initialized.
     223
     224        Send out cached stanzas and call each handler's
     225        C{connectionInitialized} method.
     226        """
    166227        # Flush all pending packets
    167228        for p in self._packetQueue:
     
    175236            e.connectionInitialized()
    176237
     238
    177239    def initializationFailed(self, reason):
    178240        """
     
    188250        """
    189251
     252
    190253    def _disconnected(self, _):
     254        """
     255        Called when the stream has been closed.
     256
     257        From this point on, the manager doesn't interact with the
     258        L{XmlStream} anymore and notifies each handler that the connection
     259        was lost by calling its C{connectionLost} method.
     260        """
    191261        self.xmlstream = None
    192262        self._initialized = False
     
    195265        # the IService interface
    196266        for e in self:
    197             e.xmlstream = None
    198267            e.connectionLost(None)
     268
    199269
    200270    def send(self, obj):
     
    208278                    L{xmlstream.XmlStream.send} for details.
    209279        """
    210 
    211280        if self._initialized:
    212281            self.xmlstream.send(obj)
    213282        else:
    214283            self._packetQueue.append(obj)
     284
    215285
    216286
Note: See TracChangeset for help on using the changeset viewer.