source: wokkel/component.py @ 30:68535ae85c8d

Last change on this file since 30:68535ae85c8d was 6:283b6997cf7d, checked in by Ralph Meijer <ralphm@…>, 14 years ago

Support services on a JID.

Author: ralphm
Fixes #2.

File size: 1.5 KB
Line 
1# Copyright (c) 2003-2007 Ralph Meijer
2# See LICENSE for details.
3
4"""
5XMPP External Component utilities
6"""
7
8from twisted.application import service
9from twisted.internet import reactor
10from twisted.words.protocols.jabber import component
11from twisted.words.xish import domish
12
13from wokkel.subprotocols import StreamManager
14
15class Component(StreamManager, service.Service):
16    def __init__(self, host, port, jid, password):
17        self.host = host
18        self.port = port
19
20        factory = component.componentFactory(jid, password)
21
22        StreamManager.__init__(self, factory)
23
24    def _authd(self, xs):
25        old_send = xs.send
26
27        def send(obj):
28            if domish.IElement.providedBy(obj) and \
29                    not obj.getAttribute('from'):
30                obj['from'] = self.xmlstream.thisEntity.full()
31            old_send(obj)
32
33        xs.send = send
34        StreamManager._authd(self, xs)
35
36    def initializationFailed(self, reason):
37        """
38        Called when stream initialization has failed.
39
40        Stop the service (thereby disconnecting the current stream) and
41        raise the exception.
42        """
43        self.stopService()
44        reason.raiseException()
45
46    def startService(self):
47        service.Service.startService(self)
48
49        self.factory.stopTrying()
50        self._connection = self._getConnection()
51
52    def stopService(self):
53        service.Service.stopService(self)
54
55        self._connection.disconnect()
56
57    def _getConnection(self):
58        return reactor.connectTCP(self.host, self.port, self.factory)
Note: See TracBrowser for help on using the repository browser.