[8] | 1 | # -*- test-case-name: wokkel.test.test_compat -*- |
---|
| 2 | # |
---|
[34] | 3 | # Copyright (c) 2001-2008 Twisted Matrix Laboratories. |
---|
[8] | 4 | # See LICENSE for details. |
---|
| 5 | |
---|
[34] | 6 | from twisted.internet import protocol |
---|
| 7 | from twisted.words.protocols.jabber import xmlstream |
---|
[8] | 8 | from twisted.words.xish import domish |
---|
| 9 | |
---|
[34] | 10 | class BootstrapMixin(object): |
---|
[8] | 11 | """ |
---|
[34] | 12 | XmlStream factory mixin to install bootstrap event observers. |
---|
[8] | 13 | |
---|
[34] | 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}. |
---|
[8] | 19 | |
---|
[34] | 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 |
---|
[8] | 22 | C{observerfn} arguments to L{utility.EventDispatcher.addObserver}. |
---|
[34] | 23 | |
---|
[44] | 24 | @since: 8.2. |
---|
[34] | 25 | @ivar bootstraps: The list of registered bootstrap event observers. |
---|
| 26 | @type bootstrap: C{list} |
---|
[8] | 27 | """ |
---|
| 28 | |
---|
[34] | 29 | def __init__(self): |
---|
[8] | 30 | self.bootstraps = [] |
---|
| 31 | |
---|
[34] | 32 | |
---|
| 33 | def installBootstraps(self, dispatcher): |
---|
[8] | 34 | """ |
---|
[34] | 35 | Install registered bootstrap observers. |
---|
[8] | 36 | |
---|
[34] | 37 | @param dispatcher: Event dispatcher to add the observers to. |
---|
| 38 | @type dispatcher: L{utility.EventDispatcher} |
---|
[8] | 39 | """ |
---|
| 40 | for event, fn in self.bootstraps: |
---|
[34] | 41 | dispatcher.addObserver(event, fn) |
---|
| 42 | |
---|
[8] | 43 | |
---|
| 44 | def addBootstrap(self, event, fn): |
---|
| 45 | """ |
---|
| 46 | Add a bootstrap event handler. |
---|
[44] | 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. |
---|
[8] | 51 | """ |
---|
| 52 | self.bootstraps.append((event, fn)) |
---|
| 53 | |
---|
[34] | 54 | |
---|
[8] | 55 | def removeBootstrap(self, event, fn): |
---|
| 56 | """ |
---|
| 57 | Remove a bootstrap event handler. |
---|
[44] | 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. |
---|
[8] | 62 | """ |
---|
| 63 | self.bootstraps.remove((event, fn)) |
---|
[34] | 64 | |
---|
| 65 | |
---|
| 66 | |
---|
| 67 | class 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): |
---|
[38] | 81 | BootstrapMixin.__init__(self) |
---|
[34] | 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 |
---|