X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=processor.py;h=84af96273a67fb0c67cc34a3f1a33eb4e935ad79;hb=549ff3f84b6ea240285659e29ed613aa9a2182d7;hp=d0fe91f45bbc85b254f8fa4081337b850256704d;hpb=70f23254388eb74ca7a05009ccf2a5d7a723ec36;p=electrum-server.git diff --git a/processor.py b/processor.py index d0fe91f..84af962 100644 --- a/processor.py +++ b/processor.py @@ -2,8 +2,17 @@ import json import socket import threading import time +import traceback, sys import Queue as queue +def random_string(N): + import random, string + return ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(N)) + +def timestr(): + return time.strftime("[%d/%m/%Y-%H:%M:%S]") + + class Shared: def __init__(self): @@ -23,7 +32,39 @@ class Shared: class Processor(threading.Thread): def __init__(self): - self.shared = None + threading.Thread.__init__(self) + self.daemon = True + self.dispatcher = None + + def process(self, request): + pass + + def push_response(self, response): + self.dispatcher.request_dispatcher.push_response(response) + + + +class Dispatcher: + + def __init__(self): + self.shared = Shared() + self.request_dispatcher = RequestDispatcher(self.shared) + self.request_dispatcher.start() + self.response_dispatcher = ResponseDispatcher(self.shared, self.request_dispatcher) + self.response_dispatcher.start() + + def register(self, prefix, processor): + processor.dispatcher = self + processor.shared = self.shared + processor.start() + self.request_dispatcher.processors[prefix] = processor + + + +class RequestDispatcher(threading.Thread): + + def __init__(self, shared): + self.shared = shared threading.Thread.__init__(self) self.daemon = True self.request_queue = queue.Queue() @@ -32,6 +73,7 @@ class Processor(threading.Thread): self.internal_id = 1 self.lock = threading.Lock() self.sessions = [] + self.processors = {} def push_response(self, item): self.response_queue.put(item) @@ -61,28 +103,39 @@ class Processor(threading.Thread): raise TypeError("self.shared not set in Processor") while not self.shared.stopped(): session, request = self.pop_request() - - method = request['method'] - params = request.get('params',[]) - - if method in [ 'numblocks.subscribe', 'address.subscribe', 'server.peers']: - session.subscribe_to_service(method, params) - - # store session and id locally - request['id'] = self.store_session_id(session, request['id']) - self.process(request) + self.process(session, request) self.stop() def stop(self): pass - def process(self, request): - print "New request", request - # Do stuff... - # response = request - # When ready, you call - # self.push_response(response) + def process(self, session, request): + method = request['method'] + params = request.get('params',[]) + + suffix = method.split('.')[-1] + if suffix == 'subscribe': + session.subscribe_to_service(method, params) + + # store session and id locally + request['id'] = self.store_session_id(session, request['id']) + + # dispatch request to the relevant module.. + prefix = request['method'].split('.')[0] + try: + p = self.processors[prefix] + except: + print "error: no processor for", prefix + return + try: + p.process(request) + except: + traceback.print_exc(file=sys.stdout) + + if method in ['server.version']: + session.version = params[0] + def add_session(self, session): with self.lock: @@ -107,6 +160,19 @@ class Session: self._stopped = False self.lock = threading.Lock() self.subscriptions = [] + self.address = '' + self.name = '' + threading.Timer(2, self.info).start() + + def info(self): + for s in self.subscriptions: + m, p = s + if m == 'blockchain.address.subscribe': + addr = p[0] + break + else: + addr = None + print timestr(), self.name, self.address, addr, len(self.subscriptions), self.version def stopped(self): with self.lock: @@ -117,12 +183,13 @@ class Session: self.subscriptions.append((method, params)) -class Dispatcher(threading.Thread): +class ResponseDispatcher(threading.Thread): def __init__(self, shared, processor): self.shared = shared self.processor = processor threading.Thread.__init__(self) + self.daemon = True def run(self): while not self.shared.stopped():