commit 087490a197004b4e5fd5ca823a628c18c15e44d6
parent 8e5fbadc58ee2606f3433dcdbadeaa285b5cb410
Author: ThomasV <thomasv@gitorious>
Date: Mon, 28 Jul 2014 23:42:14 +0200
spawn daemon using daemon.py
Diffstat:
3 files changed, 36 insertions(+), 47 deletions(-)
diff --git a/electrum b/electrum
@@ -46,6 +46,7 @@ if is_local:
from electrum import util
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.daemon import get_daemon
# get password routine
def prompt_password(prompt, confirm=True):
@@ -105,13 +106,11 @@ def print_help_cb(self, opt, value, parser):
sys.exit(1)
-
-
def run_command(cmd, password=None, args=None):
if args is None:
args = [] # Do not use mutables as default values!
if cmd.requires_network and not options.offline:
- s = daemon_socket()
+ s = get_daemon(config, True)
network = NetworkProxy(s, config)
network.start()
while network.is_connecting():
@@ -147,32 +146,6 @@ def run_command(cmd, password=None, args=None):
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__':
@@ -222,7 +195,7 @@ if __name__ == '__main__':
# network interface
if not options.offline:
- s = daemon_socket(start_daemon=options.daemon)
+ s = get_daemon(config, start_daemon=options.daemon)
network = NetworkProxy(s, config)
network.start()
else:
@@ -236,30 +209,20 @@ if __name__ == '__main__':
# we use daemon threads, their termination is enforced.
# this sleep command gives them time to terminate cleanly.
- time.sleep(0.1)
+ time.sleep(0.2)
sys.exit(0)
if cmd == 'daemon':
- if len(args) == 1:
- 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]
+ arg = args[1] if len(args)>1 else None
if arg not in ['start', 'stop', 'status']:
print_msg("syntax: electrum daemon <start|status|stop>")
sys.exit(1)
- s = daemon_socket(start_daemon=False)
+ s = get_daemon(config, False)
if arg == 'start':
if s:
print_msg("Daemon already running")
sys.exit(1)
- do_start_daemon()
+ get_daemon(config, True)
sys.exit(0)
elif arg in ['status','stop']:
if not s:
diff --git a/lib/daemon.py b/lib/daemon.py
@@ -30,11 +30,34 @@ from network import Network
from util import print_error, print_stderr, parse_json
from simple_config import SimpleConfig
-
-
DAEMON_PORT=8001
+def do_start_daemon(config):
+ import subprocess
+ logfile = open(os.path.join(config.path, 'daemon.log'),'w')
+ p = subprocess.Popen(["python",__file__], stderr=logfile, stdout=logfile, close_fds=True)
+ print_stderr("starting daemon (PID %d)"%p.pid)
+
+
+def get_daemon(config, start_daemon=True):
+ import socket
+ 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(config)
+ daemon_started = True
+ else:
+ time.sleep(0.1)
+
class ClientThread(threading.Thread):
diff --git a/scripts/block_headers b/scripts/block_headers
@@ -5,7 +5,10 @@
import time, electrum
# start network
-network = electrum.NetworkProxy(False)
+
+c = electrum.SimpleConfig()
+s = electrum.daemon.get_daemon(c,True)
+network = electrum.NetworkProxy(s,c)
network.start()
# wait until connected