2007-02-20 02:42:12 +01:00
|
|
|
#include "libtorrent/session.hpp"
|
|
|
|
#include "libtorrent/hasher.hpp"
|
|
|
|
#include "libtorrent/file_pool.hpp"
|
|
|
|
#include "libtorrent/storage.hpp"
|
2007-08-21 03:17:42 +02:00
|
|
|
#include "libtorrent/bencode.hpp"
|
2007-02-20 02:42:12 +01:00
|
|
|
#include <boost/thread.hpp>
|
|
|
|
#include <boost/tuple/tuple.hpp>
|
|
|
|
#include <boost/filesystem/operations.hpp>
|
2007-08-21 03:17:42 +02:00
|
|
|
#include <fstream>
|
2007-02-20 02:42:12 +01:00
|
|
|
|
|
|
|
#include "test.hpp"
|
|
|
|
#include "setup_transfer.hpp"
|
|
|
|
|
|
|
|
using namespace boost::filesystem;
|
|
|
|
using namespace libtorrent;
|
|
|
|
|
|
|
|
void add_files(
|
|
|
|
torrent_info& t
|
|
|
|
, path const& p
|
|
|
|
, path const& l)
|
|
|
|
{
|
|
|
|
if (l.leaf()[0] == '.') return;
|
|
|
|
path f(p / l);
|
|
|
|
if (is_directory(f))
|
|
|
|
{
|
|
|
|
for (directory_iterator i(f), end; i != end; ++i)
|
|
|
|
add_files(t, p, l / i->leaf());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << "adding \"" << l.string() << "\"\n";
|
|
|
|
t.add_file(l, file_size(f));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_transfer()
|
|
|
|
{
|
|
|
|
using namespace libtorrent;
|
|
|
|
|
2007-09-01 05:00:31 +02:00
|
|
|
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");
|
2007-02-20 02:42:12 +01:00
|
|
|
|
|
|
|
path full_path = "/Library/WebServer/Documents/bravia_paint_ad_70sec_1280x720.mov";
|
2007-09-01 05:00:31 +02:00
|
|
|
add_files(*torrent_file, full_path.branch_path(), full_path.leaf());
|
2007-02-20 02:42:12 +01:00
|
|
|
|
|
|
|
file_pool fp;
|
2007-04-02 08:21:53 +02:00
|
|
|
boost::scoped_ptr<storage_interface> s(default_storage_constructor(
|
|
|
|
torrent_file, full_path.branch_path(), fp));
|
2007-02-20 02:42:12 +01:00
|
|
|
// calculate the hash for all pieces
|
2007-09-01 05:00:31 +02:00
|
|
|
int num = torrent_file->num_pieces();
|
|
|
|
std::vector<char> buf(torrent_file->piece_length());
|
2007-02-20 02:42:12 +01:00
|
|
|
for (int i = 0; i < num; ++i)
|
|
|
|
{
|
2007-09-01 05:00:31 +02:00
|
|
|
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());
|
2007-02-20 02:42:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// to calculate the info_hash
|
2007-09-01 05:00:31 +02:00
|
|
|
entry te = torrent_file->create_torrent();
|
2007-08-21 03:17:42 +02:00
|
|
|
|
|
|
|
te.print(std::cout);
|
|
|
|
// std::ofstream torrent("web_seed.torrent", std::ios::binary | std::ios::trunc);
|
|
|
|
// bencode(std::ostream_iterator<char>(torrent), te);
|
2007-02-20 02:42:12 +01:00
|
|
|
|
|
|
|
session ses;
|
2007-06-10 22:46:09 +02:00
|
|
|
ses.set_severity_level(alert::debug);
|
2007-04-02 08:21:53 +02:00
|
|
|
ses.listen_on(std::make_pair(49000, 50000));
|
2007-02-20 02:42:12 +01:00
|
|
|
remove_all("./tmp1");
|
|
|
|
torrent_handle th = ses.add_torrent(torrent_file, "./tmp1");
|
|
|
|
|
2007-06-10 22:46:09 +02:00
|
|
|
for (int i = 0; i < 30; ++i)
|
2007-02-20 02:42:12 +01:00
|
|
|
{
|
|
|
|
torrent_status s = th.status();
|
2007-06-10 22:46:09 +02:00
|
|
|
std::cerr << s.progress << " " << (s.download_rate / 1000.f) << std::endl;
|
2007-02-20 02:42:12 +01:00
|
|
|
std::auto_ptr<alert> a;
|
|
|
|
a = ses.pop_alert();
|
|
|
|
if (a.get())
|
|
|
|
std::cerr << a->msg() << "\n";
|
|
|
|
|
|
|
|
if (th.is_seed()) break;
|
2007-06-10 22:46:09 +02:00
|
|
|
test_sleep(1000);
|
2007-02-20 02:42:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CHECK(th.is_seed());
|
|
|
|
}
|
|
|
|
|
|
|
|
int test_main()
|
|
|
|
{
|
|
|
|
using namespace libtorrent;
|
|
|
|
using namespace boost::filesystem;
|
|
|
|
|
|
|
|
test_transfer();
|
|
|
|
|
|
|
|
remove_all("./tmp1");
|
|
|
|
remove_all("./tmp2");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|