Notify on new blocks.
authorgenjix <fake@lol.u>
Wed, 21 Mar 2012 23:23:05 +0000 (23:23 +0000)
committergenjix <fake@lol.u>
Wed, 21 Mar 2012 23:23:05 +0000 (23:23 +0000)
server-genjix.py

index 58dd38c..be44dba 100644 (file)
@@ -100,20 +100,32 @@ class NumblocksSubscribe:
         self.backend.blockchain.subscribe_reorganize(self.reorganize)
         self.backend.blockchain.fetch_last_depth(self.set_last_depth)
         self.latest = GhostValue()
+        self.subscribed = []
 
     def subscribe(self, session, request):
         last = self.latest.get()
         session.push_response({"id": request["id"], "result": last})
+        with self.lock:
+            self.subscribed.append((session, request))
 
     def set_last_depth(self, ec, last_depth):
         if ec:
             print "Error retrieving last depth", ec
         else:
-            with self.lock:
-                self.latest.set(last_depth)
+            self.latest.set(last_depth)
 
     def reorganize(self, ec, fork_point, arrivals, replaced):
-        pass
+        latest = fork_point + len(arrivals)
+        self.latest.set(latest)
+        subscribed = self.spring_clean()
+        for session, request in subscribed:
+            session.push_response({"id": request["id"], "result": latest})
+
+    def spring_clean(self):
+        with self.lock:
+            self.subscribed = [sub for sub in self.subscribed
+                               if not sub[0].stopped()]
+            return self.subscribed[:]
 
 class LibbitcoinProcessor(stratum.Processor):