| 1 | from twisted.application import service |
|---|
| 2 | from twisted.words.protocols.jabber import component, xmlstream |
|---|
| 3 | from twisted.words.xish import utility |
|---|
| 4 | |
|---|
| 5 | from wokkel import disco |
|---|
| 6 | from wokkel.component import Component |
|---|
| 7 | |
|---|
| 8 | class 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 | |
|---|
| 55 | class 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 | |
|---|
| 70 | application = service.Application("Jabber Router") |
|---|
| 71 | |
|---|
| 72 | router = component.RouterService() |
|---|
| 73 | router.setServiceParent(application) |
|---|
| 74 | |
|---|
| 75 | componentService = component.ComponentServer(router, port=5347) |
|---|
| 76 | componentService.logTraffic = True |
|---|
| 77 | componentService.setServiceParent(application) |
|---|
| 78 | |
|---|
| 79 | #cmp1 = Component('localhost', 5347, 'cmp1', 'secret') |
|---|
| 80 | cmp1 = InternalComponent(router, 'cmp1') |
|---|
| 81 | cmp1.logTraffic = True |
|---|
| 82 | cmp1.setServiceParent(application) |
|---|
| 83 | disco.DiscoHandler().setHandlerParent(cmp1) |
|---|
| 84 | |
|---|
| 85 | cmp2 = Component('localhost', 5347, 'cmp2', 'secret') |
|---|
| 86 | #cmp2 = InternalComponent(router, 'cmp2') |
|---|
| 87 | cmp2.logTraffic = True |
|---|
| 88 | cmp2.setServiceParent(application) |
|---|
| 89 | DiscoClientSender().setHandlerParent(cmp2) |
|---|