electrum

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

commit 7dd296273fdf426158473f638eb689ac61865268
parent 0bb16f083626815ffc6df39b3dcf6e46a625dfc5
Author: ThomasV <thomasv@gitorious>
Date:   Sun, 16 Mar 2014 12:00:08 +0100

move daemon spawning code into NetworkProxy class

Diffstat:
Melectrum | 37++++++-------------------------------
Mlib/daemon.py | 42++++++++++++++++++++++++++++++++++++++----
Mscripts/get_history | 9++++-----
3 files changed, 48 insertions(+), 40 deletions(-)

diff --git a/electrum b/electrum @@ -110,38 +110,13 @@ def print_help_cb(self, opt, value, parser): def run_command(cmd, password=None, args=[]): import socket if cmd.requires_network and not options.offline: - daemon_started = False - while True: - try: - network = NetworkProxy(config) - break - except socket.error: - if cmd != 'daemon': - if not daemon_started: - print "Starting daemon [%s]"%config.get('server') - daemon_started = True - pid = os.fork() - if (pid == 0): # The first child. - os.chdir("/") - os.setsid() - os.umask(0) - pid2 = os.fork() - if (pid2 == 0): # Second child - server = NetworkServer(config) - try: - server.main_loop() - except KeyboardInterrupt: - print "Ctrl C - Stopping server" - sys.exit(1) - sys.exit(0) - else: - time.sleep(0.1) - else: - print "Daemon not running" - sys.exit(1) + network = NetworkProxy(config) + if not network.start(start_daemon= (True if cmd.name!='daemon' else False)): + print "Daemon not running" + sys.exit(1) + + - network = network - network.start() if wallet: wallet.start_threads(network) wallet.update() diff --git a/lib/daemon.py b/lib/daemon.py @@ -27,26 +27,60 @@ import json import Queue from network import Network from util import print_msg +from simple_config import SimpleConfig class NetworkProxy(threading.Thread): # connects to daemon # sends requests, runs callbacks - def __init__(self, config): + def __init__(self, config = {}): threading.Thread.__init__(self) self.daemon = True - self.config = config + self.config = SimpleConfig(config) if type(config) == type({}) else config self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.daemon_port = config.get('daemon_port', 8000) - self.socket.connect(('', self.daemon_port)) self.message_id = 0 self.unanswered_requests = {} self.subscriptions = {} self.debug = False self.lock = threading.Lock() - + + + def start(self, start_daemon=False): + daemon_started = False + while True: + try: + self.socket.connect(('', self.daemon_port)) + threading.Thread.start(self) + return True + + except socket.error: + if not start_daemon: + return False + + elif not daemon_started: + print "Starting daemon [%s]"%self.config.get('server') + daemon_started = True + pid = os.fork() + if (pid == 0): # The first child. + os.chdir("/") + os.setsid() + os.umask(0) + pid2 = os.fork() + if (pid2 == 0): # Second child + server = NetworkServer(self.config) + try: + server.main_loop() + except KeyboardInterrupt: + print "Ctrl C - Stopping server" + sys.exit(1) + sys.exit(0) + else: + time.sleep(0.1) + + def parse_json(self, message): s = message.find('\n') diff --git a/scripts/get_history b/scripts/get_history @@ -1,7 +1,7 @@ #!/usr/bin/env python import sys -from electrum import Network +from electrum import NetworkProxy, print_json try: addr = sys.argv[1] @@ -9,9 +9,8 @@ except Exception: print "usage: get_history <bitcoin_address>" sys.exit(1) -n = Network() -n.start(wait=True) +n = NetworkProxy() +n.start(start_daemon=True) h = n.synchronous_get([ ('blockchain.address.get_history',[addr]) ])[0] -for item in h: - print item['tx_hash'], item['height'] +print_json(h)