def _do_verify(dht_value, customer_idurl_bin): if customer_idurl_bin in rotated_idurls: rotated_idurls.remove(customer_idurl_bin) ret = { 'suppliers': [], 'ecc_map': None, 'customer_idurl': customer_idurl, 'revision': 0, 'publisher_idurl': None, 'timestamp': None, } if not dht_value or not isinstance(dht_value, dict): if not rotated_idurls: result.callback(ret) return ret another_customer_idurl_bin = rotated_idurls.pop(0) lg.warn( 'found another rotated idurl %r and re-try reading customer suppliers' % another_customer_idurl_bin) d = dht_records.get_suppliers(another_customer_idurl_bin, return_details=True, use_cache=False) d.addCallback(_do_verify, another_customer_idurl_bin) d.addErrback(_on_error) return ret try: _ecc_map = strng.to_text(dht_value['ecc_map']) if as_fields: _customer_idurl = id_url.field(dht_value['customer_idurl']) _publisher_idurl = id_url.field( dht_value.get('publisher_idurl')) _suppliers_list = id_url.fields_list(dht_value['suppliers']) else: _customer_idurl = id_url.to_bin(dht_value['customer_idurl']) _publisher_idurl = id_url.to_bin( dht_value.get('publisher_idurl')) _suppliers_list = id_url.to_bin_list(dht_value['suppliers']) _revision = int(dht_value.get('revision')) _timestamp = int(dht_value.get('timestamp')) except: lg.exc() result.callback(ret) return ret ret.update({ 'suppliers': _suppliers_list, 'ecc_map': _ecc_map, 'customer_idurl': _customer_idurl, 'revision': _revision, 'publisher_idurl': _publisher_idurl, 'timestamp': _timestamp, }) return _do_identity_cache(ret)
def read_customer_suppliers(customer_idurl, as_fields=True, use_cache=True): if as_fields: customer_idurl = id_url.field(customer_idurl) else: customer_idurl = id_url.to_bin(customer_idurl) rotated_idurls = id_url.list_known_idurls(customer_idurl, num_revisions=3) if _Debug: lg.args(_DebugLevel, customer_idurl=customer_idurl, rotated_idurls=rotated_idurls, as_fields=as_fields, use_cache=use_cache) result = Deferred() def _do_identity_cache(ret): all_stories = [] for _supplier_idurl in ret['suppliers']: if _supplier_idurl: _supplier_idurl = id_url.to_bin(_supplier_idurl) if not id_url.is_cached( _supplier_idurl) or not identitycache.HasFile( _supplier_idurl): one_supplier_story = identitycache.immediatelyCaching( _supplier_idurl) if _Debug: one_supplier_story.addErrback( lg.errback, debug=_Debug, debug_level=_DebugLevel, method='read_customer_suppliers._do_identity_cache' ) all_stories.append(one_supplier_story) _customer_idurl = id_url.to_bin(ret['customer_idurl']) if _customer_idurl and (not id_url.is_cached(_customer_idurl) or not identitycache.HasFile(_customer_idurl)): one_customer_story = identitycache.immediatelyCaching( _customer_idurl) if _Debug: one_customer_story.addErrback( lg.errback, debug=_Debug, debug_level=_DebugLevel, method='read_customer_suppliers._do_identity_cache') all_stories.append(one_customer_story) if _Debug: lg.args(_DebugLevel, all_stories=len(all_stories), ret=ret) id_cache_story = DeferredList(all_stories, consumeErrors=True) id_cache_story.addCallback(_do_save_customer_suppliers, ret) if _Debug: id_cache_story.addErrback( lg.errback, debug=_Debug, debug_level=_DebugLevel, method='read_customer_suppliers._do_identity_cache') id_cache_story.addErrback(result.errback) return id_cache_story def _do_verify(dht_value, customer_idurl_bin): if customer_idurl_bin in rotated_idurls: rotated_idurls.remove(customer_idurl_bin) ret = { 'suppliers': [], 'ecc_map': None, 'customer_idurl': customer_idurl, 'revision': 0, 'publisher_idurl': None, 'timestamp': None, } if not dht_value or not isinstance(dht_value, dict): if not rotated_idurls: result.callback(ret) return ret another_customer_idurl_bin = rotated_idurls.pop(0) lg.warn( 'found another rotated idurl %r and re-try reading customer suppliers' % another_customer_idurl_bin) d = dht_records.get_suppliers(another_customer_idurl_bin, return_details=True, use_cache=False) d.addCallback(_do_verify, another_customer_idurl_bin) d.addErrback(_on_error) return ret try: _ecc_map = strng.to_text(dht_value['ecc_map']) if as_fields: _customer_idurl = id_url.field(dht_value['customer_idurl']) _publisher_idurl = id_url.field( dht_value.get('publisher_idurl')) _suppliers_list = id_url.fields_list(dht_value['suppliers']) else: _customer_idurl = id_url.to_bin(dht_value['customer_idurl']) _publisher_idurl = id_url.to_bin( dht_value.get('publisher_idurl')) _suppliers_list = id_url.to_bin_list(dht_value['suppliers']) _revision = int(dht_value.get('revision')) _timestamp = int(dht_value.get('timestamp')) except: lg.exc() result.callback(ret) return ret ret.update({ 'suppliers': _suppliers_list, 'ecc_map': _ecc_map, 'customer_idurl': _customer_idurl, 'revision': _revision, 'publisher_idurl': _publisher_idurl, 'timestamp': _timestamp, }) return _do_identity_cache(ret) def _do_save_customer_suppliers(id_cached_result, ret): if my_id.getIDURL() != id_url.field(ret['customer_idurl']): contactsdb.set_suppliers(ret['suppliers'], customer_idurl=ret['customer_idurl']) contactsdb.save_suppliers(customer_idurl=ret['customer_idurl']) if ret.get('ecc_map'): for supplier_idurl in ret['suppliers']: if supplier_idurl and id_url.is_cached(supplier_idurl): contactsdb.add_supplier_meta_info( supplier_idurl=supplier_idurl, info={ 'ecc_map': ret['ecc_map'], }, customer_idurl=ret['customer_idurl'], ) else: if _Debug: lg.out( _DebugLevel, 'dht_relations._do_save_customer_suppliers SKIP processing my own suppliers' ) if _Debug: lg.out( _DebugLevel, 'dht_relations._do_save_customer_suppliers OK for %r returned %d suppliers' % ( ret['customer_idurl'], len(ret['suppliers']), )) result.callback(ret) return ret def _on_error(err): try: msg = err.getErrorMessage() except: msg = str(err).replace('Exception:', '') if _Debug: lg.out( _DebugLevel, 'dht_relations.read_customer_suppliers ERROR %r failed with %r' % ( customer_idurl, msg, )) result.errback(err) return None customer_idurl_bin = id_url.to_bin(customer_idurl) # if customer_idurl_bin in rotated_idurls: # rotated_idurls.remove(customer_idurl_bin) d = dht_records.get_suppliers(customer_idurl_bin, return_details=True, use_cache=use_cache) d.addCallback(_do_verify, customer_idurl_bin) d.addErrback(_on_error) return result
def read_customer_suppliers(customer_idurl): result = Deferred() def _do_verify(dht_value): try: _ecc_map = dht_value['ecc_map'] _customer_idurl = strng.to_bin(dht_value['customer_idurl']) _publisher_idurl = dht_value.get('publisher_idurl') _suppliers_list = list(map(strng.to_bin, dht_value['suppliers'])) _revision = dht_value.get('revision') _timestamp = dht_value.get('timestamp') except: lg.exc() result.callback(None) return None ret = { 'suppliers': _suppliers_list, 'ecc_map': _ecc_map, 'customer_idurl': _customer_idurl, 'revision': _revision, 'publisher_idurl': _publisher_idurl, 'timestamp': _timestamp, } if customer_idurl == my_id.getLocalIDURL(): if _Debug: lg.out( _DebugLevel, 'dht_relations.read_customer_suppliers skip caching my own suppliers list received from DHT: %s' % ret) else: contactsdb.set_suppliers(_suppliers_list, customer_idurl=customer_idurl) contactsdb.save_suppliers(customer_idurl=customer_idurl) if _Debug: lg.out( _DebugLevel, 'dht_relations.read_customer_suppliers %r returned %r' % ( customer_idurl, ret, )) result.callback(ret) return None def _on_error(err): try: msg = err.getErrorMessage() except: msg = str(err).replace('Exception:', '') if _Debug: lg.out( _DebugLevel, 'dht_relations.read_customer_suppliers %r failed with %r' % ( customer_idurl, msg, )) result.callback(None) return None d = dht_records.get_suppliers(customer_idurl) d.addCallback(_do_verify) d.addErrback(_on_error) return result