From 95ed1e044bab5ce4aed1e4d04092cbddd65a5650 Mon Sep 17 00:00:00 2001 From: Jack Robison Date: Wed, 23 May 2018 17:41:56 -0400 Subject: [PATCH] raise TransportNotConnected instead of logging a warning -add a _listening Deferred to KademliaProtocol which is called when the protocol is started --- lbrynet/dht/error.py | 4 ++++ lbrynet/dht/node.py | 3 ++- lbrynet/dht/protocol.py | 8 +++++--- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/lbrynet/dht/error.py b/lbrynet/dht/error.py index 3d44cf3f1..89cf89fab 100644 --- a/lbrynet/dht/error.py +++ b/lbrynet/dht/error.py @@ -39,3 +39,7 @@ class TimeoutError(Exception): msg = 'Timeout connecting to uninitialized node' Exception.__init__(self, msg) self.remote_contact_id = remote_contact_id + + +class TransportNotConnected(Exception): + pass diff --git a/lbrynet/dht/node.py b/lbrynet/dht/node.py index 434b1b1f9..34327a964 100644 --- a/lbrynet/dht/node.py +++ b/lbrynet/dht/node.py @@ -223,7 +223,8 @@ class Node(MockKademliaHelper): """ self.start_listening() - # #TODO: Refresh all k-buckets further away than this node's closest neighbour + yield self._protocol._listening + # TODO: Refresh all k-buckets further away than this node's closest neighbour self.safe_start_looping_call(self._change_token_lc, constants.tokenSecretChangeInterval) # Start refreshing k-buckets periodically, if necessary self.bootstrap_join(known_node_addresses or [], self._joinDeferred) diff --git a/lbrynet/dht/protocol.py b/lbrynet/dht/protocol.py index 43a7a16f8..52c99475c 100644 --- a/lbrynet/dht/protocol.py +++ b/lbrynet/dht/protocol.py @@ -4,12 +4,12 @@ import errno from twisted.internet import protocol, defer from lbrynet.core.call_later_manager import CallLaterManager +from error import BUILTIN_EXCEPTIONS, UnknownRemoteException, TimeoutError, TransportNotConnected import constants import encoding import msgtypes import msgformat -from error import BUILTIN_EXCEPTIONS, UnknownRemoteException, TimeoutError log = logging.getLogger(__name__) @@ -26,6 +26,7 @@ class KademliaProtocol(protocol.DatagramProtocol): self._sentMessages = {} self._partialMessages = {} self._partialMessagesProgress = {} + self._listening = defer.Deferred(None) def sendRPC(self, contact, method, args, rawResponse=False): """ @@ -97,7 +98,8 @@ class KademliaProtocol(protocol.DatagramProtocol): return df def startProtocol(self): - log.info("DHT listening on UDP %i", self._node.port) + log.info("DHT listening on UDP %s:%i", self._node.externalIP, self._node.port) + self._listening.callback(True) def datagramReceived(self, datagram, address): """ Handles and parses incoming RPC messages (and responses) @@ -279,7 +281,7 @@ class KademliaProtocol(protocol.DatagramProtocol): log.error("DHT socket error: %s (%i)", err.message, err.errno) raise err else: - log.warning("transport not connected!") + raise TransportNotConnected() def _sendResponse(self, contact, rpcID, response): """ Send a RPC response to the specified contact