electrum

Electrum Bitcoin wallet
git clone https://git.parazyd.org/electrum
Log | Files | Refs | Submodules

commit 40e393187a96c8504b0c5e364af1e45251b36547
parent a22ae33a818a0fb8015f90f7c2164900473cd135
Author: ThomasV <thomasv@gitorious>
Date:   Wed,  2 Oct 2013 13:00:02 +0200

manage subscriptions in network.py

Diffstat:
Mlib/interface.py | 1+
Mlib/network.py | 42++++++++++++++++++++++++++++++++++--------
Mlib/wallet.py | 2+-
3 files changed, 36 insertions(+), 9 deletions(-)

diff --git a/lib/interface.py b/lib/interface.py @@ -482,6 +482,7 @@ class Interface(threading.Thread): self.subscriptions[callback].append(message) if not self.is_connected: + print_error("interface: trying to send while not connected") return if self.protocol in 'st': diff --git a/lib/network.py b/lib/network.py @@ -61,6 +61,29 @@ class Network(threading.Thread): os.mkdir(dir_path) + # default subscriptions + self.subscriptions = {} + self.subscriptions[self.on_banner] = [('server.banner',[])] + self.subscriptions[self.on_peers] = [('server.peers.subscribe',[])] + + + 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: + self.subscriptions[callback] = [] + for message in messages: + if message not in self.subscriptions[callback]: + self.subscriptions[callback].append(message) + + if self.interface and self.interface.is_connected: + self.interface.send( messages, callback ) + + def register_callback(self, event, callback): with self.lock: if not self.callbacks.get(event): @@ -138,17 +161,21 @@ class Network(threading.Thread): return # stop the interface in order to terminate subscriptions - subscriptions = self.interface.subscriptions self.interface.stop() # notify gui self.trigger_callback('disconnecting') # start interface self.default_server = server - self.start_interface(server) - self.interface = self.interfaces[server] - # send subscriptions - for cb, sub in subscriptions.items(): - self.interface.send(sub, cb) + + if server in self.interfaces.keys(): + self.interface = self.interfaces[server] + self.send_subscriptions() + else: + self.start_interface(server) + self.interface = self.interfaces[server] + + + def run(self): @@ -163,8 +190,7 @@ class Network(threading.Thread): if i.is_connected: i.send([ ('blockchain.headers.subscribe',[])], self.on_header) if i == self.interface: - i.send([('server.banner',[])], self.on_banner) - i.send([('server.peers.subscribe',[])], self.on_peers) + self.send_subscriptions() self.trigger_callback('connected') else: self.disconnected_servers.append(i.server) diff --git a/lib/wallet.py b/lib/wallet.py @@ -1492,7 +1492,7 @@ class WalletSynchronizer(threading.Thread): messages = [] for addr in addresses: messages.append(('blockchain.address.subscribe', [addr])) - self.network.interface.send( messages, lambda i,r: self.queue.put(r)) + self.network.subscribe( messages, lambda i,r: self.queue.put(r)) def run(self):