premiere-libtorrent/examples/client_test.cpp

462 lines
11 KiB
C++
Raw Normal View History

/*
Copyright (c) 2003, Arvid Norberg
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution.
* Neither the name of the author nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include <iostream>
#include <fstream>
#include <iterator>
#include <exception>
#ifdef _MSC_VER
#pragma warning(push, 1)
#endif
#include <boost/format.hpp>
2003-12-14 06:56:12 +01:00
#include <boost/date_time/posix_time/posix_time.hpp>
2004-01-13 04:08:59 +01:00
#include <boost/filesystem/fstream.hpp>
2004-02-18 01:08:20 +01:00
#include <boost/filesystem/exception.hpp>
2004-01-17 21:04:19 +01:00
#include <boost/bind.hpp>
#ifdef _MSC_VER
#pragma warning(pop)
#endif
#include "libtorrent/entry.hpp"
#include "libtorrent/bencode.hpp"
#include "libtorrent/session.hpp"
#include "libtorrent/http_settings.hpp"
2003-12-22 08:14:35 +01:00
#include "libtorrent/identify_client.hpp"
2004-02-01 01:55:28 +01:00
#include "libtorrent/alert_types.hpp"
2003-10-30 00:28:09 +01:00
#ifdef WIN32
2003-11-09 19:17:09 +01:00
#if defined(_MSC_VER)
# define for if (false) {} else for
#endif
#include <windows.h>
2003-10-30 00:28:09 +01:00
#include <conio.h>
bool sleep_and_input(char* c)
{
2004-01-17 21:04:19 +01:00
Sleep(500);
2003-10-30 00:28:09 +01:00
if (kbhit())
{
*c = getch();
return true;
}
return false;
};
void set_cursor(int x, int y)
{
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
COORD c = {x, y};
SetConsoleCursorPosition(h, c);
}
void clear()
{
2003-12-14 23:55:32 +01:00
CONSOLE_SCREEN_BUFFER_INFO si;
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
2003-12-14 23:55:32 +01:00
GetConsoleScreenBufferInfo(h, &si);
COORD c = {0, 0};
DWORD n;
2003-12-14 23:55:32 +01:00
FillConsoleOutputCharacter(h, ' ', si.dwSize.X * si.dwSize.Y, c, &n);
}
2003-10-31 12:17:45 +01:00
#else
#include <stdlib.h>
#include <stdio.h>
#include <termios.h>
#include <string.h>
struct set_keypress
{
set_keypress()
{
termios new_settings;
tcgetattr(0,&stored_settings);
new_settings = stored_settings;
// Disable canonical mode, and set buffer size to 1 byte
new_settings.c_lflag &= (~ICANON);
new_settings.c_cc[VTIME] = 0;
new_settings.c_cc[VMIN] = 1;
tcsetattr(0,TCSANOW,&new_settings);
}
~set_keypress() { tcsetattr(0,TCSANOW,&stored_settings); }
termios stored_settings;
};
bool sleep_and_input(char* c)
{
// sets the terminal to single-character mode
// and resets when destructed
set_keypress s;
fd_set set;
FD_ZERO(&set);
FD_SET(0, &set);
2004-01-17 21:04:19 +01:00
timeval tv = {0, 500000};
2003-10-31 12:17:45 +01:00
if (select(1, &set, 0, 0, &tv) > 0)
{
*c = getc(stdin);
return true;
}
return false;
}
2003-11-05 00:27:06 +01:00
void set_cursor(int x, int y)
{
std::cout << "\033[" << y << ";" << x << "H";
2003-11-05 00:27:06 +01:00
}
void clear()
{
std::cout << "\033[2J";
2003-11-05 00:27:06 +01:00
}
2003-10-30 00:28:09 +01:00
#endif
2003-12-14 23:55:32 +01:00
std::string to_string(float v, int width)
2003-12-08 02:37:30 +01:00
{
std::stringstream s;
2003-12-14 23:55:32 +01:00
s.precision(width-2);
2003-12-08 02:37:30 +01:00
s.flags(std::ios_base::right);
2003-12-14 23:55:32 +01:00
s.width(width);
2003-12-08 02:37:30 +01:00
s.fill(' ');
s << v;
return s.str();
}
2004-01-12 04:05:10 +01:00
std::string pos_to_string(float v, int width)
{
std::stringstream s;
s.precision(width-1);
s.flags(std::ios_base::right);
s.width(width);
s.fill(' ');
s << fabs(v);
return s.str();
}
std::string ratio(float a, float b)
{
std::stringstream s;
if (a > b)
{
if (b < 0.001f) s << " inf:1";
else s << pos_to_string(a/b, 4) << ":1";
}
else if (a < b)
{
if (a < 0.001f) s << " 1:inf";
else s << "1:" << pos_to_string(b/a, 4);
}
else
{
s << " 1:1";
}
return s.str();
}
2003-10-31 05:02:51 +01:00
std::string add_suffix(float val)
{
2003-12-14 23:55:32 +01:00
const char* prefix[] = {"B", "kB", "MB", "GB", "TB"};
2003-10-31 05:02:51 +01:00
const int num_prefix = sizeof(prefix) / sizeof(const char*);
int i;
for (i = 0; i < num_prefix; ++i)
{
2004-01-13 04:08:59 +01:00
if (fabs(val) < 1000.f)
2003-12-14 23:55:32 +01:00
return to_string(val, i==0?7:6) + prefix[i];
2004-01-13 04:08:59 +01:00
val /= 1000.f;
2003-10-31 05:02:51 +01:00
}
2003-12-14 23:55:32 +01:00
return to_string(val, 6) + prefix[i];
2003-10-31 05:02:51 +01:00
}
2004-01-15 17:45:34 +01:00
std::string progress_bar(float progress, int width)
{
std::vector<char> bar;
bar.reserve(width);
std::fill_n(std::back_inserter(bar), progress * width, '#');
std::fill_n(std::back_inserter(bar), width - (progress * width), '-');
return std::string(bar.begin(), bar.end());
}
int main(int argc, char* argv[])
{
using namespace libtorrent;
if (argc < 2)
{
2003-10-26 04:18:17 +01:00
std::cerr << "usage: ./client_test torrent-files ...\n"
2003-11-06 11:44:19 +01:00
"to stop the client, press q.\n";
return 1;
}
2004-02-18 01:08:20 +01:00
boost::filesystem::path::default_name_check(boost::filesystem::native);
http_settings settings;
// settings.proxy_ip = "192.168.0.1";
// settings.proxy_port = 80;
// settings.proxy_login = "hyd";
// settings.proxy_password = "foobar";
2004-01-17 21:04:19 +01:00
settings.user_agent = "client_test";
2003-12-22 08:14:35 +01:00
std::deque<std::string> events;
try
{
std::vector<torrent_handle> handles;
2004-01-26 11:29:00 +01:00
session ses(std::make_pair(6881, 6889));
2003-12-07 06:53:04 +01:00
2004-02-25 00:48:02 +01:00
ses.set_upload_rate_limit(20 * 1024);
2003-12-22 08:14:35 +01:00
ses.set_http_settings(settings);
2004-01-05 02:30:34 +01:00
ses.set_severity_level(alert::debug);
2003-12-07 06:53:04 +01:00
for (int i = 0; i < argc-1; ++i)
{
try
{
std::ifstream in(argv[i+1], std::ios_base::binary);
in.unsetf(std::ios_base::skipws);
entry e = bdecode(std::istream_iterator<char>(in), std::istream_iterator<char>());
torrent_info t(e);
t.print(std::cout);
2004-01-02 21:46:24 +01:00
2004-01-13 04:08:59 +01:00
boost::filesystem::path save_path("");
2004-01-07 03:45:03 +01:00
entry resume_data;
try
{
2004-01-13 04:08:59 +01:00
std::stringstream s;
s << t.name() << ".fastresume";
boost::filesystem::ifstream resume_file(save_path / s.str(), std::ios_base::binary);
2004-01-07 03:45:03 +01:00
resume_file.unsetf(std::ios_base::skipws);
resume_data = bdecode(std::istream_iterator<char>(resume_file)
, std::istream_iterator<char>());
}
2004-02-18 01:08:20 +01:00
catch (invalid_encoding&) {}
catch (boost::filesystem::filesystem_error&) {}
2004-01-02 21:46:24 +01:00
2004-01-13 04:08:59 +01:00
handles.push_back(ses.add_torrent(t, save_path, resume_data));
handles.back().set_max_connections(60);
2004-01-14 12:46:26 +01:00
handles.back().set_max_uploads(-1);
handles.back().set_ratio(1.02f);
}
catch (std::exception& e)
{
std::cout << e.what() << "\n";
}
}
2003-10-30 00:28:09 +01:00
std::vector<peer_info> peers;
std::vector<partial_piece_info> queue;
2003-10-30 00:28:09 +01:00
for (;;)
{
2003-10-30 00:28:09 +01:00
char c;
if (sleep_and_input(&c))
{
2004-01-02 21:46:24 +01:00
if (c == 'q')
{
2004-01-13 04:08:59 +01:00
for (std::vector<torrent_handle>::iterator i = handles.begin();
i != handles.end();
++i)
{
torrent_handle h = *i;
entry data = h.write_resume_data();
std::stringstream s;
s << h.get_torrent_info().name() << ".fastresume";
boost::filesystem::ofstream out(h.save_path() / s.str(), std::ios_base::binary);
out.unsetf(std::ios_base::skipws);
bencode(std::ostream_iterator<char>(out), data);
}
2004-01-02 21:46:24 +01:00
break;
}
2004-01-14 17:22:49 +01:00
2004-01-17 21:04:19 +01:00
if(c == 'r')
2004-01-14 17:22:49 +01:00
{
2004-01-17 21:04:19 +01:00
// force reannounce on all torrents
std::for_each(
handles.begin()
, handles.end()
, boost::bind(&torrent_handle::force_reannounce, _1));
2004-01-14 17:22:49 +01:00
}
2003-10-30 00:28:09 +01:00
}
2003-12-22 08:14:35 +01:00
std::auto_ptr<alert> a;
a = ses.pop_alert();
while (a.get())
{
2004-02-01 01:50:18 +01:00
torrent_finished_alert* p = dynamic_cast<torrent_finished_alert*>(a.get());
if (p)
{
2004-02-01 14:48:30 +01:00
ses.set_upload_rate_limit(30000);
p->handle.set_max_connections(10);
p->handle.set_max_uploads(5);
2004-02-01 01:50:18 +01:00
}
2004-01-05 02:30:34 +01:00
if (events.size() >= 10) events.pop_front();
2004-01-14 20:24:11 +01:00
events.push_back(a->msg());
2003-12-22 08:14:35 +01:00
a = ses.pop_alert();
}
2003-12-08 22:59:48 +01:00
std::stringstream out;
2003-10-31 05:02:51 +01:00
for (std::vector<torrent_handle>::iterator i = handles.begin();
i != handles.end();
2003-10-30 00:28:09 +01:00
++i)
{
2003-10-31 05:02:51 +01:00
torrent_status s = i->status();
switch(s.state)
{
case torrent_status::queued_for_checking:
2003-12-08 22:59:48 +01:00
out << "queued ";
2003-10-31 05:02:51 +01:00
break;
case torrent_status::checking_files:
2003-12-08 22:59:48 +01:00
out << "checking ";
2003-10-31 05:02:51 +01:00
break;
2003-12-22 08:14:35 +01:00
case torrent_status::connecting_to_tracker:
out << "connecting to tracker ";
break;
2003-10-31 05:02:51 +01:00
case torrent_status::downloading:
2003-12-22 08:14:35 +01:00
out << "downloading ";
2003-10-31 05:02:51 +01:00
break;
case torrent_status::seeding:
2003-12-08 22:59:48 +01:00
out << "seeding ";
2003-10-31 05:02:51 +01:00
break;
};
i->get_peer_info(peers);
2003-12-14 06:56:12 +01:00
2003-12-08 22:59:48 +01:00
out.precision(4);
out.width(5);
out.fill(' ');
out << (s.progress*100) << "% ";
2004-01-15 17:45:34 +01:00
out << progress_bar(s.progress, 49);
2003-12-08 22:59:48 +01:00
out << "\n";
2004-01-15 17:45:34 +01:00
out << "total downloaded: " << s.total_done << " Bytes\n";
2004-01-31 12:03:48 +01:00
out << "peers: " << (int)peers.size() << " "
<< "d:" << add_suffix(s.download_rate) << "/s "
<< "(" << add_suffix(s.total_download) << ") "
<< "u:" << add_suffix(s.upload_rate) << "/s "
<< "(" << add_suffix(s.total_upload) << ") "
<< "ratio: " << ratio(s.total_payload_download, s.total_payload_upload) << "\n";
2003-11-05 00:27:06 +01:00
boost::posix_time::time_duration t = s.next_announce;
2003-12-14 06:56:12 +01:00
out << "next announce: " << boost::posix_time::to_simple_string(t) << "\n";
2004-01-31 11:20:19 +01:00
out << "tracker: " << s.current_tracker << "\n";
2003-12-08 02:37:30 +01:00
2004-02-24 20:23:37 +01:00
// out << "___________________________________\n";
2004-01-15 17:45:34 +01:00
2003-12-07 06:53:04 +01:00
for (std::vector<peer_info>::iterator i = peers.begin();
i != peers.end();
++i)
{
2004-02-23 23:54:54 +01:00
out << "d:" << add_suffix(i->down_speed) << "/s "
2003-12-08 17:39:05 +01:00
<< "(" << add_suffix(i->total_download) << ") "
2004-02-23 23:54:54 +01:00
<< "u:" << add_suffix(i->up_speed) << "/s "
2003-12-08 17:39:05 +01:00
<< "(" << add_suffix(i->total_upload) << ") "
2004-01-14 12:46:26 +01:00
<< "ul:" << add_suffix(i->upload_limit) << "/s "
2004-02-24 20:23:37 +01:00
<< "uc:" << add_suffix(i->upload_ceiling) << "/s "
2004-02-23 23:54:54 +01:00
// << "df:" << ratio(i->total_download, i->total_upload) << " "
2004-02-24 20:23:37 +01:00
// << "q:" << i->download_queue_length << " "
// << "r:" << i->upload_queue_length << " "
// << "f:"
// << static_cast<const char*>((i->flags & peer_info::interesting)?"I":"_")
// << static_cast<const char*>((i->flags & peer_info::choked)?"C":"_")
// << static_cast<const char*>((i->flags & peer_info::remote_interested)?"i":"_")
// << static_cast<const char*>((i->flags & peer_info::remote_choked)?"c":"_")
// << static_cast<const char*>((i->flags & peer_info::supports_extensions)?"e":"_")
// << static_cast<const char*>((i->flags & peer_info::local_connection)?"l":"r")
2004-01-04 05:29:13 +01:00
<< "\n";
2004-01-21 01:59:38 +01:00
/*
2003-12-10 01:24:16 +01:00
if (i->downloading_piece_index >= 0)
{
2004-01-15 17:45:34 +01:00
out.width(5);
out.fill('0');
2003-12-10 01:24:16 +01:00
out << i->downloading_piece_index << ";"
<< i->downloading_block_index << ": ";
2004-01-15 17:45:34 +01:00
out << progress_bar(
i->downloading_progress / static_cast<float>(i->downloading_total)
, 50);
2004-01-07 01:48:02 +01:00
out << "\n";
2003-12-10 01:24:16 +01:00
}
2004-01-21 01:59:38 +01:00
*/
2003-12-07 06:53:04 +01:00
}
2004-02-24 20:23:37 +01:00
/*
2003-12-08 22:59:48 +01:00
out << "___________________________________\n";
2003-12-14 06:56:12 +01:00
i->get_download_queue(queue);
for (std::vector<partial_piece_info>::iterator i = queue.begin();
i != queue.end();
++i)
{
2003-12-08 22:59:48 +01:00
out.width(4);
out.fill(' ');
out << i->piece_index << ": |";
for (int j = 0; j < i->blocks_in_piece; ++j)
{
2003-12-08 22:59:48 +01:00
if (i->finished_blocks[j]) out << "#";
2004-01-15 17:45:34 +01:00
else if (i->requested_blocks[j]) out << "+";
2003-12-14 06:56:12 +01:00
else out << ".";
}
2003-12-08 22:59:48 +01:00
out << "|\n";
}
2003-12-08 10:38:40 +01:00
2003-12-08 22:59:48 +01:00
out << "___________________________________\n";
2004-02-24 20:23:37 +01:00
*/
2003-10-30 00:28:09 +01:00
}
2004-01-14 17:22:49 +01:00
2003-12-22 08:14:35 +01:00
for (std::deque<std::string>::iterator i = events.begin();
i != events.end();
++i)
{
out << *i << "\n";
}
2004-01-14 17:22:49 +01:00
2003-12-08 22:59:48 +01:00
clear();
set_cursor(0, 0);
2004-01-21 01:59:38 +01:00
puts(out.str().c_str());
// std::cout << out.str();
// std::cout.flush();
2003-10-30 00:28:09 +01:00
}
}
catch (std::exception& e)
{
std::cout << e.what() << "\n";
}
return 0;
}