commit 241b3b073ff9c5f80acc9d16e752d9b12f83666b
parent 43b8e202fdee6fed4324419729fac858cd0065f6
Author: ThomasV <electrumdev@gmail.com>
Date: Wed, 6 May 2015 11:28:03 +0200
Merge pull request #1198 from kyuupichan/if-cleanup7b
Move response getting to its own function.
Diffstat:
1 file changed, 29 insertions(+), 25 deletions(-)
diff --git a/lib/interface.py b/lib/interface.py
@@ -61,6 +61,8 @@ class TcpInterface(threading.Thread):
self.response_queue = response_queue
self.request_queue = Queue.Queue()
self.unanswered_requests = {}
+ # request timeouts
+ self.request_time = False
# are we waiting for a pong?
self.is_ping = False
self.ping_time = 0
@@ -263,7 +265,7 @@ class TcpInterface(threading.Thread):
self.pipe.send(r)
except socket.error, e:
self.print_error("socket error:", e)
- self.connected = False
+ self.stop()
return
if self.debug:
self.print_error("-->", r)
@@ -291,6 +293,31 @@ class TcpInterface(threading.Thread):
self.is_ping = True
self.ping_time = time.time()
+ def get_and_process_one_response(self):
+ if self.is_connected():
+ try:
+ response = self.pipe.get()
+ except util.timeout:
+ if self.unanswered_requests:
+ if self.request_time is False:
+ self.request_time = time.time()
+ self.print_error("setting timer")
+ else:
+ if time.time() - self.request_time > 10:
+ self.print_error("request timeout", len(self.unanswered_requests))
+ self.stop()
+ return
+
+ if self.request_time is not False:
+ self.print_error("stopping timer")
+ self.request_time = False
+
+ # If remote side closed the socket, SocketPipe closes our socket and returns None
+ if response is None:
+ self.connected = False
+ else:
+ self.process_response(response)
+
def run(self):
self.s = self.get_socket()
if self.s:
@@ -303,33 +330,10 @@ class TcpInterface(threading.Thread):
if not self.connected:
return
- # request timer
- request_time = False
while self.connected:
self.maybe_ping()
self.send_requests()
- if not self.connected:
- break
- try:
- response = self.pipe.get()
- except util.timeout:
- if self.unanswered_requests:
- if request_time is False:
- request_time = time.time()
- self.print_error("setting timer")
- else:
- if time.time() - request_time > 10:
- self.print_error("request timeout", len(self.unanswered_requests))
- self.connected = False
- break
- continue
- if response is None:
- self.connected = False
- break
- if request_time is not False:
- self.print_error("stopping timer")
- request_time = False
- self.process_response(response)
+ self.get_and_process_one_response()
self.change_status()