+ # Lookup hash in cache
+ locations = self.db.lookupHash(hash.expected())
+ self.getCachedFile(hash, req, path, d, locations)
+
+ def getCachedFile(self, hash, req, path, d, locations):
+ if not locations:
+ log.msg('Failed to return file from cache: %s' % path)
+ self.lookupHash(hash, path, d)
+ return
+
+ # Get the first possible location from the list
+ file = locations.pop(0)['path']
+ log.msg('Returning cached file: %s' % file.path)
+
+ # Get it's response
+ resp = static.File(file.path).renderHTTP(req)
+ if isinstance(resp, defer.Deferred):
+ resp.addBoth(self._getCachedFile, hash, req, path, d, locations)
+ else:
+ self._getCachedFile(resp, hash, req, path, d, locations)
+
+ def _getCachedFile(self, resp, hash, req, path, d, locations):
+ if isinstance(resp, failure.Failure):
+ log.msg('Got error trying to get cached file')
+ log.err()
+ # Try the next possible location
+ self.getCachedFile(hash, req, path, d, locations)
+ return
+
+ log.msg('Cached response: %r' % resp)
+
+ if resp.code >= 200 and resp.code < 400:
+ d.callback(resp)
+ else:
+ # Try the next possible location
+ self.getCachedFile(hash, req, path, d, locations)
+
+ def lookupHash(self, hash, path, d):
+ log.msg('Looking up hash in DHT for file: %s' % path)
+ key = hash.normexpected(bits = config.getint(config.get('DEFAULT', 'DHT'), 'HASH_LENGTH'))
+ lookupDefer = self.dht.getValue(key)
+ lookupDefer.addCallback(self.lookupHash_done, hash, path, d)
+
+ def lookupHash_done(self, values, hash, path, d):
+ if not values:
+ log.msg('Peers for %s were not found' % 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, values))
+ # Download from the found peers
+ 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)
+ 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(hash, path)
+ return getDefer
+ return response
+
+ def new_cached_file(self, file_path, hash, new_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_contact and hash and new_hash and (hash.expected() is not None or forceDHT):
+ key = hash.norm(bits = config.getint(config.get('DEFAULT', 'DHT'), 'HASH_LENGTH'))
+ value = {'c': self.my_contact}
+ 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.hexdigest(), result))
+
\ No newline at end of file