Ignore:
Timestamp:
Oct 11, 2008, 10:41:03 PM (14 years ago)
Author:
Ralph Meijer <ralphm@…>
Branch:
default
Convert:
svn:b33ecbfc-034c-dc11-8662-000475d9059e/trunk@106
Message:

Add Internal Component service that connects to a router in-process.

Author: ralphm.
Fixes #31.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wokkel/component.py

    r35 r39  
    1515from twisted.words.xish import domish
    1616
     17from wokkel.generic import XmlPipe
    1718from wokkel.subprotocols import StreamManager
    1819
     
    6364    def _getConnection(self):
    6465        return reactor.connectTCP(self.host, self.port, self.factory)
     66
     67
     68
     69class InternalComponent(xmlstream.XMPPHandlerCollection, service.Service):
     70    """
     71    Component service that connects directly to a router.
     72
     73    Instead of opening a socket to connect to a router, like L{Component},
     74    components of this type connect to a router in the same process. This
     75    allows for one-process XMPP servers.
     76    """
     77
     78    def __init__(self, router, domain):
     79        xmlstream.XMPPHandlerCollection.__init__(self)
     80        self.router = router
     81        self.domain = domain
     82
     83        self.xmlstream = None
     84
     85    def startService(self):
     86        """
     87        Create a XML pipe, connect to the router and setup handlers.
     88        """
     89        service.Service.startService(self)
     90
     91        self.pipe = XmlPipe()
     92        self.xmlstream = self.pipe.source
     93        self.router.addRoute(self.domain, self.pipe.sink)
     94
     95        for e in self:
     96            e.makeConnection(self.xmlstream)
     97            e.connectionInitialized()
     98
     99
     100    def stopService(self):
     101        """
     102        Disconnect from the router and handlers.
     103        """
     104        service.Service.stopService(self)
     105
     106        self.router.removeRoute(self.domain, self.pipe.sink)
     107        self.pipe = None
     108        self.xmlstream = None
     109
     110        for e in self:
     111            e.connectionLost(None)
     112
     113
     114    def addHandler(self, handler):
     115        """
     116        Add a new handler and connect it to the stream.
     117        """
     118        xmlstream.XMPPHandlerCollection.addHandler(self, handler)
     119
     120        if self.xmlstream:
     121            handler.makeConnection(self.xmlstream)
     122            handler.connectionInitialized()
     123
     124
     125    def send(self, obj):
     126        """
     127        Send data to the XML stream, so it ends up at the router.
     128        """
     129        self.xmlstream.send(obj)
    65130
    66131
Note: See TracChangeset for help on using the changeset viewer.