Skip to content

Commit

Permalink
move websocket api to electrum-merchant
Browse files Browse the repository at this point in the history
  • Loading branch information
ysangkok committed Jan 31, 2019
1 parent 320c31d commit 8fb8b97
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 59 deletions.
12 changes: 1 addition & 11 deletions electrum/wallet.py
Expand Up @@ -1009,17 +1009,7 @@ def get_payment_request(self, addr, config):
self.print_stderr('Invalid config setting for "url_rewrite". err:', e)
out['request_url'] = os.path.join(baseurl, 'req', key[0], key[1], key, key)
out['URI'] += '&r=' + out['request_url']
out['index_url'] = os.path.join(baseurl, 'index.html') + '?id=' + key
websocket_server_announce = config.get('websocket_server_announce')
if websocket_server_announce:
out['websocket_server'] = websocket_server_announce
else:
out['websocket_server'] = config.get('websocket_server', 'localhost')
websocket_port_announce = config.get('websocket_port_announce')
if websocket_port_announce:
out['websocket_port'] = websocket_port_announce
else:
out['websocket_port'] = config.get('websocket_port', 9999)
out['index_url'] = os.path.join(baseurl, 'static/index.html') + '?id=' + key
return out

def get_request_status(self, key):
Expand Down
50 changes: 5 additions & 45 deletions electrum/websockets.py
Expand Up @@ -31,11 +31,6 @@
import traceback
import sys

try:
from SimpleWebSocketServer import WebSocket, SimpleSSLWebSocketServer
except ImportError:
sys.exit("install SimpleWebSocketServer")

from .util import PrintError
from . import bitcoin
from .synchronizer import SynchronizerBase
Expand All @@ -47,25 +42,10 @@

request_queue = asyncio.Queue()


class ElectrumWebSocket(WebSocket, PrintError):

def handleMessage(self):
assert self.data[0:3] == 'id:'
self.print_error("message received", self.data)
request_id = self.data[3:]
asyncio.run_coroutine_threadsafe(
request_queue.put((self, request_id)), asyncio.get_event_loop())

def handleConnected(self):
self.print_error("connected", self.address)

def handleClose(self):
self.print_error("closed", self.address)


class BalanceMonitor(SynchronizerBase):

"""
Used in electrum-merchant
"""
def __init__(self, config: 'SimpleConfig', network: 'Network'):
SynchronizerBase.__init__(self, network)
self.config = config
Expand Down Expand Up @@ -104,25 +84,5 @@ async def _on_address_status(self, addr, status):
for ws, amount in self.expected_payments[addr]:
if not ws.closed:
if sum(balance.values()) >= amount:
ws.sendMessage('paid')


class WebSocketServer(threading.Thread):

def __init__(self, config: 'SimpleConfig', network: 'Network'):
threading.Thread.__init__(self)
self.config = config
self.network = network
asyncio.set_event_loop(network.asyncio_loop)
self.daemon = True
self.balance_monitor = BalanceMonitor(self.config, self.network)
self.start()

def run(self):
asyncio.set_event_loop(self.network.asyncio_loop)
host = self.config.get('websocket_server')
port = self.config.get('websocket_port', 9999)
certfile = self.config.get('ssl_chain')
keyfile = self.config.get('ssl_privkey')
self.server = SimpleSSLWebSocketServer(host, port, ElectrumWebSocket, certfile, keyfile)
self.server.serveforever()
await ws.send_str('paid')
await ws.close()
3 changes: 0 additions & 3 deletions run_electrum
Expand Up @@ -358,9 +358,6 @@ if __name__ == '__main__':
sys.exit(0)
init_plugins(config, 'cmdline')
d = daemon.Daemon(config, fd)
if config.get('websocket_server'):
from electrum import websockets
websockets.WebSocketServer(config, d.network)
if config.get('requests_dir'):
path = os.path.join(config.get('requests_dir'), 'index.html')
if not os.path.exists(path):
Expand Down

0 comments on commit 8fb8b97

Please sign in to comment.