]> git.mxchange.org Git - quix0rs-apt-p2p.git/blobdiff - apt_dht/apt_dht.py
Move the translating of hashes from hex into the MirrorManager.
[quix0rs-apt-p2p.git] / apt_dht / apt_dht.py
index 448d44f9e915355ba74d56b0d02ae97f58150d20..e9b5df30a8928e45cc60ea257676a43ed540c49b 100644 (file)
@@ -21,39 +21,54 @@ class AptDHT:
         return self.http_site
     
     def check_freshness(self, path, modtime, resp):
+        log.msg('Checking if %s is still fresh' % path)
         d = self.peers.get([path], "HEAD", modtime)
         d.addCallback(self.check_freshness_done, path, resp)
         return d
     
     def check_freshness_done(self, resp, path, orig_resp):
-        if resp.code == "304":
+        if resp.code == 304:
+            log.msg('Still fresh, returning: %s' % path)
             return orig_resp
         else:
+            log.msg('Stale, need to redownload: %s' % path)
             return self.get_resp(path)
     
     def get_resp(self, path):
         d = defer.Deferred()
         
+        log.msg('Trying to find hash for %s' % path)
         findDefer = self.mirrors.findHash(path)
         
-        findDefer.addcallback(self.findHash_done, path, d)
+        findDefer.addCallback(self.findHash_done, path, d)
+        findDefer.addErrback(self.findHash_error, path, d)
         return d
+    
+    def findHash_error(self, failure, path, d):
+        log.err(failure)
+        self.findHash_done((None, None), path, d)
         
     def findHash_done(self, (hash, size), path, d):
         if hash is None:
-            getDefer = self.peers.get([path])
-            getDefer.addCallback(d.callback)
+            log.msg('Hash for %s was not found' % path)
+            self.download_file([path], hash, size, path, d)
         else:
+            log.msg('Found hash %s for %s' % (hash, path))
             # Lookup hash from DHT
             lookupDefer = self.dht.getValue(hash)
             lookupDefer.addCallback(self.lookupHash_done, hash, size, path, d)
             
     def lookupHash_done(self, locations, hash, size, path, d):
         if not locations:
-            getDefer = self.peers.get([path])
-            getDefer.addCallback(d.callback)
+            log.msg('Peers for %s were not found' % path)
+            self.download_file([path], hash, size, path, d)
         else:
+            log.msg('Found peers for $s: %r' % (path, locations))
             # Download from the found peers
-            getDefer = self.peers.get(locations)
-            getDefer.addCallback(d.callback)
-            
\ No newline at end of file
+            self.download_file(locations, hash, size, path, d)
+            
+    def download_file(self, locations, hash, size, path, d):
+        getDefer = self.peers.get(locations)
+        getDefer.addCallback(self.mirrors.save_file, hash, size, path)
+        getDefer.addErrback(self.mirrors.save_error, path)
+        getDefer.addCallbacks(d.callback, d.errback)