Ejemplo n.º 1
0
            def _attempt_connect(**kwargs):
                ignore = kwargs.pop('ignore', [])
                try:
                    # Attempt to connect to automatically selected port.
                    dropbot = db.SerialProxy(port=port,
                                             ignore=ignore,
                                             **kwargs)
                    raise asyncio.Return(dropbot)
                except db.proxy.NoPower as exception:
                    # No 12V power supply detected on DropBot.
                    _L().debug('No 12V power supply detected.')
                    responses = signals.signal('no-power').send('keep_alive')

                    for t in asyncio.as_completed(
                        [loop.create_task(r[1]) for r in responses]):
                        response = yield asyncio.From(t)
                        if response == 'ignore':
                            ignore.append(db.proxy.NoPower)
                            break
                    else:
                        raise exception
                except bnr.proxy.DeviceVersionMismatch as exception:
                    # Firmware version does not match driver version.
                    _L().debug(
                        'Driver version (`%s`) does not match firmware '
                        'version (`%s`)', db.__version__,
                        exception.device_version)
                    responses = signals.signal('version-mismatch')\
                        .send('keep_alive', driver_version=db.__version__,
                            firmware_version=exception.device_version)

                    update = False

                    for t in asyncio.as_completed(
                        [loop.create_task(r[1]) for r in responses]):
                        response = yield asyncio.From(t)
                        if response == 'ignore':
                            ignore.append(bnr.proxy.DeviceVersionMismatch)
                            break
                        elif response == 'update':
                            update = True
                            break
                    else:
                        raise

                    if update:
                        # Flash firmware and retry connection.
                        _L().info('Flash firmware and retry connection.')
                        yield asyncio.From(co_flash_firmware())

                dropbot = yield asyncio.From(
                    _attempt_connect(ignore=ignore, **kwargs))
                raise asyncio.Return(dropbot)
Ejemplo n.º 2
0
    def _connect_dual_stack(self, primary_address, secondary_address):
        '''Connect using happy eyeballs.'''
        self._primary_connection = self._connection_factory(primary_address)
        self._secondary_connection = self._connection_factory(
            secondary_address)

        @trollius.coroutine
        def connect_primary():
            yield From(self._primary_connection.connect())
            raise Return(self._primary_connection)

        @trollius.coroutine
        def connect_secondary():
            yield From(self._secondary_connection.connect())
            raise Return(self._secondary_connection)

        primary_fut = connect_primary()
        secondary_fut = connect_secondary()

        failed = False

        for fut in trollius.as_completed((primary_fut, secondary_fut)):
            if not self._active_connection:
                try:
                    self._active_connection = yield From(fut)
                except NetworkError:
                    if not failed:
                        _logger.debug('Original dual stack exception',
                                      exc_info=True)
                        failed = True
                    else:
                        raise
                else:
                    _logger.debug('Got first of dual stack.')

            else:

                @trollius.coroutine
                def cleanup():
                    try:
                        conn = yield From(fut)
                    except NetworkError:
                        pass
                    else:
                        conn.close()
                    _logger.debug('Closed abandoned connection.')

                trollius.get_event_loop().create_task(cleanup())

        if self._active_connection.address == secondary_address:
            preferred_addr = secondary_address
        else:
            preferred_addr = primary_address

        self._happy_eyeballs_table.set_preferred(preferred_addr,
                                                 primary_address,
                                                 secondary_address)
Ejemplo n.º 3
0
    def _connect_dual_stack(self, primary_address, secondary_address):
        '''Connect using happy eyeballs.'''
        self._primary_connection = self._connection_factory(primary_address)
        self._secondary_connection = self._connection_factory(secondary_address)

        @trollius.coroutine
        def connect_primary():
            yield From(self._primary_connection.connect())
            raise Return(self._primary_connection)

        @trollius.coroutine
        def connect_secondary():
            yield From(self._secondary_connection.connect())
            raise Return(self._secondary_connection)

        primary_fut = connect_primary()
        secondary_fut = connect_secondary()

        failed = False

        for fut in trollius.as_completed((primary_fut, secondary_fut)):
            if not self._active_connection:
                try:
                    self._active_connection = yield From(fut)
                except NetworkError:
                    if not failed:
                        _logger.debug('Original dual stack exception', exc_info=True)
                        failed = True
                    else:
                        raise
                else:
                    _logger.debug('Got first of dual stack.')

            else:
                @trollius.coroutine
                def cleanup():
                    try:
                        conn = yield From(fut)
                    except NetworkError:
                        pass
                    else:
                        conn.close()
                    _logger.debug('Closed abandoned connection.')

                trollius.get_event_loop().create_task(cleanup())

        if self._active_connection.address == secondary_address:
            preferred_addr = secondary_address
        else:
            preferred_addr = primary_address

        self._happy_eyeballs_table.set_preferred(preferred_addr, primary_address, secondary_address)
Ejemplo n.º 4
0
def watcher(tasks, delay=False):
    res = []
    for t in asyncio.as_completed(tasks):
        r = yield From(t)
        res.append(r)
        if delay:
            # simulate processing delay
            process_time = random.random() / 10
            yield From(asyncio.sleep(process_time))
    #print(res)
    #assert(sorted(res) == res)
    if sorted(res) != res:
        print('FAIL', res)
        print('------------')
    else:
        print('.', end='')
        sys.stdout.flush()
Ejemplo n.º 5
0
def watcher(tasks,delay=False):
    res = []
    for t in asyncio.as_completed(tasks):
        r = yield From(t)
        res.append(r)
        if delay:
            # simulate processing delay
            process_time = random.random() / 10
            yield From(asyncio.sleep(process_time))
    #print(res)
    #assert(sorted(res) == res)
    if sorted(res) != res:
        print('FAIL', res)
        print('------------')
    else:
        print('.', end='')
        sys.stdout.flush()
Ejemplo n.º 6
0
    def test_as_completed(self):
        fut = asyncio.Future()
        fut.set_result("ok")

        with self.assertRaises(TypeError):
            for f in trollius.as_completed(fut):
                pass

        @trollius.coroutine
        def get_results(fut):
            results = []
            for f in trollius.as_completed([fut]):
                res = yield trollius.From(f)
                results.append(res)
            raise trollius.Return(results)

        results = self.loop.run_until_complete(get_results(fut))
        self.assertEqual(results, ["ok"])
Ejemplo n.º 7
0
 def collectResults(self, allData):
     try:
         for record in allData:
             parentDoc = {k:v for (k,v) in record.items() if k not in [u'_id', u'value']} 
             aDataVal = str(record[u'value'])
             keyTs = aDataVal[:8]
             keyDataName = Name('/ndn/ucla.edu/bms/melnitz/kds').append(keyTs).append(self.keyId)
             yield From(self._decryptAndPrintRecord(aDataVal, keyDataName, parentDoc))
         receivedVals = []
         try:
             for i in asyncio.as_completed([
                 self.resultQueue.get() for n in range(len(allData))], timeout=5):
                 v = yield From(i)
                 receivedVals.append(v)
         except asyncio.TimeoutError:
             pass
         self.prettifyResults(receivedVals)
         print
     finally:
         self.dataReady.set()
Ejemplo n.º 8
0
 def get_results(fut):
     results = []
     for f in trollius.as_completed([fut]):
         res = yield trollius.From(f)
         results.append(res)
     raise trollius.Return(results)