Ejemplo n.º 1
0
    def _shutdown_subsystems(self):
        # cleanup internals
        if self._timer is not None and self._timer.active():
            self._timer.cancel()
        self._timer = None

        # shutdown middleware components
        dns_manager = DNSManager()
        account_manager = AccountManager()
        addressbook_manager = AddressbookManager()
        session_manager = SessionManager()
        procs = [
            proc.spawn(dns_manager.stop),
            proc.spawn(account_manager.stop),
            proc.spawn(addressbook_manager.stop),
            proc.spawn(session_manager.stop)
        ]
        proc.waitall(procs)

        # stop video device
        self.video_device.producer.close()

        # shutdown engine
        self.engine.stop()
        self.engine.join(timeout=5)

        # stop threads
        thread_manager = ThreadManager()
        thread_manager.stop()

        # stop the reactor
        reactor.stop()
Ejemplo n.º 2
0
    def _shutdown_subsystems(self):
        dns_manager = DNSManager()
        account_manager = AccountManager()
        session_manager = SessionManager()

        # terminate all sessions
        p = proc.spawn(session_manager.stop)
        p.wait()

        # shutdown SylkServer components
        procs = [proc.spawn(self.web_server.stop), proc.spawn(self.request_handler.stop), proc.spawn(self.thor_interface.stop)]
        proc.waitall(procs)

        # shutdown other middleware components
        procs = [proc.spawn(dns_manager.stop), proc.spawn(account_manager.stop)]
        proc.waitall(procs)

        # shutdown engine
        self.engine.stop()
        self.engine.join(timeout=5)

        # stop threads
        thread_manager = ThreadManager()
        thread_manager.stop()

        # stop the reactor
        reactor.stop()
Ejemplo n.º 3
0
def handler(local):
    client = str(local.getHost())
    print 'accepted connection from %s' % client
    remote = GreenClientCreator(reactor, UnbufferedTransport).connectTCP(
        remote_host, remote_port)
    a = proc.spawn(forward, remote, local)
    b = proc.spawn(forward, local, remote)
    proc.waitall([a, b], trap_errors=True)
    print 'closed connection to %s' % client
Ejemplo n.º 4
0
 def start(self):
     """
     Start the accounts, which will determine the ones with the enabled flag
     set to activate.
     """
     notification_center = NotificationCenter()
     notification_center.post_notification('SIPAccountManagerWillStart', sender=self)
     proc.waitall([proc.spawn(account.start) for account in self.accounts.itervalues()])
     notification_center.post_notification('SIPAccountManagerDidStart', sender=self)
Ejemplo n.º 5
0
 def stop(self):
     """
     Stop the accounts, which will determine the ones that were enabled to
     deactivate. This method returns only once the accounts were stopped
     successfully or they timed out trying.
     """
     notification_center = NotificationCenter()
     notification_center.post_notification('SIPAccountManagerWillEnd', sender=self)
     proc.waitall([proc.spawn(account.stop) for account in self.accounts.itervalues()])
     notification_center.post_notification('SIPAccountManagerDidEnd', sender=self)
Ejemplo n.º 6
0
 def start(self):
     """
     Start the accounts, which will determine the ones with the enabled flag
     set to activate.
     """
     notification_center = NotificationCenter()
     notification_center.post_notification('SIPAccountManagerWillStart',
                                           sender=self)
     proc.waitall(
         [proc.spawn(account.start) for account in self.accounts.values()])
     notification_center.post_notification('SIPAccountManagerDidStart',
                                           sender=self)
Ejemplo n.º 7
0
 def _deactivate(self):
     with self._activation_lock:
         if not self._active:
             return
         notification_center = NotificationCenter()
         notification_center.post_notification('SIPAccountWillDeactivate', sender=self)
         self._active = False
         handlers = [self._registrar, self._mwi_subscriber, self._pwi_subscriber, self._dwi_subscriber,
                     self._presence_subscriber, self._self_presence_subscriber, self._dialog_subscriber,
                     self._presence_publisher, self._dialog_publisher, self.xcap_manager]
         proc.waitall([proc.spawn(handler.stop) for handler in handlers])
         notification_center.post_notification('SIPAccountDidDeactivate', sender=self)
