Browse Source

separate Network and Plugins

283
ThomasV 9 years ago
parent
commit
74a9e2296c
  1. 17
      electrum
  2. 8
      lib/network.py
  3. 24
      lib/plugins.py
  4. 2
      plugins/exchange_rate/exchange_rate.py

17
electrum

@ -346,15 +346,15 @@ if __name__ == '__main__':
# daemon is not running # daemon is not running
if cmd_name == 'gui': if cmd_name == 'gui':
if not config.get('offline'): if not config.get('offline'):
network = Network(config, plugins) network = Network(config)
network.start() network.start()
plugins.start()
else: else:
network = None network = None
gui = init_gui(config, network, plugins) gui = init_gui(config, network, plugins)
server = Daemon(config, network, gui) daemon = Daemon(config, network, gui)
server.start() daemon.start()
gui.main() gui.main()
server.stop()
sys.exit(0) sys.exit(0)
elif cmd_name == 'daemon': elif cmd_name == 'daemon':
@ -365,16 +365,17 @@ if __name__ == '__main__':
elif subcommand == 'start': elif subcommand == 'start':
p = os.fork() p = os.fork()
if p == 0: if p == 0:
network = Network(config, plugins) network = Network(config)
network.start() network.start()
server = Daemon(config, network) plugins.start()
daemon = Daemon(config, network)
if config.get('websocket_server'): if config.get('websocket_server'):
from electrum import websockets from electrum import websockets
websockets.WebSocketServer(config, network).start() websockets.WebSocketServer(config, network).start()
if config.get('requests_dir'): if config.get('requests_dir'):
util.check_www_dir(config.get('requests_dir')) util.check_www_dir(config.get('requests_dir'))
server.start() daemon.start()
server.join() daemon.join()
else: else:
print_stderr("starting daemon (PID %d)"%p) print_stderr("starting daemon (PID %d)"%p)
sys.exit(0) sys.exit(0)

8
lib/network.py

@ -135,7 +135,7 @@ class Network(util.DaemonThread):
stop() stop()
""" """
def __init__(self, config=None, plugins=None): def __init__(self, config=None):
if config is None: if config is None:
config = {} # Do not use mutables as default values! config = {} # Do not use mutables as default values!
util.DaemonThread.__init__(self) util.DaemonThread.__init__(self)
@ -193,9 +193,6 @@ class Network(util.DaemonThread):
self.socket_queue = Queue.Queue() self.socket_queue = Queue.Queue()
self.start_network(deserialize_server(self.default_server)[2], self.start_network(deserialize_server(self.default_server)[2],
deserialize_proxy(self.config.get('proxy'))) deserialize_proxy(self.config.get('proxy')))
self.plugins = plugins
if self.plugins:
self.plugins.set_network(self)
def register_callback(self, callback, events): def register_callback(self, callback, events):
with self.lock: with self.lock:
@ -354,6 +351,7 @@ class Network(util.DaemonThread):
def set_proxy(self, proxy): def set_proxy(self, proxy):
self.proxy = proxy self.proxy = proxy
if proxy: if proxy:
self.print_error('setting proxy', proxy)
proxy_mode = proxy_modes.index(proxy["mode"]) + 1 proxy_mode = proxy_modes.index(proxy["mode"]) + 1
socks.setdefaultproxy(proxy_mode, proxy["host"], int(proxy["port"])) socks.setdefaultproxy(proxy_mode, proxy["host"], int(proxy["port"]))
socket.socket = socks.socksocket socket.socket = socks.socksocket
@ -750,8 +748,6 @@ class Network(util.DaemonThread):
self.process_pending_sends() self.process_pending_sends()
self.stop_network() self.stop_network()
if self.plugins:
self.plugins.set_network(None)
self.print_error("stopped") self.print_error("stopped")
def on_header(self, i, header): def on_header(self, i, header):

24
lib/plugins.py

@ -21,15 +21,17 @@ import sys
import os import os
import imp import imp
import pkgutil import pkgutil
import time
from util import * from util import *
from i18n import _ from i18n import _
from util import profiler, PrintError from util import profiler, PrintError, DaemonThread
class Plugins(PrintError): class Plugins(DaemonThread):
@profiler @profiler
def __init__(self, config, is_local, gui_name): def __init__(self, config, is_local, gui_name):
DaemonThread.__init__(self)
if is_local: if is_local:
find = imp.find_module('plugins') find = imp.find_module('plugins')
plugins = imp.load_module('electrum_plugins', *find) plugins = imp.load_module('electrum_plugins', *find)
@ -116,16 +118,14 @@ class Plugins(PrintError):
x += (lambda: self.wallet_plugin_loader(config, name),) x += (lambda: self.wallet_plugin_loader(config, name),)
wallet.wallet_types.append(x) wallet.wallet_types.append(x)
def set_network(self, network): def run(self):
if network != self.network: jobs = [job for plugin in self.plugins.values()
jobs = [job for plugin in self.plugins.values() for job in plugin.thread_jobs()]
for job in plugin.thread_jobs()] self.add_jobs(jobs)
if self.network: while self.is_running():
self.network.remove_jobs(jobs) time.sleep(0.1)
self.network = network self.run_jobs()
if network: self.print_error("stopped")
network.add_jobs(jobs)
hook_names = set() hook_names = set()

2
plugins/exchange_rate/exchange_rate.py

@ -265,7 +265,7 @@ class FxPlugin(BasePlugin, ThreadJob):
return [self] return [self]
def run(self): def run(self):
# This runs from the network thread which catches exceptions # This runs from the plugins thread which catches exceptions
if self.timeout <= time.time(): if self.timeout <= time.time():
self.timeout = time.time() + 150 self.timeout = time.time() + 150
self.exchange.update(self.ccy) self.exchange.update(self.ccy)

Loading…
Cancel
Save