From 6920747a5d8431735ac145316913248b7347f574 Mon Sep 17 00:00:00 2001 From: Neil Booth Date: Tue, 5 May 2015 17:50:10 +0900 Subject: [PATCH] Move ping functionality into its own function. First step in cleaning up the run() function. Calls stop() rather than setting is_connected to False on case of timeout, which cleanly closes the socket. --- lib/interface.py | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/lib/interface.py b/lib/interface.py index fef53854..ffdb3d68 100644 --- a/lib/interface.py +++ b/lib/interface.py @@ -63,6 +63,7 @@ class TcpInterface(threading.Thread): self.unanswered_requests = {} # are we waiting for a pong? self.is_ping = False + self.ping_time = 0 # parse server self.server = server self.host, self.port, self.protocol = self.server.split(':') @@ -276,6 +277,17 @@ class TcpInterface(threading.Thread): self.connected = False self.print_error("stopped") + def maybe_ping(self): + # ping the server with server.version? + if time.time() - self.ping_time > 60: + if self.is_ping: + self.print_error("ping timeout") + self.stop() + else: + self.send_request({'method':'server.version', 'params':[ELECTRUM_VERSION, PROTOCOL_VERSION]}) + self.is_ping = True + self.ping_time = time.time() + def run(self): self.s = self.get_socket() if self.s: @@ -288,21 +300,12 @@ class TcpInterface(threading.Thread): if not self.connected: return - # ping timer - ping_time = 0 # request timer request_time = False while self.connected: - # ping the server with server.version - if time.time() - ping_time > 60: - if self.is_ping: - self.print_error("ping timeout") - self.connected = False - break - else: - self.send_request({'method':'server.version', 'params':[ELECTRUM_VERSION, PROTOCOL_VERSION]}) - self.is_ping = True - ping_time = time.time() + self.maybe_ping() + if not self.connected: + break try: response = self.pipe.get() except util.timeout: