]> git.mxchange.org Git - quix0rs-apt-p2p.git/blobdiff - apt_dht/apt_dht.py
Unload the AptPackages caches after a period of inactivity.
[quix0rs-apt-p2p.git] / apt_dht / apt_dht.py
index 264be0f65aba5e499425a9d83feb33a4e0e3d9cc..eb103bd34cd2f4eb285fa2c9d07437f50f8e9073 100644 (file)
@@ -15,7 +15,7 @@ from MirrorManager import MirrorManager
 from CacheManager import CacheManager
 from Hash import HashObject
 from db import DB
-from util import findMyIPAddr
+from util import findMyIPAddr, compact
 
 download_dir = 'cache'
 
@@ -32,7 +32,7 @@ class AptDHT:
         self.http_server = TopLevel(self.cache_dir.child(download_dir), self.db, self)
         self.getHTTPFactory = self.http_server.getHTTPFactory
         self.peers = PeerManager()
-        self.mirrors = MirrorManager(self.cache_dir)
+        self.mirrors = MirrorManager(self.cache_dir, config.gettime('DEFAULT', 'UNLOAD_PACKAGES_CACHE'))
         other_dirs = [FilePath(f) for f in config.getstringlist('DEFAULT', 'OTHER_DIRS')]
         self.cache = CacheManager(self.cache_dir.child(download_dir), self.db, other_dirs, self)
         self.my_addr = None
@@ -52,7 +52,7 @@ class AptDHT:
     
     def check_freshness(self, req, path, modtime, resp):
         log.msg('Checking if %s is still fresh' % path)
-        d = self.peers.get([path], "HEAD", modtime)
+        d = self.peers.get('', path, method = "HEAD", modtime = modtime)
         d.addCallback(self.check_freshness_done, req, path, resp)
         return d
     
@@ -129,17 +129,17 @@ class AptDHT:
         lookupDefer = self.dht.getValue(key)
         lookupDefer.addCallback(self.lookupHash_done, hash, path, d)
 
-    def lookupHash_done(self, locations, hash, path, d):
-        if not locations:
+    def lookupHash_done(self, values, hash, path, d):
+        if not values:
             log.msg('Peers for %s were not found' % path)
-            getDefer = self.peers.get([path])
+            getDefer = self.peers.get(hash, path)
             getDefer.addCallback(self.cache.save_file, hash, path)
             getDefer.addErrback(self.cache.save_error, path)
             getDefer.addCallbacks(d.callback, d.errback)
         else:
-            log.msg('Found peers for %s: %r' % (path, locations))
+            log.msg('Found peers for %s: %r' % (path, values))
             # Download from the found peers
-            getDefer = self.peers.get(locations)
+            getDefer = self.peers.get(hash, path, values)
             getDefer.addCallback(self.check_response, hash, path)
             getDefer.addCallback(self.cache.save_file, hash, path)
             getDefer.addErrback(self.cache.save_error, path)
@@ -148,26 +148,29 @@ class AptDHT:
     def check_response(self, response, hash, path):
         if response.code < 200 or response.code >= 300:
             log.msg('Download from peers failed, going to direct download: %s' % path)
-            getDefer = self.peers.get([path])
+            getDefer = self.peers.get(hash, path)
             return getDefer
         return response
         
-    def new_cached_file(self, file_path, hash, url = None):
+    def new_cached_file(self, file_path, hash, url = None, forceDHT = False):
         """Add a newly cached file to the DHT.
         
         If the file was downloaded, set url to the path it was downloaded for.
+        Don't add a file to the DHT unless a hash was found for it
+        (but do add it anyway if forceDHT is True).
         """
         if url:
             self.mirrors.updatedFile(url, file_path)
         
-        if self.my_addr and hash:
-            site = self.my_addr + ':' + str(config.getint('DEFAULT', 'PORT'))
+        if self.my_addr and hash and (hash.expected() is not None or forceDHT):
+            contact = compact(self.my_addr, config.getint('DEFAULT', 'PORT'))
+            value = {'c': contact}
             key = hash.norm(bits = config.getint(config.get('DEFAULT', 'DHT'), 'HASH_LENGTH'))
-            storeDefer = self.dht.storeValue(key, site)
+            storeDefer = self.dht.storeValue(key, value)
             storeDefer.addCallback(self.store_done, hash)
             return storeDefer
         return None
 
     def store_done(self, result, hash):
-        log.msg('Added %s to the DHT: %r' % (hash, result))
+        log.msg('Added %s to the DHT: %r' % (hash.hexdigest(), result))
         
\ No newline at end of file