2006-05-28 21:03:54 +02:00
|
|
|
#include "libtorrent/session.hpp"
|
|
|
|
#include "libtorrent/hasher.hpp"
|
2006-10-11 16:02:21 +02:00
|
|
|
#include <fstream>
|
2006-05-28 21:03:54 +02:00
|
|
|
#include <boost/thread.hpp>
|
|
|
|
#include <boost/tuple/tuple.hpp>
|
2006-10-11 16:02:21 +02:00
|
|
|
#include <boost/filesystem/operations.hpp>
|
|
|
|
#include <boost/filesystem/convenience.hpp>
|
2006-05-28 21:03:54 +02:00
|
|
|
|
|
|
|
#include "test.hpp"
|
|
|
|
|
2006-10-11 16:02:21 +02:00
|
|
|
using boost::filesystem::remove_all;
|
|
|
|
using boost::filesystem::create_directory;
|
|
|
|
|
2007-06-10 22:46:09 +02:00
|
|
|
void test_sleep(int millisec)
|
2006-05-28 21:03:54 +02:00
|
|
|
{
|
|
|
|
boost::xtime xt;
|
|
|
|
boost::xtime_get(&xt, boost::TIME_UTC);
|
2007-06-10 22:46:09 +02:00
|
|
|
boost::uint64_t nanosec = (millisec % 1000) * 1000000 + xt.nsec;
|
|
|
|
int sec = millisec / 1000;
|
|
|
|
if (nanosec > 1000000000)
|
|
|
|
{
|
|
|
|
nanosec -= 1000000000;
|
|
|
|
sec++;
|
|
|
|
}
|
|
|
|
xt.nsec = nanosec;
|
|
|
|
xt.sec += sec;
|
2006-05-28 21:03:54 +02:00
|
|
|
boost::thread::sleep(xt);
|
|
|
|
}
|
|
|
|
|
|
|
|
using namespace libtorrent;
|
|
|
|
|
2007-06-09 01:02:31 +02:00
|
|
|
boost::tuple<torrent_handle, torrent_handle, torrent_handle>
|
|
|
|
setup_transfer(session* ses1, session* ses2, session* ses3
|
|
|
|
, bool clear_files, bool use_metadata_transfer)
|
2006-05-28 21:03:54 +02:00
|
|
|
{
|
|
|
|
using namespace boost::filesystem;
|
|
|
|
|
2007-06-09 01:02:31 +02:00
|
|
|
assert(ses1);
|
|
|
|
assert(ses2);
|
|
|
|
|
|
|
|
assert(ses1->id() != ses2->id());
|
|
|
|
if (ses3)
|
|
|
|
assert(ses3->id() != ses2->id());
|
|
|
|
|
2006-05-28 21:03:54 +02:00
|
|
|
char const* tracker_url = "http://non-existent-name.com/announce";
|
|
|
|
|
|
|
|
torrent_info t;
|
2007-06-09 01:02:31 +02:00
|
|
|
int total_size = 2 * 1024 * 1024;
|
|
|
|
t.add_file(path("temporary"), total_size);
|
|
|
|
t.set_piece_size(16 * 1024);
|
2006-05-28 21:03:54 +02:00
|
|
|
t.add_tracker(tracker_url);
|
|
|
|
|
2007-06-09 01:02:31 +02:00
|
|
|
std::vector<char> piece(16 * 1024);
|
2007-07-03 01:48:06 +02:00
|
|
|
for (int i = 0; i < int(piece.size()); ++i)
|
2007-06-20 20:41:53 +02:00
|
|
|
piece[i] = (i % 26) + 'A';
|
2006-05-28 21:03:54 +02:00
|
|
|
|
|
|
|
// calculate the hash for all pieces
|
|
|
|
int num = t.num_pieces();
|
2007-06-09 01:02:31 +02:00
|
|
|
sha1_hash ph = hasher(&piece[0], piece.size()).final();
|
2006-05-28 21:03:54 +02:00
|
|
|
for (int i = 0; i < num; ++i)
|
2007-06-09 01:02:31 +02:00
|
|
|
t.set_hash(i, ph);
|
2006-05-28 21:03:54 +02:00
|
|
|
|
|
|
|
create_directory("./tmp1");
|
|
|
|
std::ofstream file("./tmp1/temporary");
|
2007-06-09 01:02:31 +02:00
|
|
|
while (total_size > 0)
|
|
|
|
{
|
2007-08-21 09:33:06 +02:00
|
|
|
file.write(&piece[0], (std::min)(int(piece.size()), total_size));
|
2007-06-09 01:02:31 +02:00
|
|
|
total_size -= piece.size();
|
|
|
|
}
|
2006-05-28 21:03:54 +02:00
|
|
|
file.close();
|
|
|
|
if (clear_files) remove_all("./tmp2/temporary");
|
|
|
|
|
|
|
|
t.create_torrent();
|
2007-06-20 20:41:53 +02:00
|
|
|
std::cerr << "generated torrent: " << t.info_hash() << std::endl;
|
2006-05-28 21:03:54 +02:00
|
|
|
|
2007-06-09 01:02:31 +02:00
|
|
|
ses1->set_severity_level(alert::debug);
|
|
|
|
ses2->set_severity_level(alert::debug);
|
2006-05-28 21:03:54 +02:00
|
|
|
|
|
|
|
// they should not use the same save dir, because the
|
|
|
|
// file pool will complain if two torrents are trying to
|
|
|
|
// use the same files
|
2007-06-10 22:46:09 +02:00
|
|
|
sha1_hash info_hash = t.info_hash();
|
2007-06-09 01:02:31 +02:00
|
|
|
torrent_handle tor1 = ses1->add_torrent(t, "./tmp1");
|
2007-06-06 02:41:20 +02:00
|
|
|
torrent_handle tor2;
|
2007-06-09 01:02:31 +02:00
|
|
|
torrent_handle tor3;
|
|
|
|
if (ses3) tor3 = ses3->add_torrent(t, "./tmp3");
|
|
|
|
|
2007-06-06 02:41:20 +02:00
|
|
|
if (use_metadata_transfer)
|
2007-06-09 01:02:31 +02:00
|
|
|
tor2 = ses2->add_torrent(tracker_url
|
2006-11-14 01:08:16 +01:00
|
|
|
, t.info_hash(), 0, "./tmp2");
|
2007-06-06 02:41:20 +02:00
|
|
|
else
|
2007-06-09 01:02:31 +02:00
|
|
|
tor2 = ses2->add_torrent(t, "./tmp2");
|
2006-11-14 01:08:16 +01:00
|
|
|
|
2007-06-10 22:46:09 +02:00
|
|
|
assert(ses1->get_torrents().size() == 1);
|
|
|
|
assert(ses2->get_torrents().size() == 1);
|
|
|
|
|
|
|
|
test_sleep(100);
|
2006-05-28 21:03:54 +02:00
|
|
|
|
|
|
|
std::cerr << "connecting peer\n";
|
|
|
|
tor1.connect_peer(tcp::endpoint(address::from_string("127.0.0.1")
|
2007-06-09 01:02:31 +02:00
|
|
|
, ses2->listen_port()));
|
|
|
|
|
|
|
|
if (ses3)
|
|
|
|
{
|
|
|
|
// give the other peers some time to get an initial
|
|
|
|
// set of pieces before they start sharing with each-other
|
|
|
|
tor3.connect_peer(tcp::endpoint(
|
|
|
|
address::from_string("127.0.0.1")
|
|
|
|
, ses2->listen_port()));
|
|
|
|
tor3.connect_peer(tcp::endpoint(
|
|
|
|
address::from_string("127.0.0.1")
|
|
|
|
, ses1->listen_port()));
|
|
|
|
}
|
2006-05-28 21:03:54 +02:00
|
|
|
|
2007-06-09 01:02:31 +02:00
|
|
|
return boost::make_tuple(tor1, tor2, tor3);
|
2006-05-28 21:03:54 +02:00
|
|
|
}
|
|
|
|
|