commit 4fbd2ea66e6a50b701ad34ec877f585155331069
parent 33eb749c6a2671915e91ad71bef735769ae1ea1e
Author: thomasv <thomasv@gitorious>
Date: Mon, 22 Oct 2012 11:34:21 +0200
use named callbacks with the interface
Diffstat:
6 files changed, 40 insertions(+), 32 deletions(-)
diff --git a/electrum b/electrum
@@ -185,8 +185,10 @@ if __name__ == '__main__':
sys.exit("Error: Unknown GUI: " + pref_gui )
gui = gui.ElectrumGui(wallet, config)
- wallet.interface = Interface(config, True, gui.server_list_changed)
- wallet.interface.start()
+ interface = Interface(config, True)
+ interface.register_callback('peers', gui.server_list_changed)
+ interface.start()
+ wallet.interface = interface
WalletSynchronizer(wallet, config).start()
WalletVerifier(wallet, config).start()
@@ -293,8 +295,10 @@ if __name__ == '__main__':
# open session
if cmd not in offline_commands and not options.offline:
- wallet.interface = Interface(config)
- wallet.interface.start()
+ interface = Interface(config)
+ interface.register_callback('connected', lambda: print_error("Connected to " + interface.connection_msg))
+ interface.start()
+ wallet.interface = interface
WalletSynchronizer(wallet, config).start()
wallet.update()
wallet.save()
diff --git a/lib/gui_lite.py b/lib/gui_lite.py
@@ -800,7 +800,7 @@ class MiniDriver(QObject):
self.wallet = wallet
self.window = window
- self.wallet.interface.register_callback(self.update_callback)
+ self.wallet.interface.register_callback('updated',self.update_callback)
self.state = None
diff --git a/lib/gui_qt.py b/lib/gui_qt.py
@@ -207,7 +207,7 @@ class ElectrumWindow(QMainWindow):
QMainWindow.__init__(self)
self.wallet = wallet
self.config = config
- self.wallet.interface.register_callback(self.update_callback)
+ self.wallet.interface.register_callback('updated', self.update_callback)
self.detailed_view = config.get('qt_detailed_view', False)
diff --git a/lib/interface.py b/lib/interface.py
@@ -47,14 +47,16 @@ def pick_random_server():
class Interface(threading.Thread):
- def register_callback(self, update_callback):
+ def register_callback(self, event, callback):
with self.lock:
- self.update_callbacks.append(update_callback)
+ self.callbacks[event] = callback
- def trigger_callbacks(self):
+ def trigger_callback(self, event):
with self.lock:
- callbacks = self.update_callbacks[:]
- [update() for update in callbacks]
+ callback = self.callbacks.get(event)
+ if callback:
+ callback()
+
def init_server(self, host, port, proxy=None, use_ssl=True):
@@ -112,6 +114,7 @@ class Interface(threading.Thread):
result = params[1]
params = [addr]
+
response_queue = self.responses[channel]
response_queue.put({'method':method, 'params':params, 'result':result, 'id':msg_id})
@@ -299,7 +302,7 @@ class Interface(threading.Thread):
- def __init__(self, config=None, loop=False, servers_loaded_callback=None):
+ def __init__(self, config=None, loop=False):
if config is None:
from simple_config import SimpleConfig
@@ -309,13 +312,12 @@ class Interface(threading.Thread):
self.daemon = True
self.loop = loop
self.config = config
- self.servers_loaded_callback = servers_loaded_callback
self.subscriptions = {}
self.responses = {}
self.responses['default'] = Queue.Queue()
- self.update_callbacks = []
+ self.callbacks = {}
self.lock = threading.Lock()
self.init_interface()
@@ -338,11 +340,11 @@ class Interface(threading.Thread):
raise BaseException('no server available')
if self.is_connected:
- print "Connected to " + self.connection_msg
self.send([('server.version', [ELECTRUM_VERSION])])
- #self.send([('server.banner',[])], 'synchronizer')
+ self.trigger_callback('connected')
else:
- print_error("Failed to connect " + self.connection_msg)
+ self.trigger_callback('notconnected')
+ #print_error("Failed to connect " + self.connection_msg)
def init_with_server(self, config):
@@ -471,7 +473,7 @@ class Interface(threading.Thread):
def run(self):
while True:
self.run_tcp() if self.protocol in 'st' else self.run_http()
- self.trigger_callbacks()
+ self.trigger_callback('disconnected')
if not self.loop: break
time.sleep(5)
diff --git a/lib/wallet.py b/lib/wallet.py
@@ -845,7 +845,7 @@ class WalletSynchronizer(threading.Thread):
self.wallet.was_updated = True
if self.wallet.was_updated:
- self.interface.trigger_callbacks()
+ self.interface.trigger_callback('updated')
self.wallet.was_updated = False
@@ -859,7 +859,9 @@ class WalletSynchronizer(threading.Thread):
def run(self):
# subscriptions
- self.interface.send([('blockchain.numblocks.subscribe',[]), ('server.peers.subscribe',[])], 'synchronizer')
+ self.interface.send([('server.banner',[])],'synchronizer')
+ self.interface.send([('blockchain.numblocks.subscribe',[])], 'synchronizer')
+ self.interface.send([('server.peers.subscribe',[])],'synchronizer')
self.subscribe_to_addresses(self.wallet.all_addresses())
while True:
@@ -893,9 +895,8 @@ class WalletSynchronizer(threading.Thread):
self.wallet.blocks = result
self.wallet.was_updated = True
- elif method == 'server.banner':
- self.wallet.banner = result
- self.wallet.was_updated = True
+ elif method == 'server.version':
+ pass
elif method == 'server.peers.subscribe':
servers = []
@@ -913,17 +914,14 @@ class WalletSynchronizer(threading.Thread):
if ports and version:
servers.append((host, ports))
self.interface.servers = servers
+ self.interface.trigger_callback('peers')
- # servers_loaded_callback is None for commands, but should
- # NEVER be None when using the GUI.
- #if self.servers_loaded_callback is not None:
- # self.servers_loaded_callback()
-
- elif method == 'server.version':
- pass
+ elif method == 'server.banner':
+ self.wallet.banner = result
+ self.interface.trigger_callback('updated')
else:
- print_error("Error: Unknown message:" + method + ", " + params + ", " + result)
+ print_error("Error: Unknown message:" + method + ", " + repr(params) + ", " + repr(result) )
encode = lambda x: x[::-1].encode('hex')
diff --git a/scripts/servers b/scripts/servers
@@ -38,5 +38,9 @@ v = d.values()
numblocks = d.keys()[v.index(max(v))]
for i in interfaces:
- print "%30s %s "%(i.host, i.status) #, "ok" if abs(n-numblocks)<2 else "lagging"
+ if i.status == 'ok':
+ if abs(i.blocks-numblocks)>1: i.status = "lagging"
+ else:
+ i.blocks = 0
+ print "%30s %d %s "%(i.host, i.blocks, i.status)