示例#1
0
 def _do_broker_identity_cache(dht_record, position, broker_result):
     one_broker_task = identitycache.GetLatest(
         id_url.to_bin(dht_record['broker_idurl']))
     one_broker_task.addCallback(
         lambda xmlsrc: broker_result.callback(dht_record))
     one_broker_task.addErrback(_on_borker_identity_cache_failed, position,
                                broker_result)
     # if _Debug:
     #     lg.args(_DebugLevel, position=position, broker_idurl=dht_record['broker_idurl'])
     return None
示例#2
0
 def _do_broker_identity_cache(dht_record, position, broker_result):
     one_broker_task = identitycache.GetLatest(dht_record['broker_idurl'])
     if _Debug:
         one_broker_task.addErrback(
             lg.errback,
             debug=_Debug,
             debug_level=_DebugLevel,
             method='read_customer_message_brokers._do_broker_identity_cache'
         )
     one_broker_task.addCallback(
         lambda xmlsrc: broker_result.callback(dht_record))
     return None
示例#3
0
def do_update_contacts():
    global _Contacts
    if _Debug:
        lg.out(_DebugLevel, 'http_node.update_contacts')
    _Contacts.clear()

    for idurl in contactsdb.contacts(include_all=True):
        lg.out(10, 'http_node.update_contacts want ' + idurl)
        if idurl == my_id.getIDURL():
            continue
        latest_identity = identitycache.GetLatest(idurl)
        if isinstance(latest_identity, Deferred):
            latest_identity.addCallback(lambda src: push_contact(idurl))
            latest_identity.addErrback(lambda err: lg.out(
                _DebugLevel, 'http_node.update_contacts "%s" failed to cache' % idurl) if _Debug else None)
        else:
            push_contact(idurl)
示例#4
0
def read_customer_message_brokers(customer_idurl,
                                  positions=[
                                      0,
                                  ],
                                  return_details=True,
                                  as_fields=True,
                                  use_cache=True):
    if _Debug:
        lg.args(_DebugLevel,
                customer_idurl=customer_idurl,
                use_cache=use_cache,
                positions=positions)
    if as_fields:
        customer_idurl = id_url.field(customer_idurl)
    else:
        customer_idurl = id_url.to_bin(customer_idurl)
    result = Deferred()

    def _on_borker_identity_cache_failed(err, position, broker_result):
        if _Debug:
            lg.args(_DebugLevel, position=position, err=err)
        broker_result.callback({
            'timestamp': None,
            'revision': 0,
            'customer_idurl': customer_idurl,
            'broker_idurl': None,
            'position': position,
        })
        return None

    def _do_broker_identity_cache(dht_record, position, broker_result):
        one_broker_task = identitycache.GetLatest(
            id_url.to_bin(dht_record['broker_idurl']))
        one_broker_task.addCallback(
            lambda xmlsrc: broker_result.callback(dht_record))
        one_broker_task.addErrback(_on_borker_identity_cache_failed, position,
                                   broker_result)
        # if _Debug:
        #     lg.args(_DebugLevel, position=position, broker_idurl=dht_record['broker_idurl'])
        return None

    def _do_verify(dht_value, position, broker_result):
        ret = {
            'timestamp': None,
            'revision': 0,
            'customer_idurl': customer_idurl,
            'broker_idurl': None,
            'position': position,
        }
        if not dht_value or not isinstance(dht_value, dict):
            if _Debug:
                lg.args(_DebugLevel,
                        c=customer_idurl,
                        p=position,
                        dht_value=type(dht_value))
            broker_result.callback(ret)
            return ret
        try:
            if as_fields:
                _customer_idurl = id_url.field(dht_value['customer_idurl'])
                _broker_idurl = id_url.field(dht_value['broker_idurl'])
            else:
                _customer_idurl = id_url.to_bin(dht_value['customer_idurl'])
                _broker_idurl = id_url.to_bin(dht_value['broker_idurl'])
            _position = int(dht_value['position'])
            _revision = int(dht_value.get('revision'))
            _timestamp = int(dht_value.get('timestamp'))
        except:
            lg.exc()
            broker_result.callback(ret)
            return ret
        if _Debug:
            lg.args(_DebugLevel, p=position, b=_broker_idurl, r=_revision)
        if as_fields:
            if _customer_idurl != customer_idurl:
                lg.err(
                    'wrong customer idurl %r in message broker DHT record for %r at position %d'
                    % (_customer_idurl, customer_idurl, position))
                broker_result.callback(ret)
                return ret
        if position != _position:
            lg.err(
                'wrong position value %d in message broker DHT record for %r at position %d'
                % (_position, customer_idurl, position))
            broker_result.callback(ret)
            return ret
        ret.update({
            'customer_idurl': _customer_idurl,
            'broker_idurl': _broker_idurl,
            'position': _position,
            'revision': _revision,
            'timestamp': _timestamp,
        })
        _do_broker_identity_cache(ret, position, broker_result)
        return None

    def _on_error(err, position, broker_result):
        try:
            msg = err.getErrorMessage()
        except:
            msg = str(err).replace('Exception:', '')
        if _Debug:
            lg.out(
                _DebugLevel,
                'dht_relations.read_customer_message_brokers ERROR %r at position %d failed with %r'
                % (
                    customer_idurl,
                    position,
                    msg,
                ))
        broker_result.errback(err)
        return None

    def _do_collect_results(all_results):
        # if _Debug:
        #     lg.args(_DebugLevel, all_results=len(all_results))
        final_result = []
        for one_success, one_result in all_results:
            if one_success and one_result['broker_idurl']:
                final_result.append(one_result)
        final_result.sort(key=lambda i: i.get('position'))
        if _Debug:
            lg.args(_DebugLevel, results=len(final_result))
        result.callback(final_result)
        return None

    def _do_read_brokers():
        all_brokers_results = []
        for position in positions:
            one_broker_result = Deferred()
            all_brokers_results.append(one_broker_result)
            d = dht_records.get_message_broker(
                customer_idurl=customer_idurl,
                position=position,
                return_details=return_details,
                use_cache=use_cache,
            )
            d.addCallback(_do_verify, position, one_broker_result)
            if _Debug:
                d.addErrback(
                    lg.errback,
                    debug=_Debug,
                    debug_level=_DebugLevel,
                    method='read_customer_message_brokers._do_read_brokers')
            d.addErrback(_on_error, position, one_broker_result)
        join_all_brokers = DeferredList(all_brokers_results,
                                        consumeErrors=False)
        join_all_brokers.addCallback(_do_collect_results)
        if _Debug:
            join_all_brokers.addErrback(
                lg.errback,
                debug=_Debug,
                debug_level=_DebugLevel,
                method='read_customer_message_brokers._do_read_brokers')
        join_all_brokers.addErrback(result.errback)
        return None

    d = identitycache.GetLatest(customer_idurl)
    d.addCallback(lambda _: _do_read_brokers())
    if _Debug:
        d.addErrback(lg.errback,
                     debug=_Debug,
                     debug_level=_DebugLevel,
                     method='read_customer_message_brokers')
    d.addErrback(result.errback)
    return result