Changeset 45:125f9d902a20 for wokkel/subprotocols.py
- Timestamp:
- Nov 1, 2008, 7:18:50 PM (14 years ago)
- Branch:
- default
- Convert:
- svn:b33ecbfc-034c-dc11-8662-000475d9059e/trunk@134
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
wokkel/subprotocols.py
r22 r45 24 24 25 25 class 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 26 33 implements(IXMPPHandler) 34 35 def __init__(self): 36 self.parent = None 37 self.xmlstream = None 38 27 39 28 40 def setHandlerParent(self, parent): … … 30 42 self.parent.addHandler(self) 31 43 44 32 45 def disownHandlerParent(self, parent): 33 46 self.parent.removeHandler(self) 34 47 self.parent = None 35 48 49 36 50 def makeConnection(self, xs): 37 51 self.xmlstream = xs 38 52 self.connectionMade() 39 53 54 40 55 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 42 62 43 63 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 45 71 46 72 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 """ 47 79 self.xmlstream = None 80 48 81 49 82 def send(self, obj): … … 65 98 66 99 100 67 101 class XMPPHandlerCollection(object): 68 102 """ … … 72 106 L{XMPPHandler} itself, so this is not recursive. 73 107 74 @ivar xmlstream: Currently managed XML stream.75 @type xmlstream: L{XmlStream}76 108 @ivar handlers: List of protocol handlers. 77 109 @type handlers: L{list} of objects providing … … 83 115 def __init__(self): 84 116 self.handlers = [] 85 self.xmlstream = None 86 self._initialized = False 117 87 118 88 119 def __iter__(self): … … 92 123 return iter(self.handlers) 93 124 125 94 126 def addHandler(self, handler): 95 127 """ … … 97 129 98 130 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 """ 104 132 self.handlers.append(handler) 105 133 106 # get protocol handler up to speed when a connection has already107 # been established108 if self.xmlstream and self._initialized:109 handler.makeConnection(self.xmlstream)110 handler.connectionInitialized()111 134 112 135 def removeHandler(self, handler): … … 114 137 Remove protocol handler. 115 138 """ 116 117 139 self.handlers.remove(handler) 140 141 118 142 119 143 class StreamManager(XMPPHandlerCollection): … … 126 150 using L{addHandler}. 127 151 152 @ivar xmlstream: currently managed XML stream 153 @type xmlstream: L{XmlStream} 128 154 @ivar logTraffic: if true, log all traffic. 129 155 @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} 130 160 @ivar _packetQueue: internal buffer of unsent data. See L{send} for details. 131 161 @type _packetQueue: L{list} … … 135 165 136 166 def __init__(self, factory): 137 self.handlers = []167 XMPPHandlerCollection.__init__(self) 138 168 self.xmlstream = None 139 169 self._packetQueue = [] … … 147 177 self.factory = factory 148 178 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 149 196 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 """ 150 204 def logDataIn(buf): 151 205 log.msg("RECV: %r" % buf) … … 163 217 e.makeConnection(xs) 164 218 219 165 220 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 """ 166 227 # Flush all pending packets 167 228 for p in self._packetQueue: … … 175 236 e.connectionInitialized() 176 237 238 177 239 def initializationFailed(self, reason): 178 240 """ … … 188 250 """ 189 251 252 190 253 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 """ 191 261 self.xmlstream = None 192 262 self._initialized = False … … 195 265 # the IService interface 196 266 for e in self: 197 e.xmlstream = None198 267 e.connectionLost(None) 268 199 269 200 270 def send(self, obj): … … 208 278 L{xmlstream.XmlStream.send} for details. 209 279 """ 210 211 280 if self._initialized: 212 281 self.xmlstream.send(obj) 213 282 else: 214 283 self._packetQueue.append(obj) 284 215 285 216 286
Note: See TracChangeset
for help on using the changeset viewer.