source: ralphm-patches/c2s_server_factory.patch @ 66:b713f442b222

Last change on this file since 66:b713f442b222 was 66:b713f442b222, checked in by Ralph Meijer <ralphm@…>, 8 years ago

Add many tests, docstrings for authenticator, make example functional.

File size: 2.9 KB
  • wokkel/client.py

    # HG changeset patch
    # Parent 23dbe722e4482b8286153d29bf87902b464f919f
    Add factory for accepting client connections.
    
    The new XMPPC2SServerFactory is a server factory for accepting client
    connections. It uses `XMPPClientListenAuthenticator` to perform the
    steps for authentication and binding of a resource, and keeps a list
    of all streams.
    
    Upon loss of the connection, the service is called with `unbindResource`.
    Received stanzas cause the service's `onElement` to be called.
    
    The factory has a `deliverStanza` method to deliver stanzas to a particular
    recipient. This is used for stanzas that have different recipient addressing
    than the actual recipient (for presence and messages from a different (or no)
    resource).
    
    TODO:
    
     * Add docstrings.
     * Add tests.
    
    diff --git a/wokkel/client.py b/wokkel/client.py
    a b  
    2121from twisted.words.xish import domish
    2222
    2323from wokkel import generic
     24from wokkel.compat import XmlStreamServerFactory
    2425from wokkel.iwokkel import IUserSession
    2526from wokkel.subprotocols import StreamManager
    2627
     
    401402            self.portal = self.portals[self.xmlstream.thisEntity]
    402403        except KeyError:
    403404            raise error.StreamError('host-unknown')
     405
     406
     407
     408class XMPPC2SServerFactory(XmlStreamServerFactory):
     409
     410    def __init__(self, portal):
     411        self.portal = portal
     412
     413        def authenticatorFactory():
     414            return XMPPClientListenAuthenticator(portal)
     415
     416        XmlStreamServerFactory.__init__(self, authenticatorFactory)
     417        self.addBootstrap(xmlstream.STREAM_CONNECTED_EVENT,
     418                          self.onConnectionMade)
     419        self.addBootstrap(xmlstream.STREAM_AUTHD_EVENT,
     420                          self.onAuthenticated)
     421
     422        self.serial = 0
     423
     424
     425    def onConnectionMade(self, xs):
     426        """
     427        Called when a client-to-server connection was made.
     428
     429        This enables traffic debugging on incoming streams.
     430        """
     431        xs.serial = self.serial
     432        self.serial += 1
     433
     434        log.msg("Client connection %d made" % xs.serial)
     435
     436        def logDataIn(buf):
     437            log.msg("RECV (%d): %r" % (xs.serial, buf))
     438
     439        def logDataOut(buf):
     440            log.msg("SEND (%d): %r" % (xs.serial, buf))
     441
     442        if self.logTraffic:
     443            xs.rawDataInFn = logDataIn
     444            xs.rawDataOutFn = logDataOut
     445
     446        xs.addObserver(xmlstream.STREAM_END_EVENT, self.onConnectionLost,
     447                                                   0, xs)
     448        xs.addObserver(xmlstream.STREAM_ERROR_EVENT, self.onError)
     449
     450
     451    def onAuthenticated(self, xs):
     452        log.msg("Client connection %d authenticated" % xs.serial)
     453
     454        xs.addObserver('/*', xs.avatar.send)
     455
     456
     457    def onConnectionLost(self, xs, reason):
     458        log.msg("Client connection %d disconnected" % xs.serial)
     459
     460
     461    def onError(self, reason):
     462        log.err(reason, "Stream Error")
Note: See TracBrowser for help on using the repository browser.