From 89c1a1a0ab7083553d9de72dbccc221ef4441cac Mon Sep 17 00:00:00 2001 From: Neil Booth Date: Sat, 23 May 2015 23:21:59 +0900 Subject: [PATCH] Improve logic in network.py's set_parameters() 1) For new proxy or protocol, restart the network and default to the requested server. 2) Otherwise if we aren't using the requested server, switch to it 3) Otherwise choose a random server if the requested server is stale and auto_connect is True As switch_to_interface() now has another user, move the logic there whereby we close the old interface in order to terminate subscriptions, in order to have it in one place. --- lib/network.py | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/lib/network.py b/lib/network.py index 92e51120..5ed42a49 100644 --- a/lib/network.py +++ b/lib/network.py @@ -336,22 +336,16 @@ class Network(util.DaemonThread): self.interfaces = {} def set_parameters(self, host, port, protocol, proxy, auto_connect): + server = serialize_server(host, port, protocol) if self.proxy != proxy or self.protocol != protocol: + # Restart the network defaulting to the given server self.stop_network() + self.default_server = server self.start_network(protocol, proxy) - if auto_connect: - return - - if auto_connect: - if not self.is_connected(): - self.switch_to_random_interface() - else: - if self.server_is_lagging(): - self.stop_interface() - else: - server_str = serialize_server(host, port, protocol) - self.set_server(server_str) - + elif self.default_server != server: + self.switch_to_interface(server) + elif auto_connect and (not self.is_connected() or self.server_is_lagging()): + self.switch_to_random_interface() def switch_to_random_interface(self): if self.interfaces: @@ -364,6 +358,8 @@ class Network(util.DaemonThread): self.default_server = server if server in self.interfaces: self.print_error("switching to", server) + # stop any current interface in order to terminate subscriptions + self.stop_interface() self.interface = self.interfaces[server] self.send_subscriptions() self.set_status('connected') @@ -373,8 +369,9 @@ class Network(util.DaemonThread): self.start_interface(server) def stop_interface(self): - self.interface.stop() - self.interface = None + if self.interface: + self.interface.stop() + self.interface = None def set_server(self, server): if self.default_server == server and self.is_connected(): @@ -383,10 +380,6 @@ class Network(util.DaemonThread): if self.protocol != deserialize_server(server)[2]: return - # stop the interface in order to terminate subscriptions - if self.is_connected(): - self.stop_interface() - self.switch_to_interface(server)