source: wokkel/compat.py @ 53:4aec395fda5b

Last change on this file since 53:4aec395fda5b was 53:4aec395fda5b, checked in by Ralph Meijer <ralphm@…>, 11 years ago

Release Wokkel 0.5.0.

File size: 3.0 KB
Line 
1# -*- test-case-name: wokkel.test.test_compat -*-
2#
3# Copyright (c) 2001-2008 Twisted Matrix Laboratories.
4# See LICENSE for details.
5
6from twisted.internet import protocol
7from twisted.words.protocols.jabber import xmlstream
8from twisted.words.xish import domish
9
10class BootstrapMixin(object):
11    """
12    XmlStream factory mixin to install bootstrap event observers.
13
14    This mixin is for factories providing
15    L{IProtocolFactory<twisted.internet.interfaces.IProtocolFactory>} to make
16    sure bootstrap event observers are set up on protocols, before incoming
17    data is processed. Such protocols typically derive from
18    L{utility.EventDispatcher}, like L{XmlStream}.
19
20    You can set up bootstrap event observers using C{addBootstrap}. The
21    C{event} and C{fn} parameters correspond with the C{event} and
22    C{observerfn} arguments to L{utility.EventDispatcher.addObserver}.
23
24    @since: 8.2.
25    @ivar bootstraps: The list of registered bootstrap event observers.
26    @type bootstrap: C{list}
27    """
28
29    def __init__(self):
30        self.bootstraps = []
31
32
33    def installBootstraps(self, dispatcher):
34        """
35        Install registered bootstrap observers.
36
37        @param dispatcher: Event dispatcher to add the observers to.
38        @type dispatcher: L{utility.EventDispatcher}
39        """
40        for event, fn in self.bootstraps:
41            dispatcher.addObserver(event, fn)
42
43
44    def addBootstrap(self, event, fn):
45        """
46        Add a bootstrap event handler.
47
48        @param event: The event to register an observer for.
49        @type event: C{str} or L{xpath.XPathQuery}
50        @param fn: The observer callable to be registered.
51        """
52        self.bootstraps.append((event, fn))
53
54
55    def removeBootstrap(self, event, fn):
56        """
57        Remove a bootstrap event handler.
58
59        @param event: The event the observer is registered for.
60        @type event: C{str} or L{xpath.XPathQuery}
61        @param fn: The registered observer callable.
62        """
63        self.bootstraps.remove((event, fn))
64
65
66
67class XmlStreamServerFactory(BootstrapMixin,
68                             protocol.ServerFactory):
69    """
70    Factory for Jabber XmlStream objects as a server.
71
72    @since: 8.2.
73    @ivar authenticatorFactory: Factory callable that takes no arguments, to
74                                create a fresh authenticator to be associated
75                                with the XmlStream.
76    """
77
78    protocol = xmlstream.XmlStream
79
80    def __init__(self, authenticatorFactory):
81        BootstrapMixin.__init__(self)
82        self.authenticatorFactory = authenticatorFactory
83
84
85    def buildProtocol(self, addr):
86        """
87        Create an instance of XmlStream.
88
89        A new authenticator instance will be created and passed to the new
90        XmlStream. Registered bootstrap event observers are installed as well.
91        """
92        authenticator = self.authenticatorFactory()
93        xs = self.protocol(authenticator)
94        xs.factory = self
95        self.installBootstraps(xs)
96        return xs
Note: See TracBrowser for help on using the repository browser.