9 from utils import random_string, timestr, print_log
14 def __init__(self, config):
15 self.lock = threading.Lock()
20 print_log("Stopping Stratum")
29 class Processor(threading.Thread):
32 threading.Thread.__init__(self)
34 self.dispatcher = None
35 self.queue = queue.Queue()
37 def process(self, session, request):
40 def add_request(self, session, request):
41 self.queue.put((session, request))
43 def push_response(self, session, response):
44 #print "response", response
45 self.dispatcher.request_dispatcher.push_response(session, response)
48 while not self.shared.stopped():
49 request, session = self.queue.get(10000000000)
51 self.process(request, session)
53 traceback.print_exc(file=sys.stdout)
55 print_log("processor terminating")
60 def __init__(self, config):
61 self.shared = Shared(config)
62 self.request_dispatcher = RequestDispatcher(self.shared)
63 self.request_dispatcher.start()
64 self.response_dispatcher = \
65 ResponseDispatcher(self.shared, self.request_dispatcher)
66 self.response_dispatcher.start()
68 def register(self, prefix, processor):
69 processor.dispatcher = self
70 processor.shared = self.shared
72 self.request_dispatcher.processors[prefix] = processor
75 class RequestDispatcher(threading.Thread):
77 def __init__(self, shared):
79 threading.Thread.__init__(self)
81 self.request_queue = queue.Queue()
82 self.response_queue = queue.Queue()
83 self.lock = threading.Lock()
84 self.idlock = threading.Lock()
88 def push_response(self, session, item):
89 self.response_queue.put((session, item))
91 def pop_response(self):
92 return self.response_queue.get()
94 def push_request(self, session, item):
95 self.request_queue.put((session, item))
97 def pop_request(self):
98 return self.request_queue.get()
100 def get_session_by_address(self, address):
101 for x in self.sessions.values():
102 if x.address == address:
106 if self.shared is None:
107 raise TypeError("self.shared not set in Processor")
111 while not self.shared.stopped():
112 session, request = self.pop_request()
114 self.do_dispatch(session, request)
116 traceback.print_exc(file=sys.stdout)
118 if time.time() - lastgc > 60.0:
119 self.collect_garbage()
127 def do_dispatch(self, session, request):
128 """ dispatch request to the relevant processor """
130 method = request['method']
131 params = request.get('params', [])
132 suffix = method.split('.')[-1]
134 if session is not None:
135 if suffix == 'subscribe':
136 session.subscribe_to_service(method, params)
138 prefix = request['method'].split('.')[0]
140 p = self.processors[prefix]
142 print_log("error: no processor for", prefix)
145 p.add_request(session, request)
147 if method in ['server.version']:
148 session.version = params[0]
150 session.protocol_version = float(params[1])
155 def get_sessions(self):
157 r = self.sessions.values()
160 def add_session(self, session):
163 self.sessions[key] = session
165 def remove_session(self, session):
168 self.sessions.pop(key)
170 def collect_garbage(self):
172 for session in self.sessions.values():
173 if (now - session.time) > session.timeout:
180 def __init__(self, dispatcher):
181 self.dispatcher = dispatcher
182 self.bp = self.dispatcher.processors['blockchain']
183 self._stopped = False
184 self.lock = threading.Lock()
185 self.subscriptions = []
188 self.version = 'unknown'
189 self.protocol_version = 0.
190 self.time = time.time()
191 threading.Timer(2, self.info).start()
195 return self.name + self.address
198 # Debugging method. Doesn't need to be threadsafe.
200 for sub in self.subscriptions:
203 if method == 'blockchain.address.subscribe':
209 if self.subscriptions:
210 print_log("%4s" % self.name,
211 "%15s" % self.address,
213 "%3d" % len(self.subscriptions),
223 self.dispatcher.remove_session(self)
224 self.stop_subscriptions()
236 def subscribe_to_service(self, method, params):
238 if (method, params) not in self.subscriptions:
239 self.subscriptions.append((method,params))
242 def stop_subscriptions(self):
246 s = self.subscriptions[:]
248 for method, params in s:
250 if method == 'blockchain.numblocks.subscribe':
251 if self in bp.watch_blocks:
252 bp.watch_blocks.remove(self)
253 elif method == 'blockchain.headers.subscribe':
254 if self in bp.watch_headers:
255 bp.watch_headers.remove(self)
256 elif method == "blockchain.address.subscribe":
258 l = bp.watched_addresses.get(addr)
268 bp.watched_addresses.pop(addr)
271 self.subscriptions = []
274 class ResponseDispatcher(threading.Thread):
276 def __init__(self, shared, request_dispatcher):
278 self.request_dispatcher = request_dispatcher
279 threading.Thread.__init__(self)
283 while not self.shared.stopped():
284 session, response = self.request_dispatcher.pop_response()
285 session.send_response(response)