Browse Source

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.
283
Neil Booth 10 years ago
parent
commit
89c1a1a0ab
  1. 31
      lib/network.py

31
lib/network.py

@ -336,22 +336,16 @@ class Network(util.DaemonThread):
self.interfaces = {} self.interfaces = {}
def set_parameters(self, host, port, protocol, proxy, auto_connect): def set_parameters(self, host, port, protocol, proxy, auto_connect):
server = serialize_server(host, port, protocol)
if self.proxy != proxy or self.protocol != protocol: if self.proxy != proxy or self.protocol != protocol:
# Restart the network defaulting to the given server
self.stop_network() self.stop_network()
self.default_server = server
self.start_network(protocol, proxy) self.start_network(protocol, proxy)
if auto_connect: elif self.default_server != server:
return self.switch_to_interface(server)
elif auto_connect and (not self.is_connected() or self.server_is_lagging()):
if auto_connect: self.switch_to_random_interface()
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)
def switch_to_random_interface(self): def switch_to_random_interface(self):
if self.interfaces: if self.interfaces:
@ -364,6 +358,8 @@ class Network(util.DaemonThread):
self.default_server = server self.default_server = server
if server in self.interfaces: if server in self.interfaces:
self.print_error("switching to", server) self.print_error("switching to", server)
# stop any current interface in order to terminate subscriptions
self.stop_interface()
self.interface = self.interfaces[server] self.interface = self.interfaces[server]
self.send_subscriptions() self.send_subscriptions()
self.set_status('connected') self.set_status('connected')
@ -373,8 +369,9 @@ class Network(util.DaemonThread):
self.start_interface(server) self.start_interface(server)
def stop_interface(self): def stop_interface(self):
self.interface.stop() if self.interface:
self.interface = None self.interface.stop()
self.interface = None
def set_server(self, server): def set_server(self, server):
if self.default_server == server and self.is_connected(): if self.default_server == server and self.is_connected():
@ -383,10 +380,6 @@ class Network(util.DaemonThread):
if self.protocol != deserialize_server(server)[2]: if self.protocol != deserialize_server(server)[2]:
return return
# stop the interface in order to terminate subscriptions
if self.is_connected():
self.stop_interface()
self.switch_to_interface(server) self.switch_to_interface(server)

Loading…
Cancel
Save