improved web_seed test (now requires lighty) and fixed a bug in web_peer_connection

This commit is contained in:
Arvid Norberg 2007-11-25 23:11:29 +00:00
parent 81b9d5b6ef
commit 9d094245ba
2 changed files with 58 additions and 18 deletions

View File

@ -306,10 +306,12 @@ namespace libtorrent
namespace
{
bool range_contains(peer_request const& range, peer_request const& req)
bool range_contains(peer_request const& range, peer_request const& req, int piece_size)
{
return range.start <= req.start
&& range.start + range.length >= req.start + req.length;
size_type range_start = size_type(range.piece) * piece_size + range.start;
size_type req_start = size_type(req.piece) * piece_size + req.start;
return range_start <= req_start
&& range_start + range.length >= req_start + req.length;
}
}
@ -470,6 +472,9 @@ namespace libtorrent
}
}
// std::cerr << "REQUESTS: m_requests: " << m_requests.size()
// << " file_requests: " << m_file_requests.size() << std::endl;
torrent_info const& info = t->torrent_file();
if (m_requests.empty() || m_file_requests.empty())
@ -480,16 +485,16 @@ namespace libtorrent
, range_end - range_start);
peer_request front_request = m_requests.front();
/*
size_type rs = size_type(in_range.piece) * info.piece_length() + in_range.start;
size_type re = rs + in_range.length;
size_type fs = size_type(front_request.piece) * info.piece_length() + front_request.start;
size_type fe = fs + front_request.length;
if (fs < rs || fe > re)
{
throw std::runtime_error("invalid range in HTTP response");
}
std::cerr << "RANGE: r = (" << rs << ", " << re << " ) "
"f = (" << fs << ", " << fe << ") "
"file_index = " << file_index << " received_body = " << m_received_body << std::endl;
*/
// skip the http header and the blocks we've already read. The
// http_body.begin is now in sync with the request at the front
// of the request queue
@ -504,11 +509,16 @@ namespace libtorrent
bool range_overlaps_request = in_range.start + in_range.length
> front_request.start + int(m_piece.size());
if (!range_overlaps_request)
{
throw std::runtime_error("invalid range in HTTP response");
}
// if the request is contained in the range (i.e. the entire request
// fits in the range) we should not start a partial piece, since we soon
// will receive enough to call incoming_piece() and pass the read buffer
// directly (in the next loop below).
if (range_overlaps_request && !range_contains(in_range, front_request))
if (range_overlaps_request && !range_contains(in_range, front_request, info.piece_length()))
{
// the start of the next block to receive is stored
// in m_piece. We need to append the rest of that
@ -549,7 +559,7 @@ namespace libtorrent
// report all received blocks to the bittorrent engine
while (!m_requests.empty()
&& range_contains(in_range, m_requests.front())
&& range_contains(in_range, m_requests.front(), info.piece_length())
&& recv_buffer.left() >= m_requests.front().length)
{
peer_request r = m_requests.front();

View File

@ -33,19 +33,50 @@ void add_files(
}
}
void start_web_server()
{
std::ofstream f("./lighty_config");
f << "server.modules = (\"mod_access\")\n"
"server.document-root = \"" << initial_path().string() << "\"\n"
"server.range-requests = \"enable\"\n"
"server.port = 8000\n"
"server.pid-file = \"./lighty.pid\"\n";
f.close();
system("lighttpd -f lighty_config &");
}
void stop_web_server()
{
system("kill `cat ./lighty.pid`");
}
void test_transfer()
{
using namespace libtorrent;
boost::intrusive_ptr<torrent_info> torrent_file(new torrent_info);
torrent_file->add_url_seed("http://127.0.0.1/bravia_paint_ad_70sec_1280x720.mov");
torrent_file->add_url_seed("http://127.0.0.1:8000/");
path full_path = "/Library/WebServer/Documents/bravia_paint_ad_70sec_1280x720.mov";
add_files(*torrent_file, full_path.branch_path(), full_path.leaf());
create_directory("test_torrent");
char random_data[300000];
std::srand(std::time(0));
std::generate(random_data, random_data + sizeof(random_data), &std::rand);
std::ofstream("./test_torrent/test1").write(random_data, 35);
std::ofstream("./test_torrent/test2").write(random_data, 16536 - 35);
std::ofstream("./test_torrent/test3").write(random_data, 16536);
std::ofstream("./test_torrent/test4").write(random_data, 17);
std::ofstream("./test_torrent/test5").write(random_data, 16536);
std::ofstream("./test_torrent/test6").write(random_data, 300000);
std::ofstream("./test_torrent/test7").write(random_data, 300000);
add_files(*torrent_file, complete("."), "test_torrent");
start_web_server();
file_pool fp;
boost::scoped_ptr<storage_interface> s(default_storage_constructor(
torrent_file, full_path.branch_path(), fp));
torrent_file, ".", fp));
// calculate the hash for all pieces
int num = torrent_file->num_pieces();
std::vector<char> buf(torrent_file->piece_length());
@ -59,10 +90,6 @@ void test_transfer()
// to calculate the info_hash
entry te = torrent_file->create_torrent();
te.print(std::cout);
// std::ofstream torrent("web_seed.torrent", std::ios::binary | std::ios::trunc);
// bencode(std::ostream_iterator<char>(torrent), te);
session ses;
ses.set_severity_level(alert::debug);
ses.listen_on(std::make_pair(49000, 50000));
@ -86,6 +113,9 @@ void test_transfer()
}
TEST_CHECK(th.is_seed());
remove_all("./test_torrent");
stop_web_server();
}
int test_main()