factored out torrent creation functionality from torrent_info into create_torrent. Modified torrent_info to use lazy_bdecoder for increased performance
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
#include "test.hpp"
|
||||
#include "libtorrent/assert.hpp"
|
||||
#include "libtorrent/alert_types.hpp"
|
||||
#include "libtorrent/create_torrent.hpp"
|
||||
|
||||
using boost::filesystem::remove_all;
|
||||
using boost::filesystem::create_directory;
|
||||
@@ -102,7 +103,7 @@ void start_web_server(int port, bool ssl)
|
||||
<< (ssl?"https":"http") << "://127.0.0.1:" << port << "/infinite_redirect\")\n"
|
||||
"$HTTP[\"url\"] == \"/test_file.gz\" {\n"
|
||||
" setenv.add-response-header = ( \"Content-Encoding\" => \"gzip\" )\n"
|
||||
" mimetype.assign = ()\n"
|
||||
"# mimetype.assign = ()\n"
|
||||
"}\n";
|
||||
// this requires lighttpd to be built with ssl support.
|
||||
// The port distribution for mac is not built with ssl
|
||||
@@ -168,22 +169,21 @@ boost::intrusive_ptr<torrent_info> create_torrent(std::ostream* file)
|
||||
|
||||
using namespace boost::filesystem;
|
||||
|
||||
boost::intrusive_ptr<torrent_info> t(new torrent_info);
|
||||
libtorrent::create_torrent t;
|
||||
int total_size = 2 * 1024 * 1024;
|
||||
t->add_file(path("temporary"), total_size);
|
||||
t->set_piece_size(16 * 1024);
|
||||
t->add_tracker(tracker_url);
|
||||
t.add_file(path("temporary"), total_size);
|
||||
t.set_piece_size(16 * 1024);
|
||||
t.add_tracker(tracker_url);
|
||||
|
||||
std::vector<char> piece(16 * 1024);
|
||||
for (int i = 0; i < int(piece.size()); ++i)
|
||||
piece[i] = (i % 26) + 'A';
|
||||
|
||||
// calculate the hash for all pieces
|
||||
int num = t->num_pieces();
|
||||
int num = t.num_pieces();
|
||||
sha1_hash ph = hasher(&piece[0], piece.size()).final();
|
||||
for (int i = 0; i < num; ++i)
|
||||
t->set_hash(i, ph);
|
||||
t->create_torrent();
|
||||
t.set_hash(i, ph);
|
||||
|
||||
if (file)
|
||||
{
|
||||
@@ -194,7 +194,10 @@ boost::intrusive_ptr<torrent_info> create_torrent(std::ostream* file)
|
||||
}
|
||||
}
|
||||
|
||||
return t;
|
||||
std::vector<char> tmp;
|
||||
std::back_insert_iterator<std::vector<char> > out(tmp);
|
||||
bencode(out, t.generate());
|
||||
return boost::intrusive_ptr<torrent_info>(new torrent_info(&tmp[0], tmp.size()));
|
||||
}
|
||||
|
||||
boost::tuple<torrent_handle, torrent_handle, torrent_handle>
|
||||
@@ -214,7 +217,7 @@ setup_transfer(session* ses1, session* ses2, session* ses3
|
||||
|
||||
create_directory("./tmp1" + suffix);
|
||||
std::ofstream file(("./tmp1" + suffix + "/temporary").c_str());
|
||||
boost::intrusive_ptr<torrent_info> t = create_torrent(&file);
|
||||
boost::intrusive_ptr<torrent_info> t = ::create_torrent(&file);
|
||||
file.close();
|
||||
if (clear_files)
|
||||
{
|
||||
|
@@ -127,7 +127,7 @@ void do_handshake(stream_socket& s, sha1_hash const& ih, char* buffer)
|
||||
// rejected aren't requested again
|
||||
void test_reject_fast()
|
||||
{
|
||||
boost::intrusive_ptr<torrent_info> t = create_torrent();
|
||||
boost::intrusive_ptr<torrent_info> t = ::create_torrent();
|
||||
sha1_hash ih = t->info_hash();
|
||||
session ses1(fingerprint("LT", 0, 1, 0, 0), std::make_pair(48900, 49000));
|
||||
ses1.add_torrent(t, "./tmp1");
|
||||
@@ -190,7 +190,7 @@ void test_reject_fast()
|
||||
|
||||
void test_respect_suggest()
|
||||
{
|
||||
boost::intrusive_ptr<torrent_info> t = create_torrent();
|
||||
boost::intrusive_ptr<torrent_info> t = ::create_torrent();
|
||||
sha1_hash ih = t->info_hash();
|
||||
session ses1(fingerprint("LT", 0, 1, 0, 0), std::make_pair(48900, 49000));
|
||||
ses1.add_torrent(t, "./tmp1");
|
||||
|
@@ -4,6 +4,7 @@
|
||||
#include "libtorrent/session.hpp"
|
||||
#include "libtorrent/alert_types.hpp"
|
||||
#include "libtorrent/aux_/session_impl.hpp"
|
||||
#include "libtorrent/create_torrent.hpp"
|
||||
|
||||
#include <boost/utility.hpp>
|
||||
#include <boost/filesystem/operations.hpp>
|
||||
@@ -18,6 +19,20 @@ using namespace boost::filesystem;
|
||||
|
||||
const int piece_size = 16;
|
||||
|
||||
const int half = piece_size / 2;
|
||||
|
||||
char piece0[piece_size] =
|
||||
{ 6, 6, 6, 6, 6, 6, 6, 6
|
||||
, 9, 9, 9, 9, 9, 9, 9, 9};
|
||||
|
||||
char piece1[piece_size] =
|
||||
{ 0, 0, 0, 0, 0, 0, 0, 0
|
||||
, 1, 1, 1, 1, 1, 1, 1, 1};
|
||||
|
||||
char piece2[piece_size] =
|
||||
{ 0, 0, 1, 0, 0, 0, 0, 0
|
||||
, 1, 1, 1, 1, 1, 1, 1, 1};
|
||||
|
||||
void on_read_piece(int ret, disk_io_job const& j, char const* data, int size)
|
||||
{
|
||||
std::cerr << "on_read_piece piece: " << j.piece << std::endl;
|
||||
@@ -39,26 +54,6 @@ void run_storage_tests(boost::intrusive_ptr<torrent_info> info
|
||||
, path const& test_path
|
||||
, libtorrent::storage_mode_t storage_mode)
|
||||
{
|
||||
const int half = piece_size / 2;
|
||||
|
||||
char piece0[piece_size] =
|
||||
{ 6, 6, 6, 6, 6, 6, 6, 6
|
||||
, 9, 9, 9, 9, 9, 9, 9, 9};
|
||||
|
||||
char piece1[piece_size] =
|
||||
{ 0, 0, 0, 0, 0, 0, 0, 0
|
||||
, 1, 1, 1, 1, 1, 1, 1, 1};
|
||||
|
||||
char piece2[piece_size] =
|
||||
{ 0, 0, 1, 0, 0, 0, 0, 0
|
||||
, 1, 1, 1, 1, 1, 1, 1, 1};
|
||||
|
||||
info->set_hash(0, hasher(piece0, piece_size).final());
|
||||
info->set_hash(1, hasher(piece1, piece_size).final());
|
||||
info->set_hash(2, hasher(piece2, piece_size).final());
|
||||
|
||||
info->create_torrent();
|
||||
|
||||
create_directory(test_path / "temp_storage");
|
||||
|
||||
int num_pieces = (1 + 612 + 17 + piece_size - 1) / piece_size;
|
||||
@@ -105,10 +100,11 @@ void run_storage_tests(boost::intrusive_ptr<torrent_info> info
|
||||
|
||||
entry frd;
|
||||
pm->async_check_fastresume(&frd, &on_check_resume_data);
|
||||
test_sleep(2000);
|
||||
ios.run();
|
||||
|
||||
pm->async_check_files(&on_check_files);
|
||||
test_sleep(2000);
|
||||
for (int i = 0; i < 4; ++i)
|
||||
ios.run_one();
|
||||
|
||||
boost::function<void(int, disk_io_job const&)> none;
|
||||
TEST_CHECK(exists(test_path / "temp_storage"));
|
||||
@@ -140,19 +136,19 @@ void run_storage_tests(boost::intrusive_ptr<torrent_info> info
|
||||
|
||||
void test_remove(path const& test_path)
|
||||
{
|
||||
boost::intrusive_ptr<torrent_info> info(new torrent_info());
|
||||
info->set_piece_size(4);
|
||||
info->add_file("temp_storage/test1.tmp", 8);
|
||||
info->add_file("temp_storage/folder1/test2.tmp", 8);
|
||||
info->add_file("temp_storage/folder2/test3.tmp", 0);
|
||||
info->add_file("temp_storage/_folder3/test4.tmp", 0);
|
||||
info->add_file("temp_storage/_folder3/subfolder/test5.tmp", 8);
|
||||
libtorrent::create_torrent t;
|
||||
t.set_piece_size(4);
|
||||
t.add_file("temp_storage/test1.tmp", 8);
|
||||
t.add_file("temp_storage/folder1/test2.tmp", 8);
|
||||
t.add_file("temp_storage/folder2/test3.tmp", 0);
|
||||
t.add_file("temp_storage/_folder3/test4.tmp", 0);
|
||||
t.add_file("temp_storage/_folder3/subfolder/test5.tmp", 8);
|
||||
|
||||
char buf[4] = {0, 0, 0, 0};
|
||||
sha1_hash h = hasher(buf, 4).final();
|
||||
for (int i = 0; i < 6; ++i) info->set_hash(i, h);
|
||||
for (int i = 0; i < 6; ++i) t.set_hash(i, h);
|
||||
|
||||
info->create_torrent();
|
||||
boost::intrusive_ptr<torrent_info> info(new torrent_info(t.generate()));
|
||||
|
||||
file_pool fp;
|
||||
boost::scoped_ptr<storage_interface> s(
|
||||
@@ -173,14 +169,23 @@ void run_test(path const& test_path)
|
||||
{
|
||||
std::cerr << "\n=== " << test_path.string() << " ===\n" << std::endl;
|
||||
|
||||
boost::intrusive_ptr<torrent_info> info(new torrent_info());
|
||||
info->set_piece_size(piece_size);
|
||||
info->add_file("temp_storage/test1.tmp", 17);
|
||||
info->add_file("temp_storage/test2.tmp", 612);
|
||||
info->add_file("temp_storage/test3.tmp", 0);
|
||||
info->add_file("temp_storage/test4.tmp", 0);
|
||||
info->add_file("temp_storage/test5.tmp", 1);
|
||||
boost::intrusive_ptr<torrent_info> info;
|
||||
|
||||
{
|
||||
libtorrent::create_torrent t;
|
||||
t.set_piece_size(piece_size);
|
||||
t.add_file("temp_storage/test1.tmp", 17);
|
||||
t.add_file("temp_storage/test2.tmp", 612);
|
||||
t.add_file("temp_storage/test3.tmp", 0);
|
||||
t.add_file("temp_storage/test4.tmp", 0);
|
||||
t.add_file("temp_storage/test5.tmp", 1);
|
||||
|
||||
t.set_hash(0, hasher(piece0, piece_size).final());
|
||||
t.set_hash(1, hasher(piece1, piece_size).final());
|
||||
t.set_hash(2, hasher(piece2, piece_size).final());
|
||||
|
||||
|
||||
info = new torrent_info(t.generate());
|
||||
std::cerr << "=== test 1 ===" << std::endl;
|
||||
|
||||
run_storage_tests(info, test_path, storage_mode_compact);
|
||||
@@ -193,6 +198,7 @@ void run_test(path const& test_path)
|
||||
TEST_CHECK(exists(test_path / "temp_storage/test3.tmp"));
|
||||
TEST_CHECK(exists(test_path / "temp_storage/test4.tmp"));
|
||||
remove_all(test_path / "temp_storage");
|
||||
}
|
||||
|
||||
// ==============================================
|
||||
|
||||
@@ -221,10 +227,15 @@ void run_test(path const& test_path)
|
||||
remove_all(test_path / "temp_storage");
|
||||
|
||||
// ==============================================
|
||||
|
||||
info = new torrent_info();
|
||||
info->set_piece_size(piece_size);
|
||||
info->add_file("temp_storage/test1.tmp", 17 + 612 + 1);
|
||||
|
||||
{
|
||||
libtorrent::create_torrent t;
|
||||
t.set_piece_size(piece_size);
|
||||
t.add_file("temp_storage/test1.tmp", 17 + 612 + 1);
|
||||
t.set_hash(0, hasher(piece0, piece_size).final());
|
||||
t.set_hash(1, hasher(piece1, piece_size).final());
|
||||
t.set_hash(2, hasher(piece2, piece_size).final());
|
||||
info = new torrent_info(t.generate());
|
||||
|
||||
std::cerr << "=== test 3 ===" << std::endl;
|
||||
|
||||
@@ -233,6 +244,7 @@ void run_test(path const& test_path)
|
||||
// 48 = piece_size * 3
|
||||
TEST_CHECK(file_size(test_path / "temp_storage" / "test1.tmp") == 48);
|
||||
remove_all(test_path / "temp_storage");
|
||||
}
|
||||
|
||||
// ==============================================
|
||||
|
||||
@@ -258,7 +270,7 @@ void test_fastresume()
|
||||
std::cout << "=== test fastresume ===" << std::endl;
|
||||
create_directory("tmp1");
|
||||
std::ofstream file("tmp1/temporary");
|
||||
boost::intrusive_ptr<torrent_info> t = create_torrent(&file);
|
||||
boost::intrusive_ptr<torrent_info> t = ::create_torrent(&file);
|
||||
file.close();
|
||||
TEST_CHECK(exists("tmp1/temporary"));
|
||||
|
||||
|
@@ -3,6 +3,7 @@
|
||||
#include "libtorrent/file_pool.hpp"
|
||||
#include "libtorrent/storage.hpp"
|
||||
#include "libtorrent/bencode.hpp"
|
||||
#include "libtorrent/create_torrent.hpp"
|
||||
#include <boost/thread.hpp>
|
||||
#include <boost/tuple/tuple.hpp>
|
||||
#include <boost/filesystem/operations.hpp>
|
||||
@@ -14,10 +15,7 @@
|
||||
using namespace boost::filesystem;
|
||||
using namespace libtorrent;
|
||||
|
||||
void add_files(
|
||||
torrent_info& t
|
||||
, path const& p
|
||||
, path const& l)
|
||||
void add_files(libtorrent::create_torrent& t, path const& p, path const& l)
|
||||
{
|
||||
if (l.leaf()[0] == '.') return;
|
||||
path f(p / l);
|
||||
@@ -34,7 +32,7 @@ void add_files(
|
||||
}
|
||||
|
||||
// proxy: 0=none, 1=socks4, 2=socks5, 3=socks5_pw 4=http 5=http_pw
|
||||
void test_transfer(torrent_info torrent_file, int proxy)
|
||||
void test_transfer(boost::intrusive_ptr<torrent_info> torrent_file, int proxy)
|
||||
{
|
||||
using namespace libtorrent;
|
||||
|
||||
@@ -59,7 +57,7 @@ void test_transfer(torrent_info torrent_file, int proxy)
|
||||
ses.set_web_seed_proxy(ps);
|
||||
}
|
||||
|
||||
torrent_handle th = ses.add_torrent(torrent_file, "./tmp1");
|
||||
torrent_handle th = ses.add_torrent(*torrent_file, "./tmp1");
|
||||
|
||||
std::vector<announce_entry> empty;
|
||||
th.replace_trackers(empty);
|
||||
@@ -89,8 +87,8 @@ int test_main()
|
||||
using namespace libtorrent;
|
||||
using namespace boost::filesystem;
|
||||
|
||||
boost::intrusive_ptr<torrent_info> torrent_file(new torrent_info);
|
||||
torrent_file->add_url_seed("http://127.0.0.1:8000/");
|
||||
libtorrent::create_torrent t;
|
||||
t.add_url_seed("http://127.0.0.1:8000/");
|
||||
|
||||
create_directory("test_torrent");
|
||||
char random_data[300000];
|
||||
@@ -104,31 +102,32 @@ int test_main()
|
||||
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");
|
||||
add_files(t, complete("."), "test_torrent");
|
||||
|
||||
start_web_server(8000);
|
||||
|
||||
// calculate the hash for all pieces
|
||||
int num = t.num_pieces();
|
||||
std::vector<char> buf(t.piece_length());
|
||||
|
||||
file_pool fp;
|
||||
boost::intrusive_ptr<torrent_info> torrent_file(new torrent_info(t.generate()));
|
||||
boost::scoped_ptr<storage_interface> s(default_storage_constructor(
|
||||
torrent_file, ".", fp));
|
||||
// calculate the hash for all pieces
|
||||
int num = torrent_file->num_pieces();
|
||||
std::vector<char> buf(torrent_file->piece_length());
|
||||
|
||||
for (int i = 0; i < num; ++i)
|
||||
{
|
||||
s->read(&buf[0], i, 0, torrent_file->piece_size(i));
|
||||
hasher h(&buf[0], torrent_file->piece_size(i));
|
||||
torrent_file->set_hash(i, h.final());
|
||||
s->read(&buf[0], i, 0, t.piece_size(i));
|
||||
hasher h(&buf[0], t.piece_size(i));
|
||||
t.set_hash(i, h.final());
|
||||
}
|
||||
|
||||
// to calculate the info_hash
|
||||
entry te = torrent_file->create_torrent();
|
||||
|
||||
entry e = t.generate();
|
||||
torrent_file = new torrent_info(e);
|
||||
s.reset(default_storage_constructor(torrent_file, ".", fp));
|
||||
|
||||
for (int i = 0; i < 6; ++i)
|
||||
test_transfer(*torrent_file, i);
|
||||
|
||||
|
||||
test_transfer(torrent_file, i);
|
||||
|
||||
stop_web_server(8000);
|
||||
remove_all("./test_torrent");
|
||||
|
Reference in New Issue
Block a user