def field(idurl): """ Translates string into `ID_URL_FIELD` object. Also we try to read from local identity cache folder if do not know given "idurl". """ global _KnownIDURLs if isinstance(idurl, ID_URL_FIELD): return idurl if idurl in [None, 'None', '', b'None', b'', False, ]: return ID_URL_FIELD(idurl) idurl = strng.to_bin(idurl.strip()) if idurl not in _KnownIDURLs: if _Debug: lg.out(_DebugLevel, 'id_url.field will try to find %r in local identity cache' % idurl) from contacts import identitydb cached_ident = identitydb.get_ident(idurl) if cached_ident: identity_cached(cached_ident) else: if _Debug: cod = sys._getframe(1).f_back.f_code modul = os.path.basename(cod.co_filename).replace('.py', '') caller = cod.co_name lg.warn('unknown yet idurl %s, call from %s.%s' % (idurl, modul, caller, )) return ID_URL_FIELD(idurl)
def OverrideIdentity(idurl, xml_src): """ Used by proxy router to remember the original identity of the routing node. """ global _OverriddenIdentities idurl = id_url.field(idurl) if not idurl.is_latest(): if idurl.original() in _OverriddenIdentities: if idurl.to_bin() not in _OverriddenIdentities: _OverriddenIdentities[ idurl.to_bin()] = _OverriddenIdentities.pop( idurl.original()) lg.info( 'detected and processed idurl rotate for overridden identity : %r -> %r' % (idurl.original(), idurl.to_bin())) idurl = id_url.to_bin(idurl) xml_src = strng.to_text(xml_src.strip()) if idurl in _OverriddenIdentities: if _OverriddenIdentities[idurl] == xml_src: if _Debug: lg.out( _DebugLevel, 'identitycache.OverrideIdentity SKIPPED "%s", no changes' % idurl) return False if _Debug: lg.out( _DebugLevel, 'identitycache.OverrideIdentity replacing overriden identity %r with new one' % idurl) lg.out(_DebugLevel, '\nOVERRIDDEN OLD:\n' + _OverriddenIdentities[idurl]) lg.out(_DebugLevel, '\nOVERRIDDEN NEW:\n' + xml_src) else: orig = '' if identitydb.has_idurl(idurl): orig = identitydb.get_ident(idurl).serialize(as_text=True) if orig and orig == xml_src: if _Debug: lg.out( _DebugLevel, 'identitycache.OverrideIdentity SKIPPED %r , overridden copy is the same as original' % idurl) return False if _Debug: lg.out( _DebugLevel, 'identitycache.OverrideIdentity replacing original identity for %r' % idurl) lg.out(_DebugLevel, '\nORIGINAL:\n' + orig) lg.out(_DebugLevel, '\nNEW:\n' + xml_src) _OverriddenIdentities[idurl] = xml_src if _Debug: lg.out( _DebugLevel, ' total number of overrides: %d' % len(_OverriddenIdentities)) return True
def FromCache(idurl): """ Get identity object from cache. """ if IsOverridden(idurl): overridden_xmlsrc = ReadOverriddenIdentityXMLSource(idurl) if overridden_xmlsrc: if _Debug: lg.out(_DebugLevel, ' returning overridden identity (%d bytes) for %s' % (len(overridden_xmlsrc), idurl)) return identity.identity(xmlsrc=overridden_xmlsrc) return identitydb.get_ident(idurl)
def _fail(err, idurl): global _CachingTasks idurl = id_url.to_original(idurl) result = _CachingTasks.pop(idurl) if not try_other_sources: if result: result.errback(err) else: lg.warn('caching task for %s was not found' % idurl) p2p_stats.count_identity_cache(idurl, 0) lg.warn('[cache failed] %s : %s' % ( idurl, err.getErrorMessage(), )) return None latest_idurl, latest_rev = id_url.get_latest_revision(idurl) latest_ident = None sources = [] if latest_idurl: latest_ident = identitydb.get_ident(latest_idurl) if latest_ident: sources = latest_ident.getSources(as_fields=False) if sources: if idurl in sources: sources = sources.remove(idurl) if sources: lg.warn('[cache failed] %s : %s but will try %d more sources' % ( idurl, err.getErrorMessage(), len(sources), )) _next_source(err, sources, 0, result) return result if result: result.errback(err) else: lg.warn('caching task for %s was not found' % idurl) p2p_stats.count_identity_cache(idurl, 0) lg.warn('[cache failed] and also no other sources found %s : %s' % ( idurl, err.getErrorMessage(), )) return None
def _fail(err, idurl): global _LastTimeCached idurl = id_url.to_original(idurl) if _Debug: lg.args(_DebugLevel, err=err, idurl=idurl) if not try_other_sources: p2p_stats.count_identity_cache(idurl, 0) _LastTimeCached.pop(idurl, None) lg.warn('[cache failed] %s : %s' % ( idurl, err.getErrorMessage(), )) defer_results = caching().pop(idurl, []) for result in defer_results: if result and not result.called: reactor.callLater(0, result.errback, err) # @UndefinedVariable defer_results = [] del defer_results return None sources = [] latest_ident = None latest_idurl, _ = id_url.get_latest_revision(idurl) if not latest_idurl: latest_idurl = idurl if latest_idurl: latest_ident = identitydb.get_ident(latest_idurl) if latest_ident: sources.extend(list(latest_ident.getSources(as_originals=True))) if not sources: pub_key = id_url.known().get(latest_idurl) if pub_key: known_sources = id_url.sources(pub_key) for another_idurl in reversed(known_sources): if another_idurl != latest_idurl and another_idurl != idurl: if another_idurl not in sources: sources.append(another_idurl) if idurl in sources: sources.remove(idurl) if _Debug: lg.args(_DebugLevel, idurl=idurl, latest_idurl=latest_idurl, latest_ident=latest_ident, sources=sources) if sources: lg.warn('[cache failed] %s : %s but will try %d more sources' % ( idurl, err.getErrorMessage(), len(sources), )) _next_source(None, idurl, sources, 0) return None p2p_stats.count_identity_cache(idurl, 0) _LastTimeCached.pop(idurl, None) lg.warn('[cache failed] and also no other sources found %s : %s' % ( idurl, err.getErrorMessage(), )) defer_results = caching().pop(idurl, []) if _Debug: lg.args(_DebugLevel, known=len(id_url.known().keys()), defer_results=len(defer_results)) for result in defer_results: if result and not result.called: reactor.callLater(0, result.errback, err) # @UndefinedVariable defer_results = [] del defer_results return None