|
|
@ -46,15 +46,12 @@ class ElectrumWebSocket(WebSocket): |
|
|
|
|
|
|
|
class WsClientThread(util.DaemonThread): |
|
|
|
|
|
|
|
def __init__(self, config, server): |
|
|
|
def __init__(self, config, network): |
|
|
|
util.DaemonThread.__init__(self) |
|
|
|
self.server = server |
|
|
|
self.network = network |
|
|
|
self.config = config |
|
|
|
self.response_queue = Queue.Queue() |
|
|
|
self.server.add_client(self) |
|
|
|
self.subscriptions = defaultdict(list) |
|
|
|
self.sub_ws = defaultdict(list) |
|
|
|
self.counter = 0 |
|
|
|
|
|
|
|
def make_request(self, request_id): |
|
|
|
# read json file |
|
|
@ -77,13 +74,11 @@ class WsClientThread(util.DaemonThread): |
|
|
|
addr, amount = self.make_request(request_id) |
|
|
|
except: |
|
|
|
continue |
|
|
|
method = 'blockchain.address.subscribe' |
|
|
|
params = [addr] |
|
|
|
request = {'method':method, 'params':params, 'id':self.counter} |
|
|
|
self.subscriptions[method].append(params) |
|
|
|
self.sub_ws[self.counter] = ws, amount, request |
|
|
|
self.counter += 1 |
|
|
|
self.server.send_request(self, request) |
|
|
|
l = self.subscriptions.get(addr, []) |
|
|
|
l.append((ws, amount)) |
|
|
|
self.subscriptions[addr] = l |
|
|
|
self.network.send([('blockchain.address.subscribe', [addr])], self.response_queue.put) |
|
|
|
|
|
|
|
|
|
|
|
def run(self): |
|
|
|
threading.Thread(target=self.reading_thread).start() |
|
|
@ -92,32 +87,21 @@ class WsClientThread(util.DaemonThread): |
|
|
|
r = self.response_queue.get(timeout=0.1) |
|
|
|
except Queue.Empty: |
|
|
|
continue |
|
|
|
id = r.get('id') |
|
|
|
if id is None: |
|
|
|
method = r.get('method') |
|
|
|
params = r.get('params') |
|
|
|
else: |
|
|
|
ws, amount, rr = self.sub_ws[id] |
|
|
|
method = rr.get('method') |
|
|
|
params = rr.get('params') |
|
|
|
|
|
|
|
util.print_error('response', r) |
|
|
|
method = r.get('method') |
|
|
|
params = r.get('params') |
|
|
|
result = r.get('result') |
|
|
|
|
|
|
|
if method == 'blockchain.address.subscribe': |
|
|
|
util.print_error('response', r) |
|
|
|
if result is not None: |
|
|
|
request = {'method':'blockchain.address.get_balance', 'params':params, 'id':self.counter} |
|
|
|
self.server.send_request(self, request) |
|
|
|
self.sub_ws[self.counter] = ws, amount, request |
|
|
|
self.counter += 1 |
|
|
|
|
|
|
|
if r.get('method') == 'blockchain.address.get_balance': |
|
|
|
util.print_error('response', r) |
|
|
|
if not ws.closed: |
|
|
|
if sum(result.values()) >=amount: |
|
|
|
ws.sendMessage(unicode('paid')) |
|
|
|
|
|
|
|
self.server.remove_client(self) |
|
|
|
self.network.send([('blockchain.address.get_balance', params)], self.response_queue.put) |
|
|
|
elif method == 'blockchain.address.get_balance': |
|
|
|
addr = params[0] |
|
|
|
l = self.subscriptions.get(addr, []) |
|
|
|
for ws, amount in :l: |
|
|
|
if not ws.closed: |
|
|
|
if sum(result.values()) >=amount: |
|
|
|
ws.sendMessage(unicode('paid')) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class WebSocketServer(threading.Thread): |
|
|
|