change abe backend to print_log
[electrum-server.git] / processor.py
1 import json
2 import socket
3 import threading
4 import time
5 import traceback, sys
6 import Queue as queue
7
8 def random_string(N):
9     import random, string
10     return ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(N))
11
12 def timestr():
13     return time.strftime("[%d/%m/%Y-%H:%M:%S]")
14
15
16 print_lock = threading.Lock()
17 def print_log(*args):
18     args = [str(item) for item in args]
19     with print_lock:
20         sys.stderr.write(timestr() + " " + " ".join(args) + "\n")
21         sys.stderr.flush()
22
23
24
25 class Shared:
26
27     def __init__(self, config):
28         self.lock = threading.Lock()
29         self._stopped = False
30         self.config = config
31
32     def stop(self):
33         print_log( "Stopping Stratum" )
34         with self.lock:
35             self._stopped = True
36
37     def stopped(self):
38         with self.lock:
39             return self._stopped
40
41
42 class Processor(threading.Thread):
43
44     def __init__(self):
45         threading.Thread.__init__(self)
46         self.daemon = True
47         self.dispatcher = None
48         self.queue = queue.Queue()
49
50     def process(self, request):
51         pass
52
53     def add_request(self, request):
54         self.queue.put(request)
55
56     def push_response(self, response):
57         #print "response", response
58         self.dispatcher.request_dispatcher.push_response(response)
59
60     def run(self):
61         while not self.shared.stopped():
62             request = self.queue.get(10000000000)
63             try:
64                 self.process(request)
65             except:
66                 traceback.print_exc(file=sys.stdout)
67
68         print_log( "processor terminating")
69             
70
71
72 class Dispatcher:
73
74     def __init__(self, config):
75         self.shared = Shared(config)
76         self.request_dispatcher = RequestDispatcher(self.shared)
77         self.request_dispatcher.start()
78         self.response_dispatcher = \
79             ResponseDispatcher(self.shared, self.request_dispatcher)
80         self.response_dispatcher.start()
81
82     def register(self, prefix, processor):
83         processor.dispatcher = self
84         processor.shared = self.shared
85         processor.start()
86         self.request_dispatcher.processors[prefix] = processor
87
88
89
90 class RequestDispatcher(threading.Thread):
91
92     def __init__(self, shared):
93         self.shared = shared
94         threading.Thread.__init__(self)
95         self.daemon = True
96         self.request_queue = queue.Queue()
97         self.response_queue = queue.Queue()
98         self.internal_ids = {}
99         self.internal_id = 1
100         self.lock = threading.Lock()
101         self.sessions = []
102         self.processors = {}
103
104     def push_response(self, item):
105         self.response_queue.put(item)
106
107     def pop_response(self):
108         return self.response_queue.get()
109
110     def push_request(self, session, item):
111         self.request_queue.put((session,item))
112
113     def pop_request(self):
114         return self.request_queue.get()
115
116     def get_session_by_address(self, address):
117         for x in self.sessions:
118             if x.address == address:
119                 return x
120
121     def get_session_id(self, internal_id):
122         with self.lock:
123             return self.internal_ids.pop(internal_id)
124
125     def store_session_id(self, session, msgid):
126         with self.lock:
127             self.internal_ids[self.internal_id] = session, msgid
128             r = self.internal_id
129             self.internal_id += 1
130             return r
131
132     def run(self):
133         if self.shared is None:
134             raise TypeError("self.shared not set in Processor")
135         while not self.shared.stopped():
136             session, request = self.pop_request()
137             try:
138                 self.do_dispatch(session, request)
139             except:
140                 traceback.print_exc(file=sys.stdout)
141                 
142
143         self.stop()
144
145     def stop(self):
146         pass
147
148     def do_dispatch(self, session, request):
149         """ dispatch request to the relevant processor """
150
151         method = request['method']
152         params = request.get('params',[])
153         suffix = method.split('.')[-1]
154
155         if session is not None:
156             is_new = session.protocol_version >= 0.5
157             if suffix == 'subscribe':
158                 session.subscribe_to_service(method, params)
159
160         # store session and id locally
161         request['id'] = self.store_session_id(session, request['id'])
162
163         prefix = request['method'].split('.')[0]
164         try:
165             p = self.processors[prefix]
166         except:
167             print_log( "error: no processor for", prefix)
168             return
169
170         p.add_request(request)
171
172         if method in ['server.version']:
173             session.version = params[0]
174             try:
175                 session.protocol_version = float(params[1])
176             except:
177                 pass
178
179             #if session.protocol_version < 0.6:
180             #    print_log("stopping session from old client", session.protocol_version)
181             #    session.stop()
182
183     def get_sessions(self):
184         with self.lock:
185             r = self.sessions[:]
186         return r
187
188     def add_session(self, session):
189         with self.lock:
190             self.sessions.append(session)
191
192     def collect_garbage(self):
193         # Deep copy entire sessions list and blank it
194         # This is done to minimise lock contention
195         with self.lock:
196             sessions = self.sessions[:]
197             self.sessions = []
198         for session in sessions:
199             if not session.stopped():
200                 # If session is still alive then re-add it back
201                 # to our internal register
202                 self.add_session(session)
203
204
205 class Session:
206
207     def __init__(self):
208         self._stopped = False
209         self.lock = threading.Lock()
210         self.subscriptions = []
211         self.address = ''
212         self.name = ''
213         self.version = 'unknown'
214         self.protocol_version = 0.
215         self.time = time.time()
216         threading.Timer(2, self.info).start()
217
218     # Debugging method. Doesn't need to be threadsafe.
219     def info(self):
220         for sub in self.subscriptions:
221             #print sub
222             method = sub[0]
223             if method == 'blockchain.address.subscribe':
224                 addr = sub[1]
225                 break
226         else:
227             addr = None
228
229         if self.subscriptions:
230             print_log( "%4s"%self.name, "%15s"%self.address, "%35s"%addr, "%3d"%len(self.subscriptions), self.version )
231
232     def stopped(self):
233         with self.lock:
234             return self._stopped
235
236     def subscribe_to_service(self, method, params):
237         subdesc = self.build_subdesc(method, params)
238         with self.lock:
239             if subdesc is not None:
240                 self.subscriptions.append(subdesc)
241
242     # subdesc = A subscription description
243     @staticmethod
244     def build_subdesc(method, params):
245         if method == "blockchain.numblocks.subscribe":
246             return method,
247         elif method == "blockchain.headers.subscribe":
248             return method,
249         elif method in ["blockchain.address.subscribe"]:
250             if not params:
251                 return None
252             else:
253                 return method, params[0]
254         else:
255             return None
256
257     def contains_subscription(self, subdesc):
258         with self.lock:
259             return subdesc in self.subscriptions
260     
261
262 class ResponseDispatcher(threading.Thread):
263
264     def __init__(self, shared, request_dispatcher):
265         self.shared = shared
266         self.request_dispatcher = request_dispatcher
267         threading.Thread.__init__(self)
268         self.daemon = True
269
270     def run(self):
271         while not self.shared.stopped():
272             self.update()
273
274     def update(self):
275         response = self.request_dispatcher.pop_response()
276         #print "pop response", response
277         internal_id = response.get('id')
278         method = response.get('method')
279         params = response.get('params')
280
281         # A notification
282         if internal_id is None: # and method is not None and params is not None:
283             found = self.notification(method, params, response)
284             if not found and method == 'blockchain.address.subscribe':
285                 params2 = [self.shared.config.get('server','password')] + params
286                 self.request_dispatcher.push_request(None,{'method':method.replace('.subscribe', '.unsubscribe'), 'params':params2, 'id':None})
287
288         # A response
289         elif internal_id is not None: 
290             self.send_response(internal_id, response)
291         else:
292             print_log( "no method", response)
293
294     def notification(self, method, params, response):
295         subdesc = Session.build_subdesc(method, params)
296         found = False
297         for session in self.request_dispatcher.sessions:
298             if session.stopped():
299                 continue
300             if session.contains_subscription(subdesc):
301                 session.send_response(response)
302                 found = True
303         # if not found: print_log( "no subscriber for", subdesc)
304         return found
305
306     def send_response(self, internal_id, response):
307         session, message_id = self.request_dispatcher.get_session_id(internal_id)
308         if session:
309             response['id'] = message_id
310             session.send_response(response)
311         #else:
312         #    print_log( "send_response: no session", message_id, internal_id, response )
313