Browse Source

Merge pull request #1193 from kyuupichan/if-cleanup4

Move ping functionality into its own function.
283
ThomasV 10 years ago
parent
commit
583d36a763
  1. 27
      lib/interface.py

27
lib/interface.py

@ -63,6 +63,7 @@ class TcpInterface(threading.Thread):
self.unanswered_requests = {} self.unanswered_requests = {}
# are we waiting for a pong? # are we waiting for a pong?
self.is_ping = False self.is_ping = False
self.ping_time = 0
# parse server # parse server
self.server = server self.server = server
self.host, self.port, self.protocol = self.server.split(':') self.host, self.port, self.protocol = self.server.split(':')
@ -276,6 +277,17 @@ class TcpInterface(threading.Thread):
self.connected = False self.connected = False
self.print_error("stopped") 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): def run(self):
self.s = self.get_socket() self.s = self.get_socket()
if self.s: if self.s:
@ -288,21 +300,12 @@ class TcpInterface(threading.Thread):
if not self.connected: if not self.connected:
return return
# ping timer
ping_time = 0
# request timer # request timer
request_time = False request_time = False
while self.connected: while self.connected:
# ping the server with server.version self.maybe_ping()
if time.time() - ping_time > 60: if not self.connected:
if self.is_ping: break
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()
try: try:
response = self.pipe.get() response = self.pipe.get()
except util.timeout: except util.timeout:

Loading…
Cancel
Save