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

8
lib/network.py

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

24
lib/plugins.py

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

2
plugins/exchange_rate/exchange_rate.py

@ -265,7 +265,7 @@ class FxPlugin(BasePlugin, ThreadJob):
return [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():
self.timeout = time.time() + 150
self.exchange.update(self.ccy)

Loading…
Cancel
Save