commit d6b2cdd595781fb6cb47a518e33a633dee3f3fa5
parent 5e2e82d5166df32e314818da852c0a721554bde4
Author: ThomasV <electrumdev@gmail.com>
Date: Tue, 5 May 2015 13:33:35 +0200
Merge pull request #1191 from kyuupichan/if-cleanup2
Make is_connected into a member function. No change in logic.
Diffstat:
3 files changed, 25 insertions(+), 26 deletions(-)
diff --git a/lib/interface.py b/lib/interface.py
@@ -37,10 +37,10 @@ def Interface(server, config = None):
exposed API is:
- Inherits everything from threading.Thread.
- - Member functions start(), send_request(), stop()
- - Member variables is_connected, server.
+ - Member functions start(), send_request(), stop(), is_connected()
+ - Member variable server.
- "is_connected" is currently racy. "server" is constant for the object's lifetime and hence
+ "is_connected()" is currently racy. "server" is constant for the object's lifetime and hence
synchronization is unnecessary.
"""
host, port, protocol = server.split(':')
@@ -56,7 +56,7 @@ class TcpInterface(threading.Thread):
self.daemon = True
self.config = config if config is not None else SimpleConfig()
self.lock = threading.Lock()
- self.is_connected = False
+ self.connected = False
self.debug = False # dump network messages. can be changed at runtime using the console
self.message_id = 0
self.unanswered_requests = {}
@@ -260,16 +260,19 @@ class TcpInterface(threading.Thread):
self.print_error("-->", r)
except socket.error, e:
self.print_error("socked error:", e)
- self.is_connected = False
+ self.connected = False
return
self.unanswered_requests[self.message_id] = method, params, _id, queue
self.message_id += 1
+ def is_connected(self):
+ return self.connected
+
def stop(self):
- if self.is_connected and self.protocol in 'st' and self.s:
+ if self.connected and self.protocol in 'st' and self.s:
self.s.shutdown(socket.SHUT_RDWR)
self.s.close()
- self.is_connected = False
+ self.connected = False
self.print_error("stopped")
def start(self, response_queue):
@@ -281,23 +284,23 @@ class TcpInterface(threading.Thread):
if self.s:
self.pipe = util.SocketPipe(self.s)
self.s.settimeout(2)
- self.is_connected = True
+ self.connected = True
self.print_error("connected")
self.change_status()
- if not self.is_connected:
+ if not self.connected:
return
# ping timer
ping_time = 0
# request timer
request_time = False
- while self.is_connected:
+ 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.is_connected = False
+ self.connected = False
break
else:
self.send_request({'method':'server.version', 'params':[ELECTRUM_VERSION, PROTOCOL_VERSION]})
@@ -313,11 +316,11 @@ class TcpInterface(threading.Thread):
else:
if time.time() - request_time > 10:
self.print_error("request timeout", len(self.unanswered_requests))
- self.is_connected = False
+ self.connected = False
break
continue
if response is None:
- self.is_connected = False
+ self.connected = False
break
if request_time is not False:
self.print_error("stopping timer")
@@ -326,16 +329,12 @@ class TcpInterface(threading.Thread):
self.change_status()
-
def change_status(self):
- # print_error( "change status", self.server, self.is_connected)
self.response_queue.put((self, None))
-
-
def check_cert(host, cert):
try:
x = x509.X509()
diff --git a/lib/network.py b/lib/network.py
@@ -206,7 +206,7 @@ class Network(util.DaemonThread):
self.notify('status')
def is_connected(self):
- return self.interface and self.interface.is_connected
+ return self.interface and self.interface.is_connected()
def send_subscriptions(self):
for addr in self.addresses:
@@ -322,7 +322,7 @@ class Network(util.DaemonThread):
return
if auto_connect:
- if not self.interface.is_connected:
+ if not self.interface.is_connected():
self.switch_to_random_interface()
else:
if self.server_is_lagging():
@@ -335,7 +335,7 @@ class Network(util.DaemonThread):
def switch_to_random_interface(self):
while self.interfaces:
i = random.choice(self.interfaces.values())
- if i.is_connected:
+ if i.is_connected():
self.switch_to_interface(i)
break
else:
@@ -356,14 +356,14 @@ class Network(util.DaemonThread):
def set_server(self, server):
- if self.default_server == server and self.interface.is_connected:
+ if self.default_server == server and self.interface.is_connected():
return
if self.protocol != deserialize_server(server)[2]:
return
# stop the interface in order to terminate subscriptions
- if self.interface.is_connected:
+ if self.interface.is_connected():
self.stop_interface()
# notify gui
@@ -474,7 +474,7 @@ class Network(util.DaemonThread):
self.print_error('network: retrying connections')
self.disconnected_servers = set([])
nodes_retry_time = now
- if not self.interface.is_connected:
+ if not self.interface.is_connected():
if self.config.get('auto_cycle'):
if self.interfaces:
self.switch_to_random_interface()
@@ -500,7 +500,7 @@ class Network(util.DaemonThread):
if i.server in self.pending_servers:
self.pending_servers.remove(i.server)
- if i.is_connected:
+ if i.is_connected():
self.add_interface(i)
self.add_recent_server(i)
i.send_request({'method':'blockchain.headers.subscribe','params':[]})
diff --git a/scripts/util.py b/scripts/util.py
@@ -10,7 +10,7 @@ def get_peers():
q = Queue.Queue()
interface.start(q)
i, r = q.get()
- if not interface.is_connected:
+ if not interface.is_connected():
raise BaseException("not connected")
# 2. get list of peers
interface.send_request({'id':0, 'method':'server.peers.subscribe','params':[]})
@@ -41,7 +41,7 @@ def send_request(peers, request):
continue
if i.server in peers:
peers.remove(i.server)
- if i.is_connected:
+ if i.is_connected():
reached_servers.append(i)
else:
print "Connection failed:", i.server