Ignore:
Timestamp:
Oct 18, 2008, 12:18:02 PM (14 years ago)
Author:
Ralph Meijer <ralphm@…>
Branch:
default
Convert:
svn:b33ecbfc-034c-dc11-8662-000475d9059e/trunk@112
Message:

Track changes to Twisted branch for the XMPP router.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wokkel/component.py

    r39 r40  
    134134    """
    135135    Authenticator for accepting components.
    136     """
     136
     137    @ivar secret: The shared used to authorized incoming component connections.
     138    @type secret: C{str}.
     139    """
     140
    137141    namespace = NS_COMPONENT_ACCEPT
    138142
     
    143147
    144148    def associateWithStream(self, xs):
     149        """
     150        Associate the authenticator with a stream.
     151
     152        This sets the stream's version to 0.0, because the XEP-0114 component
     153        protocol was not designed for XMPP 1.0.
     154        """
    145155        xs.version = (0, 0)
    146156        xmlstream.ListenAuthenticator.associateWithStream(self, xs)
     
    148158
    149159    def streamStarted(self, rootElement):
     160        """
     161        Called by the stream when it has started.
     162
     163        This examines the default namespace of the incoming stream and whether
     164        there is a requested hostname for the component. Then it generates a
     165        stream identifier, sends a response header and adds an observer for
     166        the first incoming element, triggering L{onElement}.
     167        """
     168
    150169        xmlstream.ListenAuthenticator.streamStarted(self, rootElement)
     170
     171        # Compatibility fix
     172        if not self.xmlstream.sid:
     173            from twisted.python import randbytes
     174            self.xmlstream.sid = randbytes.secureRandom(8).encode('hex')
    151175
    152176        if rootElement.defaultUri != self.namespace:
     
    156180
    157181        # self.xmlstream.thisEntity is set to the address the component
    158         # wants to assume. This should probably be checked.
     182        # wants to assume.
    159183        if not self.xmlstream.thisEntity:
    160184            exc = error.StreamError('improper-addressing')
     
    162186            return
    163187
    164         self.xmlstream.sid = 'random' # FIXME
    165 
    166188        self.xmlstream.sendHeader()
    167189        self.xmlstream.addOnetimeObserver('/*', self.onElement)
     
    169191
    170192    def onElement(self, element):
     193        """
     194        Called on incoming XML Stanzas.
     195
     196        The very first element received should be a request for handshake.
     197        Otherwise, the stream is dropped with a 'not-authorized' error. If a
     198        handshake request was received, the hash is extracted and passed to
     199        L{onHandshake}.
     200        """
    171201        if (element.uri, element.name) == (self.namespace, 'handshake'):
    172202            self.onHandshake(unicode(element))
    173203        else:
    174             exc = error.streamError('not-authorized')
     204            exc = error.StreamError('not-authorized')
    175205            self.xmlstream.sendStreamError(exc)
    176206
    177207
    178208    def onHandshake(self, handshake):
     209        """
     210        Called upon receiving the handshake request.
     211
     212        This checks that the given hash in C{handshake} is equal to a
     213        calculated hash, responding with a handshake reply or a stream error.
     214        If the handshake was ok, the stream is authorized, and  XML Stanzas may
     215        be exchanged.
     216        """
    179217        calculatedHash = xmlstream.hashPassword(self.xmlstream.sid, self.secret)
    180218        if handshake != calculatedHash:
     
    188226
    189227
    190 class RouterService(service.Service):
    191     """
    192     XMPP Server's Router Service.
    193 
    194     This service connects the different components of the XMPP service and
    195     routes messages between them based on the given routing table.
     228class Router(object):
     229    """
     230    XMPP Server's Router.
     231
     232    A router connects the different components of the XMPP service and routes
     233    messages between them based on the given routing table.
    196234
    197235    Connected components are trusted to have correct addressing in the
     
    251289        @type stanza: L{domish.Element}.
    252290        """
    253         if not list(stanza.elements()):
    254             return
    255 
    256291        destination = JID(stanza['to'])
    257292
     
    265300
    266301
    267 class ComponentServer(service.Service):
    268     """
    269     XMPP Component Server service.
    270 
    271     This service accepts XMPP external component connections and makes
     302class XMPPComponentServerFactory(xmlstream.XmlStreamServerFactory):
     303    """
     304    XMPP Component Server factory.
     305
     306    This factory accepts XMPP external component connections and makes
    272307    the router service route traffic for a component's bound domain
    273308    to that component.
     
    276311    logTraffic = False
    277312
    278     def __init__(self, router, port=5347, secret='secret'):
     313    def __init__(self, router, secret='secret'):
    279314        self.router = router
    280         self.port = port
    281315        self.secret = secret
    282316
     
    284318            return ListenComponentAuthenticator(self.secret)
    285319
    286         self.factory = xmlstream.XmlStreamServerFactory(authenticatorFactory)
    287         self.factory.addBootstrap(xmlstream.STREAM_CONNECTED_EVENT,
    288                                   self.makeConnection)
    289         self.factory.addBootstrap(xmlstream.STREAM_AUTHD_EVENT,
    290                                   self.connectionInitialized)
     320        xmlstream.XmlStreamServerFactory.__init__(self, authenticatorFactory)
     321        self.addBootstrap(xmlstream.STREAM_CONNECTED_EVENT,
     322                          self.makeConnection)
     323        self.addBootstrap(xmlstream.STREAM_AUTHD_EVENT,
     324                          self.connectionInitialized)
    291325
    292326        self.serial = 0
    293 
    294 
    295     def startService(self):
    296         service.Service.startService(self)
    297         reactor.listenTCP(self.port, self.factory)
    298327
    299328
Note: See TracChangeset for help on using the changeset viewer.