from random import choice
+from urlparse import urlparse, urlunparse
from twisted.internet import reactor, defer
from twisted.trial import unittest
def __init__(self):
self.clients = {}
- def get(self, location_list, method="GET", modtime=None):
+ def get(self, locations, 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
+ @type locations: C{list} of C{string}
+ @var locations: a list of the locations where the file can be found
"""
- host, port, path = choice(location_list)
+ url = choice(locations)
+ parsed = urlparse(url)
+ assert(parsed[0] == "http", "Only HTTP is supported, not '%s'" % parsed[0])
+ host = parsed[1]
+ path = urlunparse(('', '') + parsed[2:])
+
+ # Make sure a port is included for consistency
+ if host.find(':') >= 0:
+ host, port = host.split(':', 1)
+ port = int(port)
+ else:
+ port = 80
return self.getPeer(host, port, path, method, modtime)
def getPeer(self, host, port, path, method="GET", modtime=None):
self.timeout = 10
host = 'www.camrdale.org'
- d = self.manager.get([(host, 80, '/robots.txt')])
+ d = self.manager.get(['http://' + host + '/robots.txt'])
d.addCallback(self.gotResp, 1, 309)
return d
self.timeout = 10
host = 'www.camrdale.org'
- d = self.manager.get([(host, 80, '/robots.txt')], "HEAD")
+ d = self.manager.get(['http://' + host + '/robots.txt'], "HEAD")
d.addCallback(self.gotResp, 1, 0)
return d
lastDefer = defer.Deferred()
def newRequest(host, path, num, expect, last=False):
- d = self.manager.get([(host, 80, path)])
+ d = self.manager.get(['http://' + host + ':' + str(80) + path])
d.addCallback(self.gotResp, num, expect)
if last:
d.addBoth(lastDefer.callback)
return self.http_site
def check_freshness(self, path, modtime, 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 = self.peers.get([path], "HEAD", modtime)
d.addCallback(self.check_freshness_done, path, resp)
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
+ getDefer = self.peers.get([path])
+ getDefer.addCallback(d.callback)
+ else:
+ # Lookup hash from DHT
+ lookupDefer = self.dht.getValue(hash)
+ lookupDefer.addCallback(self.lookupHash_done, hash, size, path, d)
- # 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)])
+ def lookupHash_done(self, locations, hash, size, path, d):
+ if not locations:
+ getDefer = self.peers.get([path])
+ getDefer.addCallback(d.callback)
+ else:
+ # Download from the found peers
+ getDefer = self.peers.get(locations)
getDefer.addCallback(d.callback)
\ No newline at end of file