10 return ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(N))
13 return time.strftime("[%d/%m/%Y-%H:%M:%S]")
19 self.lock = threading.Lock()
23 print "Stopping Stratum"
32 class Processor(threading.Thread):
35 threading.Thread.__init__(self)
37 self.dispatcher = None
39 def process(self, request):
42 def push_response(self, response):
43 self.dispatcher.request_dispatcher.push_response(response)
50 self.shared = Shared()
51 self.request_dispatcher = RequestDispatcher(self.shared)
52 self.request_dispatcher.start()
53 self.response_dispatcher = ResponseDispatcher(self.shared, self.request_dispatcher)
54 self.response_dispatcher.start()
56 def register(self, prefix, processor):
57 processor.dispatcher = self
58 processor.shared = self.shared
60 self.request_dispatcher.processors[prefix] = processor
64 class RequestDispatcher(threading.Thread):
66 def __init__(self, shared):
68 threading.Thread.__init__(self)
70 self.request_queue = queue.Queue()
71 self.response_queue = queue.Queue()
72 self.internal_ids = {}
74 self.lock = threading.Lock()
78 def push_response(self, item):
79 self.response_queue.put(item)
81 def pop_response(self):
82 return self.response_queue.get()
84 def push_request(self, session, item):
85 self.request_queue.put((session,item))
87 def pop_request(self):
88 return self.request_queue.get()
90 def get_session_id(self, internal_id):
92 return self.internal_ids.pop(internal_id)
94 def store_session_id(self, session, msgid):
96 self.internal_ids[self.internal_id] = session, msgid
102 if self.shared is None:
103 raise TypeError("self.shared not set in Processor")
104 while not self.shared.stopped():
105 session, request = self.pop_request()
106 self.process(session, request)
113 def process(self, session, request):
114 method = request['method']
115 params = request.get('params',[])
117 suffix = method.split('.')[-1]
118 if suffix == 'subscribe':
119 session.subscribe_to_service(method, params)
121 # store session and id locally
122 request['id'] = self.store_session_id(session, request['id'])
124 # dispatch request to the relevant module..
125 prefix = request['method'].split('.')[0]
127 p = self.processors[prefix]
129 print "error: no processor for", prefix
134 traceback.print_exc(file=sys.stdout)
136 if method in ['server.version']:
137 session.version = params[0]
140 def add_session(self, session):
142 self.sessions.append(session)
144 def collect_garbage(self):
145 # Deep copy entire sessions list and blank it
146 # This is done to minimise lock contention
148 sessions = self.sessions[:]
150 for session in sessions:
151 if not session.stopped():
152 # If session is still alive then re-add it back
153 # to our internal register
154 self.add_session(session)
160 self._stopped = False
161 self.lock = threading.Lock()
162 self.subscriptions = []
165 threading.Timer(2, self.info).start()
168 for s in self.subscriptions:
170 if m == 'blockchain.address.subscribe':
175 print timestr(), self.name, self.address, addr, len(self.subscriptions), self.version
181 def subscribe_to_service(self, method, params):
183 self.subscriptions.append((method, params))
186 class ResponseDispatcher(threading.Thread):
188 def __init__(self, shared, processor):
190 self.processor = processor
191 threading.Thread.__init__(self)
195 while not self.shared.stopped():
196 response = self.processor.pop_response()
197 #print "pop response", response
198 internal_id = response.get('id')
199 params = response.get('params',[])
201 method = response['method']
203 print "no method", response
207 session, message_id = self.processor.get_session_id(internal_id)
208 response['id'] = message_id
209 session.send_response(response)
212 for session in self.processor.sessions:
213 if not session.stopped():
214 if (method,params) in session.subscriptions:
215 session.send_response(response)