]> git.mxchange.org Git - quix0rs-apt-p2p.git/commitdiff
Add some of the fetching logic.
authorCameron Dale <camrdale@gmail.com>
Sun, 16 Dec 2007 09:09:41 +0000 (01:09 -0800)
committerCameron Dale <camrdale@gmail.com>
Sun, 16 Dec 2007 09:09:41 +0000 (01:09 -0800)
apt_dht/HTTPDownloader.py
apt_dht/PeerManager.py
apt_dht/apt_dht.py
apt_dht/apt_dht_conf.py

index 7e5a06cb6880a10bcb8ce0a20c43ef56e1862ab9..45ab4f790efaaf61fcb91030c9211a19c08eaf20 100644 (file)
@@ -116,8 +116,10 @@ class HTTPClientManager(ClientFactory):
                           (version.short(), twisted_version.short(), web2_version.short()))
         return headers
     
-    def get(self, path, method="GET"):
+    def get(self, path, method="GET", modtime=None):
         headers = self.setCommonHeaders()
+        if modtime:
+            headers.setHeader('If-Modified-Since', modtime)
         return self.submitRequest(ClientRequest(method, path, headers, None))
     
     def getRange(self, path, rangeStart, rangeEnd, method="GET"):
index e372f694d432a3d8d63713d156defb419b098358..6a3b77dd4e56df366941dcf2f4c5e6bd6d1944a3 100644 (file)
@@ -11,22 +11,22 @@ class PeerManager:
     def __init__(self):
         self.clients = {}
         
-    def get(self, location_list, method="GET"):
+    def get(self, location_list, method="GET", modtime=None):
         """Download from a list of peers.
         
         @type location_list: C{list} of (C{string}, C{int}, C{string})
         @var location_list: a list of the locations where the file can be found
         """
         host, port, path = choice(location_list)
-        return self.getPeer(host, port, path, method)
+        return self.getPeer(host, port, path, method, modtime)
         
-    def getPeer(self, host, port, path, method="GET"):
+    def getPeer(self, host, port, path, method="GET", modtime=None):
         if not port:
             port = 80
         site = host + ":" + str(port)
         if site not in self.clients:
             self.clients[site] = HTTPClientManager(host, port)
-        return self.clients[site].get(path, method)
+        return self.clients[site].get(path, method, modtime)
     
     def close(self):
         for site in self.clients:
index ecbfab67e90bcc429781ee7c8b6edad2e371bb09..642c3f7ee5e0986491860c25f25aa18b917d9221 100644 (file)
@@ -1,24 +1,66 @@
 
+from twisted.internet import defer
 from twisted.web2 import server, http, http_headers
 
 from apt_dht_conf import config
+from PeerManager import PeerManager
 from HTTPServer import TopLevel
+from MirrorManager import MirrorManager
 
 class AptDHT:
     def __init__(self):
         self.http_server = TopLevel(config.defaults()['cache_dir'], self)
         self.http_site = server.Site(self.http_server)
+        self.peers = PeerManager()
+        self.mirrors = MirrorManager(config.defaults()['cache_dir'])
     
     def getSite(self):
         return self.http_site
     
     def check_freshness(self, path, modtime, resp):
-        return resp
+        host, path = path.split('/',1)
+        if not host:
+            host, path = path.split('/',1)
+        path = '/'+path
+        
+        # Make sure a port is included for consistency
+        if host.find(':') >= 0:
+            host, port = host.split(':', 1)
+            port = int(port)
+        else:
+            port = 80
+        
+        d = self.peers.get([(host, port, path)], "HEAD", modtime)
+        d.addCallback(self.check_freshness_done, path, resp)
+        return d
+    
+    def check_freshness_done(self, resp, path, orig_resp):
+        if resp.code == "304":
+            return orig_resp
+        else:
+            return self.get_resp(path)
     
     def get_resp(self, path):
-        return http.Response(
-            200,
-            {'content-type': http_headers.MimeType('text', 'html')},
-            """<html><body>
-            <h2>P2P Download</h2>
-            <p>TODO: eventually this will cause a P2P lookup.</body></html>""")
+        d = defer.Deferred()
+        
+        findDefer = self.mirrors.findHash(path)
+        
+        findDefer.addcallback(self.findHash_done, path, d)
+        return d
+        
+    def findHash_done(self, (hash, size), path, d):
+        if hash is None:
+            host, path = path.split('/',1)
+            if not host:
+                host, path = path.split('/',1)
+            path = '/'+path
+            
+            # Make sure a port is included for consistency
+            if host.find(':') >= 0:
+                host, port = host.split(':', 1)
+                port = int(port)
+            else:
+                port = 80
+            getDefer = self.peers.get([(host, port, path)])
+            getDefer.addCallback(d.callback)
+            
\ No newline at end of file
index 32ffa06d62bcb6464f52163c0bbc2de43b1e40e8..626f0ffc32e394a580b1e122e5d38299319f9539 100644 (file)
@@ -29,7 +29,7 @@ DEFAULTS = {
     'username': '',
 
     # Which DHT implementation to use
-    'DHT': 'Khashmir',
+    'DHT': 'apt_dht_Khashmir',
 }
 
 DHT_DEFAULTS = {