Ejemplo n.º 8
0
    def test_wait_all_exception_order(self):
        # if there're several exceptions raised, the earliest one must be raised by wait
        def badint():
            sleep(0.1)
            int('first')

        a = proc.spawn(badint)
        b = proc.spawn(int, 'second')
        try:
            proc.waitall([a, b])
        except ValueError, ex:
            assert 'second' in str(ex), repr(str(ex))
Ejemplo n.º 9
0
 def stop(self):
     """
     Stop the accounts, which will determine the ones that were enabled to
     deactivate. This method returns only once the accounts were stopped
     successfully or they timed out trying.
     """
     notification_center = NotificationCenter()
     notification_center.post_notification('SIPAccountManagerWillEnd',
                                           sender=self)
     proc.waitall(
         [proc.spawn(account.stop) for account in self.accounts.values()])
     notification_center.post_notification('SIPAccountManagerDidEnd',
                                           sender=self)
Ejemplo n.º 10
0
 def test_wait_noerrors(self):
     x = proc.spawn(lambda: 1)
     y = proc.spawn(lambda: 2)
     z = proc.spawn(lambda: 3)
     self.assertEqual(proc.waitall([x, y, z]), [1, 2, 3])
     e = coros.event()
     x.link(e)
     self.assertEqual(e.wait(), 1)
     x.unlink(e)
     e = coros.event()
     x.link(e)
     self.assertEqual(e.wait(), 1)
     self.assertEqual([proc.waitall([X]) for X in [x, y, z]],
                      [[1], [2], [3]])
Ejemplo n.º 11
0
 def test_write_chunk(self):
     client, server = proc.waitall(self.setup_two_endpoints())
     self._test_write_chunk(client, server)
     self._test_write_chunk(server, client)
     #self.assertNoIncoming(0.1, client, server)
     client.loseConnection()
     server.loseConnection()
Ejemplo n.º 12
0
 def test_close_connection__read(self):
     client, server = proc.waitall(self.setup_two_endpoints())
     client.loseConnection()
     self.assertRaises(ConnectionDone, server.read_chunk)
     self.assertRaises(ConnectionDone, server.write, self.make_hello(server).encode())
     self.assertRaises(ConnectionDone, client.read_chunk)
     self.assertRaises(ConnectionDone, client.write, self.make_hello(client).encode())
Ejemplo n.º 13
0
 def test_write_chunk(self):
     client, server = proc.waitall(self.setup_two_endpoints())
     self._test_write_chunk(client, server)
     self._test_write_chunk(server, client)
     #self.assertNoIncoming(0.1, client, server)
     client.loseConnection()
     server.loseConnection()
Ejemplo n.º 14
0
 def test_deliver_chunk(self):
     client, server = proc.waitall(self.setup_two_endpoints())
     client, server = GreenMSRPSession(client), GreenMSRPSession(server)
     self._test_deliver_chunk(client, server)
     self._test_deliver_chunk(server, client)
     #self.assertNoIncoming(0.1, client, server)
     client.shutdown()
     server.shutdown()
Ejemplo n.º 15
0
 def test_deliver_chunk(self):
     client, server = proc.waitall(self.setup_two_endpoints())
     client, server = GreenMSRPSession(client), GreenMSRPSession(server)
     self._test_deliver_chunk(client, server)
     self._test_deliver_chunk(server, client)
     #self.assertNoIncoming(0.1, client, server)
     client.shutdown()
     server.shutdown()
Ejemplo n.º 16
0
 def test_close_connection__receive(self):
     client, server = proc.waitall(self.setup_two_endpoints())
     assert isinstance(client, MSRPTransport), repr(client)
     client, server = GreenMSRPSession(client), GreenMSRPSession(server)
     client.shutdown()
     self.assertRaises(ConnectionDone, server.receive_chunk)
     self.assertRaises(MSRPSessionError, server.send_chunk, self.make_hello(server.msrp))
     self.assertRaises(ConnectionDone, client.receive_chunk)
     self.assertRaises(MSRPSessionError, client.send_chunk, self.make_hello(client.msrp))
Ejemplo n.º 17
0
 def start(self):
     """
     Start the accounts, which will determine the ones with the enabled flag
     set to activate.
     """
     notification_center = NotificationCenter()
     notification_center.post_notification(
         'SIPAccountManagerWillStart',
         sender=self,
         data=NotificationData(bonjour_available=_bonjour.available,
                               accounts=list(
                                   a.id for a in self.accounts.values())))
     proc.waitall([
         proc.spawn(account.start)
         for account in list(self.accounts.values())
     ])
     notification_center.post_notification('SIPAccountManagerDidStart',
                                           sender=self)
