X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=apt_dht%2Fapt_dht.py;h=f1732e5ee1cc55be2daf0a8aa1f5f3410e3ad7a8;hb=99242333df43581eb351665681617ec02ca7e074;hp=d212729a5f30e34ff081eb21eab0a69fdcbd56ab;hpb=73e507f17f01b02ee458cfd75abb955e195aa824;p=quix0rs-apt-p2p.git diff --git a/apt_dht/apt_dht.py b/apt_dht/apt_dht.py index d212729..f1732e5 100644 --- a/apt_dht/apt_dht.py +++ b/apt_dht/apt_dht.py @@ -29,20 +29,18 @@ class AptDHT: self.dht = dht self.dht.loadConfig(config, config.get('DEFAULT', 'DHT')) self.dht.join().addCallbacks(self.joinComplete, self.joinError) - self.http_server = TopLevel(self.cache_dir.child(download_dir), self) - self.setDirectories = self.http_server.setDirectories - self.http_site = server.Site(self.http_server) + 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) 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 - def getSite(self): - return self.http_site - def joinComplete(self, result): - self.my_addr = findMyIPAddr(result, config.getint(config.get('DEFAULT', 'DHT'), 'PORT')) + self.my_addr = findMyIPAddr(result, + config.getint(config.get('DEFAULT', 'DHT'), 'PORT'), + config.getboolean('DEFAULT', 'LOCAL_OK')) if not self.my_addr: raise RuntimeError, "IP address for this machine could not be found" self.cache.scanDirectories() @@ -54,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 @@ -134,14 +132,14 @@ class AptDHT: def lookupHash_done(self, locations, hash, path, d): if not locations: 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)) # Download from the found peers - getDefer = self.peers.get(locations) + getDefer = self.peers.get(hash, path, locations) getDefer.addCallback(self.check_response, hash, path) getDefer.addCallback(self.cache.save_file, hash, path) getDefer.addErrback(self.cache.save_error, path) @@ -150,26 +148,28 @@ 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, urlpath, 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: + if self.my_addr and hash and (hash.expected() is not None or forceDHT): 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) - storeDefer.addErrback(log.err) + storeDefer = self.dht.storeValue(key, site) + storeDefer.addCallback(self.store_done, hash) + return storeDefer + return None - def store_done(self, result, path): - log.msg('Added %s to the DHT: %r' % (path, result)) + def store_done(self, result, hash): + log.msg('Added %s to the DHT: %r' % (hash.hexdigest(), result)) \ No newline at end of file