electrum

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

commit 690c8629239b8eb615f52613cc850ec8ab02d4e9
parent 3b00f62d4681c43fafa6f1903bb6d5cce6f8efa8
Author: ThomasV <thomasv@electrum.org>
Date:   Mon,  1 Feb 2016 10:20:22 +0100

move static methods away from daemon class; restore initial logic

Diffstat:
Melectrum | 63++++++++++++++++++++++++++++++++-------------------------------
Mlib/daemon.py | 92+++++++++++++++++++++++++++++++++++++++----------------------------------------
2 files changed, 77 insertions(+), 78 deletions(-)

diff --git a/electrum b/electrum @@ -74,7 +74,7 @@ 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 +from electrum import daemon # get password routine @@ -313,48 +313,49 @@ if __name__ == '__main__': sys.exit(0) if cmdname == 'gui': - lockfile = Daemon.lockfile(config) - fd = Daemon.get_fd_or_server(lockfile) + fd = daemon.get_fd_or_server(config) if isinstance(fd, int): - daemon = Daemon(config, fd) - daemon.start() - daemon.init_gui(config, plugins) + d = daemon.Daemon(config, fd) + d.start() + d.init_gui(config, plugins) sys.exit(0) else: server = fd result = server.gui(config_options) elif cmdname == 'daemon': - lockfile = Daemon.lockfile(config) - fd = Daemon.get_fd_or_server(lockfile) - if isinstance(fd, int): - subcommand = config.get('subcommand') - assert subcommand in ['start', 'stop', 'status'] - if subcommand != 'start': - print_msg("Daemon not running") - os.close(fd) - Daemon.remove_lockfile(lockfile) - sys.exit(1) - pid = os.fork() - if pid: - print_stderr("starting daemon (PID %d)" % pid) + subcommand = config.get('subcommand') + assert subcommand in ['start', 'stop', 'status'] + if subcommand == 'start': + fd = daemon.get_fd_or_server(config) + if isinstance(fd, int): + pid = os.fork() + if pid: + print_stderr("starting daemon (PID %d)" % pid) + sys.exit(0) + d = daemon.Daemon(config, fd) + d.start() + if config.get('websocket_server'): + from electrum import websockets + websockets.WebSocketServer(config, d.network).start() + if config.get('requests_dir'): + util.check_www_dir(config.get('requests_dir')) + d.join() sys.exit(0) - daemon = Daemon(config, fd) - daemon.start() - 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.join() - sys.exit(0) + else: + server = fd + result = server.daemon(config_options) else: - server = fd - result = server.daemon(config_options) + server = daemon.get_server(config) + if server is not None: + result = server.daemon(config_options) + else: + print_msg("Daemon not running") + sys.exit(1) else: # command line init_cmdline(config_options) - server = Daemon.get_server(Daemon.lockfile(config)) + server = daemon.get_server(config) if server is not None: result = server.run_cmdline(config_options) else: diff --git a/lib/daemon.py b/lib/daemon.py @@ -33,6 +33,50 @@ from commands import known_commands, Commands from simple_config import SimpleConfig +def get_lockfile(config): + return os.path.join(config.path, 'daemon') + +def remove_lockfile(lockfile): + os.unlink(lockfile) + +def get_fd_or_server(config): + '''Tries to create the lockfile, using O_EXCL to + prevent races. If it succeeds it returns the FD. + Otherwise try and connect to the server specified in the lockfile. + If this succeeds, the server is returned. Otherwise remove the + lockfile and try again.''' + lockfile = get_lockfile(config) + while True: + try: + return os.open(lockfile, os.O_CREAT | os.O_EXCL | os.O_WRONLY) + except OSError: + pass + server = get_server(config) + if server is not None: + return server + # Couldn't connect; remove lockfile and try again. + remove_lockfile(lockfile) + +def get_server(config): + lockfile = get_lockfile(config) + while True: + create_time = None + try: + with open(lockfile) as f: + (host, port), create_time = ast.literal_eval(f.read()) + server = jsonrpclib.Server('http://%s:%d' % (host, port)) + # Test daemon is running + server.ping() + return server + except: + pass + if not create_time or create_time < time.time() - 1.0: + return None + # Sleep a bit and try again; it might have just been started + time.sleep(1.0) + + + class RequestHandler(SimpleJSONRPCRequestHandler): def do_OPTIONS(self): @@ -172,7 +216,7 @@ class Daemon(DaemonThread): def stop(self): self.print_error("stopping, removing lockfile") - Daemon.remove_lockfile(Daemon.lockfile(self.config)) + remove_lockfile(get_lockfile(self.config)) DaemonThread.stop(self) def init_gui(self, config, plugins): @@ -182,49 +226,3 @@ class Daemon(DaemonThread): gui = __import__('electrum_gui.' + gui_name, fromlist=['electrum_gui']) self.gui = gui.ElectrumGui(config, self, plugins) self.gui.main() - - @staticmethod - def lockfile(config): - return os.path.join(config.path, 'daemon') - - @staticmethod - def remove_lockfile(lockfile): - os.unlink(lockfile) - - @staticmethod - def get_fd_or_server(lockfile): - '''If create is True, tries to create the lockfile, using O_EXCL to - prevent races. If it succeeds it returns the FD. - - Otherwise try and connect to the server specified in the lockfile. - If this succeeds, the server is returned. Otherwise remove the - lockfile and try again.''' - while True: - try: - return os.open(lockfile, os.O_CREAT | os.O_EXCL | os.O_WRONLY) - except OSError: - pass - server = Daemon.get_server(lockfile) - if server is not None: - return server - # Couldn't connect; remove lockfile and try again. - Daemon.remove_lockfile(lockfile) - - @staticmethod - def get_server(lockfile): - while True: - create_time = None - try: - with open(lockfile) as f: - (host, port), create_time = ast.literal_eval(f.read()) - server = jsonrpclib.Server('http://%s:%d' % (host, port)) - # Test daemon is running - server.ping() - return server - except: - pass - if not create_time or create_time < time.time() - 1.0: - return None - # Sleep a bit and try again; it might have just been started - time.sleep(1.0) -