Ignore:
Timestamp:
Oct 10, 2008, 4:35:43 PM (14 years ago)
Author:
Ralph Meijer <ralphm@…>
Branch:
default
Convert:
svn:b33ecbfc-034c-dc11-8662-000475d9059e/trunk@88
Message:

Backport XmlStreamServerFactory?.

Author: ralphm.
Fixes #29.

This also replaces XmlStreamFactoryMixin? with BootstrapMixin? for DeferredClientFactory?.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wokkel/compat.py

    r8 r34  
    11# -*- test-case-name: wokkel.test.test_compat -*-
    22#
    3 # Copyright (c) 2001-2007 Twisted Matrix Laboratories.
     3# Copyright (c) 2001-2008 Twisted Matrix Laboratories.
    44# See LICENSE for details.
    55
     6from twisted.internet import protocol
     7from twisted.words.protocols.jabber import xmlstream
    68from twisted.words.xish import domish
    79
     
    3941
    4042
    41 class XmlStreamFactoryMixin(object):
     43
     44class BootstrapMixin(object):
    4245    """
    43     XmlStream factory mixin that takes care of event handlers.
     46    XmlStream factory mixin to install bootstrap event observers.
    4447
    45     To make sure certain event observers are set up before incoming data is
    46     processed, you can set up bootstrap event observers using C{addBootstrap}.
     48    This mixin is for factories providing
     49    L{IProtocolFactory<twisted.internet.interfaces.IProtocolFactory>} to make
     50    sure bootstrap event observers are set up on protocols, before incoming
     51    data is processed. Such protocols typically derive from
     52    L{utility.EventDispatcher}, like L{XmlStream}.
    4753
    48     The C{event} and C{fn} parameters correspond with the C{event} and
     54    You can set up bootstrap event observers using C{addBootstrap}. The
     55    C{event} and C{fn} parameters correspond with the C{event} and
    4956    C{observerfn} arguments to L{utility.EventDispatcher.addObserver}.
     57
     58    @ivar bootstraps: The list of registered bootstrap event observers.
     59    @type bootstrap: C{list}
    5060    """
    5161
    52     def __init__(self, *args, **kwargs):
     62    def __init__(self):
    5363        self.bootstraps = []
    54         self.args = args
    55         self.kwargs = kwargs
    5664
    57     def buildProtocol(self, addr):
     65
     66    def installBootstraps(self, dispatcher):
    5867        """
    59         Create an instance of XmlStream.
     68        Install registered bootstrap observers.
    6069
    61         The returned instance will have bootstrap event observers registered
    62         and will proceed to handle input on an incoming connection.
     70        @param dispatcher: Event dispatcher to add the observers to.
     71        @type dispatcher: L{utility.EventDispatcher}
    6372        """
    64         xs = self.protocol(*self.args, **self.kwargs)
    65         xs.factory = self
    6673        for event, fn in self.bootstraps:
    67             xs.addObserver(event, fn)
    68         return xs
     74            dispatcher.addObserver(event, fn)
     75
    6976
    7077    def addBootstrap(self, event, fn):
     
    7481        self.bootstraps.append((event, fn))
    7582
     83
    7684    def removeBootstrap(self, event, fn):
    7785        """
     
    7987        """
    8088        self.bootstraps.remove((event, fn))
     89
     90
     91
     92class XmlStreamServerFactory(BootstrapMixin,
     93                             protocol.ServerFactory):
     94    """
     95    Factory for Jabber XmlStream objects as a server.
     96
     97    @since: 8.2.
     98    @ivar authenticatorFactory: Factory callable that takes no arguments, to
     99                                create a fresh authenticator to be associated
     100                                with the XmlStream.
     101    """
     102
     103    protocol = xmlstream.XmlStream
     104
     105    def __init__(self, authenticatorFactory):
     106        xmlstream.BootstrapMixin.__init__(self)
     107        self.authenticatorFactory = authenticatorFactory
     108
     109
     110    def buildProtocol(self, addr):
     111        """
     112        Create an instance of XmlStream.
     113
     114        A new authenticator instance will be created and passed to the new
     115        XmlStream. Registered bootstrap event observers are installed as well.
     116        """
     117        authenticator = self.authenticatorFactory()
     118        xs = self.protocol(authenticator)
     119        xs.factory = self
     120        self.installBootstraps(xs)
     121        return xs
Note: See TracChangeset for help on using the changeset viewer.