Browse Source

adapt network dialog to daemon

283
ThomasV 11 years ago
parent
commit
8196bc5779
  1. 36
      gui/qt/network_dialog.py
  2. 19
      lib/daemon.py
  3. 19
      lib/network.py
  4. 15
      lib/network_proxy.py
  5. 2
      lib/synchronizer.py

36
gui/qt/network_dialog.py

@ -42,29 +42,26 @@ class NetworkDialog(QDialog):
self.config = config self.config = config
self.protocol = None 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: if parent:
n = len(network.interfaces) n = len(network.get_interfaces())
if n: 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: else:
status = _("Not connected") status = _("Not connected")
if network.is_connected(): if network.is_connected():
status += "\n" + _("Server") + ": %s"%(network.interface.host) status += "\n" + _("Server") + ": %s"%(host)
else: else:
status += "\n" + _("Disconnected from server") status += "\n" + _("Disconnected from server")
else: else:
import random
status = _("Please choose a server.") + "\n" + _("Select 'Cancel' if you are offline.") status = _("Please choose a server.") + "\n" + _("Select 'Cancel' if you are offline.")
server = network.default_server
self.servers = network.get_servers()
vbox = QVBoxLayout() vbox = QVBoxLayout()
vbox.setSpacing(30) vbox.setSpacing(30)
hbox = QHBoxLayout() hbox = QHBoxLayout()
l = QLabel() l = QLabel()
l.setPixmap(QPixmap(":icons/network.png")) l.setPixmap(QPixmap(":icons/network.png"))
@ -95,34 +92,28 @@ class NetworkDialog(QDialog):
grid.addWidget(QLabel(_('Protocol') + ':'), 3, 0) grid.addWidget(QLabel(_('Protocol') + ':'), 3, 0)
grid.addWidget(self.server_protocol, 3, 1) grid.addWidget(self.server_protocol, 3, 1)
# server # server
grid.addWidget(QLabel(_('Server') + ':'), 0, 0) grid.addWidget(QLabel(_('Server') + ':'), 0, 0)
# auto connect # auto connect
self.autocycle_cb = QCheckBox(_('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) grid.addWidget(self.autocycle_cb, 0, 1)
if not self.config.is_modifiable('auto_cycle'): self.autocycle_cb.setEnabled(False) 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.") + " " \ 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.") + _("If it is disabled, Electrum will warn you if your server is lagging.")
grid.addWidget(HelpButton(msg), 0, 4) grid.addWidget(HelpButton(msg), 0, 4)
grid.addWidget(self.server_host, 0, 2, 1, 2) grid.addWidget(self.server_host, 0, 2, 1, 2)
grid.addWidget(self.server_port, 0, 3) grid.addWidget(self.server_port, 0, 3)
label = _('Servers') #_('Active Servers') if network.irc_servers else _('Default Servers')
label = _('Active Servers') if network.irc_servers else _('Default Servers')
self.servers_list_widget = QTreeWidget(parent) self.servers_list_widget = QTreeWidget(parent)
self.servers_list_widget.setHeaderLabels( [ label, _('Limit') ] ) self.servers_list_widget.setHeaderLabels( [ label, _('Limit') ] )
self.servers_list_widget.setMaximumHeight(150) self.servers_list_widget.setMaximumHeight(150)
self.servers_list_widget.setColumnWidth(0, 240) self.servers_list_widget.setColumnWidth(0, 240)
if server: self.change_server(host, protocol)
host, port, protocol = server.split(':') self.set_protocol(protocol)
self.change_server(host, protocol)
self.set_protocol(self.network.protocol)
self.servers_list_widget.connect(self.servers_list_widget, self.servers_list_widget.connect(self.servers_list_widget,
SIGNAL('currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)'), SIGNAL('currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)'),
@ -131,8 +122,6 @@ class NetworkDialog(QDialog):
if not config.is_modifiable('server'): 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) for w in [self.server_host, self.server_port, self.server_protocol, self.servers_list_widget]: w.setEnabled(False)
def enable_set_server(): def enable_set_server():
enabled = not self.autocycle_cb.isChecked() enabled = not self.autocycle_cb.isChecked()
@ -165,7 +154,6 @@ class NetworkDialog(QDialog):
if not self.config.is_modifiable('proxy'): if not self.config.is_modifiable('proxy'):
for w in [self.proxy_host, self.proxy_port, self.proxy_mode]: w.setEnabled(False) 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_mode.setCurrentIndex(self.proxy_mode.findText(str(proxy_config.get("mode").upper())))
self.proxy_host.setText(proxy_config.get("host")) self.proxy_host.setText(proxy_config.get("host"))
self.proxy_port.setText(proxy_config.get("port")) self.proxy_port.setText(proxy_config.get("port"))

19
lib/daemon.py

@ -109,8 +109,10 @@ class ClientThread(threading.Thread):
try: try:
out['result'] = f(*params) out['result'] = f(*params)
except BaseException as e: except BaseException as e:
out['error'] =str(e) out['error'] = str(e)
self.queue.put(out) print_error("network error", str(e))
self.queue.put(out)
return return
def cb(i,r): def cb(i,r):
@ -120,7 +122,12 @@ class ClientThread(threading.Thread):
r['id'] = my_id r['id'] = my_id
self.queue.put(r) 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 self.unanswered_requests[new_id] = _id
@ -162,7 +169,7 @@ class NetworkServer:
def add_client(self, client): 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) value = self.get_status_value(key)
client.queue.put({'method':'network.status', 'params':[key, value]}) client.queue.put({'method':'network.status', 'params':[key, value]})
with self.lock: with self.lock:
@ -182,7 +189,9 @@ class NetworkServer:
elif key == 'updated': elif key == 'updated':
value = self.network.get_local_height() value = self.network.get_local_height()
elif key == 'servers': elif key == 'servers':
value = self.network.irc_servers value = self.network.get_servers()
elif key == 'interfaces':
value = self.network.get_interfaces()
return value return value
def trigger_callback(self, key): def trigger_callback(self, key):

19
lib/network.py

@ -94,7 +94,7 @@ class Network(threading.Thread):
if not self.default_server: if not self.default_server:
self.default_server = pick_random_server(self.protocol) 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.pending_servers = set([])
self.disconnected_servers = set([]) self.disconnected_servers = set([])
self.recent_servers = self.config.get('recent_servers',[]) # successful connections self.recent_servers = self.config.get('recent_servers',[]) # successful connections
@ -119,29 +119,20 @@ class Network(threading.Thread):
self.connection_status = 'disconnected' self.connection_status = 'disconnected'
def set_status(self, status): def set_status(self, status):
self.connection_status = status self.connection_status = status
self.trigger_callback('status') self.trigger_callback('status')
def is_connected(self): def is_connected(self):
return self.interface and self.interface.is_connected return self.interface and self.interface.is_connected
def is_up_to_date(self): def is_up_to_date(self):
return self.interface.is_up_to_date() return self.interface.is_up_to_date()
def main_server(self):
return self.interface.server
def send_subscriptions(self): def send_subscriptions(self):
for cb, sub in self.subscriptions.items(): for cb, sub in self.subscriptions.items():
self.interface.send(sub, cb) self.interface.send(sub, cb)
def subscribe(self, messages, callback): def subscribe(self, messages, callback):
with self.lock: with self.lock:
if self.subscriptions.get(callback) is None: if self.subscriptions.get(callback) is None:
@ -195,6 +186,14 @@ class Network(threading.Thread):
server = random.choice( choice_list ) server = random.choice( choice_list )
return server 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): def get_servers(self):
if self.irc_servers: if self.irc_servers:

