|
@ -46,6 +46,7 @@ if is_local: |
|
|
from electrum import util |
|
|
from electrum import util |
|
|
from electrum import SimpleConfig, Network, Wallet, WalletStorage, NetworkProxy, Commands, known_commands, pick_random_server |
|
|
from electrum import SimpleConfig, Network, Wallet, WalletStorage, NetworkProxy, Commands, known_commands, pick_random_server |
|
|
from electrum.util import print_msg, print_stderr, print_json, set_verbosity |
|
|
from electrum.util import print_msg, print_stderr, print_json, set_verbosity |
|
|
|
|
|
from electrum.daemon import get_daemon |
|
|
|
|
|
|
|
|
# get password routine |
|
|
# get password routine |
|
|
def prompt_password(prompt, confirm=True): |
|
|
def prompt_password(prompt, confirm=True): |
|
@ -105,13 +106,11 @@ def print_help_cb(self, opt, value, parser): |
|
|
sys.exit(1) |
|
|
sys.exit(1) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def run_command(cmd, password=None, args=None): |
|
|
def run_command(cmd, password=None, args=None): |
|
|
if args is None: |
|
|
if args is None: |
|
|
args = [] # Do not use mutables as default values! |
|
|
args = [] # Do not use mutables as default values! |
|
|
if cmd.requires_network and not options.offline: |
|
|
if cmd.requires_network and not options.offline: |
|
|
s = daemon_socket() |
|
|
s = get_daemon(config, True) |
|
|
network = NetworkProxy(s, config) |
|
|
network = NetworkProxy(s, config) |
|
|
network.start() |
|
|
network.start() |
|
|
while network.is_connecting(): |
|
|
while network.is_connecting(): |
|
@ -147,32 +146,6 @@ def run_command(cmd, password=None, args=None): |
|
|
print_json(result) |
|
|
print_json(result) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def do_start_daemon(): |
|
|
|
|
|
import subprocess |
|
|
|
|
|
logfile = open(os.path.join(config.path, 'daemon.log'),'w') |
|
|
|
|
|
p = subprocess.Popen([__file__,"daemon"], stderr=logfile, stdout=logfile, close_fds=True) |
|
|
|
|
|
print_stderr("starting daemon (PID %d)"%p.pid) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def daemon_socket(start_daemon=True): |
|
|
|
|
|
import socket |
|
|
|
|
|
from electrum.daemon import DAEMON_PORT |
|
|
|
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
|
|
|
|
|
daemon_port = config.get('daemon_port', DAEMON_PORT) |
|
|
|
|
|
daemon_started = False |
|
|
|
|
|
while True: |
|
|
|
|
|
try: |
|
|
|
|
|
s.connect(('', daemon_port)) |
|
|
|
|
|
return s |
|
|
|
|
|
except socket.error: |
|
|
|
|
|
if not start_daemon: |
|
|
|
|
|
return False |
|
|
|
|
|
elif not daemon_started: |
|
|
|
|
|
do_start_daemon() |
|
|
|
|
|
daemon_started = True |
|
|
|
|
|
else: |
|
|
|
|
|
time.sleep(0.1) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
if __name__ == '__main__': |
|
|
|
|
|
|
|
@ -222,7 +195,7 @@ if __name__ == '__main__': |
|
|
|
|
|
|
|
|
# network interface |
|
|
# network interface |
|
|
if not options.offline: |
|
|
if not options.offline: |
|
|
s = daemon_socket(start_daemon=options.daemon) |
|
|
s = get_daemon(config, start_daemon=options.daemon) |
|
|
network = NetworkProxy(s, config) |
|
|
network = NetworkProxy(s, config) |
|
|
network.start() |
|
|
network.start() |
|
|
else: |
|
|
else: |
|
@ -236,30 +209,20 @@ if __name__ == '__main__': |
|
|
|
|
|
|
|
|
# we use daemon threads, their termination is enforced. |
|
|
# we use daemon threads, their termination is enforced. |
|
|
# this sleep command gives them time to terminate cleanly. |
|
|
# this sleep command gives them time to terminate cleanly. |
|
|
time.sleep(0.1) |
|
|
time.sleep(0.2) |
|
|
sys.exit(0) |
|
|
sys.exit(0) |
|
|
|
|
|
|
|
|
if cmd == 'daemon': |
|
|
if cmd == 'daemon': |
|
|
if len(args) == 1: |
|
|
arg = args[1] if len(args)>1 else None |
|
|
from electrum import daemon, util |
|
|
|
|
|
util.set_verbosity(True) |
|
|
|
|
|
print_stderr( "Starting daemon [%s]"%config.get('server')) |
|
|
|
|
|
server = daemon.NetworkServer(config) |
|
|
|
|
|
try: |
|
|
|
|
|
server.main_loop() |
|
|
|
|
|
except KeyboardInterrupt: |
|
|
|
|
|
print_msg("Ctrl C - Stopping server") |
|
|
|
|
|
sys.exit(0) |
|
|
|
|
|
arg = args[1] |
|
|
|
|
|
if arg not in ['start', 'stop', 'status']: |
|
|
if arg not in ['start', 'stop', 'status']: |
|
|
print_msg("syntax: electrum daemon <start|status|stop>") |
|
|
print_msg("syntax: electrum daemon <start|status|stop>") |
|
|
sys.exit(1) |
|
|
sys.exit(1) |
|
|
s = daemon_socket(start_daemon=False) |
|
|
s = get_daemon(config, False) |
|
|
if arg == 'start': |
|
|
if arg == 'start': |
|
|
if s: |
|
|
if s: |
|
|
print_msg("Daemon already running") |
|
|
print_msg("Daemon already running") |
|
|
sys.exit(1) |
|
|
sys.exit(1) |
|
|
do_start_daemon() |
|
|
get_daemon(config, True) |
|
|
sys.exit(0) |
|
|
sys.exit(0) |
|
|
elif arg in ['status','stop']: |
|
|
elif arg in ['status','stop']: |
|
|
if not s: |
|
|
if not s: |
|
|