password
[electrum-server.git] / processor.py
index 1fd7909..c6da150 100644 (file)
@@ -24,7 +24,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()
@@ -58,50 +90,41 @@ class Processor(threading.Thread):
             self.internal_id += 1
             return r
 
-    def register(self, prefix, function):
-        self.processors[prefix] = function
-
     def run(self):
         if self.shared is None:
             raise TypeError("self.shared not set in Processor")
         while not self.shared.stopped():
             session, request = self.pop_request()
+            self.process(session, request)
 
-            method = request['method']
-            params = request.get('params',[])
-
-            suffix = method.split('.')[-1]
-            if suffix == 'subscribe':
-                session.subscribe_to_service(method, params)
+        self.stop()
 
-            # store session and id locally
-            request['id'] = self.store_session_id(session, request['id'])
+    def stop(self):
+        pass
 
-            # dispatch request to the relevant module..
-            prefix = method.split('.')[0]
-            try:
-                func = self.processors[prefix]
-            except:
-                print "error: no processor for", prefix
-                continue
+    def process(self, session, request):
+        method = request['method']
+        params = request.get('params',[])
 
-            try:
-                func(request,self.response_queue)
-            except:
-                traceback.print_exc(file=sys.stdout)
-                continue
+        suffix = method.split('.')[-1]
+        if suffix == 'subscribe':
+            session.subscribe_to_service(method, params)
 
-        self.stop()
+        # store session and id locally
+        request['id'] = self.store_session_id(session, request['id'])
 
-    def stop(self):
-        pass
+        # 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)
 
-    def process(self, request):
-        print "New request", request
-        # Do stuff...
-        # response = request
-        # When ready, you call
-        # self.push_response(response)
 
     def add_session(self, session):
         with self.lock:
@@ -136,7 +159,7 @@ class Session:
             self.subscriptions.append((method, params))
     
 
-class Dispatcher(threading.Thread):
+class ResponseDispatcher(threading.Thread):
 
     def __init__(self, shared, processor):
         self.shared = shared