~justin-fathomdb/nova/justinsb-openstack-api-volumes

« back to all changes in this revision

Viewing changes to vendor/Twisted-10.0.0/doc/words/examples/xmpp_client.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2001-2009 Twisted Matrix Laboratories.
 
2
# See LICENSE for details.
 
3
 
 
4
import sys
 
5
from twisted.internet import reactor
 
6
from twisted.names.srvconnect import SRVConnector
 
7
from twisted.words.xish import domish
 
8
from twisted.words.protocols.jabber import xmlstream, client, jid
 
9
 
 
10
 
 
11
class XMPPClientConnector(SRVConnector):
 
12
    def __init__(self, reactor, domain, factory):
 
13
        SRVConnector.__init__(self, reactor, 'xmpp-client', domain, factory)
 
14
 
 
15
 
 
16
    def pickServer(self):
 
17
        host, port = SRVConnector.pickServer(self)
 
18
 
 
19
        if not self.servers and not self.orderedServers:
 
20
            # no SRV record, fall back..
 
21
            port = 5222
 
22
 
 
23
        return host, port
 
24
 
 
25
 
 
26
 
 
27
class Client(object):
 
28
    def __init__(self, client_jid, secret):
 
29
        f = client.XMPPClientFactory(client_jid, secret)
 
30
        f.addBootstrap(xmlstream.STREAM_CONNECTED_EVENT, self.connected)
 
31
        f.addBootstrap(xmlstream.STREAM_END_EVENT, self.disconnected)
 
32
        f.addBootstrap(xmlstream.STREAM_AUTHD_EVENT, self.authenticated)
 
33
        f.addBootstrap(xmlstream.INIT_FAILED_EVENT, self.init_failed)
 
34
        connector = XMPPClientConnector(reactor, client_jid.host, f)
 
35
        connector.connect()
 
36
 
 
37
 
 
38
    def rawDataIn(self, buf):
 
39
        print "RECV: %s" % unicode(buf, 'utf-8').encode('ascii', 'replace')
 
40
 
 
41
 
 
42
    def rawDataOut(self, buf):
 
43
        print "SEND: %s" % unicode(buf, 'utf-8').encode('ascii', 'replace')
 
44
 
 
45
 
 
46
    def connected(self, xs):
 
47
        print 'Connected.'
 
48
 
 
49
        self.xmlstream = xs
 
50
 
 
51
        # Log all traffic
 
52
        xs.rawDataInFn = self.rawDataIn
 
53
        xs.rawDataOutFn = self.rawDataOut
 
54
 
 
55
 
 
56
    def disconnected(self, xs):
 
57
        print 'Disconnected.'
 
58
 
 
59
        reactor.stop()
 
60
 
 
61
 
 
62
    def authenticated(self, xs):
 
63
        print "Authenticated."
 
64
 
 
65
        presence = domish.Element((None, 'presence'))
 
66
        xs.send(presence)
 
67
 
 
68
        reactor.callLater(5, xs.sendFooter)
 
69
 
 
70
 
 
71
    def init_failed(self, failure):
 
72
        print "Initialization failed."
 
73
        print failure
 
74
 
 
75
        self.xmlstream.sendFooter()
 
76
 
 
77
 
 
78
client_jid = jid.JID(sys.argv[1])
 
79
secret = sys.argv[2]
 
80
c = Client(client_jid, secret)
 
81
 
 
82
reactor.run()