Browse Source

manage subscriptions in network.py

283
ThomasV 11 years ago
parent
commit
40e393187a
  1. 1
      lib/interface.py
  2. 42
      lib/network.py
  3. 2
      lib/wallet.py

1
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':

42
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)

2
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):

Loading…
Cancel
Save