Exemplo n.º 1
0
 def __init__(self):
     self.data = ReplayStreamData(self._data_length, self._data_slice,
                                  self._data_bytes, self._data_view)
     self.future_data = ReplayStreamData(self._future_data_length,
                                         self._future_data_slice,
                                         self._future_data_bytes,
                                         self._future_data_view)
     self._ended = Event()
     self._header_read_or_ended = Event()
     self._new_data_or_ended = Event()
Exemplo n.º 2
0
    async def DoLogin(self):
        login_completed = Event()

        async def HandleLogin(username, password):
            result = await self.session.get("/user/login", {
                "email": username,
                "password": password
            })
            login_completed.set()

        async def HandleCreateAccountStep1(username):
            await self.get("/user/register", {"email": username})

        async def HandleCreateAccountStep2(confirmation_code, username,
                                           password, otp_secret):
            await self.get(
                "/user/register_confirm", {
                    "confirmation_code": confirmation_code,
                    "password": password,
                    "otp_secret": otp_secret
                })
            await self.session.get("/user/login", {
                "email": username,
                "password": password
            })
            login_completed.set()

        login_dialog = LoginDialog(
            None,
            HandleLogin=HandleLogin,
            HandleCreateAccountStep1=HandleCreateAccountStep1,
            HandleCreateAccountStep2=HandleCreateAccountStep2)
        login_dialog.Show()
        await login_completed.wait()
        login_dialog.Hide()
Exemplo n.º 3
0
async def test_tasklet_grouping(event_loop: AbstractEventLoop):
    no_tasks = 2
    gates = [Event() for _ in range(no_tasks)]
    statuses = [event_loop.create_future() for _ in range(no_tasks)]

    def on_done(status: Future, task: Task) -> None:
        status.set_result("cancelled" if task.cancelled() else "done")

    def group_by(_, group: int) -> int:
        return group

    @tasklet
    async def suspend(e: Event, group: int):
        await e.wait()

    suspend.group_by = group_by

    (await suspend(gates[0],
                   group=1)).add_done_callback(partial(on_done, statuses[0]))
    (await suspend(gates[1],
                   group=2)).add_done_callback(partial(on_done, statuses[1]))

    for g in gates:
        g.set()

    await wait_for(gather(*statuses), timeout=1)

    assert statuses[0].result() == "done"
    assert statuses[1].result() == "done"
Exemplo n.º 4
0
async def AsyncShowDialog(dlg):
    closed = Event()

    def end_dialog(return_code):
        dlg.SetReturnCode(return_code)
        dlg.Hide()
        closed.set()

    async def on_button(event):
        # Same code as in wxwidgets:/src/common/dlgcmn.cpp:OnButton
        # to automatically handle OK, CANCEL, APPLY,... buttons
        id = event.GetId()
        if id == dlg.GetAffirmativeId():
            if dlg.Validate() and dlg.TransferDataFromWindow():
                end_dialog(id)
        elif id == wx.ID_APPLY:
            if dlg.Validate():
                dlg.TransferDataFromWindow()
        elif id == dlg.GetEscapeId() or (id == wx.ID_CANCEL
                                         and dlg.GetEscapeId() == wx.ID_ANY):
            end_dialog(wx.ID_CANCEL)
        else:
            event.Skip()

    async def on_close(event):
        closed.set()
        dlg.Hide()

    AsyncBind(wx.EVT_CLOSE, on_close, dlg)
    AsyncBind(wx.EVT_BUTTON, on_button, dlg)
    dlg.Show()
    await closed.wait()
    return dlg.GetReturnCode()
Exemplo n.º 5
0
    def get():
        manual_end = Event(loop=event_loop)

        async def manual_wait(*args, **kwargs):
            await manual_end.wait()

        ended_wait_mock = asynctest.CoroutineMock(side_effect=manual_wait)
        return (manual_end, ended_wait_mock)
