2003-10-23 01:00:57 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
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>
|
|
|
|
|
2004-01-25 19:18:36 +01:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(push, 1)
|
|
|
|
#endif
|
|
|
|
|
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>
|
2003-11-02 22:06:50 +01:00
|
|
|
|
2004-01-25 19:18:36 +01:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
#include "libtorrent/entry.hpp"
|
|
|
|
#include "libtorrent/bencode.hpp"
|
|
|
|
#include "libtorrent/session.hpp"
|
2005-08-25 15:11:39 +02:00
|
|
|
#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-23 01:00:57 +02:00
|
|
|
|
2005-08-15 00:04:58 +02:00
|
|
|
using boost::bind;
|
|
|
|
|
2004-09-12 15:53:00 +02:00
|
|
|
#ifdef _WIN32
|
2003-11-09 19:17:09 +01:00
|
|
|
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
# define for if (false) {} else for
|
|
|
|
#endif
|
|
|
|
|
2003-11-02 22:06:50 +01:00
|
|
|
#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;
|
|
|
|
};
|
|
|
|
|
2003-11-02 22:06:50 +01:00
|
|
|
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;
|
2003-11-02 22:06:50 +01:00
|
|
|
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
|
2003-12-14 23:55:32 +01:00
|
|
|
GetConsoleScreenBufferInfo(h, &si);
|
2003-11-02 22:06:50 +01:00
|
|
|
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-11-02 22:06:50 +01:00
|
|
|
}
|
|
|
|
|
2003-10-31 12:17:45 +01:00
|
|
|
#else
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <termios.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2005-08-15 00:04:58 +02:00
|
|
|
#define ANSI_TERMINAL_COLORS
|
|
|
|
|
2003-10-31 12:17:45 +01:00
|
|
|
struct set_keypress
|
|
|
|
{
|
|
|
|
set_keypress()
|
|
|
|
{
|
2005-08-25 15:11:39 +02:00
|
|
|
termios new_settings;
|
|
|
|
tcgetattr(0,&stored_settings);
|
|
|
|
new_settings = stored_settings;
|
2003-10-31 12:17:45 +01:00
|
|
|
// Disable canonical mode, and set buffer size to 1 byte
|
2005-08-25 15:11:39 +02:00
|
|
|
new_settings.c_lflag &= (~ICANON);
|
|
|
|
new_settings.c_cc[VTIME] = 0;
|
|
|
|
new_settings.c_cc[VMIN] = 1;
|
|
|
|
tcsetattr(0,TCSANOW,&new_settings);
|
2003-10-31 12:17:45 +01:00
|
|
|
}
|
2005-08-25 15:11:39 +02:00
|
|
|
~set_keypress() { tcsetattr(0,TCSANOW,&stored_settings); }
|
|
|
|
termios stored_settings;
|
2003-10-31 12:17:45 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2003-11-23 04:00:45 +01:00
|
|
|
std::cout << "\033[" << y << ";" << x << "H";
|
2003-11-05 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void clear()
|
|
|
|
{
|
2003-11-23 04:00:45 +01:00
|
|
|
std::cout << "\033[2J";
|
2003-11-05 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2003-10-30 00:28:09 +01:00
|
|
|
#endif
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2005-08-15 00:04:58 +02:00
|
|
|
std::string esc(char const* code)
|
|
|
|
{
|
|
|
|
#ifdef ANSI_TERMINAL_COLORS
|
|
|
|
std::string ret;
|
|
|
|
ret += char(0x1b);
|
|
|
|
ret += "[";
|
|
|
|
ret += code;
|
|
|
|
ret += "m";
|
|
|
|
return ret;
|
|
|
|
#else
|
|
|
|
return std::string();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2005-05-30 19:43:03 +02:00
|
|
|
std::string to_string(float v, int width, int precision = 3)
|
2003-12-08 02:37:30 +01:00
|
|
|
{
|
|
|
|
std::stringstream s;
|
2005-05-25 12:01:01 +02:00
|
|
|
s.precision(precision);
|
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();
|
|
|
|
}
|
|
|
|
|
2005-05-25 12:01:01 +02:00
|
|
|
std::string pos_to_string(float v, int width, int precision = 4)
|
2004-01-12 04:05:10 +01:00
|
|
|
{
|
|
|
|
std::stringstream s;
|
2005-05-25 12:01:01 +02:00
|
|
|
s.precision(precision);
|
2004-01-12 04:05:10 +01:00
|
|
|
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*);
|
2005-05-25 12:01:01 +02:00
|
|
|
for (int i = 0; i < num_prefix; ++i)
|
2003-10-31 05:02:51 +01:00
|
|
|
{
|
2004-01-13 04:08:59 +01:00
|
|
|
if (fabs(val) < 1000.f)
|
2005-05-30 19:43:03 +02:00
|
|
|
return to_string(val, i==0?5:4) + prefix[i];
|
2005-05-25 12:01:01 +02:00
|
|
|
val /= 1000.f;
|
2003-10-31 05:02:51 +01:00
|
|
|
}
|
2004-06-05 14:24:35 +02:00
|
|
|
return to_string(val, 6) + "PB";
|
2003-10-31 05:02:51 +01:00
|
|
|
}
|
|
|
|
|
2005-08-15 00:04:58 +02:00
|
|
|
std::string progress_bar(float progress, int width, char const* code = "33")
|
2004-01-15 17:45:34 +01:00
|
|
|
{
|
2005-08-15 00:04:58 +02:00
|
|
|
std::string bar;
|
2004-01-15 17:45:34 +01:00
|
|
|
bar.reserve(width);
|
|
|
|
|
2005-05-31 00:50:54 +02:00
|
|
|
int progress_chars = static_cast<int>(progress * width + .5f);
|
2005-08-15 00:04:58 +02:00
|
|
|
bar = esc(code);
|
2005-05-30 19:43:03 +02:00
|
|
|
std::fill_n(std::back_inserter(bar), progress_chars, '#');
|
2005-08-15 00:04:58 +02:00
|
|
|
bar += esc("0");
|
2005-05-30 19:43:03 +02:00
|
|
|
std::fill_n(std::back_inserter(bar), width - progress_chars, '-');
|
2004-01-15 17:45:34 +01:00
|
|
|
return std::string(bar.begin(), bar.end());
|
|
|
|
}
|
|
|
|
|
2005-08-15 00:04:58 +02:00
|
|
|
char const* peer_index(libtorrent::address addr, std::vector<libtorrent::peer_info> const& peers)
|
|
|
|
{
|
|
|
|
using namespace libtorrent;
|
|
|
|
std::vector<peer_info>::const_iterator i = std::find_if(peers.begin()
|
|
|
|
, peers.end(), bind(std::equal_to<address>(), bind(&peer_info::ip, _1), addr));
|
|
|
|
if (i == peers.end()) return "+";
|
|
|
|
|
|
|
|
static char str[] = " ";
|
|
|
|
int index = i - peers.begin();
|
|
|
|
str[0] = (index < 10)?'0' + index:'A' + index - 10;
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2004-07-24 13:54:17 +02:00
|
|
|
void print_peer_info(std::ostream& out, std::vector<libtorrent::peer_info> const& peers)
|
|
|
|
{
|
|
|
|
using namespace libtorrent;
|
|
|
|
|
2005-05-30 19:43:03 +02:00
|
|
|
out << " down up q r flags block progress client \n";
|
2004-07-24 13:54:17 +02:00
|
|
|
|
|
|
|
for (std::vector<peer_info>::const_iterator i = peers.begin();
|
2005-05-25 12:01:01 +02:00
|
|
|
i != peers.end(); ++i)
|
2004-07-24 13:54:17 +02:00
|
|
|
{
|
|
|
|
out.fill(' ');
|
|
|
|
out.width(2);
|
2005-08-15 00:04:58 +02:00
|
|
|
out << esc("32") << add_suffix(i->down_speed) << "/s " << esc("0")
|
2004-07-24 13:54:17 +02:00
|
|
|
// << "(" << add_suffix(i->total_download) << ") "
|
2005-08-15 00:04:58 +02:00
|
|
|
<< esc("31") << add_suffix(i->up_speed) << "/s " << esc("0")
|
2004-07-24 13:54:17 +02:00
|
|
|
// << "(" << add_suffix(i->total_upload) << ") "
|
|
|
|
// << "ul:" << add_suffix(i->upload_limit) << "/s "
|
|
|
|
// << "uc:" << add_suffix(i->upload_ceiling) << "/s "
|
|
|
|
// << "df:" << ratio(i->total_download, i->total_upload) << " "
|
2005-05-25 12:01:01 +02:00
|
|
|
<< to_string(i->download_queue_length, 2, 2) << " "
|
|
|
|
<< to_string(i->upload_queue_length, 2, 2) << " "
|
2004-07-24 13:54:17 +02:00
|
|
|
<< 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":"_")
|
2005-05-30 19:43:03 +02:00
|
|
|
<< static_cast<const char*>((i->flags & peer_info::local_connection)?"l":"r") << " ";
|
2004-07-24 13:54:17 +02:00
|
|
|
|
|
|
|
if (i->downloading_piece_index >= 0)
|
|
|
|
{
|
2005-05-30 19:43:03 +02:00
|
|
|
out << progress_bar(
|
2004-07-24 13:54:17 +02:00
|
|
|
i->downloading_progress / static_cast<float>(i->downloading_total)
|
|
|
|
, 15);
|
|
|
|
}
|
2005-05-30 19:43:03 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
out << progress_bar(0.f, 15);
|
|
|
|
}
|
2004-07-24 13:54:17 +02:00
|
|
|
|
2005-05-30 19:43:03 +02:00
|
|
|
out << " " << identify_client(i->id) << "\n";
|
2004-07-24 13:54:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
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";
|
2003-10-23 01:00:57 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2004-06-14 01:30:42 +02:00
|
|
|
namespace fs = boost::filesystem;
|
2004-03-17 13:55:26 +01:00
|
|
|
fs::path::default_name_check(fs::no_check);
|
2004-02-18 01:08:20 +01:00
|
|
|
|
2005-08-25 15:11:39 +02:00
|
|
|
http_settings settings;
|
|
|
|
// settings.proxy_ip = "192.168.0.1";
|
|
|
|
// settings.proxy_port = 80;
|
|
|
|
// settings.proxy_login = "hyd";
|
|
|
|
// settings.proxy_password = "foobar";
|
|
|
|
settings.user_agent = "client_test";
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2003-12-22 08:14:35 +01:00
|
|
|
std::deque<std::string> events;
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
std::vector<torrent_handle> handles;
|
2004-08-08 23:26:40 +02:00
|
|
|
session ses;
|
2003-12-07 06:53:04 +01:00
|
|
|
|
2004-09-08 01:16:11 +02:00
|
|
|
ses.listen_on(std::make_pair(6880, 6889));
|
2005-08-23 11:59:56 +02:00
|
|
|
//ses.set_upload_rate_limit(512 * 1024);
|
2005-08-25 15:11:39 +02:00
|
|
|
ses.set_http_settings(settings);
|
2004-04-14 05:32:02 +02:00
|
|
|
ses.set_severity_level(alert::debug);
|
|
|
|
// ses.set_severity_level(alert::info);
|
2003-12-07 06:53:04 +01:00
|
|
|
|
2005-07-06 02:58:23 +02:00
|
|
|
// look for ipfilter.dat
|
|
|
|
// poor man's parser
|
|
|
|
// reads emule ipfilter files.
|
|
|
|
// with the following format:
|
|
|
|
//
|
|
|
|
// <first-ip> - <last-ip> , <access> , <comment>
|
|
|
|
//
|
|
|
|
// first-ip is an ip address that defines the first
|
|
|
|
// address of the range
|
|
|
|
// last-ip is the last ip address in the range
|
|
|
|
// access is a number specifying the access control
|
|
|
|
// for this ip-range. Right now values > 127 = allowed
|
|
|
|
// and numbers <= 127 = blocked
|
|
|
|
// the rest of the line is ignored
|
|
|
|
//
|
|
|
|
// In the original spec ranges may not overlap, but
|
|
|
|
// here ranges may overlap, and it is the last added
|
|
|
|
// rule that has precedence for addresses that may fall
|
|
|
|
// into more than one range.
|
|
|
|
std::ifstream in("ipfilter.dat");
|
|
|
|
ip_filter filter;
|
|
|
|
while (in.good())
|
|
|
|
{
|
|
|
|
char line[300];
|
|
|
|
in.getline(line, 300);
|
|
|
|
int len = in.gcount();
|
|
|
|
if (len <= 0) continue;
|
|
|
|
if (line[0] == '#') continue;
|
|
|
|
int a, b, c, d;
|
|
|
|
char dummy;
|
|
|
|
in >> a >> dummy >> b >> dummy >> c >> dummy >> d >> dummy;
|
|
|
|
address start(a, b, c, d, 0);
|
|
|
|
in >> a >> dummy >> b >> dummy >> c >> dummy >> d >> dummy;
|
|
|
|
address last(a, b, c, d, 0);
|
|
|
|
int flags;
|
|
|
|
in >> flags;
|
|
|
|
if (flags <= 127) flags = ip_filter::blocked;
|
|
|
|
else flags = 0;
|
|
|
|
if (in.fail()) break;
|
|
|
|
filter.add_rule(start, last, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
ses.set_ip_filter(filter);
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
for (int i = 0; i < argc-1; ++i)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2004-10-18 12:46:55 +02:00
|
|
|
boost::filesystem::path save_path("./");
|
2004-06-14 01:30:42 +02:00
|
|
|
|
|
|
|
if (std::string(argv[i+1]).substr(0, 7) == "http://")
|
|
|
|
{
|
|
|
|
sha1_hash info_hash = boost::lexical_cast<sha1_hash>(argv[i+2]);
|
|
|
|
|
|
|
|
handles.push_back(ses.add_torrent(argv[i+1], info_hash, save_path));
|
|
|
|
handles.back().set_max_connections(60);
|
2005-08-05 04:43:44 +02:00
|
|
|
handles.back().set_max_uploads(-1);
|
|
|
|
// handles.back().set_ratio(1.1f);
|
2004-06-14 01:30:42 +02:00
|
|
|
++i;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
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-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);
|
2004-03-05 12:58:38 +01:00
|
|
|
resume_data = bdecode(
|
|
|
|
std::istream_iterator<char>(resume_file)
|
2004-01-07 03:45:03 +01:00
|
|
|
, 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
|
|
|
|
2005-09-18 12:18:23 +02:00
|
|
|
handles.push_back(ses.add_torrent(e, save_path, resume_data, true, 16 * 1024));
|
2005-08-05 04:43:44 +02:00
|
|
|
handles.back().set_max_connections(60);
|
2004-08-05 15:56:26 +02:00
|
|
|
handles.back().set_max_uploads(-1);
|
2005-08-05 04:43:44 +02:00
|
|
|
// handles.back().set_ratio(1.02f);
|
2005-09-01 23:04:21 +02:00
|
|
|
|
|
|
|
// std::vector<bool> ffilter(t.num_files(), true);
|
|
|
|
// ffilter[0] = false;
|
|
|
|
// handles.back().filter_files(ffilter);
|
2005-07-24 07:44:12 +02:00
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
catch (std::exception& e)
|
|
|
|
{
|
|
|
|
std::cout << e.what() << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-06-14 01:30:42 +02:00
|
|
|
if (handles.empty()) return 1;
|
|
|
|
|
2003-10-30 00:28:09 +01:00
|
|
|
std::vector<peer_info> peers;
|
2003-11-02 22:06:50 +01:00
|
|
|
std::vector<partial_piece_info> queue;
|
2003-10-30 00:28:09 +01:00
|
|
|
|
2004-07-24 13:54:17 +02:00
|
|
|
bool print_peers = false;
|
|
|
|
bool print_log = false;
|
|
|
|
bool print_downloads = false;
|
|
|
|
|
2003-10-30 00:28:09 +01:00
|
|
|
for (;;)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
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();
|
2005-05-25 12:01:01 +02:00
|
|
|
i != handles.end(); ++i)
|
2004-01-13 04:08:59 +01:00
|
|
|
{
|
|
|
|
torrent_handle h = *i;
|
2004-06-14 01:30:42 +02:00
|
|
|
if (!h.get_torrent_info().is_valid()) continue;
|
|
|
|
|
2005-07-06 02:58:23 +02:00
|
|
|
h.pause();
|
2004-01-13 04:08:59 +01:00
|
|
|
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-02-29 22:33:17 +01:00
|
|
|
ses.remove_torrent(*i);
|
2004-01-13 04:08:59 +01:00
|
|
|
}
|
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
|
|
|
}
|
2004-03-21 03:03:37 +01:00
|
|
|
|
|
|
|
if(c == 'p')
|
|
|
|
{
|
|
|
|
// pause all torrents
|
|
|
|
std::for_each(
|
|
|
|
handles.begin()
|
|
|
|
, handles.end()
|
|
|
|
, boost::bind(&torrent_handle::pause, _1));
|
|
|
|
}
|
|
|
|
|
|
|
|
if(c == 'u')
|
|
|
|
{
|
|
|
|
// unpause all torrents
|
|
|
|
std::for_each(
|
|
|
|
handles.begin()
|
|
|
|
, handles.end()
|
|
|
|
, boost::bind(&torrent_handle::resume, _1));
|
|
|
|
}
|
|
|
|
|
2004-07-24 13:54:17 +02:00
|
|
|
if (c == 'i') print_peers = !print_peers;
|
|
|
|
if (c == 'l') print_log = !print_log;
|
|
|
|
if (c == 'd') print_downloads = !print_downloads;
|
|
|
|
|
2003-10-30 00:28:09 +01:00
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2004-03-23 23:58:18 +01:00
|
|
|
// loop through the alert queue to see if anything has happened.
|
2003-12-22 08:14:35 +01:00
|
|
|
std::auto_ptr<alert> a;
|
|
|
|
a = ses.pop_alert();
|
|
|
|
while (a.get())
|
|
|
|
{
|
2004-03-28 19:45:37 +02:00
|
|
|
if (torrent_finished_alert* p = dynamic_cast<torrent_finished_alert*>(a.get()))
|
2004-02-01 01:50:18 +01:00
|
|
|
{
|
2004-03-23 23:58:18 +01:00
|
|
|
// limit the bandwidth for all seeding torrents
|
2004-02-01 14:48:30 +01:00
|
|
|
p->handle.set_max_connections(10);
|
2005-07-06 02:58:23 +02:00
|
|
|
//p->handle.set_max_uploads(5);
|
|
|
|
//p->handle.set_upload_limit(10000);
|
2004-07-18 02:39:58 +02:00
|
|
|
|
|
|
|
// all finished downloades are
|
|
|
|
// moved into this directory
|
2005-07-06 02:58:23 +02:00
|
|
|
//p->handle.move_storage("finished");
|
2004-03-28 19:45:37 +02:00
|
|
|
events.push_back(
|
|
|
|
p->handle.get_torrent_info().name() + ": " + a->msg());
|
2004-02-01 01:50:18 +01:00
|
|
|
}
|
2004-03-28 19:45:37 +02:00
|
|
|
else if (peer_error_alert* p = dynamic_cast<peer_error_alert*>(a.get()))
|
|
|
|
{
|
|
|
|
events.push_back(identify_client(p->id) + ": " + a->msg());
|
|
|
|
}
|
|
|
|
else if (invalid_request_alert* p = dynamic_cast<invalid_request_alert*>(a.get()))
|
|
|
|
{
|
|
|
|
events.push_back(identify_client(p->id) + ": " + a->msg());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
events.push_back(a->msg());
|
|
|
|
}
|
|
|
|
|
2004-01-05 02:30:34 +01:00
|
|
|
if (events.size() >= 10) events.pop_front();
|
2003-12-22 08:14:35 +01:00
|
|
|
a = ses.pop_alert();
|
|
|
|
}
|
|
|
|
|
2005-08-15 00:04:58 +02:00
|
|
|
session_status sess_stat = ses.status();
|
|
|
|
|
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();
|
2005-05-25 12:01:01 +02:00
|
|
|
i != handles.end(); ++i)
|
2003-10-30 00:28:09 +01:00
|
|
|
{
|
2004-03-03 14:47:12 +01:00
|
|
|
if (!i->is_valid())
|
|
|
|
{
|
|
|
|
handles.erase(i);
|
|
|
|
--i;
|
|
|
|
continue;
|
|
|
|
}
|
2005-08-15 00:04:58 +02:00
|
|
|
out << "name: " << esc("37");
|
2005-05-30 19:43:03 +02:00
|
|
|
if (i->has_metadata()) out << i->get_torrent_info().name();
|
|
|
|
else out << "-";
|
2005-08-15 00:04:58 +02:00
|
|
|
out << esc("0") << "\n";
|
2003-10-31 05:02:51 +01:00
|
|
|
torrent_status s = i->status();
|
|
|
|
|
2005-07-06 20:40:01 +02:00
|
|
|
if (s.state != torrent_status::seeding)
|
2003-10-31 05:02:51 +01:00
|
|
|
{
|
2005-07-06 20:40:01 +02:00
|
|
|
switch(s.state)
|
|
|
|
{
|
|
|
|
case torrent_status::queued_for_checking:
|
|
|
|
out << "queued ";
|
|
|
|
break;
|
|
|
|
case torrent_status::checking_files:
|
|
|
|
out << "checking ";
|
|
|
|
break;
|
|
|
|
case torrent_status::connecting_to_tracker:
|
|
|
|
out << "connecting to tracker ";
|
|
|
|
break;
|
|
|
|
case torrent_status::downloading_metadata:
|
|
|
|
out << "downloading metadata ";
|
|
|
|
break;
|
|
|
|
case torrent_status::downloading:
|
|
|
|
out << "downloading ";
|
|
|
|
break;
|
|
|
|
case torrent_status::finished:
|
|
|
|
out << "finished ";
|
|
|
|
break;
|
|
|
|
case torrent_status::seeding:
|
|
|
|
out << "seeding ";
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
}
|
2004-07-24 13:54:17 +02:00
|
|
|
|
2003-10-31 05:02:51 +01:00
|
|
|
i->get_peer_info(peers);
|
2003-12-14 06:56:12 +01:00
|
|
|
|
2005-07-06 20:40:01 +02:00
|
|
|
if (s.state != torrent_status::seeding)
|
|
|
|
{
|
2005-08-15 00:04:58 +02:00
|
|
|
char const* progress_bar_color = "33"; // yellow
|
|
|
|
if (s.state == torrent_status::checking_files
|
|
|
|
|| s.state == torrent_status::downloading_metadata)
|
|
|
|
{
|
|
|
|
progress_bar_color = "35"; // magenta
|
|
|
|
}
|
|
|
|
else if (s.current_tracker.empty())
|
|
|
|
{
|
|
|
|
progress_bar_color = "31"; // red
|
|
|
|
}
|
|
|
|
else if (sess_stat.has_incoming_connections)
|
|
|
|
{
|
|
|
|
progress_bar_color = "32"; // green
|
|
|
|
}
|
2005-07-06 20:40:01 +02:00
|
|
|
out.precision(4);
|
|
|
|
out.width(5);
|
|
|
|
out.fill(' ');
|
|
|
|
out << (s.progress*100) << "% ";
|
2005-08-15 00:04:58 +02:00
|
|
|
out << progress_bar(s.progress, 49, progress_bar_color);
|
2005-07-06 20:40:01 +02:00
|
|
|
out << "\n";
|
2005-08-15 00:04:58 +02:00
|
|
|
out << "total downloaded: " << esc("32") << s.total_done << esc("0") << " Bytes\n";
|
2005-07-06 20:40:01 +02:00
|
|
|
out << "peers: " << s.num_peers << " "
|
|
|
|
<< "seeds: " << s.num_seeds << " "
|
|
|
|
<< "distributed copies: " << s.distributed_copies << "\n";
|
|
|
|
}
|
2005-08-15 00:04:58 +02:00
|
|
|
out << "download: " << esc("32") << add_suffix(s.download_rate) << "/s " << esc("0")
|
|
|
|
<< "(" << esc("32") << add_suffix(s.total_download) << esc("0") << ") "
|
|
|
|
<< "upload: " << esc("31") << add_suffix(s.upload_rate) << "/s " << esc("0")
|
|
|
|
<< "(" << esc("31") << add_suffix(s.total_upload) << esc("0") << ") "
|
2004-01-31 12:03:48 +01:00
|
|
|
<< "ratio: " << ratio(s.total_payload_download, s.total_payload_upload) << "\n";
|
2005-07-06 20:40:01 +02:00
|
|
|
if (s.state != torrent_status::seeding)
|
|
|
|
{
|
|
|
|
out << "info-hash: " << i->info_hash() << "\n";
|
2003-11-02 22:06:50 +01:00
|
|
|
|
2005-07-06 20:40:01 +02:00
|
|
|
boost::posix_time::time_duration t = s.next_announce;
|
2005-08-15 00:04:58 +02:00
|
|
|
out << "next announce: " << esc("37") << boost::posix_time::to_simple_string(t) << esc("0") << "\n";
|
2005-07-06 20:40:01 +02:00
|
|
|
out << "tracker: " << s.current_tracker << "\n";
|
|
|
|
}
|
2003-12-08 02:37:30 +01:00
|
|
|
|
2004-07-18 02:39:58 +02:00
|
|
|
out << "___________________________________\n";
|
|
|
|
|
2005-07-06 20:40:01 +02:00
|
|
|
if (print_peers && !peers.empty())
|
2003-12-07 06:53:04 +01:00
|
|
|
{
|
2004-07-24 13:54:17 +02:00
|
|
|
print_peer_info(out, peers);
|
|
|
|
out << "___________________________________\n";
|
2003-12-07 06:53:04 +01:00
|
|
|
}
|
2004-02-25 00:55:42 +01:00
|
|
|
|
2005-07-06 20:40:01 +02:00
|
|
|
if (print_downloads && s.state != torrent_status::seeding)
|
2003-11-02 22:06:50 +01:00
|
|
|
{
|
2004-07-24 13:54:17 +02:00
|
|
|
i->get_download_queue(queue);
|
|
|
|
for (std::vector<partial_piece_info>::iterator i = queue.begin();
|
2005-06-16 17:41:04 +02:00
|
|
|
i != queue.end(); ++i)
|
2003-11-02 22:06:50 +01:00
|
|
|
{
|
2004-07-24 13:54:17 +02:00
|
|
|
out.width(4);
|
|
|
|
out.fill(' ');
|
2005-05-25 12:01:01 +02:00
|
|
|
out << i->piece_index << ": [";
|
2004-07-24 13:54:17 +02:00
|
|
|
for (int j = 0; j < i->blocks_in_piece; ++j)
|
|
|
|
{
|
2005-08-15 00:04:58 +02:00
|
|
|
char const* peer_str = peer_index(i->peer[j], peers);
|
|
|
|
#ifdef ANSI_TERMINAL_COLORS
|
|
|
|
if (i->finished_blocks[j]) out << esc("32;7") << peer_str << esc("0");
|
|
|
|
else if (i->requested_blocks[j]) out << peer_str;
|
|
|
|
else out << "-";
|
|
|
|
#else
|
2004-07-24 13:54:17 +02:00
|
|
|
if (i->finished_blocks[j]) out << "#";
|
2005-08-15 00:04:58 +02:00
|
|
|
else if (i->requested_blocks[j]) out << peer_str;
|
2005-05-25 12:01:01 +02:00
|
|
|
else out << "-";
|
2005-08-15 00:04:58 +02:00
|
|
|
#endif
|
2004-07-24 13:54:17 +02:00
|
|
|
}
|
2005-05-25 12:01:01 +02:00
|
|
|
out << "]\n";
|
2003-11-02 22:06:50 +01:00
|
|
|
}
|
2004-02-25 00:55:42 +01:00
|
|
|
|
2004-07-24 13:54:17 +02:00
|
|
|
out << "___________________________________\n";
|
|
|
|
}
|
2003-10-30 00:28:09 +01:00
|
|
|
}
|
2004-01-14 17:22:49 +01:00
|
|
|
|
2004-07-24 13:54:17 +02:00
|
|
|
if (print_log)
|
2003-12-22 08:14:35 +01:00
|
|
|
{
|
2004-07-24 13:54:17 +02:00
|
|
|
for (std::deque<std::string>::iterator i = events.begin();
|
2005-08-15 00:04:58 +02:00
|
|
|
i != events.end(); ++i)
|
2004-07-24 13:54:17 +02:00
|
|
|
{
|
|
|
|
out << *i << "\n";
|
|
|
|
}
|
2003-12-22 08:14:35 +01:00
|
|
|
}
|
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
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
catch (std::exception& e)
|
|
|
|
{
|
|
|
|
std::cout << e.what() << "\n";
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|