Ejemplo n.º 18
0
 def _deactivate(self):
     with self._activation_lock:
         if not self._active:
             return
         notification_center = NotificationCenter()
         notification_center.post_notification('SIPAccountWillDeactivate',
                                               sender=self)
         self._active = False
         handlers = [
             self._registrar, self._mwi_subscriber, self._pwi_subscriber,
             self._dwi_subscriber, self._presence_subscriber,
             self._self_presence_subscriber, self._dialog_subscriber,
             self._presence_publisher, self._dialog_publisher,
             self.xcap_manager
         ]
         proc.waitall([proc.spawn(handler.stop) for handler in handlers])
         notification_center.post_notification('SIPAccountDidDeactivate',
                                               sender=self)
Ejemplo n.º 19
0
 def test_close_connection__read(self):
     client, server = proc.waitall(self.setup_two_endpoints())
     client.loseConnection()
     self.assertRaises(ConnectionDone, server.read_chunk)
     self.assertRaises(ConnectionDone, server.write,
                       self.make_hello(server).encode())
     self.assertRaises(ConnectionDone, client.read_chunk)
     self.assertRaises(ConnectionDone, client.write,
                       self.make_hello(client).encode())
Ejemplo n.º 20
0
 def test_reader_failed__receive(self):
     # if reader fails with an exception, receive_chunk raises that exception
     # send_chunk raises an error and the other party gets closed connection
     client, server = proc.waitall(self.setup_two_endpoints())
     client, server = GreenMSRPSession(client), GreenMSRPSession(server)
     client.reader_job.kill(InjectedError("Killing client's reader_job"))
     self.assertRaises(InjectedError, client.receive_chunk)
     self.assertRaises(MSRPSessionError, client.send_chunk, self.make_hello(client.msrp))
     self.assertRaises(ConnectionClosed, server.receive_chunk)
     self.assertRaises(MSRPSessionError, server.send_chunk, self.make_hello(server.msrp))
Ejemplo n.º 21
0
 def test_send_chunk_response_localtimeout(self):
     client, server = proc.waitall(self.setup_two_endpoints())
     client, server = GreenMSRPSession_ZeroTimeout(client), GreenMSRPSession(server)
     x = self.make_hello(client.msrp)
     self.assertRaisesCode(LocalResponse, 408, client.deliver_chunk, x)
     y = server.receive_chunk()
     self.assertSameData(x, y)
     #self.assertNoIncoming(0.1, client, server)
     server.shutdown()
     client.shutdown()
Ejemplo n.º 22
0
 def test_reader_failed__send(self):
     client, server = proc.waitall(self.setup_two_endpoints())
     client, server = GreenMSRPSession(client), GreenMSRPSession(server)
     client.reader_job.kill(InjectedError("Killing client's reader_job"))
     api.sleep(0.1)
     self.assertRaises(MSRPSessionError, client.send_chunk, self.make_hello(client.msrp))
     self.assertRaises(InjectedError, client.receive_chunk)
     api.sleep(0.1)
     self.assertRaises(MSRPSessionError, server.send_chunk, self.make_hello(server.msrp))
     self.assertRaises(ConnectionClosed, server.receive_chunk)
Ejemplo n.º 23
0
 def test_close_connection__receive(self):
     client, server = proc.waitall(self.setup_two_endpoints())
     assert isinstance(client, MSRPTransport), repr(client)
     client, server = GreenMSRPSession(client), GreenMSRPSession(server)
     client.shutdown()
     self.assertRaises(ConnectionDone, server.receive_chunk)
     self.assertRaises(MSRPSessionError, server.send_chunk,
                       self.make_hello(server.msrp))
     self.assertRaises(ConnectionDone, client.receive_chunk)
     self.assertRaises(MSRPSessionError, client.send_chunk,
                       self.make_hello(client.msrp))
Ejemplo n.º 24
0
 def test_send_chunk_response_localtimeout(self):
     client, server = proc.waitall(self.setup_two_endpoints())
     client, server = GreenMSRPSession_ZeroTimeout(
         client), GreenMSRPSession(server)
     x = self.make_hello(client.msrp)
     self.assertRaisesCode(LocalResponse, 408, client.deliver_chunk, x)
     y = server.receive_chunk()
     self.assertSameData(x, y)
     #self.assertNoIncoming(0.1, client, server)
     server.shutdown()
     client.shutdown()
