source: ralphm-patches/component_multiple.patch @ 2:ec684efc6a7b

Last change on this file since 2:ec684efc6a7b was 2:ec684efc6a7b, checked in by Ralph Meijer <ralphm@…>, 11 years ago

Allow multiple domains per internal component.

File size: 5.2 KB
  • wokkel/component.py

    diff -r 5de8d8c9e331 wokkel/component.py
    a b  
    8585    Instead of opening a socket to connect to a router, like L{Component},
    8686    components of this type connect to a router in the same process. This
    8787    allows for one-process XMPP servers.
     88
     89    @ivar domains: Domains (as C{str}) this component will handle traffic for.
     90    @type domains: L{set}
    8891    """
    8992
    90     def __init__(self, router, domain):
     93    def __init__(self, router, domain=None):
    9194        XMPPHandlerCollection.__init__(self)
    92         self.router = router
    93         self.domain = domain
     95
     96        self._router = router
     97        self.domains = set()
     98        if domain:
     99            self.domains.add(domain)
    94100
    95101        self.xmlstream = None
    96102
     
    100106        """
    101107        service.Service.startService(self)
    102108
    103         self.pipe = XmlPipe()
    104         self.xmlstream = self.pipe.source
    105         self.router.addRoute(self.domain, self.pipe.sink)
     109        self._pipe = XmlPipe()
     110        self.xmlstream = self._pipe.source
     111
     112        for domain in self.domains:
     113            self._router.addRoute(domain, self._pipe.sink)
    106114
    107115        for e in self:
    108116            e.makeConnection(self.xmlstream)
     
    115123        """
    116124        service.Service.stopService(self)
    117125
    118         self.router.removeRoute(self.domain, self.pipe.sink)
    119         self.pipe = None
     126        for domain in self.domains:
     127            self._router.removeRoute(domain, self._pipe.sink)
     128
     129        self._pipe = None
    120130        self.xmlstream = None
    121131
    122132        for e in self:
  • wokkel/test/test_component.py

    diff -r 5de8d8c9e331 wokkel/test/test_component.py
    a b  
    4141        verifyObject(IXMPPHandlerCollection, self.component)
    4242
    4343
    44     def test_startService(self):
     44
     45    def test_startServiceRunning(self):
    4546        """
    46         Starting the service creates a new route and hooks up handlers.
     47        Starting the service makes it running.
    4748        """
     49        self.assertFalse(self.component.running)
     50        self.component.startService()
     51        self.assertTrue(self.component.running)
    4852
     53
     54    def test_startServiceAddRoute(self):
     55        """
     56        Starting the service creates a new route.
     57        """
     58        self.component.startService()
     59        self.assertIn('component', self.router.routes)
     60
     61
     62    def test_startServiceNoDomain(self):
     63        self.component = component.InternalComponent(self.router)
     64        self.component.startService()
     65
     66
     67    def test_startServiceAddMultipleRoutes(self):
     68        """
     69        Starting the service creates a new route.
     70        """
     71        self.component.domains.add('component2')
     72        self.component.startService()
     73        self.assertIn('component', self.router.routes)
     74        self.assertIn('component2', self.router.routes)
     75
     76
     77    def test_startServiceHandlerDispatch(self):
     78        """
     79        Starting the service hooks up handlers.
     80        """
    4981        events = []
    5082
    5183        class TestHandler(XMPPHandler):
     
    5688
    5789        TestHandler().setHandlerParent(self.component)
    5890
    59         self.assertFalse(self.component.running)
    60 
    6191        self.component.startService()
    62 
    63         self.assertTrue(self.component.running)
    64         self.assertIn('component', self.router.routes)
    65 
    6692        self.assertEquals([], events)
    6793        self.component.xmlstream.dispatch(None, '//event/test')
    6894        self.assertEquals([None], events)
    6995
    7096
    71     def test_stopService(self):
     97    def test_stopServiceNotRunning(self):
    7298        """
    73         Stopping the service removes the route and disconnects handlers.
     99        Stopping the service makes it not running.
    74100        """
     101        self.component.startService()
     102        self.component.stopService()
     103        self.assertFalse(self.component.running)
    75104
     105
     106    def test_stopServiceRemoveRoute(self):
     107        """
     108        Stopping the service removes routes.
     109        """
     110        self.component.startService()
     111        self.component.stopService()
     112        self.assertNotIn('component', self.router.routes)
     113
     114
     115    def test_stopServiceNoDomain(self):
     116        self.component = component.InternalComponent(self.router)
     117        self.component.startService()
     118        self.component.stopService()
     119
     120
     121    def test_startServiceRemoveMultipleRoutes(self):
     122        """
     123        Starting the service creates a new route.
     124        """
     125        self.component.domains.add('component2')
     126        self.component.startService()
     127        self.component.stopService()
     128        self.assertNotIn('component', self.router.routes)
     129        self.assertNotIn('component2', self.router.routes)
     130
     131
     132    def test_stopServiceHandlerDispatch(self):
     133        """
     134        Stopping the service disconnects handlers.
     135        """
    76136        events = []
    77137
    78138        class TestHandler(XMPPHandler):
     
    84144
    85145        self.component.startService()
    86146        self.component.stopService()
    87 
    88         self.assertFalse(self.component.running)
    89147        self.assertEquals(1, len(events))
    90         self.assertNotIn('component', self.router.routes)
    91148
    92149
    93150    def test_addHandler(self):
Note: See TracBrowser for help on using the repository browser.