-WIP-electrum-btcp/lib/daemon.py

243 lines
8.4 KiB
Python
Raw Normal View History

2015-11-30 01:09:54 -08:00
#!/usr/bin/env python
#
# Electrum - lightweight Bitcoin client
# Copyright (C) 2015 Thomas Voegtlin
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# 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
import os
import sys
2015-12-08 01:55:34 -08:00
2015-11-30 01:09:54 -08:00
import jsonrpclib
from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer, SimpleJSONRPCRequestHandler
from network import Network
from util import check_www_dir, json_decode, DaemonThread
from util import print_msg, print_error, print_stderr
2015-11-30 01:09:54 -08:00
from wallet import WalletStorage, Wallet
2016-01-13 03:55:51 -08:00
from wizard import WizardBase
2015-11-30 01:09:54 -08:00
from commands import known_commands, Commands
from simple_config import SimpleConfig
2015-11-30 01:09:54 -08:00
2015-12-08 01:55:34 -08:00
def lockfile(config):
return os.path.join(config.path, 'daemon')
2015-11-30 01:09:54 -08:00
def get_daemon(config):
2015-12-08 01:55:34 -08:00
try:
with open(lockfile(config)) as f:
host, port = ast.literal_eval(f.read())
except:
return
2015-11-30 01:09:54 -08:00
server = jsonrpclib.Server('http://%s:%d' % (host, port))
# check if daemon is running
try:
server.ping()
return server
except:
pass
class RequestHandler(SimpleJSONRPCRequestHandler):
def do_OPTIONS(self):
self.send_response(200)
self.end_headers()
def end_headers(self):
self.send_header("Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept")
self.send_header("Access-Control-Allow-Origin", "*")
SimpleJSONRPCRequestHandler.end_headers(self)
2015-12-05 04:38:20 -08:00
class Daemon(DaemonThread):
2015-11-30 01:09:54 -08:00
def __init__(self, config):
2015-12-05 04:38:20 -08:00
DaemonThread.__init__(self)
2015-11-30 01:09:54 -08:00
self.config = config
if config.get('offline'):
self.network = None
else:
self.network = Network(config)
self.network.start()
self.gui = None
2015-11-30 01:09:54 -08:00
self.wallets = {}
self.wallet = None
2015-11-30 01:09:54 -08:00
self.cmd_runner = Commands(self.config, self.wallet, self.network)
host = config.get('rpchost', 'localhost')
2015-12-08 01:55:34 -08:00
port = config.get('rpcport', 0)
2015-11-30 01:09:54 -08:00
self.server = SimpleJSONRPCServer((host, port), requestHandler=RequestHandler, logRequests=False)
2015-12-08 01:55:34 -08:00
with open(lockfile(config), 'w') as f:
f.write(repr(self.server.socket.getsockname()))
2015-12-07 09:42:12 -08:00
self.server.timeout = 0.1
2015-11-30 01:09:54 -08:00
for cmdname in known_commands:
self.server.register_function(getattr(self.cmd_runner, cmdname), cmdname)
self.server.register_function(self.run_cmdline, 'run_cmdline')
self.server.register_function(self.ping, 'ping')
2015-11-30 23:58:00 -08:00
self.server.register_function(self.run_daemon, 'daemon')
self.server.register_function(self.run_gui, 'gui')
2015-11-30 01:09:54 -08:00
def ping(self):
return True
2015-11-30 23:58:00 -08:00
def run_daemon(self, config):
2015-11-30 01:09:54 -08:00
sub = config.get('subcommand')
assert sub in ['start', 'stop', 'status']
if sub == 'start':
response = "Daemon already running"
elif sub == 'status':
if self.network:
p = self.network.get_parameters()
response = {
'path': self.network.config.path,
'server': p[0],
'blockchain_height': self.network.get_local_height(),
'server_height': self.network.get_server_height(),
'nodes': self.network.get_interfaces(),
'connected': self.network.is_connected(),
'auto_connect': p[4],
'wallets': {k: w.is_up_to_date()
for k, w in self.wallets.items()},
}
else:
response = "Daemon offline"
2015-11-30 01:09:54 -08:00
elif sub == 'stop':
self.stop()
response = "Daemon stopped"
return response
def run_gui(self, config_options):
2015-11-30 01:09:54 -08:00
config = SimpleConfig(config_options)
if self.gui:
if hasattr(self.gui, 'new_window'):
path = config.get_wallet_path()
self.gui.new_window(path, config.get('url'))
response = "ok"
else:
response = "error: current GUI does not support multiple windows"
else:
response = "Error: Electrum is running in daemon mode. Please stop the daemon first."
return response
2016-01-13 02:31:23 -08:00
def load_wallet(self, path, get_wizard=None):
2015-11-30 01:09:54 -08:00
if path in self.wallets:
wallet = self.wallets[path]
else:
2016-01-13 05:30:09 -08:00
storage = WalletStorage(path)
2016-01-13 02:27:17 -08:00
if get_wizard:
2016-01-13 05:30:09 -08:00
if storage.file_exists:
wallet = Wallet(storage)
action = wallet.get_action()
else:
action = 'new'
if action:
wizard = get_wizard()
wallet = wizard.run(self.network, storage)
2016-01-13 06:02:58 -08:00
else:
wallet.start_threads(self.network)
else:
wallet = Wallet(storage)
wallet.start_threads(self.network)
if wallet:
self.wallets[path] = wallet
2015-11-30 01:09:54 -08:00
return wallet
def run_cmdline(self, config_options):
config = SimpleConfig(config_options)
cmdname = config.get('cmd')
cmd = known_commands[cmdname]
path = config.get_wallet_path()
wallet = self.load_wallet(path) if cmd.requires_wallet else None
2015-11-30 01:09:54 -08:00
# arguments passed to function
args = map(lambda x: config.get(x), cmd.params)
# decode json arguments
args = map(json_decode, args)
# options
args += map(lambda x: config.get(x), cmd.options)
cmd_runner = Commands(config, wallet, self.network,
password=config_options.get('password'),
new_password=config_options.get('new_password'))
2015-11-30 01:09:54 -08:00
func = getattr(cmd_runner, cmd.name)
result = func(*args)
return result
def run(self):
while self.is_running():
self.server.handle_request()
2015-12-08 01:55:34 -08:00
os.unlink(lockfile(self.config))
2015-11-30 01:09:54 -08:00
def stop(self):
for k, wallet in self.wallets.items():
wallet.stop_threads()
2015-12-05 04:38:20 -08:00
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)