2 from binascii import b2a_hex
4 from twisted.internet import defer
5 from twisted.web2 import server, http, http_headers
6 from twisted.python import log
8 from apt_dht_conf import config
9 from PeerManager import PeerManager
10 from HTTPServer import TopLevel
11 from MirrorManager import MirrorManager
14 def __init__(self, dht):
15 log.msg('Initializing the main apt_dht application')
17 self.http_server = TopLevel(config.get('DEFAULT', 'cache_dir'), self)
18 self.http_site = server.Site(self.http_server)
19 self.peers = PeerManager()
20 self.mirrors = MirrorManager(config.get('DEFAULT', 'cache_dir'))
25 def check_freshness(self, path, modtime, resp):
26 log.msg('Checking if %s is still fresh' % path)
27 d = self.peers.get([path], "HEAD", modtime)
28 d.addCallback(self.check_freshness_done, path, resp)
31 def check_freshness_done(self, resp, path, orig_resp):
33 log.msg('Still fresh, returning: %s' % path)
36 log.msg('Stale, need to redownload: %s' % path)
37 return self.get_resp(path)
39 def get_resp(self, path):
42 log.msg('Trying to find hash for %s' % path)
43 findDefer = self.mirrors.findHash(path)
45 findDefer.addCallback(self.findHash_done, path, d)
46 findDefer.addErrback(self.findHash_error, path, d)
49 def findHash_error(self, failure, path, d):
51 self.findHash_done((None, None), path, d)
53 def findHash_done(self, hash, path, d):
54 if hash.expected() is None:
55 log.msg('Hash for %s was not found' % path)
56 self.download_file([path], hash, path, d)
58 log.msg('Found hash %s for %s' % (hash.hexexpected(), path))
59 # Lookup hash from DHT
60 key = hash.normexpected(bits = config.getint(config.get('DEFAULT', 'DHT'), 'HASH_LENGTH'))
61 lookupDefer = self.dht.getValue(key)
62 lookupDefer.addCallback(self.lookupHash_done, hash, path, d)
64 def lookupHash_done(self, locations, hash, path, d):
66 log.msg('Peers for %s were not found' % path)
67 self.download_file([path], hash, path, d)
69 log.msg('Found peers for %s: %r' % (path, locations))
70 # Download from the found peers
71 self.download_file(locations, hash, path, d)
73 def download_file(self, locations, hash, path, d):
74 getDefer = self.peers.get(locations)
75 getDefer.addCallback(self.mirrors.save_file, hash, path)
76 getDefer.addErrback(self.mirrors.save_error, path)
77 getDefer.addCallbacks(d.callback, d.errback)