[Twisted-Python] XMPP server connection question
Ralph Meijer
twisted at ralphm.ik.nu
Fri Dec 5 06:23:42 EST 2008
On 2008-12-05 11:42, Phil Mayers wrote:
> Christopher Zorn wrote:
>
> Yeah, the t.w.p.j.xmlstream.XmlStreamFactory inherits from
> ReconnectingClientFactory, which is IMHO wrong.
>
> See also:
>
> http://twistedmatrix.com/trac/ticket/3492
Wrong? It was just a design choice for this particular factory, made
more than five years ago. Since then, the code has been refactored in
such a way that it is pretty straightforward to have a non-reconnecting
XMPP client:
-----8<-----8<-----8<-----
from twisted.words.xish.xmlstream import BootstrapMixin
from twisted.internet import protocol
class NonReconnectingXmlStreamFactory(BootstrapMixin,
protocol.ClientFactory):
"""
XmlStream factory that connects at most once.
@ivar authenticator: The authenticator to be associated with the
XmlStream once a connection has been
established.
"""
def __init__(self, authenticator):
xmlstream.BootstrapMixin.__init__(self)
self.authenticator = authenticator
def buildProtocol(self, addr):
"""
Create an instance of XmlStream and associate the authenticator.
"""
xs = self.protocol(authenticator)
self.installBootstraps(xs)
return xs
-----8<-----8<-----8<-----
You can then also override protocol.ClientFactory's
clientConnectionFailed and clientConnectionLost as desirable. You should
probably add bootstrap handlers for STREAM_AUTHD_EVENT,
INIT_FAILED_EVENT, STREAM_ERROR_EVENT somewhere.
I have a similar special client factory in Wokkel that fires a deferred
as soon as a connection was made and the stream is authenticated. See
wokkel.client.DeferredClientFactory.
ralphm
More information about the Twisted-Python
mailing list