def test_multiple_listeners_error(self): # if there was an error while calling a callback # it should not prevent the other listeners from being called # also, all of the errors should be logged, check the output # manually that they are p = proc.spawn(lambda: 5) results = [] def listener1(*args): results.append(10) 1 / 0 def listener2(*args): results.append(20) 2 / 0 def listener3(*args): 3 / 0 p.link(listener1) p.link(listener2) p.link(listener3) sleep(DELAY * 10) assert results in [[10, 20], [20, 10]], results p = proc.spawn(int, 'hello') results = [] p.link(listener1) p.link(listener2) p.link(listener3) sleep(DELAY * 10) assert results in [[10, 20], [20, 10]], results
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()
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()
def test_2_servers_same_port(self): server = self.get_server() server_uri_1 = server.prepare(self.get_server_uri()) server_uri_2 = server.prepare(self.get_server_uri()) assert len(server.ports) == 1, server.ports assert len(list(server.ports.values())[0]) == 1, server.ports connector = self.get_connector() client1_full_local_path = connector.prepare() server_transport_event = TimeoutEvent() proc.spawn(server.complete, client1_full_local_path).link(server_transport_event) client1_transport = connector.complete(server_uri_1) server_transport = server_transport_event.wait() self._test_write_chunk(client1_transport, server_transport) self._test_write_chunk(server_transport, client1_transport) client1_transport.loseConnection() server_transport.loseConnection() client2_full_local_path = connector.prepare() server_transport_event = TimeoutEvent() proc.spawn(server.complete, client2_full_local_path).link(server_transport_event) client2_transport = connector.complete(server_uri_2) server_transport = server_transport_event.wait() self._test_write_chunk(client2_transport, server_transport) self._test_write_chunk(server_transport, client2_transport) client2_transport.loseConnection() server_transport.loseConnection()
def start(self): notification_center = NotificationCenter() notification_center.add_observer(self, name='NetworkConditionsDidChange') self._select_proc = proc.spawn(self._process_files) proc.spawn(self._handle_commands) self._activate()
def start(self): notification_center = NotificationCenter() notification_center.add_observer(self, name='SystemIPAddressDidChange') notification_center.add_observer(self, name='SystemDidWakeUpFromSleep') self._select_proc = proc.spawn(self._process_files) proc.spawn(self._handle_commands) self._activate()
def test_2_servers_same_port(self): server = self.get_server() server_uri_1 = server.prepare(self.get_server_uri()) server_uri_2 = server.prepare(self.get_server_uri()) assert len(server.ports)==1, server.ports assert len(server.ports.values()[0])==1, server.ports connector = self.get_connector() client1_full_local_path = connector.prepare() server_transport_event = TimeoutEvent() proc.spawn(server.complete, client1_full_local_path).link(server_transport_event) client1_transport = connector.complete(server_uri_1) server_transport = server_transport_event.wait() self._test_write_chunk(client1_transport, server_transport) self._test_write_chunk(server_transport, client1_transport) client1_transport.loseConnection() server_transport.loseConnection() client2_full_local_path = connector.prepare() server_transport_event = TimeoutEvent() proc.spawn(server.complete, client2_full_local_path).link(server_transport_event) client2_transport = connector.complete(server_uri_2) server_transport = server_transport_event.wait() self._test_write_chunk(client2_transport, server_transport) self._test_write_chunk(server_transport, client2_transport) client2_transport.loseConnection() server_transport.loseConnection()
def start(self): notification_center = NotificationCenter() notification_center.add_observer(self, name='NetworkConditionsDidChange') proc.spawn(self._run) notification_center.post_notification( 'X2SMucInvitationHandlerDidStart', sender=self)
def test_proc(self): p = proc.spawn(lambda: 100) receiver = proc.spawn(api.sleep, 1) p.link(receiver) self.assertRaises(proc.LinkedCompleted, receiver.wait) receiver2 = proc.spawn(api.sleep, 1) p.link(receiver2) self.assertRaises(proc.LinkedCompleted, receiver2.wait)
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
def start(self): notification_center = NotificationCenter() notification_center.add_observer(self, name='SystemIPAddressDidChange') notification_center.add_observer(self, name='SystemDidWakeUpFromSleep') self._select_proc = proc.spawn(self._process_files) proc.spawn(self._handle_commands) # activate self._stopped = False self._command_channel.send(Command('discover'))
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))
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]])
def _start_vnc_connection(self): try: self.vnc_socket = GreenSocket(tcp_socket()) self.vnc_socket.settimeout(self.connect_timeout) self.vnc_socket.connect(self.address) self.vnc_socket.settimeout(None) except Exception as e: self.vnc_starter_thread = None # avoid issues caused by the notification handler killing this greenlet during post_notification NotificationCenter().post_notification('ScreenSharingHandlerDidFail', sender=self, data=NotificationData(context='connecting', reason=str(e))) else: self.msrp_reader_thread = spawn(self._msrp_reader) self.msrp_writer_thread = spawn(self._msrp_writer) finally: self.vnc_starter_thread = None
def __init__(self, protocol): self.account = DefaultAccount() self._protocol = protocol self._id = None self._local_identity = None self._remote_identity = None self._local_jid = None self._remote_jid = None self._channel = coros.queue() self._current_operation = None self._proc = proc.spawn(self._run) self._timer = None self._sdp_negotiator = None self._pending_transport_info_stanzas = [] self.direction = None self.state = None self.streams = None self.proposed_streams = None self.start_time = None self.end_time = None self.on_hold = False self.local_focus = False self.candidates = set()
def execute(self, func, *args, **kwargs): """Execute func in one of the coroutines maintained by the pool, when one is free. Immediately returns a Proc object which can be queried for the func's result. >>> pool = Pool() >>> task = pool.execute(lambda a: ('foo', a), 1) >>> task.wait() ('foo', 1) """ # if reentering an empty pool, don't try to wait on a coroutine freeing # itself -- instead, just execute in the current coroutine if self.sem.locked() and api.getcurrent() in self.procs: p = proc.spawn(func, *args, **kwargs) try: p.wait() except: pass else: self.sem.acquire() p = self.procs.spawn(func, *args, **kwargs) # assuming the above line cannot raise p.link(lambda p: self.sem.release()) if self.results is not None: p.link(self.results) return p
def set_links(self, p, first_time, kill_exc_type): event = coros.event() self.link(p, event) proc_flag = [] def receiver(): sleep(DELAY) proc_flag.append('finished') receiver = proc.spawn(receiver) self.link(p, receiver) queue = coros.queue(1) self.link(p, queue) try: self.link(p) except kill_exc_type: if first_time: raise else: assert first_time, 'not raising here only first time' callback_flag = ['initial'] self.link(p, lambda *args: callback_flag.remove('initial')) for _ in range(10): self.link(p, coros.event()) self.link(p, coros.queue(1)) return event, receiver, proc_flag, queue, callback_flag
def __init__(self, protocol): self.account = DefaultAccount() self._protocol = protocol self._id = None self._local_identity = None self._remote_identity = None self._local_jid = None self._remote_jid = None self._channel = coros.queue() self._current_operation = None self._proc = proc.spawn(self._run) self._timer = None self._sdp_negotiator = None self._pending_transport_info_stanzas = [] self.direction = None self.state = None self.streams = None self.proposed_streams = None self.start_time = None self.end_time = None self.on_hold = False self.local_focus = False
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])
def start(self): notification_center = NotificationCenter() self._xmpp_subscription = XMPPIncomingSubscription(local_identity=self.sip_identity, remote_identity=self.xmpp_identity) notification_center.add_observer(self, sender=self._xmpp_subscription) self._xmpp_subscription.start() self._command_proc = proc.spawn(self._run) self._subscribe_sip() notification_center.post_notification('X2SPresenceHandlerDidStart', sender=self)
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)
def _CH_subscribe(self, command): if self._subscription_timer is not None and self._subscription_timer.active(): self._subscription_timer.cancel() self._subscription_timer = None if self._subscription_proc is not None: subscription_proc = self._subscription_proc subscription_proc.kill(InterruptSubscription) subscription_proc.wait() self._subscription_proc = proc.spawn(self._subscription_handler, command)
def __init__(self, msrptransport, accept_types=['*'], on_incoming_cb=None, automatic_reports=True): self.msrp = msrptransport self.accept_types = accept_types self.automatic_reports = automatic_reports if not callable(on_incoming_cb): raise TypeError('on_incoming_cb must be callable: %r' % on_incoming_cb) self._on_incoming_cb = on_incoming_cb self.expected_responses = {} self.outgoing = coros.queue() self.reader_job = proc.spawn(self._reader) self.writer_job = proc.spawn(self._writer) self.state = 'CONNECTED' # -> 'FLUSHING' -> 'CLOSING' -> 'DONE' # in FLUSHING writer sends only while there's something in the outgoing queue # then it exits and sets state to 'CLOSING' which makes reader only pay attention # to responses and success reports. (XXX it could now discard incoming data chunks # with direct write() since writer is dead) self.reader_job.link(self.writer_job) self.last_expected_response = 0 self.keepalive_proc = proc.spawn(self._keepalive)
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)
def test_return(self): def return25(): return 25 p = self.p = proc.spawn(return25) self._test_return(p, True, 25, proc.LinkedCompleted, lambda: sleep(0)) # repeating the same with dead process for _ in xrange(3): self._test_return(p, False, 25, proc.LinkedCompleted, lambda: sleep(0))
def test_event(self): p = proc.spawn(lambda: 100) event = coros.event() p.link(event) self.assertEqual(event.wait(), 100) for i in xrange(3): event2 = coros.event() p.link(event2) self.assertEqual(event2.wait(), 100)
def start(self, welcome_prompt=True): notification_center = NotificationCenter() notification_center.add_observer(self, sender=self.session) self.proc = proc.spawn(self.play_audio_welcome, welcome_prompt) self.proc.wait() notification_center.remove_observer(self, sender=self.session) self.session = None self.room = None self.proc = None
def start(self): if self.started: return self.started = True notification_center = NotificationCenter() notification_center.add_observer(self, sender=self) notification_center.post_notification(self.__class__.__name__ + 'WillStart', sender=self) notification_center.add_observer(self, name='NetworkConditionsDidChange') self._command_proc = proc.spawn(self._run) notification_center.post_notification(self.__class__.__name__ + 'DidStart', sender=self) notification_center.remove_observer(self, sender=self)
def onOpen(self): self.factory.connections.add(self) self.accounts_map = {} self.account_handles_map = {} self.sessions_map = {} self.session_handles_map = {} self.ready_event = GreenEvent() self.resolver = DNSLookup() self.proc = proc.spawn(self._operations_handler) self.operations_queue = coros.queue() self._create_janus_session()
def start(self): if self.started: return self.started = True notification_center = NotificationCenter() notification_center.add_observer(self, name='CFGSettingsObjectDidChange', sender=self.account) notification_center.add_observer(self, name='CFGSettingsObjectDidChange', sender=SIPSimpleSettings()) notification_center.add_observer(self, name='NetworkConditionsDidChange') self._command_proc = proc.spawn(self._run) if self.account.sip.register: self.activate()
def start(self): if self.state != 'stopped': return config = get_room_configuration(self.uri.split('@')[0]) factory = IRCBotFactory(config) host, port = config.server self.irc_connector = reactor.connectTCP(host, port, factory) NotificationCenter().add_observer(self, sender=self.irc_connector.factory) self.message_dispatcher = proc.spawn(self._message_dispatcher) self.audio_conference = AudioConference() self.audio_conference.hold() self.state = 'started'
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)
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)
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)
def _NH_SIPSessionDidRenegotiateStreams(self, notification): session = notification.sender for stream in notification.data.added_streams: log.msg('Session %s added %s' % (session.call_id, stream.type)) for stream in notification.data.removed_streams: log.msg('Session %s removed %s' % (session.call_id, stream.type)) if notification.data.added_streams and self.proc is None: self.proc = proc.spawn(self._play) if notification.data.removed_streams and not session.streams: session.end()
def set_links_timeout(self, link): # stuff that won't be touched event = coros.event() link(event) proc_finished_flag = [] def myproc(): sleep(10) proc_finished_flag.append('finished') return 555 myproc = proc.spawn(myproc) link(myproc) queue = coros.queue(0) link(queue) return event, myproc, proc_finished_flag, queue
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)
def start(self): self._proc = proc.spawn(self._run) self._channel.send(Command('probe_dns'))
def _NH_MediaStreamDidStart(self, notification): self.message_queue_thread = spawn(self._message_queue_handler)
def start(self): NotificationCenter().post_notification('XMPPChatSessionDidStart', sender=self) self._proc = proc.spawn(self._run) self.state = 'started'
def _do_spawn(self, gtransport, protocol): g = proc.spawn(self._run_handler, gtransport, protocol) self.greenlets.add(g) g.link(lambda *_: self.greenlets.remove(g))
def _NH_SIPSessionDidStart(self, notification): session = notification.sender log.msg('Session %s started' % session.call_id) self.proc = proc.spawn(self._play)
def start(self): NotificationCenter().post_notification("XMPPIncomingMucSessionDidStart", sender=self) self._proc = proc.spawn(self._run) self.state = "started"
def start(self): NotificationCenter().post_notification('XMPPSubscriptionDidStart', sender=self) self._proc = proc.spawn(self._run) self.subscribe()