source: ralphm-patches/deferred_xmlstream_factory.patch @ 6:46e82ac33f4a

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

Add s2s code.

File size: 4.4 KB
  • wokkel/client.py

    diff -r 9dd177aaa50c wokkel/client.py
    a b  
    1111"""
    1212
    1313from twisted.application import service
    14 from twisted.internet import defer, protocol, reactor
     14from twisted.internet import reactor
    1515from twisted.names.srvconnect import SRVConnector
    1616from twisted.words.protocols.jabber import client, sasl, xmlstream
    1717
    18 try:
    19     from twisted.words.xish.xmlstream import BootstrapMixin
    20 except ImportError:
    21     from wokkel.compat import BootstrapMixin
    22 
    23 from wokkel.subprotocols import StreamManager, XMPPHandler
     18from wokkel import generic
     19from wokkel.subprotocols import StreamManager
    2420
    2521class CheckAuthInitializer(object):
    2622    """
     
    127123            return c
    128124
    129125
    130 class DeferredClientFactory(BootstrapMixin, protocol.ClientFactory):
    131     protocol = xmlstream.XmlStream
     126class DeferredClientFactory(generic.DeferredXmlStreamFactory):
    132127
    133128    def __init__(self, jid, password):
    134         BootstrapMixin.__init__(self)
    135 
    136         self.jid = jid
    137         self.password = password
    138 
    139         deferred = defer.Deferred()
    140         self.deferred = deferred
    141         self.addBootstrap(xmlstream.INIT_FAILED_EVENT, deferred.errback)
    142 
    143         class ConnectionInitializedHandler(XMPPHandler):
    144             def connectionInitialized(self):
    145                 deferred.callback(None)
    146 
    147         self.streamManager = StreamManager(self)
    148         self.addHandler(ConnectionInitializedHandler())
    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 
    165     def clientConnectionFailed(self, connector, reason):
    166         self.deferred.errback(reason)
     129        authenticator = client.XMPPAuthenticator(jid, password)
     130        generic.DeferredXmlStreamFactory.__init__(self, authenticator)
    167131
    168132
    169133    def addHandler(self, handler):
     
    180144        self.streamManager.removeHandler(handler)
    181145
    182146
     147
    183148def clientCreator(factory):
    184     domain = factory.jid.host
     149    domain = factory.authenticator.jid.host
    185150    c = SRVConnector(reactor, 'xmpp-client', domain, factory)
    186151    c.connect()
    187152    return factory.deferred
  • wokkel/generic.py

    diff -r 9dd177aaa50c wokkel/generic.py
    a b  
    99
    1010from zope.interface import implements
    1111
    12 from twisted.internet import defer
    13 from twisted.words.protocols.jabber import error
     12from twisted.internet import defer, protocol
     13from twisted.words.protocols.jabber import error, xmlstream
    1414from twisted.words.protocols.jabber.xmlstream import toResponse
    1515from twisted.words.xish import domish, utility
    1616
     17try:
     18    from twisted.words.xish.xmlstream import BootstrapMixin
     19except ImportError:
     20    from wokkel.compat import BootstrapMixin
     21
    1722from wokkel import disco
    1823from wokkel.iwokkel import IDisco
    1924from wokkel.subprotocols import XMPPHandler
     
    155160        self.sink = utility.EventDispatcher()
    156161        self.source.send = lambda obj: self.sink.dispatch(obj)
    157162        self.sink.send = lambda obj: self.source.dispatch(obj)
     163
     164
     165class DeferredXmlStreamFactory(BootstrapMixin, protocol.ClientFactory):
     166    protocol = xmlstream.XmlStream
     167
     168    def __init__(self, authenticator):
     169        BootstrapMixin.__init__(self)
     170
     171        self.authenticator = authenticator
     172
     173        deferred = defer.Deferred()
     174        self.deferred = deferred
     175        self.addBootstrap(xmlstream.STREAM_AUTHD_EVENT, self.deferred.callback)
     176        self.addBootstrap(xmlstream.INIT_FAILED_EVENT, deferred.errback)
     177
     178
     179    def buildProtocol(self, addr):
     180        """
     181        Create an instance of XmlStream.
     182
     183        A new authenticator instance will be created and passed to the new
     184        XmlStream. Registered bootstrap event observers are installed as well.
     185        """
     186        xs = self.protocol(self.authenticator)
     187        xs.factory = self
     188        self.installBootstraps(xs)
     189        return xs
     190
     191
     192    def clientConnectionFailed(self, connector, reason):
     193        self.deferred.errback(reason)
Note: See TracBrowser for help on using the repository browser.