From 8196bc577955f1efcc3464576ed7e9fe74e64d57 Mon Sep 17 00:00:00 2001 From: ThomasV Date: Fri, 25 Jul 2014 09:11:56 +0200 Subject: [PATCH] adapt network dialog to daemon --- gui/qt/network_dialog.py | 36 ++++++++++++------------------------ lib/daemon.py | 19 ++++++++++++++----- lib/network.py | 19 +++++++++---------- lib/network_proxy.py | 15 ++++++++++++--- lib/synchronizer.py | 2 +- 5 files changed, 48 insertions(+), 43 deletions(-) diff --git a/gui/qt/network_dialog.py b/gui/qt/network_dialog.py index 11da1a6dd..b1d9b6a7b 100644 --- a/gui/qt/network_dialog.py +++ b/gui/qt/network_dialog.py @@ -42,29 +42,26 @@ class NetworkDialog(QDialog): self.config = config self.protocol = None + self.servers = network.get_servers() + host, port, protocol, proxy_config, auto_connect = network.get_parameters() + if not proxy_config: + proxy_config = { "mode":"none", "host":"localhost", "port":"8080"} + if parent: - n = len(network.interfaces) + n = len(network.get_interfaces()) if n: - status = _("Blockchain") + ": " + "%d "%(network.blockchain.height()) + _("blocks") + ".\n" + _("Getting block headers from %d nodes.")%n + status = _("Blockchain") + ": " + "%d "%(network.get_local_height()) + _("blocks") + ".\n" + _("Getting block headers from %d nodes.")%n else: status = _("Not connected") - if network.is_connected(): - status += "\n" + _("Server") + ": %s"%(network.interface.host) + status += "\n" + _("Server") + ": %s"%(host) else: status += "\n" + _("Disconnected from server") - else: - import random status = _("Please choose a server.") + "\n" + _("Select 'Cancel' if you are offline.") - server = network.default_server - self.servers = network.get_servers() - - vbox = QVBoxLayout() vbox.setSpacing(30) - hbox = QHBoxLayout() l = QLabel() l.setPixmap(QPixmap(":icons/network.png")) @@ -95,34 +92,28 @@ class NetworkDialog(QDialog): grid.addWidget(QLabel(_('Protocol') + ':'), 3, 0) grid.addWidget(self.server_protocol, 3, 1) - # server grid.addWidget(QLabel(_('Server') + ':'), 0, 0) # auto connect self.autocycle_cb = QCheckBox(_('Auto-connect')) - self.autocycle_cb.setChecked(self.config.get('auto_cycle', True)) + self.autocycle_cb.setChecked(auto_connect) grid.addWidget(self.autocycle_cb, 0, 1) if not self.config.is_modifiable('auto_cycle'): self.autocycle_cb.setEnabled(False) msg = _("If auto-connect is enabled, Electrum will always use a server that is on the longest blockchain.") + " " \ + _("If it is disabled, Electrum will warn you if your server is lagging.") grid.addWidget(HelpButton(msg), 0, 4) - grid.addWidget(self.server_host, 0, 2, 1, 2) grid.addWidget(self.server_port, 0, 3) - - label = _('Active Servers') if network.irc_servers else _('Default Servers') + label = _('Servers') #_('Active Servers') if network.irc_servers else _('Default Servers') self.servers_list_widget = QTreeWidget(parent) self.servers_list_widget.setHeaderLabels( [ label, _('Limit') ] ) self.servers_list_widget.setMaximumHeight(150) self.servers_list_widget.setColumnWidth(0, 240) - if server: - host, port, protocol = server.split(':') - self.change_server(host, protocol) - - self.set_protocol(self.network.protocol) + self.change_server(host, protocol) + self.set_protocol(protocol) self.servers_list_widget.connect(self.servers_list_widget, SIGNAL('currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)'), @@ -131,8 +122,6 @@ class NetworkDialog(QDialog): if not config.is_modifiable('server'): for w in [self.server_host, self.server_port, self.server_protocol, self.servers_list_widget]: w.setEnabled(False) - - def enable_set_server(): enabled = not self.autocycle_cb.isChecked() @@ -165,7 +154,6 @@ class NetworkDialog(QDialog): if not self.config.is_modifiable('proxy'): for w in [self.proxy_host, self.proxy_port, self.proxy_mode]: w.setEnabled(False) - proxy_config = network.proxy if network.proxy else { "mode":"none", "host":"localhost", "port":"8080"} self.proxy_mode.setCurrentIndex(self.proxy_mode.findText(str(proxy_config.get("mode").upper()))) self.proxy_host.setText(proxy_config.get("host")) self.proxy_port.setText(proxy_config.get("port")) diff --git a/lib/daemon.py b/lib/daemon.py index 0a0ae132b..0e42bcf94 100644 --- a/lib/daemon.py +++ b/lib/daemon.py @@ -109,8 +109,10 @@ class ClientThread(threading.Thread): try: out['result'] = f(*params) except BaseException as e: - out['error'] =str(e) - self.queue.put(out) + out['error'] = str(e) + print_error("network error", str(e)) + + self.queue.put(out) return def cb(i,r): @@ -120,7 +122,12 @@ class ClientThread(threading.Thread): r['id'] = my_id self.queue.put(r) - new_id = self.network.interface.send([(method, params)], cb) [0] + try: + new_id = self.network.interface.send([(method, params)], cb) [0] + except Exception as e: + self.queue.put(out = {'id':_id, 'error':str(e)}) + return + self.unanswered_requests[new_id] = _id @@ -162,7 +169,7 @@ class NetworkServer: def add_client(self, client): - for key in ['status','banner','updated','servers']: + for key in ['status','banner','updated','servers','interfaces']: value = self.get_status_value(key) client.queue.put({'method':'network.status', 'params':[key, value]}) with self.lock: @@ -182,7 +189,9 @@ class NetworkServer: elif key == 'updated': value = self.network.get_local_height() elif key == 'servers': - value = self.network.irc_servers + value = self.network.get_servers() + elif key == 'interfaces': + value = self.network.get_interfaces() return value def trigger_callback(self, key): diff --git a/lib/network.py b/lib/network.py index 247086a2f..e932c28dd 100644 --- a/lib/network.py +++ b/lib/network.py @@ -94,7 +94,7 @@ class Network(threading.Thread): if not self.default_server: self.default_server = pick_random_server(self.protocol) - self.irc_servers = [] # returned by interface (list from irc) + self.irc_servers = {} # returned by interface (list from irc) self.pending_servers = set([]) self.disconnected_servers = set([]) self.recent_servers = self.config.get('recent_servers',[]) # successful connections @@ -119,29 +119,20 @@ class Network(threading.Thread): self.connection_status = 'disconnected' - def set_status(self, status): self.connection_status = status self.trigger_callback('status') - def is_connected(self): return self.interface and self.interface.is_connected - def is_up_to_date(self): return self.interface.is_up_to_date() - - def main_server(self): - return self.interface.server - - def send_subscriptions(self): for cb, sub in self.subscriptions.items(): self.interface.send(sub, cb) - def subscribe(self, messages, callback): with self.lock: if self.subscriptions.get(callback) is None: @@ -195,6 +186,14 @@ class Network(threading.Thread): server = random.choice( choice_list ) return server + def get_parameters(self): + host, port, protocol = self.default_server.split(':') + proxy = self.proxy + auto_connect = self.config.get('auto_cycle', True) + return host, port, protocol, proxy, auto_connect + + def get_interfaces(self): + return self.interfaces.keys() def get_servers(self): if self.irc_servers: diff --git a/lib/network_proxy.py b/lib/network_proxy.py index 37a20d3a6..b0d38d6a4 100644 --- a/lib/network_proxy.py +++ b/lib/network_proxy.py @@ -55,9 +55,10 @@ class NetworkProxy(threading.Thread): # status variables self.status = 'disconnected' - self.servers = [] + self.servers = {} self.banner = '' self.height = 0 + self.interfaces = [] def is_running(self): return self.running @@ -101,6 +102,8 @@ class NetworkProxy(threading.Thread): self.height = value elif key == 'servers': self.servers = value + elif key == 'interfaces': + self.interfaces = value self.trigger_callback(key) return @@ -166,6 +169,9 @@ class NetworkProxy(threading.Thread): def get_servers(self): return self.servers + def get_interfaces(self): + return self.interfaces + def get_header(self, height): return self.synchronous_get([('network.get_header',[height])])[0] @@ -178,8 +184,11 @@ class NetworkProxy(threading.Thread): def is_up_to_date(self): return self.synchronous_get([('network.is_up_to_date',[])])[0] - def main_server(self): - return self.synchronous_get([('network.main_server',[])])[0] + def get_parameters(self): + return self.synchronous_get([('network.get_parameters',[])])[0] + + def set_parameters(self, *args): + return self.synchronous_get([('network.set_parameters',args)])[0] def stop(self): self.running = False diff --git a/lib/synchronizer.py b/lib/synchronizer.py index 142c101f9..7c2078ca0 100644 --- a/lib/synchronizer.py +++ b/lib/synchronizer.py @@ -65,7 +65,7 @@ class WalletSynchronizer(threading.Thread): self.run_interface() def run_interface(self): - print_error("synchronizer: connected to", self.network.main_server()) + #print_error("synchronizer: connected to", self.network.get_parameters()) requested_tx = [] missing_tx = []