Changeset 34:5d34b8c88a55 for wokkel
- Timestamp:
- Oct 10, 2008, 4:35:43 PM (14 years ago)
- Branch:
- default
- Convert:
- svn:b33ecbfc-034c-dc11-8662-000475d9059e/trunk@88
- Location:
- wokkel
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
wokkel/client.py
r14 r34 17 17 18 18 try: 19 from twisted.words.xish.xmlstream import XmlStreamFactoryMixin19 from twisted.words.xish.xmlstream import BootstrapMixin 20 20 except ImportError: 21 from wokkel.compat import XmlStreamFactoryMixin21 from wokkel.compat import BootstrapMixin 22 22 23 23 from wokkel.subprotocols import StreamManager, XMPPHandler … … 128 128 129 129 130 class DeferredClientFactory( XmlStreamFactoryMixin, protocol.ClientFactory):130 class DeferredClientFactory(BootstrapMixin, protocol.ClientFactory): 131 131 protocol = xmlstream.XmlStream 132 132 133 133 def __init__(self, jid, password): 134 self.authenticator = client.XMPPAuthenticator(jid, password) 135 XmlStreamFactoryMixin.__init__(self, self.authenticator) 134 BootstrapMixin.__init__(self) 135 136 self.jid = jid 137 self.password = password 136 138 137 139 deferred = defer.Deferred() 138 140 self.deferred = deferred 139 140 141 self.addBootstrap(xmlstream.INIT_FAILED_EVENT, deferred.errback) 141 142 … … 147 148 self.addHandler(ConnectionInitializedHandler()) 148 149 150 151 def buildProtocol(self, addr): 152 """ 153 Create an instance of XmlStream. 154 155 A new authenticator instance will be created and passed to the new 156 XmlStream. Registered bootstrap event observers are installed as well. 157 """ 158 self.authenticator = client.XMPPAuthenticator(self.jid, self.password) 159 xs = self.protocol(self.authenticator) 160 xs.factory = self 161 self.installBootstraps(xs) 162 return xs 163 164 149 165 def clientConnectionFailed(self, connector, reason): 150 166 self.deferred.errback(reason) 167 151 168 152 169 def addHandler(self, handler): … … 155 172 """ 156 173 self.streamManager.addHandler(handler) 174 157 175 158 176 def removeHandler(self, handler): -
wokkel/compat.py
r8 r34 1 1 # -*- test-case-name: wokkel.test.test_compat -*- 2 2 # 3 # Copyright (c) 2001-200 7Twisted Matrix Laboratories.3 # Copyright (c) 2001-2008 Twisted Matrix Laboratories. 4 4 # See LICENSE for details. 5 5 6 from twisted.internet import protocol 7 from twisted.words.protocols.jabber import xmlstream 6 8 from twisted.words.xish import domish 7 9 … … 39 41 40 42 41 class XmlStreamFactoryMixin(object): 43 44 class BootstrapMixin(object): 42 45 """ 43 XmlStream factory mixin t hat takes care of event handlers.46 XmlStream factory mixin to install bootstrap event observers. 44 47 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}. 47 53 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 49 56 C{observerfn} arguments to L{utility.EventDispatcher.addObserver}. 57 58 @ivar bootstraps: The list of registered bootstrap event observers. 59 @type bootstrap: C{list} 50 60 """ 51 61 52 def __init__(self , *args, **kwargs):62 def __init__(self): 53 63 self.bootstraps = [] 54 self.args = args55 self.kwargs = kwargs56 64 57 def buildProtocol(self, addr): 65 66 def installBootstraps(self, dispatcher): 58 67 """ 59 Create an instance of XmlStream.68 Install registered bootstrap observers. 60 69 61 The returned instance will have bootstrap event observers registered62 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} 63 72 """ 64 xs = self.protocol(*self.args, **self.kwargs)65 xs.factory = self66 73 for event, fn in self.bootstraps: 67 xs.addObserver(event, fn)68 return xs 74 dispatcher.addObserver(event, fn) 75 69 76 70 77 def addBootstrap(self, event, fn): … … 74 81 self.bootstraps.append((event, fn)) 75 82 83 76 84 def removeBootstrap(self, event, fn): 77 85 """ … … 79 87 """ 80 88 self.bootstraps.remove((event, fn)) 89 90 91 92 class 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 -
wokkel/test/test_client.py
r8 r34 7 7 8 8 from twisted.trial import unittest 9 from twisted.words.protocols.jabber import xmlstream 10 from twisted.words.protocols.jabber.client import XMPPAuthenticator 9 11 from twisted.words.protocols.jabber.jid import JID 10 12 from twisted.words.protocols.jabber.xmlstream import STREAM_AUTHD_EVENT … … 12 14 13 15 from wokkel.client import DeferredClientFactory 16 from wokkel.test.test_compat import BootstrapMixinTest 14 17 15 class DeferredClientFactoryTest(unittest.TestCase): 18 class DeferredClientFactoryTest(BootstrapMixinTest): 19 20 21 def setUp(self): 22 self.factory = DeferredClientFactory(JID('user@example.org'), 'secret') 23 24 25 def test_buildProtocol(self): 26 """ 27 The authenticator factory should be passed to its protocol and it 28 should instantiate the authenticator and save it. 29 L{xmlstream.XmlStream}s do that, but we also want to ensure it really 30 is one. 31 """ 32 xs = self.factory.buildProtocol(None) 33 self.assertIdentical(self.factory, xs.factory) 34 self.assertIsInstance(xs, xmlstream.XmlStream) 35 self.assertIsInstance(xs.authenticator, XMPPAuthenticator) 36 16 37 17 38 def test_deferredOnInitialized(self): … … 20 41 """ 21 42 22 f = DeferredClientFactory(JID('user@example.org'), 'secret')23 x mlstream = f.buildProtocol(None)24 xmlstream.dispatch(xmlstream, STREAM_AUTHD_EVENT)25 return f.deferred 43 xs = self.factory.buildProtocol(None) 44 xs.dispatch(xs, STREAM_AUTHD_EVENT) 45 return self.factory.deferred 46 26 47 27 48 def test_deferredOnNotInitialized(self): … … 30 51 """ 31 52 32 f = DeferredClientFactory(JID('user@example.org'), 'secret')33 xmlstream = f.buildProtocol(None)34 35 53 class TestException(Exception): 36 54 pass 37 55 38 xmlstream.dispatch(TestException(), INIT_FAILED_EVENT) 39 self.assertFailure(f.deferred, TestException) 40 return f.deferred 56 xs = self.factory.buildProtocol(None) 57 xs.dispatch(TestException(), INIT_FAILED_EVENT) 58 self.assertFailure(self.factory.deferred, TestException) 59 return self.factory.deferred 60 41 61 42 62 def test_deferredOnConnectionFailure(self): … … 45 65 """ 46 66 47 f = DeferredClientFactory(JID('user@example.org'), 'secret')48 xmlstream = f.buildProtocol(None)49 50 67 class TestException(Exception): 51 68 pass 52 69 53 f.clientConnectionFailed(self, TestException()) 54 self.assertFailure(f.deferred, TestException) 55 return f.deferred 70 xs = self.factory.buildProtocol(None) 71 self.factory.clientConnectionFailed(self, TestException()) 72 self.assertFailure(self.factory.deferred, TestException) 73 return self.factory.deferred -
wokkel/test/test_compat.py
r8 r34 10 10 from twisted.trial import unittest 11 11 from twisted.words.xish import domish, utility 12 from wokkel.compat import toResponse, XmlStreamFactoryMixin12 from wokkel.compat import toResponse, BootstrapMixin 13 13 14 14 class DummyProtocol(protocol.Protocol, utility.EventDispatcher): … … 16 16 I am a protocol with an event dispatcher without further processing. 17 17 18 This protocol is only used for testing XmlStreamFactoryMixin to make18 This protocol is only used for testing BootstrapMixin to make 19 19 sure the bootstrap observers are added to the protocol instance. 20 20 """ … … 28 28 29 29 30 class XmlStreamFactoryMixinTest(unittest.TestCase):31 30 32 def test_buildProtocol(self): 31 class BootstrapMixinTest(unittest.TestCase): 32 """ 33 Tests for L{BootstrapMixin}. 34 35 @ivar factory: Instance of the factory or mixin under test. 36 """ 37 38 def setUp(self): 39 self.factory = BootstrapMixin() 40 41 42 def test_installBootstraps(self): 33 43 """ 34 Test building of protocol. 35 36 Arguments passed to Factory should be passed to protocol on 37 instantiation. Bootstrap observers should be setup. 44 Dispatching an event should fire registered bootstrap observers. 38 45 """ 39 46 d = defer.Deferred() 47 dispatcher = DummyProtocol() 48 self.factory.addBootstrap('//event/myevent', d.callback) 49 self.factory.installBootstraps(dispatcher) 50 dispatcher.dispatch(None, '//event/myevent') 51 return d 40 52 41 f = XmlStreamFactoryMixin(None, test=None)42 f.protocol = DummyProtocol43 f.addBootstrap('//event/myevent', d.callback)44 xs = f.buildProtocol(None)45 46 self.assertEquals(f, xs.factory)47 self.assertEquals((None,), xs.args)48 self.assertEquals({'test': None}, xs.kwargs)49 xs.dispatch(None, '//event/myevent')50 return d51 53 52 54 def test_addAndRemoveBootstrap(self): … … 57 59 pass 58 60 59 f = XmlStreamFactoryMixin(None, test=None) 61 self.factory.addBootstrap('//event/myevent', cb) 62 self.assertIn(('//event/myevent', cb), self.factory.bootstraps) 60 63 61 f.addBootstrap('//event/myevent', cb)62 self.assert In(('//event/myevent', cb), f.bootstraps)64 self.factory.removeBootstrap('//event/myevent', cb) 65 self.assertNotIn(('//event/myevent', cb), self.factory.bootstraps) 63 66 64 f.removeBootstrap('//event/myevent', cb) 65 self.assertNotIn(('//event/myevent', cb), f.bootstraps) 67 66 68 67 69 class ToResponseTest(unittest.TestCase):
Note: See TracChangeset
for help on using the changeset viewer.