threadsafe get_session
[electrum-server.git] / processor.py
index d9827fd..4cbaa85 100644 (file)
@@ -89,6 +89,11 @@ class RequestDispatcher(threading.Thread):
     def pop_request(self):
         return self.request_queue.get()
 
+    def get_session_by_address(self, address):
+        for x in self.sessions:
+            if x.address == address:
+                return x
+
     def get_session_id(self, internal_id):
         with self.lock:
             return self.internal_ids.pop(internal_id)
@@ -138,6 +143,10 @@ class RequestDispatcher(threading.Thread):
         if method in ['server.version']:
             session.version = params[0]
 
+    def get_sessions(self):
+        with self.lock:
+            r = self.sessions[:]
+        return r
 
     def add_session(self, session):
         with self.lock:
@@ -164,6 +173,8 @@ class Session:
         self.subscriptions = []
         self.address = ''
         self.name = ''
+        self.version = 'unknown'
+        self.time = time.time()
         threading.Timer(2, self.info).start()
 
     # Debugging method. Doesn't need to be threadsafe.
@@ -172,13 +183,14 @@ class Session:
             #print sub
             method = sub[0]
             if method == 'blockchain.address.subscribe':
-                params = sub[1]
-                addr = params[0]
+                addr = sub[1]
                 break
         else:
             addr = None
-        print timestr(), self.name, self.address, addr,\
-            len(self.subscriptions), self.version
+
+        if self.subscriptions:
+            print timestr(), self.name, self.address, addr,\
+                len(self.subscriptions), self.version
 
     def stopped(self):
         with self.lock:
@@ -195,7 +207,7 @@ class Session:
     def build_subdesc(method, params):
         if method == "blockchain.numblocks.subscribe":
             return method,
-        elif method == "blockchain.address.get_history":
+        elif method == "blockchain.address.subscribe":
             if not params:
                 return None
             else:
@@ -246,6 +258,9 @@ class ResponseDispatcher(threading.Thread):
 
     def send_response(self, internal_id, response):
         session, message_id = self.processor.get_session_id(internal_id)
-        response['id'] = message_id
-        session.send_response(response)
+        if session:
+            response['id'] = message_id
+            session.send_response(response)
+        else:
+            print "send_response: no session", message_id, internal_id, response