Exemplo n.º 6
0
    def get():
        manual_end = Event(loop=event_loop)

        async def manual_wait(*args, **kwargs):
            await manual_end.wait()

        blockable = asynctest.CoroutineMock(side_effect=manual_wait,
                                            _lock=manual_end)
        return blockable
Exemplo n.º 7
0
 def __init__(self, merger, sender, bookkeeper, timeout, game_id):
     self.merger = merger
     self.sender = sender
     self.bookkeeper = bookkeeper
     self._game_id = game_id
     self._connections = set()
     self._timeout = timeout
     self._ended = Event()
     asyncio.ensure_future(self._lifetime())
     self._force_close = asyncio.ensure_future(self._timeout_force_close())
Exemplo n.º 8
0
 def __init__(self, connection_producer, database, connections, replays,
              bookkeeper, prometheus_port):
     self._connection_producer = connection_producer
     self._database = database
     self._connections = connections
     self._replays = replays
     self._bookkeper = bookkeeper
     self._prometheus_port = prometheus_port
     self._stopped = Event()
     self._stopped.set()
Exemplo n.º 9
0
 async def background_update() -> None:
     async for _ in schedule(Event(), min_time=0, max_time=config.polling_rate):
         await depopulate(conn)
         try:
             async for words in tmux_words(
                 max_length=max_length, unifying_chars=unifying_chars
             ):
                 await populate(conn, words=words)
         except TmuxError as e:
             message = f"failed to fetch tmux{linesep}{e}"
             log.warn("%s", message)
 def __init__(self, stream_builder, grace_period_time, merge_strategy,
              canonical_stream):
     self._stream_builder = stream_builder
     self._stream_count = AsyncCounter()
     self._merge_strategy = merge_strategy
     self.canonical_stream = canonical_stream
     self._closing = False
     self._ended = Event()
     self._end_condition = MergerEndCondition(self._stream_count,
                                              grace_period_time)
     asyncio.ensure_future(self._lifetime())
Exemplo n.º 11
0
    def __init__(self, stream, interval, delay):
        self._stream = stream
        self._interval = interval
        self._delay = delay
        self._ended = False

        # Last item in deque size n+1 is from n intervals ago
        stamp_number = math.ceil(self._delay / self._interval) + 1
        self._stamps = deque([0], maxlen=stamp_number)
        self._new_stamp = Event()
        self._stamp_coro = asyncio.ensure_future(self._periodic_stamp())
        asyncio.ensure_future(self._wait_for_stream_end())
Exemplo n.º 12
0
 def __init__(self, merger, sender, bookkeeper, config, game_id):
     self.merger = merger
     self.sender = sender
     self.bookkeeper = bookkeeper
     self._game_id = game_id
     self._connections = set()
     self._ended = Event()
     self._lifetime_coroutines = [
         asyncio.ensure_future(self._force_closing(config.forced_end_time)),
         asyncio.ensure_future(self._write_phase(config.grace_period))
     ]
     asyncio.ensure_future(self._lifetime())
Exemplo n.º 13
0
async def test_tasklet_cancellation(event_loop: AbstractEventLoop):
    gates = [Event(), Event()]
    statuses = [event_loop.create_future(), event_loop.create_future()]

    def on_done(status: Future, task: Future) -> None:
        status.set_result("cancelled" if task.cancelled() else "done")

    @tasklet
    async def suspend(e: Event):
        await e.wait()

    (await suspend(gates[0])).add_done_callback(partial(on_done, statuses[0]))
    # Next call should cancel the previously scheduled task
    (await suspend(gates[1])).add_done_callback(partial(on_done, statuses[1]))
    # Opening the gate should complete the newly scheduled task
    gates[1].set()

    await wait_for(gather(*statuses), timeout=1)

    assert statuses[0].result() == "cancelled"
    assert statuses[1].result() == "done"
