+ getDefer.addCallback(self.check_response, hash, path)
+ getDefer.addCallback(self.cache.save_file, hash, path)
+ getDefer.addErrback(self.cache.save_error, path)
+ getDefer.addCallbacks(d.callback, d.errback)
+
+ 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])
+ return getDefer
+ return response
+
+ def new_cached_file(self, file_path, hash, urlpath, url = None):
+ """Add a newly cached file to the DHT.
+
+ If the file was downloaded, set url to the path it was downloaded for.
+ """
+ if url:
+ self.mirrors.updatedFile(url, file_path)
+
+ if self.my_addr and hash:
+ site = self.my_addr + ':' + str(config.getint('DEFAULT', 'PORT'))
+ full_path = urlunparse(('http', site, urlpath, None, None, None))
+ key = hash.norm(bits = config.getint(config.get('DEFAULT', 'DHT'), 'HASH_LENGTH'))
+ storeDefer = self.dht.storeValue(key, full_path)
+ storeDefer.addCallback(self.store_done, full_path)
+ return storeDefer
+ return None
+
+ def store_done(self, result, path):
+ log.msg('Added %s to the DHT: %r' % (path, result))
+
\ No newline at end of file