source: ralphm-patches/deferred_xmlstream_factory.patch @ 12:fc40892815eb

Last change on this file since 12:fc40892815eb was 6:46e82ac33f4a, checked in by Ralph Meijer <ralphm@…>, 13 years ago

Add s2s code.

File size: 4.4 KB
RevLine 
[6]1diff -r 9dd177aaa50c wokkel/client.py
2--- a/wokkel/client.py  Wed Apr 01 17:25:07 2009 +0200
3+++ b/wokkel/client.py  Thu Apr 02 09:13:20 2009 +0200
4@@ -11,16 +11,12 @@
5 """
6 
7 from twisted.application import service
8-from twisted.internet import defer, protocol, reactor
9+from twisted.internet import reactor
10 from twisted.names.srvconnect import SRVConnector
11 from twisted.words.protocols.jabber import client, sasl, xmlstream
12 
13-try:
14-    from twisted.words.xish.xmlstream import BootstrapMixin
15-except ImportError:
16-    from wokkel.compat import BootstrapMixin
17-
18-from wokkel.subprotocols import StreamManager, XMPPHandler
19+from wokkel import generic
20+from wokkel.subprotocols import StreamManager
21 
22 class CheckAuthInitializer(object):
23     """
24@@ -127,43 +123,11 @@
25             return c
26 
27 
28-class DeferredClientFactory(BootstrapMixin, protocol.ClientFactory):
29-    protocol = xmlstream.XmlStream
30+class DeferredClientFactory(generic.DeferredXmlStreamFactory):
31 
32     def __init__(self, jid, password):
33-        BootstrapMixin.__init__(self)
34-
35-        self.jid = jid
36-        self.password = password
37-
38-        deferred = defer.Deferred()
39-        self.deferred = deferred
40-        self.addBootstrap(xmlstream.INIT_FAILED_EVENT, deferred.errback)
41-
42-        class ConnectionInitializedHandler(XMPPHandler):
43-            def connectionInitialized(self):
44-                deferred.callback(None)
45-
46-        self.streamManager = StreamManager(self)
47-        self.addHandler(ConnectionInitializedHandler())
48-
49-
50-    def buildProtocol(self, addr):
51-        """
52-        Create an instance of XmlStream.
53-
54-        A new authenticator instance will be created and passed to the new
55-        XmlStream. Registered bootstrap event observers are installed as well.
56-        """
57-        self.authenticator = client.XMPPAuthenticator(self.jid, self.password)
58-        xs = self.protocol(self.authenticator)
59-        xs.factory = self
60-        self.installBootstraps(xs)
61-        return xs
62-
63-
64-    def clientConnectionFailed(self, connector, reason):
65-        self.deferred.errback(reason)
66+        authenticator = client.XMPPAuthenticator(jid, password)
67+        generic.DeferredXmlStreamFactory.__init__(self, authenticator)
68 
69 
70     def addHandler(self, handler):
71@@ -180,8 +144,9 @@
72         self.streamManager.removeHandler(handler)
73 
74 
75+
76 def clientCreator(factory):
77-    domain = factory.jid.host
78+    domain = factory.authenticator.jid.host
79     c = SRVConnector(reactor, 'xmpp-client', domain, factory)
80     c.connect()
81     return factory.deferred
82diff -r 9dd177aaa50c wokkel/generic.py
83--- a/wokkel/generic.py Wed Apr 01 17:25:07 2009 +0200
84+++ b/wokkel/generic.py Thu Apr 02 09:13:20 2009 +0200
85@@ -9,11 +9,16 @@
86 
87 from zope.interface import implements
88 
89-from twisted.internet import defer
90-from twisted.words.protocols.jabber import error
91+from twisted.internet import defer, protocol
92+from twisted.words.protocols.jabber import error, xmlstream
93 from twisted.words.protocols.jabber.xmlstream import toResponse
94 from twisted.words.xish import domish, utility
95 
96+try:
97+    from twisted.words.xish.xmlstream import BootstrapMixin
98+except ImportError:
99+    from wokkel.compat import BootstrapMixin
100+
101 from wokkel import disco
102 from wokkel.iwokkel import IDisco
103 from wokkel.subprotocols import XMPPHandler
104@@ -155,3 +160,34 @@
105         self.sink = utility.EventDispatcher()
106         self.source.send = lambda obj: self.sink.dispatch(obj)
107         self.sink.send = lambda obj: self.source.dispatch(obj)
108+
109+
110+class DeferredXmlStreamFactory(BootstrapMixin, protocol.ClientFactory):
111+    protocol = xmlstream.XmlStream
112+
113+    def __init__(self, authenticator):
114+        BootstrapMixin.__init__(self)
115+
116+        self.authenticator = authenticator
117+
118+        deferred = defer.Deferred()
119+        self.deferred = deferred
120+        self.addBootstrap(xmlstream.STREAM_AUTHD_EVENT, self.deferred.callback)
121+        self.addBootstrap(xmlstream.INIT_FAILED_EVENT, deferred.errback)
122+
123+
124+    def buildProtocol(self, addr):
125+        """
126+        Create an instance of XmlStream.
127+
128+        A new authenticator instance will be created and passed to the new
129+        XmlStream. Registered bootstrap event observers are installed as well.
130+        """
131+        xs = self.protocol(self.authenticator)
132+        xs.factory = self
133+        self.installBootstraps(xs)
134+        return xs
135+
136+
137+    def clientConnectionFailed(self, connector, reason):
138+        self.deferred.errback(reason)
Note: See TracBrowser for help on using the repository browser.