Exemplo n.º 14
0
    def __init__(self, config, type, store, loop=None):
        super(BaseAccessor, self).__init__()
        self.config = config
        self.type = type
        self._store = weakref.ref(store)
        self.loop = loop
        self.logger = logging.getLogger('accessor[{}]'.format(self.type))

        self.connected = False
        self.connecting = False
        self.disconnecting = False
        self._connected_event = Event(loop=self.loop)
        self._host = None
        self._port = None
        self._username = None
        self._password = None

        if self.CHECK_CONFIG:
            self.check_config()
Exemplo n.º 15
0
async def AsyncShowDialog(dlg):
    if type(dlg) in [
            wx.FileDialog, wx.DirDialog, wx.FontDialog, wx.ColourDialog,
            wx.MessageDialog
    ]:
        raise Exception(
            "This type of dialog cannot be shown modless, please use 'AsyncShowDialogModal'"
        )
    closed = Event()

    def end_dialog(return_code):
        dlg.SetReturnCode(return_code)
        dlg.Hide()
        closed.set()

    async def on_button(event):
        # Same code as in wxwidgets:/src/common/dlgcmn.cpp:OnButton
        # to automatically handle OK, CANCEL, APPLY,... buttons
        id = event.GetId()
        if id == dlg.GetAffirmativeId():
            if dlg.Validate() and dlg.TransferDataFromWindow():
                end_dialog(id)
        elif id == wx.ID_APPLY:
            if dlg.Validate():
                dlg.TransferDataFromWindow()
        elif id == dlg.GetEscapeId() or (id == wx.ID_CANCEL
                                         and dlg.GetEscapeId() == wx.ID_ANY):
            end_dialog(wx.ID_CANCEL)
        else:
            event.Skip()

    async def on_close(event):
        closed.set()
        dlg.Hide()

    AsyncBind(wx.EVT_CLOSE, on_close, dlg)
    AsyncBind(wx.EVT_BUTTON, on_button, dlg)
    dlg.Show()
    await closed.wait()
    return dlg.GetReturnCode()
Exemplo n.º 16
0
async def merge(*aits: AsyncIterable[_T]) -> AsyncIterator[_T]:
    ev = Event()
    q: Queue = Queue(maxsize=1)

    end = create_task(ev.wait())
    g = gather(*(_merge_helper(q, end=end, ait=ait) for ait in aits))

    try:
        while True:
            fut = create_task(q.get())
            done, _ = await wait((g, fut), return_when=FIRST_COMPLETED)
            if fut in done:
                item = await fut
                yield item

            if g in done:
                break
    except CancelledError:
        await cancel(g)
    finally:
        ev.set()
        await g
Exemplo n.º 17
0
async def test_producer_sanity_check(unused_tcp_port):
    check_ran = Event()

    async def check(connection):
        data_in = await connection.readexactly(3)
        assert data_in == b"foo"
        is_open = await connection.write(b"bar")
        assert is_open
        connection.close()
        await connection.wait_closed()
        check_ran.set()

    prod = ConnectionProducer(check, unused_tcp_port, 0.1)
    await prod.start()

    r, w = await asyncio.open_connection('127.0.0.1', unused_tcp_port)
    w.write(b"foo")
    await w.drain()
    data = await r.read()
    assert data == b"bar"
    w.close()

    await check_ran.wait()
    await prod.stop()
 def __init__(self):
     self._header_read_or_ended = Event()
 def __init__(self):
     self._ended = Event()
 def __init__(self):
     self._new_data_or_ended = Event()
Exemplo n.º 21
0
 def __init__(self):
     EmptyWaitMixin.__init__(self)
     self._dict = {}
     self._added = Event()
Exemplo n.º 22
0
 def __init__(self):
     self._denies_connections = Event()
     self._connection_count = AsyncCounter()
     self._ended = Event()
     asyncio.ensure_future(self._lifetime())
Exemplo n.º 23
0
 def __init__(self) -> None:
     self._onclose = Event()
Exemplo n.º 24
0
 def __init__(self) -> None:
     self._counter = 0
     self._event = Event()
     self._err: Optional[BaseException] = None