15
lib/network_proxy.py

@ -55,9 +55,10 @@ class NetworkProxy(threading.Thread):
# status variables # status variables
self.status = 'disconnected' self.status = 'disconnected'
self.servers = [] self.servers = {}
self.banner = '' self.banner = ''
self.height = 0 self.height = 0
self.interfaces = []
def is_running(self): def is_running(self):
return self.running return self.running
@ -101,6 +102,8 @@ class NetworkProxy(threading.Thread):
self.height = value self.height = value
elif key == 'servers': elif key == 'servers':
self.servers = value self.servers = value
elif key == 'interfaces':
self.interfaces = value
self.trigger_callback(key) self.trigger_callback(key)
return return
@ -166,6 +169,9 @@ class NetworkProxy(threading.Thread):
def get_servers(self): def get_servers(self):
return self.servers return self.servers
def get_interfaces(self):
return self.interfaces
def get_header(self, height): def get_header(self, height):
return self.synchronous_get([('network.get_header',[height])])[0] return self.synchronous_get([('network.get_header',[height])])[0]
@ -178,8 +184,11 @@ class NetworkProxy(threading.Thread):
def is_up_to_date(self): def is_up_to_date(self):
return self.synchronous_get([('network.is_up_to_date',[])])[0] return self.synchronous_get([('network.is_up_to_date',[])])[0]
def main_server(self): def get_parameters(self):
return self.synchronous_get([('network.main_server',[])])[0] 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): def stop(self):
self.running = False self.running = False

2
lib/synchronizer.py

@ -65,7 +65,7 @@ class WalletSynchronizer(threading.Thread):
self.run_interface() self.run_interface()
def run_interface(self): def run_interface(self):
print_error("synchronizer: connected to", self.network.main_server()) #print_error("synchronizer: connected to", self.network.get_parameters())
requested_tx = [] requested_tx = []
missing_tx = [] missing_tx = []

Loading…
Cancel
Save