Browse Source

Move response getting to its own function.

This makes the operation of run() more clear.
No essential change in functionality.
283
Neil Booth 10 years ago
parent
commit
6171779442
  1. 54
      lib/interface.py

54
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()

Loading…
Cancel
Save