Ticket #3407: test.py

File test.py, 2.3 KB (added by ralphm, 6 years ago)

Test script for XMPP router.

Line 
1from twisted.application import service
2from twisted.words.protocols.jabber import component, xmlstream
3from twisted.words.xish import utility
4
5from wokkel import disco
6from wokkel.component import Component
7
8class InternalComponent(xmlstream.XMPPHandlerCollection, service.Service):
9
10    logTraffic = False
11
12    def __init__(self, router, domain):
13        xmlstream.XMPPHandlerCollection.__init__(self)
14        self.router = router
15        self.domain = domain
16
17        self.xmlstream = None
18
19    def startService(self):
20        service.Service.startService(self)
21
22        self.pipe = utility.XmlPipe()
23        self.xmlstream = self.pipe.source
24        self.router.addRoute(self.domain, self.pipe.sink)
25
26        for e in self:
27            e.makeConnection(self.xmlstream)
28            e.connectionInitialized()
29
30
31    def stopService(self):
32        service.Service.stopService(self)
33
34        self.router.removeRoute(self.domain, self.pipe.sink)
35        self.pipe = None
36        self.xmlstream = None
37
38        for e in self:
39            e.connectionLost(None)
40
41
42    def addHandler(self, handler):
43        xmlstream.XMPPHandlerCollection.addHandler(self, handler)
44
45        if self.xmlstream:
46            handler.makeConnection(self.xmlstream)
47            handler.connectionInitialized()
48
49
50    def send(self, obj):
51        self.xmlstream.send(obj)
52
53
54
55class DiscoClientSender(xmlstream.XMPPHandler):
56    def connectionInitialized(self):
57
58        def cb(result):
59            print "Joepie"
60
61        iq = xmlstream.IQ(self.xmlstream, 'get')
62        iq['from'] = 'cmp2'
63        iq['to'] = 'cmp1'
64        iq.addElement((disco.NS_INFO, 'query'))
65        d = iq.send()
66        d.addCallback(cb)
67
68
69
70application = service.Application("Jabber Router")
71
72router = component.RouterService()
73router.setServiceParent(application)
74
75componentService = component.ComponentServer(router, port=5347)
76componentService.logTraffic = True
77componentService.setServiceParent(application)
78
79#cmp1 = Component('localhost', 5347, 'cmp1', 'secret')
80cmp1 = InternalComponent(router, 'cmp1')
81cmp1.logTraffic = True
82cmp1.setServiceParent(application)
83disco.DiscoHandler().setHandlerParent(cmp1)
84
85cmp2 = Component('localhost', 5347, 'cmp2', 'secret')
86#cmp2 = InternalComponent(router, 'cmp2')
87cmp2.logTraffic = True
88cmp2.setServiceParent(application)
89DiscoClientSender().setHandlerParent(cmp2)