def FetchSingle(idurl): """ Fetch single identity file from given ``idurl``. """ if _Debug: lg.out(_DebugLevel, "propagate.fetch_single " + idurl) return identitycache.scheduleForCaching(idurl)
def PingContact(idurl, timeout=30): """ Called from outside when need to "ping" some user, this will just send my Identity to that guy, he will need to respond. Previously it request his identity from ID server. """ if _Debug: lg.out(_DebugLevel, "propagate.PingContact [%s]" % idurl) ping_result = Deferred() def _cancel_ack_timeout(x, tmcall): lg.out(_DebugLevel, "propagate.PingContact._cancel_ack_timeout") if tmcall.active(): tmcall.cancel() return x def _ack_handler(response, info, tmcall, res): lg.out(_DebugLevel, "propagate.PingContact._ack_handler %s" % str( (response, info))) if tmcall: _cancel_ack_timeout((response, info), tmcall) if not res.called: res.callback((response, info)) def _ack_timed_out(tm, cache_request): lg.out(_DebugLevel, "propagate.PingContact._ack_timed_out") if not cache_request.called: cache_request.cancel() ping_result.errback( TimeoutError('response was not received within %d seconds' % tm)) def _identity_cached(x, idsrc, timeout_call, result): lg.out( _DebugLevel, "propagate.PingContact._identity_cached %s bytes for [%s]" % (len(idsrc), idurl)) # TODO Verify() SendToIDs( idlist=[ idurl, ], ack_handler=lambda response, info: _ack_handler( response, info, timeout_call, result), wide=True, ) idcache_defer = identitycache.scheduleForCaching(idurl, timeout) if timeout: timeout_call = reactor.callLater(timeout, _ack_timed_out, timeout, idcache_defer) idcache_defer.addErrback(_cancel_ack_timeout, timeout_call) else: timeout_call = None idcache_defer.addCallback(_identity_cached, idurl, timeout_call, ping_result) idcache_defer.addErrback(ping_result.errback) return ping_result
def fetch(list_ids): """ Request a list of identity files. """ lg.out(6, "propagate.fetch identities for %d users" % len(list_ids)) dl = [] for url in list_ids: if url: if not identitycache.FromCache(url): dl.append(identitycache.scheduleForCaching(url)) return DeferredList(dl, consumeErrors=True)
def _try_to_cache(attempts): if attempts > retries + 1: if not ping_result.called: ping_result.errback( Exception( 'failed to fetch remote identity after %d attempts : %s' % ( attempts, idurl, ))) return None idcache_defer = identitycache.scheduleForCaching(idurl, timeout=timeout) idcache_defer.addCallback(_identity_cached, idurl) idcache_defer.addErrback(_identity_cache_failed, idurl, attempts) # ping_result.addErrback(lg.errback) return None
def PingContact(idurl, timeout=30): """ Can be called when you need to "ping" another user. This will send your Identity to that node, and it must respond. """ if _Debug: lg.out(_DebugLevel, "propagate.PingContact [%s]" % idurl) ping_result = Deferred() def _ack_handler(response, info): lg.out( _DebugLevel, "propagate.PingContact._ack_handler: %s via %s" % (response, info)) if not ping_result.called: ping_result.callback((response, info)) return None def _response_timed_out(pkt_out): lg.out(_DebugLevel, "propagate.PingContact._response_timed_out : %s" % pkt_out) if not ping_result.called: ping_result.errback(TimeoutError('remote user did not responded')) return None def _identity_cached(idsrc, idurl): lg.out( _DebugLevel, "propagate.PingContact._identity_cached %s bytes for [%s]" % (len(idsrc), idurl)) # TODO: Verify() SendToIDs( idlist=[ idurl, ], ack_handler=_ack_handler, timeout_handler=_response_timed_out, response_timeout=timeout, wide=True, ) return idsrc def _identity_cache_failed(err, idurl): try: msg = err.getErrorMessage() except: msg = str(err) if _Debug: lg.out( _DebugLevel, "propagate.PingContact._identity_cache_failed %s : %s" % ( idurl, msg, )) if not ping_result.called: ping_result.errback( Exception('failed to fetch remote identity %s: %s' % ( idurl, msg, ))) return None idcache_defer = identitycache.scheduleForCaching(idurl, timeout=timeout) idcache_defer.addCallback(_identity_cached, idurl) idcache_defer.addErrback(_identity_cache_failed, idurl) # ping_result.addErrback(lg.errback) return ping_result