Ejemplo n.º 25
0
 def test_deliver_chunk_success_report(self):
     client, server = proc.waitall(self.setup_two_endpoints())
     client, server = GreenMSRPSession(client), GreenMSRPSession(server)
     chunk = self.make_hello(client.msrp, success_report='yes')
     self._test_deliver_chunk(client, server, chunk)
     report = client.receive_chunk()
     self.assertEqual(report.method, 'REPORT')
     self.assertEqual(report.message_id, chunk.message_id)
     self.assertEqual(report.byte_range, chunk.byte_range)
     client.shutdown()
     server.shutdown()
Ejemplo n.º 26
0
 def test_deliver_chunk_success_report(self):
     client, server = proc.waitall(self.setup_two_endpoints())
     client, server = GreenMSRPSession(client), GreenMSRPSession(server)
     chunk = self.make_hello(client.msrp, success_report='yes')
     self._test_deliver_chunk(client, server, chunk)
     report = client.receive_chunk()
     self.assertEqual(report.method, 'REPORT')
     self.assertEqual(report.message_id, chunk.message_id)
     self.assertEqual(report.byte_range, chunk.byte_range)
     client.shutdown()
     server.shutdown()
Ejemplo n.º 27
0
 def test_reader_failed__send(self):
     client, server = proc.waitall(self.setup_two_endpoints())
     client, server = GreenMSRPSession(client), GreenMSRPSession(server)
     client.reader_job.kill(InjectedError("Killing client's reader_job"))
     api.sleep(0.1)
     self.assertRaises(MSRPSessionError, client.send_chunk,
                       self.make_hello(client.msrp))
     self.assertRaises(InjectedError, client.receive_chunk)
     api.sleep(0.1)
     self.assertRaises(MSRPSessionError, server.send_chunk,
                       self.make_hello(server.msrp))
     self.assertRaises(ConnectionClosed, server.receive_chunk)
Ejemplo n.º 28
0
 def test_reader_failed__receive(self):
     # if reader fails with an exception, receive_chunk raises that exception
     # send_chunk raises an error and the other party gets closed connection
     client, server = proc.waitall(self.setup_two_endpoints())
     client, server = GreenMSRPSession(client), GreenMSRPSession(server)
     client.reader_job.kill(InjectedError("Killing client's reader_job"))
     self.assertRaises(InjectedError, client.receive_chunk)
     self.assertRaises(MSRPSessionError, client.send_chunk,
                       self.make_hello(client.msrp))
     self.assertRaises(ConnectionClosed, server.receive_chunk)
     self.assertRaises(MSRPSessionError, server.send_chunk,
                       self.make_hello(server.msrp))
Ejemplo n.º 29
0
    def test_wait_error(self):
        def x():
            sleep(DELAY)
            return 1

        x = proc.spawn(x)
        z = proc.spawn(lambda: 3)
        y = proc.spawn(int, 'badint')
        y.link(x)
        x.link(y)
        y.link(z)
        z.link(y)
        self.assertRaises(ValueError, proc.waitall, [x, y, z])
        self.assertRaises(proc.LinkedFailed, proc.waitall, [x])
        self.assertEqual(proc.waitall([z]), [3])
        self.assertRaises(ValueError, proc.waitall, [y])
Ejemplo n.º 30
0
 def waitall(self):
     return proc.waitall(self.greenlets)
Ejemplo n.º 31
0
 def waitall(self):
     return proc.waitall(self.greenlets)
Ejemplo n.º 32
0
from eventlib.green import socket

# this example works with both standard eventlib hubs and with twisted-based hub
# uncomment the following line to use twisted hub
#from twisted.internet import reactor


def geturl(url):
    c = socket.socket()
    ip = socket.gethostbyname(url)
    c.connect((ip, 80))
    print '%s connected' % url
    c.send('GET /\r\n\r\n')
    return c.recv(1024)


urls = [
    'www.google.com', 'www.yandex.ru', 'www.python.org', 'ag-projects.com',
    'sylkserver.com'
]
jobs = [proc.spawn(geturl, x) for x in urls]
print 'spawned %s jobs' % len(jobs)

# collect the results from workers
results = proc.waitall(jobs)
# Note, that any exception in the workers will be reraised by waitall
# unless trap_errors argument specifies otherwise

for url, result in zip(urls, results):
    print '%s: %s' % (url, repr(result)[:50])