Exemplo n.º 25
0
 def __init__(self, delayed_stream):
     self._stream = delayed_stream
     self._conn_count = AsyncCounter()
     self._ended = Event()
     asyncio.ensure_future(self._lifetime())
Exemplo n.º 26
0
 def __init__(self, b: Buf[T]) -> None:
     super().__init__()
     self._b = b
     self._sendable_ev, self._recvable_ev = Event(), Event()
     self._sendable_ev.set()
Exemplo n.º 27
0
class MQTTRPC(MQTTClient):
    client_uid = CLIENT_UID
    cleansession = True
    mqtt_reply_timeout = MQTT_REPLY_TIMEOUT
    mqtt_url = MQTT_URL
    request_count = 0
    rpc_replies = {}
    replied = Event(
    )  # This event is triggered every time a new reply has come.
    subscriptions = [
    ]  # We hold a list of our subscriptions not to subscribe to

    # every request to the same client.

    def __init__(self, mqtt_url=None, client_uid=None, loop=None, config=None):
        if not loop:
            loop = asyncio.get_event_loop()
        self.loop = loop
        self.protocol = JSONRPCProtocol()
        self.dispatcher = dispatcher
        self.config = config
        if mqtt_url:
            self.mqtt_url = mqtt_url
        if client_uid:
            self.client_uid = client_uid
        super(MQTTRPC, self).__init__(client_id=self.client_uid,
                                      loop=loop,
                                      config=self.config)
        for signame in ('SIGINT', 'SIGTERM'):
            self.loop.add_signal_handler(
                getattr(signal, signame),
                lambda: asyncio.ensure_future(self.stop()))

        logger.info('Client {} initialized'.format(self.client_uid))

    async def stop(self):
        logger.info('Stopping mqttrpc...')
        # Check subscriptions
        if self._connected_state.is_set():
            await self.unsubscribe(self.subscriptions)
            await self.disconnect()
        tasks = [
            task for task in asyncio.Task.all_tasks()
            if task is not asyncio.tasks.Task.current_task()
        ]
        list(map(lambda task: task.cancel(), tasks))
        results = await asyncio.gather(*tasks, return_exceptions=True)
        logger.debug('Finished cancelling tasks, result: {}'.format(results))
        self.loop.stop()

    async def process_messages(self):
        self.mqtt_url = self.config.get('broker', {}).get('uri', self.mqtt_url)
        logger.info('Connecting to {}'.format(self.mqtt_url))
        await self.connect(self.mqtt_url, cleansession=self.cleansession)
        logger.info('Connected.')
        await self.subscribe([
            ('rpc/{}/+'.format(self.client_uid), QOS_2),
        ])
        logger.debug('Starting process messages.')
        while True:
            try:
                self.loop.create_task(
                    self.process_message(await self.deliver_message()))
            except asyncio.CancelledError:
                return

    async def process_message(self, message):
        logger.debug('Message at topic {}'.format(message.topic))

        if re.search('^rpc/(\w+)/(\w+)$', message.topic):
            # RPC request
            logger.debug('RPC request at {}'.format(message.topic))
            _, _, context = message.topic.split('/')
            data_str = message.data.decode()
            await self.receive_rpc_request(context, data_str)

        elif re.search('^rpc/(\w+)/(\w+)/reply$', message.topic):
            # RPC reply
            logger.debug('RPC reply at {}'.format(message.topic))
            _, _, context, _ = message.topic.split('/')
            data_str = message.data.decode()
            waiting_replies = self.rpc_replies.get(message.topic)
            if not waiting_replies:
                logger.warning(
                    'Got an unexpected RPC reply from {}: {}'.format(
                        message.topic, data_str))
            else:
                try:
                    data_js = json.loads(data_str)
                except json.decoder.JSONDecodeError:
                    logger.error(
                        'RPC reply bad json data: {}'.format(data_str))
                else:
                    request_id = data_js.get('id')
                    if request_id not in waiting_replies.keys():
                        logger.warning(
                            'Got a reply from {} to bad request id: {}'.format(
                                message.topic, data_str))
                    else:
                        # Finally matched the request by id
                        logger.debug(
                            'Waiting reply found for request {}'.format(
                                request_id))
                        await waiting_replies[request_id].put(data_str)
        else:
            logger.debug('Passing to on_message handler')
            await self.on_message(message)

    async def on_message(self, message):
        # Override it to implement other handlres.
        logger.debug('Not implemented')

    async def receive_rpc_request(self, context, data):
        logger.debug('Request: {}'.format(data))
        self.request_count += 1
        if type(data) != str:
            data = json.dumps(data)

        message = data

        async def handle_message(context, message):
            try:
                request = self.protocol.parse_request(message)
            except RPCError as e:
                response = e.error_respond()
            else:
                # Hack to add first params as self
                if not self in request.args:
                    request.args.insert(0, self)
                response = await self.dispatcher.dispatch(
                    request, getattr(self.protocol, '_caller', None))

            # send reply
            if response is not None:
                result = response.serialize()
                logger.debug('RPC reply to {}: {}'.format(context, result))
                self.loop.create_task(
                    self.publish(
                        'rpc/{}/{}/reply'.format(self.client_uid, context),
                        result.encode()))

        await handle_message(context, message)

    def get_proxy_for(self, destination, one_way=False):
        return RPCProxy(self, destination, one_way)

    async def _send_and_handle_reply(self,
                                     destination,
                                     req,
                                     one_way,
                                     no_exception=False):
        # Convert to bytes and send to destination
        if one_way:
            # We do not need a reply it's a notification call
            await self.publish(
                'rpc/{}/{}'.format(destination, self.client_uid),
                req.serialize().encode())
            return

        # We need a reply
        reply_topic = ('rpc/{}/{}/reply'.format(destination, self.client_uid))
        self.rpc_replies.setdefault(reply_topic, {})[req.unique_id] = Queue()
        if reply_topic not in self.subscriptions:
            logger.debug(
                'Adding subscrption to reply topic {}'.format(reply_topic))
            self.subscriptions.append(reply_topic)
            await self.subscribe([(reply_topic, QOS_2)])
            logger.debug('Subscribed to reply topic {}'.format(reply_topic))
        else:
            logger.debug('Already subscribed for topic {}'.format(reply_topic))
        await self.publish('rpc/{}/{}'.format(destination, self.client_uid),
                           req.serialize().encode())
        logger.debug('Published request id {} to {}'.format(
            req.unique_id, destination))
        try:
            reply_data = await asyncio.wait_for(
                self.rpc_replies[reply_topic][req.unique_id].get(),
                self.mqtt_reply_timeout)
            self.rpc_replies[reply_topic][req.unique_id].task_done()

        except asyncio.TimeoutError:
            del self.rpc_replies[reply_topic][req.unique_id]
            raise RPCError(
                'Reply Timeout, topic {}, id {}, method {}, args {}, kwargs {}'
                .format(reply_topic, req.unique_id, req.method, req.args,
                        req.kwargs))

        else:
            # We got a reply, handle it.
            logger.debug('Got a reply for request id: {}'.format(
                req.unique_id))
            rpc_response = self.protocol.parse_reply(reply_data)
            del self.rpc_replies[reply_topic][req.unique_id]
            # Check that there is no RPC errors.
            if not no_exception and hasattr(rpc_response, 'error'):
                raise RPCError('Error calling remote procedure: %s' %\
                               rpc_response.error)
            return rpc_response

    async def _call(self, destination, method, args, kwargs, one_way=False):
        req = self.protocol.create_request(method, args, kwargs, one_way)
        rep = await self._send_and_handle_reply(destination, req, one_way)
        if one_way:
            return
        return rep.result
Exemplo n.º 28
0
 def __init__(self):
     self._empty = Event()
     self._not_empty = Event()
     self._is_empty()
 def __init__(self, count, grace_period):
     self._count = count
     self._grace_period = grace_period
     self._force_end = Event()