예제 #1
0
 def _find_random_node(self):
     # DEBUG
     # self.automat('found-one-node', 'http://p2p-id.ru/seed0_cb67.xml')
     # self.automat('found-one-node', 'https://bitdust.io:8084/seed2_b17a.xml')
     # self.automat('found-one-node', 'http://datahaven.net/seed2_916e.xml')
     # self.automat('found-one-node', 'http://bitdust.ai/seed1_c2c2.xml')
     # return
     preferred_routers = []
     preferred_routers_raw = config.conf().getData('services/proxy-transport/preferred-routers').strip()
     if preferred_routers_raw:
         preferred_routers_list = re.split('\n|,|;| ', preferred_routers_raw)
         preferred_routers.extend(preferred_routers_list)
     if preferred_routers:
         known_router = random.choice(preferred_routers)
         if _Debug:
             lg.out(_DebugLevel, 'proxy_receiver._find_random_node selected random item from preferred_routers: %s' % known_router)
         d = propagate.PingContact(known_router, timeout=5)
         d.addCallback(lambda resp_tuple: self.automat('found-one-node', known_router))
         d.addErrback(lambda err: self.automat('nodes-not-found'))
         # d.addErrback(lg.errback)
         # self.automat('found-one-node', known_router)
         return
     if _Debug:
         lg.out(_DebugLevel, 'proxy_receiver._find_random_node will start DHT lookup')
     tsk = lookup.start()
     if tsk:
         tsk.result_defer.addCallback(self._on_nodes_lookup_finished)
         tsk.result_defer.addErrback(lambda err: self.automat('nodes-not-found'))
     else:
         self.automat('nodes-not-found')
예제 #2
0
def share_private_key(key_id, trusted_idurl, timeout=10):
    result = Deferred()
    d = propagate.PingContact(trusted_idurl, timeout=timeout)
    d.addCallback(lambda resp: request_service_keys_registry(
        key_id,
        trusted_idurl,
    ).addCallbacks(callback=result.callback, errback=result.errback))
    d.addErrback(result.errback)
    return result
예제 #3
0
def send_message(json_data, recipient_global_id, packet_id=None, timeout=None):
    """
    Send command.Message() packet to remote peer.
    Returns Deferred (if remote_idurl was not cached yet) or outbox packet object.
    """
    global _LastUserPingTime
    global _PingTrustIntervalSeconds
    if not packet_id:
        packet_id = packetid.UniqueID()
    lg.out(
        4, "message.send_message to %s with PackteID=%s" %
        (recipient_global_id, packet_id))
    remote_idurl = global_id.GlobalUserToIDURL(recipient_global_id)
    if not remote_idurl:
        return fail(Exception('invalid recipient'))
    ret = Deferred()
    if remote_idurl not in _LastUserPingTime:
        is_expired = True
    else:
        is_expired = time.time(
        ) - _LastUserPingTime[remote_idurl] > _PingTrustIntervalSeconds
    remote_identity = identitycache.FromCache(remote_idurl)
    if is_expired or remote_identity is None or not online_status.isOnline(
            remote_idurl):
        d = propagate.PingContact(remote_idurl, timeout=timeout or 5)
        d.addCallback(lambda response_tuple: on_ping_success(
            response_tuple, remote_idurl))
        d.addCallback(
            lambda response_tuple: do_send_message(json_data,
                                                   recipient_global_id,
                                                   packet_id,
                                                   timeout,
                                                   result_defer=ret))
        d.addErrback(lambda err: on_message_failed(remote_idurl,
                                                   json_data,
                                                   recipient_global_id,
                                                   packet_id,
                                                   None,
                                                   None,
                                                   result_defer=ret))
        return ret
    try:
        do_send_message(json_data, recipient_global_id, packet_id, timeout,
                        ret)
    except Exception as exc:
        lg.warn(str(exc))
        on_message_failed(
            remote_idurl,
            json_data,
            recipient_global_id,
            packet_id,
            None,
            None,
        )
        ret.errback(exc)
    return ret
예제 #4
0
 def doPing(self, *args, **kwargs):
     """
     Action method.
     """
     try:
         timeout = int(args[0])
     except:
         timeout = 15
     d = propagate.PingContact(self.idurl, timeout=timeout)
     d.addCallback(self._on_ping_success)
     d.addErrback(self._on_ping_failed)
예제 #5
0
def share_key(key_id, trusted_idurl, include_private=False, timeout=10):
    """
    Returns deferred, callback will be fired with response Ack() packet argument.
    """
    result = Deferred()
    d = propagate.PingContact(trusted_idurl, timeout=timeout)
    d.addCallback(lambda response_tuple: _do_request_service_keys_registry(
        key_id, trusted_idurl, include_private, timeout, result,
    ))
    # d.addErrback(lg.errback)
    d.addErrback(result.errback)
    return result