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