electrum

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

commit ec24087b5a7a010f7c098378983e949cf4c1177d
parent f6f7113978f2e21c44b1678e4b4f45ab2e0c392c
Author: Neil Booth <kyuupichan@gmail.com>
Date:   Sun, 31 Jan 2016 11:43:11 +0900

Move some logic from electrum to daemon

Ultimate goal is to try and stop the daemon race at startup.
Need to isolate logic of checking for server and creating one.

Diffstat:
Melectrum | 66+++++++++---------------------------------------------------------
Mlib/daemon.py | 63+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
2 files changed, 70 insertions(+), 59 deletions(-)

diff --git a/electrum b/electrum @@ -69,12 +69,12 @@ if is_bundle or is_local or is_android: imp.load_module('electrum_gui', *imp.find_module('gui')) -from electrum import util from electrum import SimpleConfig, Network, Wallet, WalletStorage -from electrum.util import print_msg, print_error, print_stderr, json_encode, json_decode, set_verbosity, InvalidPassword +from electrum.util import print_msg, print_stderr, json_encode, json_decode +from electrum.util import set_verbosity, InvalidPassword from electrum.plugins import Plugins from electrum.commands import get_parser, known_commands, Commands, config_variables -from electrum.daemon import Daemon, get_daemon +from electrum.daemon import Daemon # get password routine @@ -91,15 +91,6 @@ def prompt_password(prompt, confirm=True): -def init_gui(config, daemon, plugins): - gui_name = config.get('gui', 'qt') - if gui_name in ['lite', 'classic']: - gui_name = 'qt' - gui = __import__('electrum_gui.' + gui_name, fromlist=['electrum_gui']) - gui = gui.ElectrumGui(config, daemon, plugins) - return gui - - def run_non_RPC(config): cmdname = config.get('cmd') @@ -321,59 +312,20 @@ if __name__ == '__main__': run_non_RPC(config) sys.exit(0) - # check if a daemon is running - server = get_daemon(config) - if cmdname == 'gui': - if server is not None: - result = server.gui(config_options) - else: - daemon = Daemon(config) - daemon.start() - gui = init_gui(config, daemon, plugins) - daemon.gui = gui - gui.main() - sys.exit(0) - + result = Daemon.gui_command(config, config_options, plugins) elif cmdname == 'daemon': - if server is not None: - result = server.daemon(config_options) - else: - subcommand = config.get('subcommand') - if subcommand in ['status', 'stop']: - print_msg("Daemon not running") - sys.exit(1) - elif subcommand == 'start': - p = os.fork() - if p == 0: - daemon = Daemon(config) - if config.get('websocket_server'): - from electrum import websockets - websockets.WebSocketServer(config, daemon.network).start() - if config.get('requests_dir'): - util.check_www_dir(config.get('requests_dir')) - daemon.start() - daemon.join() - sys.exit(0) - else: - print_stderr("starting daemon (PID %d)"%p) - sys.exit(0) - else: - print_msg("syntax: electrum daemon <start|status|stop>") - sys.exit(1) - + result = Daemon.daemon_command(config, config_options) else: # command line init_cmdline(config_options) - if server is not None: - result = server.run_cmdline(config_options) - else: + run_offline, result = Daemon.cmdline_command(config, config_options) + if run_offline: cmd = known_commands[cmdname] if cmd.requires_network: - print_msg("Network daemon is not running. Try 'electrum daemon start'") + print_msg("Daemon not running; try 'electrum daemon start'") sys.exit(1) - else: - result = run_offline_command(config, config_options) + result = run_offline_command(config, config_options) # print result if type(result) in [str, unicode]: diff --git a/lib/daemon.py b/lib/daemon.py @@ -16,13 +16,16 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. -import ast, os +import ast +import os +import sys import jsonrpclib from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer, SimpleJSONRPCRequestHandler from network import Network -from util import json_decode, DaemonThread +from util import check_www_dir, json_decode, DaemonThread +from util import print_msg, print_error, print_stderr from wallet import WalletStorage, Wallet from wizard import WizardBase from commands import known_commands, Commands @@ -177,3 +180,59 @@ class Daemon(DaemonThread): for k, wallet in self.wallets.items(): wallet.stop_threads() DaemonThread.stop(self) + + def init_gui(self, config, plugins): + gui_name = config.get('gui', 'qt') + if gui_name in ['lite', 'classic']: + gui_name = 'qt' + gui = __import__('electrum_gui.' + gui_name, fromlist=['electrum_gui']) + self.gui = gui.ElectrumGui(config, self, plugins) + self.gui.main() + + @staticmethod + def gui_command(config, config_options, plugins): + server = get_daemon(config) + if server is not None: + return server.gui(config_options) + + daemon = Daemon(config) + daemon.start() + daemon.init_gui(config, plugins) + sys.exit(0) + + @staticmethod + def cmdline_command(config, config_options): + server = get_daemon(config) + if server is not None: + return False, server.run_cmdline(config_options) + + return True, None + + @staticmethod + def daemon_command(config, config_options): + server = get_daemon(config) + if server is not None: + return server.daemon(config_options) + + subcommand = config.get('subcommand') + if subcommand in ['status', 'stop']: + print_msg("Daemon not running") + sys.exit(1) + elif subcommand == 'start': + pid = os.fork() + if pid == 0: + daemon = Daemon(config) + if config.get('websocket_server'): + from electrum import websockets + websockets.WebSocketServer(config, daemon.network).start() + if config.get('requests_dir'): + check_www_dir(config.get('requests_dir')) + daemon.start() + daemon.join() + sys.exit(0) + else: + print_stderr("starting daemon (PID %d)" % pid) + sys.exit(0) + else: + print_msg("syntax: electrum daemon <start|status|stop>") + sys.exit(1)