electrum-bitcoinprivate/lib/websockets.py

135 lines
4.5 KiB
Python
Raw Normal View History

2015-07-24 02:39:12 -07:00
#!/usr/bin/env python
#
# Electrum - lightweight Bitcoin client
# Copyright (C) 2015 Thomas Voegtlin
#
2016-02-23 02:36:42 -08:00
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
2015-07-24 02:39:12 -07:00
#
2016-02-23 02:36:42 -08:00
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
2015-07-24 02:39:12 -07:00
#
2016-02-23 02:36:42 -08:00
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
2015-07-24 02:39:12 -07:00
import threading, Queue, os, json, time
from collections import defaultdict
try:
from SimpleWebSocketServer import WebSocket, SimpleSSLWebSocketServer
except ImportError:
import sys
sys.exit("install SimpleWebSocketServer")
2015-07-24 02:39:12 -07:00
2015-09-30 02:06:27 -07:00
import util
2015-07-24 02:39:12 -07:00
request_queue = Queue.Queue()
class ElectrumWebSocket(WebSocket):
def handleMessage(self):
assert self.data[0:3] == 'id:'
2015-11-25 01:48:34 -08:00
util.print_error("message received", self.data)
2015-07-24 02:39:12 -07:00
request_id = self.data[3:]
request_queue.put((self, request_id))
def handleConnected(self):
2015-07-24 05:23:03 -07:00
util.print_error("connected", self.address)
2015-07-24 02:39:12 -07:00
def handleClose(self):
2015-07-24 05:23:03 -07:00
util.print_error("closed", self.address)
2015-07-24 02:39:12 -07:00
2015-09-30 02:06:27 -07:00
class WsClientThread(util.DaemonThread):
2015-07-24 02:39:12 -07:00
2015-11-25 01:32:46 -08:00
def __init__(self, config, network):
2015-07-24 02:39:12 -07:00
util.DaemonThread.__init__(self)
2015-11-25 01:32:46 -08:00
self.network = network
2015-07-24 02:39:12 -07:00
self.config = config
self.response_queue = Queue.Queue()
self.subscriptions = defaultdict(list)
def make_request(self, request_id):
# read json file
rdir = self.config.get('requests_dir')
n = os.path.join(rdir, request_id + '.json')
with open(n) as f:
s = f.read()
d = json.loads(s)
addr = d.get('address')
amount = d.get('amount')
return addr, amount
def reading_thread(self):
while self.is_running():
try:
ws, request_id = request_queue.get()
except Queue.Empty:
continue
try:
addr, amount = self.make_request(request_id)
except:
continue
2015-11-25 01:32:46 -08:00
l = self.subscriptions.get(addr, [])
l.append((ws, amount))
self.subscriptions[addr] = l
self.network.send([('blockchain.address.subscribe', [addr])], self.response_queue.put)
2015-07-24 02:39:12 -07:00
def run(self):
threading.Thread(target=self.reading_thread).start()
while self.is_running():
try:
r = self.response_queue.get(timeout=0.1)
except Queue.Empty:
continue
2015-11-25 01:32:46 -08:00
util.print_error('response', r)
method = r.get('method')
params = r.get('params')
2015-07-24 02:39:12 -07:00
result = r.get('result')
2015-11-25 02:37:10 -08:00
if result is None:
continue
2015-07-24 02:39:12 -07:00
if method == 'blockchain.address.subscribe':
2015-11-25 02:37:10 -08:00
self.network.send([('blockchain.address.get_balance', params)], self.response_queue.put)
2015-11-25 01:32:46 -08:00
elif method == 'blockchain.address.get_balance':
addr = params[0]
l = self.subscriptions.get(addr, [])
2015-11-25 01:35:14 -08:00
for ws, amount in l:
2015-11-25 01:32:46 -08:00
if not ws.closed:
if sum(result.values()) >=amount:
ws.sendMessage(unicode('paid'))
2015-07-24 02:39:12 -07:00
class WebSocketServer(threading.Thread):
def __init__(self, config, ns):
threading.Thread.__init__(self)
self.config = config
self.net_server = ns
self.daemon = True
def run(self):
t = WsClientThread(self.config, self.net_server)
t.start()
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()