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 <iterator>
|
|
|
|
|
2007-03-02 02:16:59 +01:00
|
|
|
#include "libtorrent/config.hpp"
|
|
|
|
|
2004-01-25 19:18:36 +01:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(push, 1)
|
|
|
|
#endif
|
|
|
|
|
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
|
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
#include "libtorrent/extensions/metadata_transfer.hpp"
|
2007-12-11 21:02:55 +01:00
|
|
|
#include "libtorrent/extensions/ut_metadata.hpp"
|
2006-11-26 18:44:51 +01:00
|
|
|
#include "libtorrent/extensions/ut_pex.hpp"
|
2007-12-18 07:04:54 +01:00
|
|
|
#include "libtorrent/extensions/smart_ban.hpp"
|
2006-11-14 01:08:16 +01:00
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
#include "libtorrent/entry.hpp"
|
|
|
|
#include "libtorrent/bencode.hpp"
|
|
|
|
#include "libtorrent/session.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"
|
2006-10-11 16:02:21 +02:00
|
|
|
#include "libtorrent/ip_filter.hpp"
|
2007-12-03 07:03:16 +01:00
|
|
|
#include "libtorrent/magnet_uri.hpp"
|
2008-05-28 08:50:11 +02:00
|
|
|
#include "libtorrent/bitfield.hpp"
|
2008-07-01 01:14:31 +02:00
|
|
|
#include "libtorrent/file.hpp"
|
2009-11-26 06:45:43 +01:00
|
|
|
#include "libtorrent/peer_info.hpp"
|
2009-11-28 03:43:05 +01:00
|
|
|
#include "libtorrent/socket_io.hpp" // print_address
|
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>
|
|
|
|
|
2009-05-13 18:02:43 +02:00
|
|
|
bool sleep_and_input(char* c, int sleep)
|
2003-10-30 00:28:09 +01:00
|
|
|
{
|
2009-05-13 18:02:43 +02:00
|
|
|
for (int i = 0; i < sleep * 2; ++i)
|
2003-10-30 00:28:09 +01:00
|
|
|
{
|
2009-05-13 18:02:43 +02:00
|
|
|
if (_kbhit())
|
|
|
|
{
|
|
|
|
*c = _getch();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
Sleep(500);
|
2003-10-30 00:28:09 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2005-10-16 11:15:46 +02:00
|
|
|
void clear_home()
|
2003-11-02 22:06:50 +01:00
|
|
|
{
|
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);
|
2005-10-16 11:15:46 +02:00
|
|
|
SetConsoleCursorPosition(h, c);
|
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>
|
2007-07-23 01:24:27 +02:00
|
|
|
#include <sys/ioctl.h>
|
2003-10-31 12:17:45 +01:00
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2009-05-13 18:02:43 +02:00
|
|
|
bool sleep_and_input(char* c, int sleep)
|
2003-10-31 12:17:45 +01:00
|
|
|
{
|
|
|
|
// sets the terminal to single-character mode
|
|
|
|
// and resets when destructed
|
|
|
|
set_keypress s;
|
|
|
|
|
|
|
|
fd_set set;
|
|
|
|
FD_ZERO(&set);
|
|
|
|
FD_SET(0, &set);
|
2009-05-13 18:02:43 +02:00
|
|
|
timeval tv = {sleep, 0};
|
2010-01-05 14:00:04 +01:00
|
|
|
if (select(1, &set, 0, 0, &tv) > 0)
|
|
|
|
{
|
|
|
|
*c = getc(stdin);
|
|
|
|
return true;
|
|
|
|
}
|
2009-12-15 14:11:07 +01:00
|
|
|
libtorrent::sleep(500);
|
2003-10-31 12:17:45 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-10-16 11:15:46 +02:00
|
|
|
void clear_home()
|
2003-11-05 00:27:06 +01:00
|
|
|
{
|
2009-05-13 03:02:06 +02:00
|
|
|
puts("\033[2J\033[0;0H");
|
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
|
|
|
|
2008-11-29 09:38:40 +01:00
|
|
|
bool print_trackers = false;
|
2008-04-05 06:53:22 +02:00
|
|
|
bool print_peers = false;
|
|
|
|
bool print_log = false;
|
|
|
|
bool print_downloads = false;
|
|
|
|
bool print_piece_bar = false;
|
|
|
|
bool print_file_progress = false;
|
2009-01-13 18:49:14 +01:00
|
|
|
bool show_pad_files = false;
|
2008-09-20 19:42:25 +02:00
|
|
|
bool show_dht_status = false;
|
2008-04-05 06:53:22 +02:00
|
|
|
bool sequential_download = false;
|
|
|
|
|
|
|
|
bool print_ip = true;
|
|
|
|
bool print_as = false;
|
|
|
|
bool print_timers = false;
|
|
|
|
bool print_block = false;
|
|
|
|
bool print_peer_rate = false;
|
|
|
|
bool print_fails = false;
|
|
|
|
bool print_send_bufs = true;
|
2009-05-13 03:02:06 +02:00
|
|
|
|
|
|
|
FILE* g_log_file = 0;
|
2008-04-05 06:53:22 +02:00
|
|
|
|
2008-05-07 11:28:49 +02:00
|
|
|
int active_torrent = 0;
|
|
|
|
|
2007-05-23 09:39:21 +02:00
|
|
|
char const* esc(char const* code)
|
2005-08-15 00:04:58 +02:00
|
|
|
{
|
|
|
|
#ifdef ANSI_TERMINAL_COLORS
|
2007-05-24 02:52:03 +02:00
|
|
|
// this is a silly optimization
|
|
|
|
// to avoid copying of strings
|
2008-02-28 03:46:01 +01:00
|
|
|
enum { num_strings = 200 };
|
2007-05-24 02:52:03 +02:00
|
|
|
static char buf[num_strings][20];
|
|
|
|
static int round_robin = 0;
|
|
|
|
char* ret = buf[round_robin];
|
|
|
|
++round_robin;
|
|
|
|
if (round_robin >= num_strings) round_robin = 0;
|
|
|
|
ret[0] = '\033';
|
|
|
|
ret[1] = '[';
|
2007-05-23 09:39:21 +02:00
|
|
|
int i = 2;
|
|
|
|
int j = 0;
|
|
|
|
while (code[j]) ret[i++] = code[j++];
|
|
|
|
ret[i++] = 'm';
|
|
|
|
ret[i++] = 0;
|
2005-08-15 00:04:58 +02:00
|
|
|
return ret;
|
|
|
|
#else
|
2007-05-23 09:39:21 +02:00
|
|
|
return "";
|
2005-08-15 00:04:58 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-04-15 19:44:06 +02:00
|
|
|
std::string to_string(int v, int width)
|
|
|
|
{
|
2009-11-08 07:39:43 +01:00
|
|
|
char buf[100];
|
|
|
|
snprintf(buf, sizeof(buf), "%*d", width, v);
|
|
|
|
return buf;
|
2007-04-15 19:44:06 +02:00
|
|
|
}
|
|
|
|
|
2008-02-28 04:24:38 +01:00
|
|
|
std::string& to_string(float v, int width, int precision = 3)
|
2003-12-08 02:37:30 +01:00
|
|
|
{
|
2008-02-28 04:24:38 +01:00
|
|
|
// this is a silly optimization
|
|
|
|
// to avoid copying of strings
|
|
|
|
enum { num_strings = 20 };
|
|
|
|
static std::string buf[num_strings];
|
|
|
|
static int round_robin = 0;
|
|
|
|
std::string& ret = buf[round_robin];
|
|
|
|
++round_robin;
|
|
|
|
if (round_robin >= num_strings) round_robin = 0;
|
2007-05-23 09:39:21 +02:00
|
|
|
ret.resize(20);
|
2009-05-29 03:27:08 +02:00
|
|
|
int size = snprintf(&ret[0], 20, "%*.*f", width, precision, v);
|
2007-10-01 19:17:49 +02:00
|
|
|
ret.resize((std::min)(size, width));
|
2007-05-23 09:39:21 +02:00
|
|
|
return ret;
|
2003-12-08 02:37:30 +01:00
|
|
|
}
|
|
|
|
|
2009-05-13 03:02:06 +02:00
|
|
|
std::string add_suffix(float val, char const* suffix = 0)
|
2003-10-31 05:02:51 +01:00
|
|
|
{
|
2009-05-13 03:02:06 +02:00
|
|
|
std::string ret;
|
|
|
|
if (val == 0)
|
|
|
|
{
|
|
|
|
ret.resize(4 + 2, ' ');
|
|
|
|
if (suffix) ret.resize(4 + 2 + strlen(suffix), ' ');
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-05-23 09:39:21 +02:00
|
|
|
const char* prefix[] = {"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
|
|
|
{
|
2005-05-25 12:01:01 +02:00
|
|
|
val /= 1000.f;
|
2009-01-27 09:24:48 +01:00
|
|
|
if (std::fabs(val) < 1000.f)
|
2007-05-23 09:39:21 +02:00
|
|
|
{
|
2009-05-13 03:02:06 +02:00
|
|
|
ret = to_string(val, 4);
|
2007-05-23 09:39:21 +02:00
|
|
|
ret += prefix[i];
|
2009-05-13 03:02:06 +02:00
|
|
|
if (suffix) ret += suffix;
|
2007-05-23 09:39:21 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2003-10-31 05:02:51 +01:00
|
|
|
}
|
2009-05-13 03:02:06 +02:00
|
|
|
ret = to_string(val, 4);
|
2007-05-23 09:39:21 +02:00
|
|
|
ret += "PB";
|
2009-05-13 03:02:06 +02:00
|
|
|
if (suffix) ret += suffix;
|
2007-05-23 09:39:21 +02:00
|
|
|
return ret;
|
2003-10-31 05:02:51 +01:00
|
|
|
}
|
|
|
|
|
2008-05-28 08:50:11 +02:00
|
|
|
std::string const& piece_bar(libtorrent::bitfield const& p, int width)
|
2007-07-09 02:00:35 +02:00
|
|
|
{
|
2008-01-24 04:59:08 +01:00
|
|
|
#ifdef ANSI_TERMINAL_COLORS
|
2007-07-09 02:00:35 +02:00
|
|
|
static const char* lookup[] =
|
|
|
|
{
|
|
|
|
// black, blue, cyan, white
|
|
|
|
"40", "44", "46", "47"
|
|
|
|
};
|
2008-01-24 04:59:08 +01:00
|
|
|
|
|
|
|
const int table_size = sizeof(lookup) / sizeof(lookup[0]);
|
|
|
|
#else
|
|
|
|
static const char char_lookup[] =
|
|
|
|
{ ' ', '.', ':', '-', '+', '*', '#'};
|
|
|
|
|
|
|
|
const int table_size = sizeof(char_lookup) / sizeof(char_lookup[0]);
|
|
|
|
#endif
|
2007-07-09 02:00:35 +02:00
|
|
|
|
|
|
|
double piece_per_char = p.size() / double(width);
|
|
|
|
static std::string bar;
|
|
|
|
bar.clear();
|
|
|
|
bar.reserve(width * 6);
|
|
|
|
bar += "[";
|
2008-08-29 02:42:57 +02:00
|
|
|
if (p.size() == 0)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < width; ++i) bar += ' ';
|
|
|
|
bar += "]";
|
|
|
|
return bar;
|
|
|
|
}
|
2007-07-09 02:00:35 +02:00
|
|
|
|
|
|
|
// the [piece, piece + pieces_per_char) range is the pieces that are represented by each character
|
|
|
|
double piece = 0;
|
|
|
|
for (int i = 0; i < width; ++i, piece += piece_per_char)
|
|
|
|
{
|
|
|
|
int num_pieces = 0;
|
|
|
|
int num_have = 0;
|
2007-10-01 19:17:49 +02:00
|
|
|
int end = (std::max)(int(piece + piece_per_char), int(piece) + 1);
|
2007-07-09 02:00:35 +02:00
|
|
|
for (int k = int(piece); k < end; ++k, ++num_pieces)
|
|
|
|
if (p[k]) ++num_have;
|
2008-01-24 04:59:08 +01:00
|
|
|
int color = int(std::ceil(num_have / float(num_pieces) * (table_size - 1)));
|
|
|
|
#ifdef ANSI_TERMINAL_COLORS
|
2007-07-09 02:00:35 +02:00
|
|
|
bar += esc(lookup[color]);
|
|
|
|
bar += " ";
|
2008-01-24 04:59:08 +01:00
|
|
|
#else
|
|
|
|
bar += char_lookup[color];
|
|
|
|
#endif
|
2007-07-09 02:00:35 +02:00
|
|
|
}
|
2008-01-24 04:59:08 +01:00
|
|
|
#ifdef ANSI_TERMINAL_COLORS
|
2007-07-09 02:00:35 +02:00
|
|
|
bar += esc("0");
|
2008-01-24 04:59:08 +01:00
|
|
|
#endif
|
2007-07-09 02:00:35 +02:00
|
|
|
bar += "]";
|
|
|
|
return bar;
|
|
|
|
}
|
|
|
|
|
2009-07-19 06:59:27 +02:00
|
|
|
std::string const& progress_bar(int progress, int width, char const* code = "33")
|
2004-01-15 17:45:34 +01:00
|
|
|
{
|
2007-05-23 09:39:21 +02:00
|
|
|
static std::string bar;
|
|
|
|
bar.clear();
|
|
|
|
bar.reserve(width + 10);
|
2004-01-15 17:45:34 +01:00
|
|
|
|
2009-07-19 06:59:27 +02:00
|
|
|
int progress_chars = (progress * width + 500) / 1000;
|
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, '#');
|
|
|
|
std::fill_n(std::back_inserter(bar), width - progress_chars, '-');
|
2010-01-17 22:10:28 +01:00
|
|
|
bar += esc("0");
|
2007-05-23 09:39:21 +02:00
|
|
|
return bar;
|
2004-01-15 17:45:34 +01:00
|
|
|
}
|
|
|
|
|
2007-01-10 16:02:25 +01:00
|
|
|
int peer_index(libtorrent::tcp::endpoint addr, std::vector<libtorrent::peer_info> const& peers)
|
2005-08-15 00:04:58 +02:00
|
|
|
{
|
|
|
|
using namespace libtorrent;
|
|
|
|
std::vector<peer_info>::const_iterator i = std::find_if(peers.begin()
|
2007-04-29 22:49:30 +02:00
|
|
|
, peers.end(), bind(&peer_info::ip, _1) == addr);
|
2007-01-10 16:02:25 +01:00
|
|
|
if (i == peers.end()) return -1;
|
2005-08-15 00:04:58 +02:00
|
|
|
|
2007-01-10 16:02:25 +01:00
|
|
|
return i - peers.begin();
|
2005-08-15 00:04:58 +02:00
|
|
|
}
|
|
|
|
|
2009-05-13 03:02:06 +02:00
|
|
|
void print_peer_info(std::string& out, std::vector<libtorrent::peer_info> const& peers)
|
2004-07-24 13:54:17 +02:00
|
|
|
{
|
|
|
|
using namespace libtorrent;
|
2009-05-13 03:02:06 +02:00
|
|
|
if (print_ip) out += "IP ";
|
2008-04-05 06:53:22 +02:00
|
|
|
#ifndef TORRENT_DISABLE_GEO_IP
|
2009-05-13 03:02:06 +02:00
|
|
|
if (print_as) out += "AS ";
|
2008-04-05 06:53:22 +02:00
|
|
|
#endif
|
2009-05-16 01:22:11 +02:00
|
|
|
out += "down (total | peak ) up (total | peak ) sent-req recv flags source ";
|
2009-05-13 03:02:06 +02:00
|
|
|
if (print_fails) out += "fail hshf ";
|
|
|
|
if (print_send_bufs) out += "rq sndb quota rcvb q-bytes ";
|
|
|
|
if (print_timers) out += "inactive wait timeout q-time ";
|
|
|
|
out += "disk rtt ";
|
|
|
|
if (print_block) out += "block-progress ";
|
2007-05-24 22:23:14 +02:00
|
|
|
#ifndef TORRENT_DISABLE_RESOLVE_COUNTRIES
|
2009-05-13 03:02:06 +02:00
|
|
|
out += "country ";
|
2007-05-24 22:23:14 +02:00
|
|
|
#endif
|
2010-02-09 04:04:41 +01:00
|
|
|
if (print_peer_rate) out += "peer-rate est.rec.rate ";
|
2009-05-13 03:02:06 +02:00
|
|
|
out += "client \n";
|
2004-07-24 13:54:17 +02:00
|
|
|
|
2009-05-15 18:21:36 +02:00
|
|
|
char str[500];
|
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
|
|
|
{
|
2008-12-13 04:05:29 +01:00
|
|
|
if (i->flags & (peer_info::handshake | peer_info::connecting | peer_info::queued))
|
2007-03-15 23:03:56 +01:00
|
|
|
continue;
|
|
|
|
|
2008-04-05 06:53:22 +02:00
|
|
|
if (print_ip)
|
|
|
|
{
|
2009-02-26 08:09:56 +01:00
|
|
|
error_code ec;
|
2009-05-15 18:21:36 +02:00
|
|
|
snprintf(str, sizeof(str), "%-22s:%-5d ", i->ip.address().to_string(ec).c_str(), i->ip.port());
|
2009-05-13 03:02:06 +02:00
|
|
|
out += str;
|
2008-04-05 06:53:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef TORRENT_DISABLE_GEO_IP
|
|
|
|
if (print_as)
|
|
|
|
{
|
2009-05-13 03:02:06 +02:00
|
|
|
error_code ec;
|
2009-05-15 18:21:36 +02:00
|
|
|
snprintf(str, sizeof(str), "%-42s ", i->inet_as_name.c_str());
|
2009-05-13 03:02:06 +02:00
|
|
|
out += str;
|
2008-04-05 06:53:22 +02:00
|
|
|
}
|
|
|
|
#endif
|
2009-05-15 18:21:36 +02:00
|
|
|
|
|
|
|
snprintf(str, sizeof(str)
|
2009-05-16 01:22:11 +02:00
|
|
|
, "%s%s (%s|%s) %s%s (%s|%s) %s%3d (%3d) %3d %c%c%c%c%c%c%c%c%c%c%c%c%c%c %c%c%c%c%c%c "
|
2009-05-13 03:02:06 +02:00
|
|
|
, esc("32"), add_suffix(i->down_speed, "/s").c_str()
|
|
|
|
, add_suffix(i->total_download).c_str(), add_suffix(i->download_rate_peak, "/s").c_str()
|
|
|
|
, esc("31"), add_suffix(i->up_speed, "/s").c_str(), add_suffix(i->total_upload).c_str()
|
2009-05-15 18:21:36 +02:00
|
|
|
, add_suffix(i->upload_rate_peak, "/s").c_str(), esc("0")
|
|
|
|
|
|
|
|
, i->download_queue_length
|
|
|
|
, i->target_dl_queue_length
|
|
|
|
, i->upload_queue_length
|
2009-05-13 03:02:06 +02:00
|
|
|
|
|
|
|
, (i->flags & peer_info::interesting)?'I':'.'
|
|
|
|
, (i->flags & peer_info::choked)?'C':'.'
|
|
|
|
, (i->flags & peer_info::remote_interested)?'i':'.'
|
|
|
|
, (i->flags & peer_info::remote_choked)?'c':'.'
|
|
|
|
, (i->flags & peer_info::supports_extensions)?'e':'.'
|
|
|
|
, (i->flags & peer_info::local_connection)?'l':'r'
|
|
|
|
, (i->flags & peer_info::seed)?'s':'.'
|
|
|
|
, (i->flags & peer_info::on_parole)?'p':'.'
|
|
|
|
, (i->flags & peer_info::optimistic_unchoke)?'O':'.'
|
|
|
|
, (i->read_state == peer_info::bw_limit)?'r':
|
|
|
|
(i->read_state == peer_info::bw_network)?'R':'.'
|
|
|
|
, (i->write_state == peer_info::bw_limit)?'w':
|
|
|
|
(i->write_state == peer_info::bw_network)?'W':'.'
|
|
|
|
, (i->flags & peer_info::snubbed)?'S':'.'
|
|
|
|
, (i->flags & peer_info::upload_only)?'U':'D'
|
2007-06-06 02:41:20 +02:00
|
|
|
#ifndef TORRENT_DISABLE_ENCRYPTION
|
2009-05-13 03:02:06 +02:00
|
|
|
, (i->flags & peer_info::rc4_encrypted)?'E':
|
|
|
|
(i->flags & peer_info::plaintext_encrypted)?'e':'.'
|
2007-06-07 02:05:18 +02:00
|
|
|
#else
|
2009-05-13 03:02:06 +02:00
|
|
|
, '.'
|
2007-06-06 02:41:20 +02:00
|
|
|
#endif
|
2009-05-13 03:02:06 +02:00
|
|
|
, (i->source & peer_info::tracker)?'T':'_'
|
|
|
|
, (i->source & peer_info::pex)?'P':'_'
|
|
|
|
, (i->source & peer_info::dht)?'D':'_'
|
|
|
|
, (i->source & peer_info::lsd)?'L':'_'
|
2009-05-16 01:22:11 +02:00
|
|
|
, (i->source & peer_info::resume_data)?'R':'_'
|
|
|
|
, (i->source & peer_info::incoming)?'I':'_');
|
2009-05-13 03:02:06 +02:00
|
|
|
out += str;
|
|
|
|
|
2008-04-05 06:53:22 +02:00
|
|
|
if (print_fails)
|
2004-07-24 13:54:17 +02:00
|
|
|
{
|
2009-05-13 03:02:06 +02:00
|
|
|
snprintf(str, sizeof(str), "%3d %3d "
|
|
|
|
, i->failcount, i->num_hashfails);
|
|
|
|
out += str;
|
2004-07-24 13:54:17 +02:00
|
|
|
}
|
2008-04-05 06:53:22 +02:00
|
|
|
if (print_send_bufs)
|
|
|
|
{
|
2009-05-13 03:02:06 +02:00
|
|
|
snprintf(str, sizeof(str), "%2d %6d (%s) %5d %6d (%s) %6d "
|
|
|
|
, i->requests_in_buffer, i->used_send_buffer, add_suffix(i->send_buffer_size).c_str()
|
|
|
|
, i->send_quota, i->used_receive_buffer, add_suffix(i->receive_buffer_size).c_str()
|
|
|
|
, i->queue_bytes);
|
|
|
|
out += str;
|
2008-04-05 06:53:22 +02:00
|
|
|
}
|
|
|
|
if (print_timers)
|
2005-05-30 19:43:03 +02:00
|
|
|
{
|
2009-05-13 03:02:06 +02:00
|
|
|
snprintf(str, sizeof(str), "%8d %4d %7d %6d "
|
|
|
|
, total_seconds(i->last_active)
|
|
|
|
, total_seconds(i->last_request)
|
|
|
|
, i->request_timeout
|
|
|
|
, total_seconds(i->download_queue_time));
|
|
|
|
out += str;
|
2008-04-05 06:53:22 +02:00
|
|
|
}
|
2009-05-13 03:02:06 +02:00
|
|
|
snprintf(str, sizeof(str), "%s %4d "
|
|
|
|
, add_suffix(i->pending_disk_bytes).c_str()
|
|
|
|
, i->rtt);
|
|
|
|
out += str;
|
2008-04-05 06:53:22 +02:00
|
|
|
|
|
|
|
if (print_block)
|
|
|
|
{
|
|
|
|
if (i->downloading_piece_index >= 0)
|
|
|
|
{
|
2009-05-13 03:02:06 +02:00
|
|
|
out += progress_bar(
|
2009-07-19 06:59:27 +02:00
|
|
|
i->downloading_progress * 1000 / i->downloading_total, 14);
|
2008-04-05 06:53:22 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-07-19 06:59:27 +02:00
|
|
|
out += progress_bar(0, 14);
|
2008-04-05 06:53:22 +02:00
|
|
|
}
|
2005-05-30 19:43:03 +02:00
|
|
|
}
|
2004-07-24 13:54:17 +02:00
|
|
|
|
2007-05-24 22:23:14 +02:00
|
|
|
#ifndef TORRENT_DISABLE_RESOLVE_COUNTRIES
|
2007-01-29 08:39:33 +01:00
|
|
|
if (i->country[0] == 0)
|
|
|
|
{
|
2009-05-13 03:02:06 +02:00
|
|
|
out += " ..";
|
2007-01-29 08:39:33 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-05-13 03:02:06 +02:00
|
|
|
snprintf(str, sizeof(str), " %c%c", i->country[0], i->country[1]);
|
|
|
|
out += str;
|
2007-01-29 08:39:33 +01:00
|
|
|
}
|
2007-05-24 22:23:14 +02:00
|
|
|
#endif
|
2009-05-13 03:02:06 +02:00
|
|
|
if (print_peer_rate)
|
|
|
|
{
|
2010-02-09 04:04:41 +01:00
|
|
|
bool unchoked = (i->flags & peer_info::choked) == 0;
|
|
|
|
|
2010-02-13 17:29:17 +01:00
|
|
|
snprintf(str, sizeof(str), " %s %s"
|
2010-02-09 04:04:41 +01:00
|
|
|
, add_suffix(i->remote_dl_rate, "/s").c_str()
|
|
|
|
, unchoked ? add_suffix(i->estimated_reciprocation_rate, "/s").c_str() : " ");
|
2009-05-13 03:02:06 +02:00
|
|
|
out += str;
|
|
|
|
}
|
|
|
|
out += " ";
|
2007-06-07 02:05:18 +02:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
if (i->flags & peer_info::handshake)
|
|
|
|
{
|
2009-05-13 03:02:06 +02:00
|
|
|
out += esc("31");
|
|
|
|
out += " waiting for handshake";
|
|
|
|
out += esc("0");
|
|
|
|
out += "\n";
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
else if (i->flags & peer_info::connecting)
|
2005-11-02 17:28:39 +01:00
|
|
|
{
|
2009-05-13 03:02:06 +02:00
|
|
|
out += esc("31");
|
|
|
|
out += " connecting to peer";
|
|
|
|
out += esc("0");
|
|
|
|
out += "\n";
|
2005-11-02 17:28:39 +01:00
|
|
|
}
|
|
|
|
else if (i->flags & peer_info::queued)
|
|
|
|
{
|
2009-05-13 03:02:06 +02:00
|
|
|
out += esc("33");
|
|
|
|
out += " queued";
|
|
|
|
out += esc("0");
|
|
|
|
out += "\n";
|
2005-11-02 17:28:39 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-05-13 03:02:06 +02:00
|
|
|
out += " ";
|
|
|
|
out += i->client;
|
|
|
|
out += "\n";
|
2005-11-02 17:28:39 +01:00
|
|
|
}
|
2004-07-24 13:54:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-19 02:50:38 +02:00
|
|
|
typedef std::multimap<std::string, libtorrent::torrent_handle> handles_t;
|
|
|
|
|
2010-01-12 02:56:48 +01:00
|
|
|
int listen_port = 6881;
|
|
|
|
float preferred_ratio = 0.f;
|
2010-01-18 06:29:35 +01:00
|
|
|
int allocation_mode = libtorrent::storage_mode_sparse;
|
2010-01-12 02:56:48 +01:00
|
|
|
std::string save_path(".");
|
|
|
|
int torrent_upload_limit = 0;
|
|
|
|
int torrent_download_limit = 0;
|
|
|
|
std::string monitor_dir;
|
|
|
|
std::string bind_to_interface = "";
|
|
|
|
int poll_interval = 5;
|
|
|
|
int max_connections_per_torrent = 50;
|
|
|
|
|
2005-10-19 02:50:38 +02:00
|
|
|
using boost::bind;
|
|
|
|
|
|
|
|
// monitored_dir is true if this torrent is added because
|
|
|
|
// it was found in the directory that is monitored. If it
|
|
|
|
// is, it should be remembered so that it can be removed
|
|
|
|
// if it's no longer in that directory.
|
2005-10-16 11:15:46 +02:00
|
|
|
void add_torrent(libtorrent::session& ses
|
2005-10-19 02:50:38 +02:00
|
|
|
, handles_t& handles
|
|
|
|
, std::string const& torrent
|
2005-10-16 11:15:46 +02:00
|
|
|
, float preferred_ratio
|
2010-01-18 06:29:35 +01:00
|
|
|
, int allocation_mode
|
2009-10-26 02:29:39 +01:00
|
|
|
, std::string const& save_path
|
2008-01-02 19:22:34 +01:00
|
|
|
, bool monitored_dir
|
2008-01-11 00:12:58 +01:00
|
|
|
, int torrent_upload_limit
|
2008-06-29 07:34:22 +02:00
|
|
|
, int torrent_download_limit)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
|
|
|
using namespace libtorrent;
|
|
|
|
|
2008-10-17 03:47:24 +02:00
|
|
|
boost::intrusive_ptr<torrent_info> t;
|
2009-02-26 08:09:56 +01:00
|
|
|
error_code ec;
|
|
|
|
t = new torrent_info(torrent.c_str(), ec);
|
|
|
|
if (ec)
|
2008-10-17 03:47:24 +02:00
|
|
|
{
|
2009-05-13 03:02:06 +02:00
|
|
|
fprintf(stderr, "%s: %s\n", torrent.c_str(), ec.message().c_str());
|
2008-10-17 03:47:24 +02:00
|
|
|
return;
|
|
|
|
}
|
2005-10-16 18:58:41 +02:00
|
|
|
|
2009-05-13 03:02:06 +02:00
|
|
|
printf("%s\n", t->name().c_str());
|
2005-10-16 11:15:46 +02:00
|
|
|
|
2008-04-24 05:28:48 +02:00
|
|
|
add_torrent_params p;
|
2008-07-01 01:14:31 +02:00
|
|
|
lazy_entry resume_data;
|
|
|
|
|
2009-10-26 02:29:39 +01:00
|
|
|
std::string filename = combine_path(save_path, t->name() + ".resume");
|
2008-07-01 01:14:31 +02:00
|
|
|
|
|
|
|
std::vector<char> buf;
|
|
|
|
if (load_file(filename.c_str(), buf) == 0)
|
|
|
|
p.resume_data = &buf;
|
|
|
|
|
2008-04-24 05:28:48 +02:00
|
|
|
p.ti = t;
|
|
|
|
p.save_path = save_path;
|
2010-01-18 06:29:35 +01:00
|
|
|
p.storage_mode = (storage_mode_t)allocation_mode;
|
2008-04-24 05:28:48 +02:00
|
|
|
p.paused = true;
|
|
|
|
p.duplicate_is_error = false;
|
|
|
|
p.auto_managed = true;
|
2009-02-26 08:09:56 +01:00
|
|
|
torrent_handle h = ses.add_torrent(p, ec);
|
2008-04-24 05:28:48 +02:00
|
|
|
|
2009-08-30 09:38:52 +02:00
|
|
|
handles.insert(std::pair<const std::string, torrent_handle>(
|
2005-10-19 02:50:38 +02:00
|
|
|
monitored_dir?std::string(torrent):std::string(), h));
|
|
|
|
|
2010-01-12 02:56:48 +01:00
|
|
|
h.set_max_connections(max_connections_per_torrent);
|
2005-10-19 02:50:38 +02:00
|
|
|
h.set_max_uploads(-1);
|
|
|
|
h.set_ratio(preferred_ratio);
|
2008-01-02 19:22:34 +01:00
|
|
|
h.set_upload_limit(torrent_upload_limit);
|
2008-01-11 00:12:58 +01:00
|
|
|
h.set_download_limit(torrent_download_limit);
|
2007-05-24 22:23:14 +02:00
|
|
|
#ifndef TORRENT_DISABLE_RESOLVE_COUNTRIES
|
2007-01-29 08:39:33 +01:00
|
|
|
h.resolve_countries(true);
|
2007-05-24 22:23:14 +02:00
|
|
|
#endif
|
2005-10-19 02:50:38 +02:00
|
|
|
}
|
|
|
|
|
2009-10-26 02:29:39 +01:00
|
|
|
void scan_dir(std::string const& dir_path
|
2005-10-19 02:50:38 +02:00
|
|
|
, libtorrent::session& ses
|
|
|
|
, handles_t& handles
|
|
|
|
, float preferred_ratio
|
2010-01-18 06:29:35 +01:00
|
|
|
, int allocation_mode
|
2009-10-26 02:29:39 +01:00
|
|
|
, std::string const& save_path
|
2008-01-11 00:12:58 +01:00
|
|
|
, int torrent_upload_limit
|
|
|
|
, int torrent_download_limit)
|
2005-10-19 02:50:38 +02:00
|
|
|
{
|
|
|
|
std::set<std::string> valid;
|
|
|
|
|
|
|
|
using namespace libtorrent;
|
|
|
|
|
2009-10-26 02:29:39 +01:00
|
|
|
error_code ec;
|
|
|
|
for (directory i(dir_path, ec); !i.done(); i.next(ec))
|
2005-10-19 02:50:38 +02:00
|
|
|
{
|
2009-11-02 21:50:54 +01:00
|
|
|
std::string file = combine_path(dir_path, i.file());
|
2009-10-26 02:29:39 +01:00
|
|
|
if (extension(file) != ".torrent") continue;
|
2005-10-16 18:58:41 +02:00
|
|
|
|
2005-10-19 02:50:38 +02:00
|
|
|
handles_t::iterator k = handles.find(file);
|
|
|
|
if (k != handles.end())
|
|
|
|
{
|
|
|
|
valid.insert(file);
|
|
|
|
continue;
|
|
|
|
}
|
2005-10-16 18:58:41 +02:00
|
|
|
|
2005-10-19 02:50:38 +02:00
|
|
|
// the file has been added to the dir, start
|
|
|
|
// downloading it.
|
2010-01-18 06:29:35 +01:00
|
|
|
add_torrent(ses, handles, file, preferred_ratio, allocation_mode
|
2008-01-11 00:12:58 +01:00
|
|
|
, save_path, true, torrent_upload_limit, torrent_download_limit);
|
2005-10-19 02:50:38 +02:00
|
|
|
valid.insert(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove the torrents that are no longer in the directory
|
|
|
|
|
2007-09-27 18:14:24 +02:00
|
|
|
for (handles_t::iterator i = handles.begin(); !handles.empty() && i != handles.end();)
|
2005-10-19 02:50:38 +02:00
|
|
|
{
|
2007-09-27 18:14:24 +02:00
|
|
|
if (i->first.empty() || valid.find(i->first) != valid.end())
|
|
|
|
{
|
|
|
|
++i;
|
|
|
|
continue;
|
|
|
|
}
|
2005-10-16 18:58:41 +02:00
|
|
|
|
2005-10-19 02:50:38 +02:00
|
|
|
torrent_handle& h = i->second;
|
|
|
|
if (!h.is_valid())
|
|
|
|
{
|
2007-10-02 20:52:27 +02:00
|
|
|
handles.erase(i++);
|
2005-10-19 02:50:38 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-05-29 05:37:19 +02:00
|
|
|
h.auto_managed(false);
|
2005-10-19 02:50:38 +02:00
|
|
|
h.pause();
|
2008-04-13 20:54:36 +02:00
|
|
|
// the alert handler for save_resume_data_alert
|
2008-10-19 06:04:12 +02:00
|
|
|
// will save it to disk
|
2008-04-13 20:54:36 +02:00
|
|
|
h.save_resume_data();
|
|
|
|
|
2007-10-02 20:52:27 +02:00
|
|
|
handles.erase(i++);
|
2005-10-19 02:50:38 +02:00
|
|
|
}
|
2005-10-16 11:15:46 +02:00
|
|
|
}
|
|
|
|
|
2008-06-17 10:32:32 +02:00
|
|
|
libtorrent::torrent_handle get_active_torrent(handles_t const& handles)
|
|
|
|
{
|
|
|
|
if (active_torrent >= handles.size()
|
|
|
|
|| active_torrent < 0) return libtorrent::torrent_handle();
|
|
|
|
handles_t::const_iterator i = handles.begin();
|
|
|
|
std::advance(i, active_torrent);
|
|
|
|
return i->second;
|
|
|
|
}
|
|
|
|
|
2009-05-13 03:02:06 +02:00
|
|
|
void print_alert(libtorrent::alert const* a, std::string& str)
|
2008-07-12 10:19:17 +02:00
|
|
|
{
|
|
|
|
using namespace libtorrent;
|
|
|
|
|
|
|
|
#ifdef ANSI_TERMINAL_COLORS
|
|
|
|
if (a->category() & alert::error_notification)
|
|
|
|
{
|
2009-05-13 03:02:06 +02:00
|
|
|
str += esc("31");
|
2008-07-12 10:19:17 +02:00
|
|
|
}
|
|
|
|
else if (a->category() & (alert::peer_notification | alert::storage_notification))
|
|
|
|
{
|
2009-05-13 03:02:06 +02:00
|
|
|
str += esc("33");
|
2008-07-12 10:19:17 +02:00
|
|
|
}
|
|
|
|
#endif
|
2009-05-13 03:02:06 +02:00
|
|
|
str += "[";
|
|
|
|
str += time_now_string();
|
|
|
|
str += "] ";
|
|
|
|
str += a->message();
|
2008-07-12 10:19:17 +02:00
|
|
|
#ifdef ANSI_TERMINAL_COLORS
|
2009-05-13 03:02:06 +02:00
|
|
|
str += esc("0");
|
2008-07-12 10:19:17 +02:00
|
|
|
#endif
|
|
|
|
|
2009-05-13 03:02:06 +02:00
|
|
|
if (g_log_file)
|
|
|
|
fprintf(g_log_file, "[%s] %s\n", time_now_string(), a->message().c_str());
|
|
|
|
}
|
|
|
|
|
2009-10-26 02:29:39 +01:00
|
|
|
int save_file(std::string const& filename, std::vector<char>& v)
|
2009-05-13 03:02:06 +02:00
|
|
|
{
|
|
|
|
using namespace libtorrent;
|
|
|
|
|
|
|
|
file f;
|
|
|
|
error_code ec;
|
|
|
|
if (!f.open(filename, file::write_only, ec)) return -1;
|
|
|
|
if (ec) return -1;
|
|
|
|
file::iovec_t b = {&v[0], v.size()};
|
|
|
|
size_type written = f.writev(0, &b, 1, ec);
|
|
|
|
if (written != v.size()) return -3;
|
|
|
|
if (ec) return -3;
|
|
|
|
return 0;
|
2008-07-12 10:19:17 +02:00
|
|
|
}
|
|
|
|
|
2008-09-26 02:26:35 +02:00
|
|
|
void handle_alert(libtorrent::session& ses, libtorrent::alert* a
|
|
|
|
, handles_t const& handles)
|
2008-07-06 14:22:56 +02:00
|
|
|
{
|
|
|
|
using namespace libtorrent;
|
|
|
|
|
2009-11-02 04:34:16 +01:00
|
|
|
if (torrent_finished_alert* p = alert_cast<torrent_finished_alert>(a))
|
2008-07-06 14:22:56 +02:00
|
|
|
{
|
2010-01-12 02:56:48 +01:00
|
|
|
p->handle.set_max_connections(max_connections_per_torrent / 2);
|
2008-07-06 14:22:56 +02:00
|
|
|
|
|
|
|
// write resume data for the finished torrent
|
2008-10-19 06:04:12 +02:00
|
|
|
// the alert handler for save_resume_data_alert
|
|
|
|
// will save it to disk
|
2008-07-06 14:22:56 +02:00
|
|
|
torrent_handle h = p->handle;
|
|
|
|
h.save_resume_data();
|
|
|
|
}
|
2009-11-02 04:34:16 +01:00
|
|
|
else if (save_resume_data_alert* p = alert_cast<save_resume_data_alert>(a))
|
2008-07-06 14:22:56 +02:00
|
|
|
{
|
|
|
|
torrent_handle h = p->handle;
|
|
|
|
TORRENT_ASSERT(p->resume_data);
|
|
|
|
if (p->resume_data)
|
|
|
|
{
|
2009-05-13 03:02:06 +02:00
|
|
|
std::vector<char> out;
|
|
|
|
bencode(std::back_inserter(out), *p->resume_data);
|
2009-10-26 02:29:39 +01:00
|
|
|
save_file(combine_path(h.save_path(), h.name() + ".resume"), out);
|
2008-09-26 02:26:35 +02:00
|
|
|
if (std::find_if(handles.begin(), handles.end()
|
|
|
|
, bind(&handles_t::value_type::second, _1) == h) == handles.end())
|
|
|
|
ses.remove_torrent(h);
|
2008-07-06 14:22:56 +02:00
|
|
|
}
|
|
|
|
}
|
2009-11-02 04:34:16 +01:00
|
|
|
else if (save_resume_data_failed_alert* p = alert_cast<save_resume_data_failed_alert>(a))
|
2008-11-17 02:19:46 +01:00
|
|
|
{
|
|
|
|
torrent_handle h = p->handle;
|
|
|
|
if (std::find_if(handles.begin(), handles.end()
|
|
|
|
, bind(&handles_t::value_type::second, _1) == h) == handles.end())
|
|
|
|
ses.remove_torrent(h);
|
|
|
|
}
|
2008-07-06 14:22:56 +02:00
|
|
|
}
|
|
|
|
|
2008-07-03 12:05:51 +02:00
|
|
|
static char const* state_str[] =
|
2008-08-11 16:34:37 +02:00
|
|
|
{"checking (q)", "checking", "dl metadata"
|
2008-11-19 01:46:48 +01:00
|
|
|
, "downloading", "finished", "seeding", "allocating", "checking (r)"};
|
2008-07-03 12:05:51 +02:00
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
int main(int argc, char* argv[])
|
2005-10-16 11:15:46 +02:00
|
|
|
{
|
2009-02-26 08:09:56 +01:00
|
|
|
if (argc == 1)
|
2005-10-19 02:50:38 +02:00
|
|
|
{
|
2009-05-13 03:02:06 +02:00
|
|
|
fprintf(stderr, "usage: client_test [OPTIONS] [TORRENT|MAGNETURL]\n\n"
|
2009-02-26 08:09:56 +01:00
|
|
|
"OPTIONS:\n"
|
|
|
|
" -f <log file> logs all events to the given file\n"
|
|
|
|
" -o <limit> limits the number of simultaneous\n"
|
|
|
|
" half-open TCP connections to the\n"
|
|
|
|
" given number.\n"
|
|
|
|
" -p <port> sets the listen port\n"
|
|
|
|
" -r <ratio> sets the preferred share ratio\n"
|
|
|
|
" -d <rate> limits the download rate\n"
|
|
|
|
" -u <rate> limits the upload rate\n"
|
|
|
|
" -S <limit> limits the upload slots\n"
|
|
|
|
" -a <mode> sets the allocation mode. [compact|full]\n"
|
|
|
|
" -s <path> sets the save path for downloads\n"
|
|
|
|
" -U <rate> sets per-torrent upload rate\n"
|
|
|
|
" -D <rate> sets per-torrent download rate\n"
|
|
|
|
" -m <path> sets the .torrent monitor directory\n"
|
|
|
|
" -b <IP> sets IP of the interface to bind the\n"
|
|
|
|
" listen socket to\n"
|
|
|
|
" -w <seconds> sets the retry time for failed web seeds\n"
|
|
|
|
" -t <seconds> sets the scan interval of the monitor dir\n"
|
|
|
|
" -x <file> loads an emule IP-filter file\n"
|
|
|
|
" -c <limit> sets the max number of connections\n"
|
2010-01-12 02:56:48 +01:00
|
|
|
" -T <limit> sets the max number of connections per torrent\n"
|
2009-08-20 05:19:12 +02:00
|
|
|
#if TORRENT_USE_I2P
|
|
|
|
" -i <i2p-host> the hostname to an I2P SAM bridge to use\n"
|
|
|
|
#endif
|
2009-05-02 08:52:57 +02:00
|
|
|
" -C <limit> sets the max cache size. Specified in 16kB blocks\n"
|
2009-05-13 18:02:43 +02:00
|
|
|
" -F <seconds> sets the UI refresh rate. This is the number of\n"
|
|
|
|
" seconds between screen refreshes.\n"
|
2009-12-05 18:12:30 +01:00
|
|
|
" -n announce to trackers in all tiers\n"
|
2010-01-12 02:56:48 +01:00
|
|
|
" -h allow multiple connections from the same IP\n"
|
2010-01-31 22:30:43 +01:00
|
|
|
" -A <num pieces> allowed pieces set size\n"
|
|
|
|
" -R <num blocks> number of blocks per read cache line\n"
|
|
|
|
" -O Disallow disk job reordering\n"
|
|
|
|
" "
|
2009-02-26 08:09:56 +01:00
|
|
|
"\n\n"
|
|
|
|
"TORRENT is a path to a .torrent file\n"
|
2009-05-13 03:02:06 +02:00
|
|
|
"MAGNETURL is a magnet: url\n")
|
2005-10-19 02:50:38 +02:00
|
|
|
;
|
2009-02-26 08:09:56 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2005-10-19 02:50:38 +02:00
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
using namespace libtorrent;
|
2009-05-13 03:34:37 +02:00
|
|
|
session_settings settings;
|
2006-07-27 20:07:51 +02:00
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
settings.user_agent = "client_test/" LIBTORRENT_VERSION;
|
2010-02-09 04:04:41 +01:00
|
|
|
settings.choking_algorithm = session_settings::auto_expand_choker;
|
2009-06-28 22:21:55 +02:00
|
|
|
//settings.announce_to_all_trackers = true;
|
2009-05-03 21:09:06 +02:00
|
|
|
settings.optimize_hashing_for_speed = false;
|
2009-05-23 09:35:45 +02:00
|
|
|
settings.disk_cache_algorithm = session_settings::largest_contiguous;
|
2010-01-31 20:14:00 +01:00
|
|
|
settings.volatile_read_cache = true;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2009-05-13 18:02:43 +02:00
|
|
|
int refresh_delay = 1;
|
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
std::deque<std::string> events;
|
2008-02-28 08:34:07 +01:00
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
ptime next_dir_scan = time_now();
|
2003-12-22 08:14:35 +01:00
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
// the string is the filename of the .torrent file, but only if
|
|
|
|
// it was added through the directory monitor. It is used to
|
|
|
|
// be able to remove torrents that were added via the directory
|
|
|
|
// monitor when they're not in the directory anymore.
|
|
|
|
handles_t handles;
|
2009-11-02 04:50:26 +01:00
|
|
|
session ses(fingerprint("LT", LIBTORRENT_VERSION_MAJOR, LIBTORRENT_VERSION_MINOR, 0, 0)
|
2009-08-20 05:19:12 +02:00
|
|
|
, session::add_default_plugins
|
2009-10-07 22:51:02 +02:00
|
|
|
, alert::all_categories
|
|
|
|
& ~(alert::dht_notification
|
|
|
|
+ alert::progress_notification
|
2010-01-18 23:14:35 +01:00
|
|
|
+ alert::debug_notification
|
|
|
|
+ alert::stats_notification));
|
2005-10-19 02:50:38 +02:00
|
|
|
|
2009-05-13 03:02:06 +02:00
|
|
|
std::vector<char> in;
|
|
|
|
if (load_file(".ses_state", in) == 0)
|
|
|
|
{
|
|
|
|
ses.load_state(bdecode(in.begin(), in.end()));
|
|
|
|
}
|
2008-04-05 06:53:22 +02:00
|
|
|
|
2006-09-01 05:06:00 +02:00
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
2009-02-26 08:09:56 +01:00
|
|
|
settings.use_dht_as_fallback = false;
|
2007-03-08 22:42:37 +01:00
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
ses.add_dht_router(std::make_pair(
|
2008-09-02 08:37:40 +02:00
|
|
|
std::string("router.bittorrent.com"), 6881));
|
2009-02-26 08:09:56 +01:00
|
|
|
ses.add_dht_router(std::make_pair(
|
2008-09-02 08:37:40 +02:00
|
|
|
std::string("router.utorrent.com"), 6881));
|
2009-02-26 08:09:56 +01:00
|
|
|
ses.add_dht_router(std::make_pair(
|
2008-09-02 08:37:40 +02:00
|
|
|
std::string("router.bitcomet.com"), 6881));
|
|
|
|
|
2009-05-13 03:02:06 +02:00
|
|
|
if (load_file(".dht_state", in) == 0)
|
|
|
|
{
|
|
|
|
ses.start_dht(bdecode(in.begin(), in.end()));
|
|
|
|
}
|
2009-11-14 19:55:27 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
ses.start_dht();
|
|
|
|
}
|
2006-09-01 05:06:00 +02:00
|
|
|
#endif
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2009-11-14 19:55:27 +01:00
|
|
|
ses.start_lsd();
|
|
|
|
ses.start_upnp();
|
|
|
|
ses.start_natpmp();
|
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
#ifndef TORRENT_DISABLE_GEO_IP
|
|
|
|
ses.load_asnum_db("GeoIPASNum.dat");
|
|
|
|
ses.load_country_db("GeoIP.dat");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// load the torrents given on the commandline
|
2005-10-16 11:15:46 +02:00
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
for (int i = 1; i < argc; ++i)
|
|
|
|
{
|
|
|
|
if (argv[i][0] != '-')
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2009-02-26 08:09:56 +01:00
|
|
|
// interpret this as a torrent
|
|
|
|
|
|
|
|
// first see if this is a torrentless download
|
|
|
|
if (std::strstr("magnet:", argv[i]) == argv[i])
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2009-02-26 08:09:56 +01:00
|
|
|
add_torrent_params p;
|
|
|
|
p.save_path = save_path;
|
2010-01-18 06:29:35 +01:00
|
|
|
p.storage_mode = (storage_mode_t)allocation_mode;
|
2009-05-13 03:02:06 +02:00
|
|
|
printf("adding MANGET link: %s\n", argv[i]);
|
2009-02-26 08:09:56 +01:00
|
|
|
error_code ec;
|
|
|
|
torrent_handle h = add_magnet_uri(ses, argv[i], p, ec);
|
|
|
|
if (ec)
|
2007-12-11 21:02:55 +01:00
|
|
|
{
|
2009-05-13 03:02:06 +02:00
|
|
|
fprintf(stderr, "%s\n", ec.message().c_str());
|
2007-12-11 21:02:55 +01:00
|
|
|
continue;
|
|
|
|
}
|
2009-02-26 08:09:56 +01:00
|
|
|
|
2009-08-30 09:38:52 +02:00
|
|
|
handles.insert(std::pair<const std::string, torrent_handle>(std::string(), h));
|
2009-02-26 08:09:56 +01:00
|
|
|
|
2010-01-12 02:56:48 +01:00
|
|
|
h.set_max_connections(max_connections_per_torrent);
|
2009-02-26 08:09:56 +01:00
|
|
|
h.set_max_uploads(-1);
|
|
|
|
h.set_ratio(preferred_ratio);
|
|
|
|
h.set_upload_limit(torrent_upload_limit);
|
|
|
|
h.set_download_limit(torrent_download_limit);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// match it against the <hash>@<tracker> format
|
2009-05-13 03:02:06 +02:00
|
|
|
if (strlen(argv[i]) > 45
|
|
|
|
&& is_hex(argv[i], 40)
|
|
|
|
&& string_begins_no_case(argv[i] + 40, "@http"))
|
2009-02-26 08:09:56 +01:00
|
|
|
{
|
2009-05-13 03:02:06 +02:00
|
|
|
sha1_hash info_hash;
|
|
|
|
from_hex(argv[i], 40, (char*)&info_hash[0]);
|
2009-02-26 08:09:56 +01:00
|
|
|
|
|
|
|
add_torrent_params p;
|
2009-05-13 03:02:06 +02:00
|
|
|
p.tracker_url = argv[i] + 41;
|
2009-02-26 08:09:56 +01:00
|
|
|
p.info_hash = info_hash;
|
|
|
|
p.save_path = save_path;
|
2010-01-18 06:29:35 +01:00
|
|
|
p.storage_mode = (storage_mode_t)allocation_mode;
|
2009-02-26 08:09:56 +01:00
|
|
|
p.paused = true;
|
|
|
|
p.duplicate_is_error = false;
|
|
|
|
p.auto_managed = true;
|
|
|
|
error_code ec;
|
|
|
|
torrent_handle h = ses.add_torrent(p, ec);
|
|
|
|
if (ec)
|
2004-06-14 01:30:42 +02:00
|
|
|
{
|
2009-05-13 03:02:06 +02:00
|
|
|
fprintf(stderr, "%s\n", ec.message().c_str());
|
2004-06-14 01:30:42 +02:00
|
|
|
continue;
|
|
|
|
}
|
2009-02-26 08:09:56 +01:00
|
|
|
|
2009-08-30 09:38:52 +02:00
|
|
|
handles.insert(std::pair<const std::string, torrent_handle>(std::string(), h));
|
2009-02-26 08:09:56 +01:00
|
|
|
|
2010-01-12 02:56:48 +01:00
|
|
|
h.set_max_connections(max_connections_per_torrent);
|
2009-02-26 08:09:56 +01:00
|
|
|
h.set_max_uploads(-1);
|
|
|
|
h.set_ratio(preferred_ratio);
|
|
|
|
h.set_upload_limit(torrent_upload_limit);
|
|
|
|
h.set_download_limit(torrent_download_limit);
|
|
|
|
continue;
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
2009-02-26 08:09:56 +01:00
|
|
|
|
|
|
|
// if it's a torrent file, open it as usual
|
|
|
|
add_torrent(ses, handles, argv[i], preferred_ratio
|
2010-01-18 06:29:35 +01:00
|
|
|
, allocation_mode, save_path, false
|
2009-02-26 08:09:56 +01:00
|
|
|
, torrent_upload_limit, torrent_download_limit);
|
|
|
|
continue;
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
// if there's a flag but no argument following, ignore it
|
|
|
|
if (argc == i) continue;
|
|
|
|
char const* arg = argv[i+1];
|
|
|
|
switch (argv[i][1])
|
|
|
|
{
|
2009-05-13 03:02:06 +02:00
|
|
|
case 'f': g_log_file = fopen(arg, "w+"); break;
|
2009-02-26 08:09:56 +01:00
|
|
|
case 'o': ses.set_max_half_open_connections(atoi(arg)); break;
|
2010-01-12 02:56:48 +01:00
|
|
|
case 'h': settings.allow_multiple_connections_per_ip = true; --i; break;
|
2009-02-26 08:09:56 +01:00
|
|
|
case 'p': listen_port = atoi(arg); break;
|
|
|
|
case 'r':
|
|
|
|
preferred_ratio = atoi(arg);
|
|
|
|
if (preferred_ratio != 0 && preferred_ratio < 1.f) preferred_ratio = 1.f;
|
|
|
|
break;
|
2009-12-05 18:12:30 +01:00
|
|
|
case 'n': settings.announce_to_all_tiers = true; --i; break;
|
2009-02-26 08:09:56 +01:00
|
|
|
case 'd': ses.set_download_rate_limit(atoi(arg) * 1000); break;
|
|
|
|
case 'u': ses.set_upload_rate_limit(atoi(arg) * 1000); break;
|
|
|
|
case 'S': ses.set_max_uploads(atoi(arg)); break;
|
2010-01-18 06:29:35 +01:00
|
|
|
case 'a':
|
|
|
|
if (strcmp(arg, "allocate") == 0) allocation_mode = storage_mode_allocate;
|
|
|
|
if (strcmp(arg, "compact") == 0) allocation_mode = storage_mode_compact;
|
|
|
|
break;
|
2009-02-26 08:09:56 +01:00
|
|
|
case 's': save_path = arg; break;
|
|
|
|
case 'U': torrent_upload_limit = atoi(arg) * 1000; break;
|
|
|
|
case 'D': torrent_download_limit = atoi(arg) * 1000; break;
|
|
|
|
case 'm': monitor_dir = arg; break;
|
|
|
|
case 'b': bind_to_interface = arg; break;
|
|
|
|
case 'w': settings.urlseed_wait_retry = atoi(arg); break;
|
|
|
|
case 't': poll_interval = atoi(arg); break;
|
2009-05-13 18:02:43 +02:00
|
|
|
case 'F': refresh_delay = atoi(arg); break;
|
2009-02-26 08:09:56 +01:00
|
|
|
case 'x':
|
|
|
|
{
|
2009-05-13 03:02:06 +02:00
|
|
|
/*
|
2009-02-26 08:09:56 +01:00
|
|
|
std::ifstream in(arg);
|
|
|
|
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;
|
|
|
|
std::stringstream ln(line);
|
|
|
|
ln >> a >> dummy >> b >> dummy >> c >> dummy >> d >> dummy;
|
|
|
|
address_v4 start((a << 24) + (b << 16) + (c << 8) + d);
|
|
|
|
ln >> a >> dummy >> b >> dummy >> c >> dummy >> d;
|
|
|
|
address_v4 last((a << 24) + (b << 16) + (c << 8) + d);
|
|
|
|
int flags;
|
|
|
|
ln >> flags;
|
|
|
|
if (flags <= 127) flags = ip_filter::blocked;
|
|
|
|
else flags = 0;
|
|
|
|
if (ln.fail()) break;
|
|
|
|
filter.add_rule(start, last, flags);
|
|
|
|
}
|
|
|
|
ses.set_ip_filter(filter);
|
2009-05-13 03:02:06 +02:00
|
|
|
*/
|
2009-02-26 08:09:56 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'c': ses.set_max_connections(atoi(arg)); break;
|
2010-01-12 02:56:48 +01:00
|
|
|
case 'T': max_connections_per_torrent = atoi(arg); break;
|
2009-08-20 05:19:12 +02:00
|
|
|
#if TORRENT_USE_I2P
|
|
|
|
case 'i':
|
|
|
|
{
|
|
|
|
proxy_settings ps;
|
|
|
|
ps.hostname = arg;
|
|
|
|
ps.port = 7656; // default SAM port
|
|
|
|
ps.type = proxy_settings::i2p_proxy;
|
|
|
|
ses.set_i2p_proxy(ps);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif // TORRENT_USE_I2P
|
2010-01-31 22:30:43 +01:00
|
|
|
case 'C':
|
|
|
|
settings.cache_size = atoi(arg);
|
|
|
|
settings.use_read_cache = settings.cache_size > 0;
|
|
|
|
settings.cache_buffer_chunk_size = settings.cache_size / 100;
|
|
|
|
break;
|
|
|
|
case 'A': settings.allowed_fast_set_size = atoi(arg); break;
|
|
|
|
case 'R': settings.read_cache_line_size = atoi(arg); break;
|
|
|
|
case 'O': settings.allow_reordered_disk_operations = false; --i; break;
|
2009-02-26 08:09:56 +01:00
|
|
|
}
|
2009-02-28 08:02:30 +01:00
|
|
|
++i; // skip the argument
|
2009-02-26 08:09:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ses.listen_on(std::make_pair(listen_port, listen_port + 10)
|
|
|
|
, bind_to_interface.c_str());
|
2003-10-30 00:28:09 +01:00
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
ses.set_settings(settings);
|
|
|
|
|
|
|
|
// main loop
|
|
|
|
std::vector<peer_info> peers;
|
|
|
|
std::vector<partial_piece_info> queue;
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
char c;
|
2009-05-13 18:02:43 +02:00
|
|
|
while (sleep_and_input(&c, refresh_delay))
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2009-02-26 08:09:56 +01:00
|
|
|
if (c == 27)
|
2003-10-30 00:28:09 +01:00
|
|
|
{
|
2009-02-26 08:09:56 +01:00
|
|
|
// escape code, read another character
|
2008-05-07 11:28:49 +02:00
|
|
|
#ifdef _WIN32
|
2009-02-26 08:09:56 +01:00
|
|
|
c = _getch();
|
2008-05-07 11:28:49 +02:00
|
|
|
#else
|
2009-02-26 08:09:56 +01:00
|
|
|
c = getc(stdin);
|
2008-05-07 11:28:49 +02:00
|
|
|
#endif
|
2009-02-26 08:09:56 +01:00
|
|
|
if (c != '[') break;
|
2008-05-07 11:28:49 +02:00
|
|
|
#ifdef _WIN32
|
2009-02-26 08:09:56 +01:00
|
|
|
c = _getch();
|
2008-05-07 11:28:49 +02:00
|
|
|
#else
|
2009-02-26 08:09:56 +01:00
|
|
|
c = getc(stdin);
|
2008-05-07 11:28:49 +02:00
|
|
|
#endif
|
2009-02-26 08:09:56 +01:00
|
|
|
if (c == 65)
|
|
|
|
{
|
|
|
|
// arrow up
|
|
|
|
--active_torrent;
|
|
|
|
if (active_torrent < 0) active_torrent = 0;
|
2008-05-07 11:28:49 +02:00
|
|
|
}
|
2009-02-26 08:09:56 +01:00
|
|
|
else if (c == 66)
|
2008-06-29 21:08:30 +02:00
|
|
|
{
|
2009-02-26 08:09:56 +01:00
|
|
|
// arrow down
|
|
|
|
++active_torrent;
|
|
|
|
if (active_torrent >= handles.size()) active_torrent = handles.size() - 1;
|
2008-06-29 21:08:30 +02:00
|
|
|
}
|
2009-02-26 08:09:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (c == ' ')
|
|
|
|
{
|
|
|
|
if (ses.is_paused()) ses.resume();
|
|
|
|
else ses.pause();
|
|
|
|
}
|
2008-06-29 21:08:30 +02:00
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
if (c == 'm')
|
|
|
|
{
|
2009-05-13 03:02:06 +02:00
|
|
|
printf("saving peers for torrents\n");
|
2009-02-26 08:09:56 +01:00
|
|
|
|
|
|
|
std::vector<peer_list_entry> peers;
|
|
|
|
for (handles_t::iterator i = handles.begin();
|
|
|
|
i != handles.end(); ++i)
|
2008-04-22 02:05:23 +02:00
|
|
|
{
|
2009-02-26 08:09:56 +01:00
|
|
|
i->second.get_full_peer_list(peers);
|
2009-05-13 03:02:06 +02:00
|
|
|
FILE* f = fopen(("peers_" + i->second.name()).c_str(), "w+");
|
|
|
|
if (!f) break;
|
2009-02-26 08:09:56 +01:00
|
|
|
for (std::vector<peer_list_entry>::iterator k = peers.begin()
|
|
|
|
, end(peers.end()); k != end; ++k)
|
2008-04-22 02:05:23 +02:00
|
|
|
{
|
2009-05-13 03:02:06 +02:00
|
|
|
fprintf(f, "%s\t%d\n", print_address(k->ip.address()).c_str()
|
2008-04-22 19:17:01 +02:00
|
|
|
#ifndef TORRENT_DISABLE_GEO_IP
|
2009-05-13 03:02:06 +02:00
|
|
|
, ses.as_for_ip(k->ip.address())
|
|
|
|
#else
|
|
|
|
, 0
|
2008-04-22 19:17:01 +02:00
|
|
|
#endif
|
2009-05-13 03:02:06 +02:00
|
|
|
);
|
2008-04-22 02:05:23 +02:00
|
|
|
}
|
|
|
|
}
|
2009-02-26 08:09:56 +01:00
|
|
|
}
|
|
|
|
|
2010-01-31 22:30:43 +01:00
|
|
|
if (c == 'q') break;
|
2008-01-31 18:52:29 +01:00
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
if (c == 'j')
|
|
|
|
{
|
|
|
|
torrent_handle h = get_active_torrent(handles);
|
|
|
|
if (h.is_valid()) h.force_recheck();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c == 'r')
|
|
|
|
{
|
|
|
|
torrent_handle h = get_active_torrent(handles);
|
|
|
|
if (h.is_valid()) h.force_reannounce();
|
|
|
|
}
|
2008-09-07 11:59:54 +02:00
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
if (c == 's')
|
|
|
|
{
|
|
|
|
torrent_handle h = get_active_torrent(handles);
|
|
|
|
if (h.is_valid()) h.set_sequential_download(!h.is_sequential_download());
|
|
|
|
}
|
|
|
|
|
2009-11-02 02:01:07 +01:00
|
|
|
if (c == 'o')
|
|
|
|
{
|
|
|
|
torrent_handle h = get_active_torrent(handles);
|
|
|
|
if (h.is_valid())
|
|
|
|
{
|
|
|
|
int num_pieces = h.get_torrent_info().num_pieces();
|
|
|
|
if (num_pieces > 300) num_pieces = 300;
|
|
|
|
for (int i = 0; i < num_pieces; ++i)
|
|
|
|
{
|
2009-12-05 17:55:25 +01:00
|
|
|
h.set_piece_deadline(i, (i+5) * 1000, torrent_handle::alert_when_available);
|
2009-11-02 02:01:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
if (c == 'v')
|
|
|
|
{
|
|
|
|
torrent_handle h = get_active_torrent(handles);
|
|
|
|
if (h.is_valid()) h.scrape_tracker();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c == 'p')
|
|
|
|
{
|
|
|
|
torrent_handle h = get_active_torrent(handles);
|
|
|
|
if (h.is_valid())
|
2004-03-21 03:03:37 +01:00
|
|
|
{
|
2009-02-26 08:09:56 +01:00
|
|
|
if (!h.is_auto_managed() && h.is_paused())
|
2008-06-17 10:32:32 +02:00
|
|
|
{
|
2009-02-26 08:09:56 +01:00
|
|
|
h.auto_managed(true);
|
2008-06-17 10:32:32 +02:00
|
|
|
}
|
2009-02-26 08:09:56 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
h.auto_managed(false);
|
|
|
|
h.pause();
|
|
|
|
}
|
|
|
|
// the alert handler for save_resume_data_alert
|
|
|
|
// will save it to disk
|
|
|
|
h.save_resume_data();
|
2005-10-19 15:58:41 +02:00
|
|
|
}
|
2009-02-26 08:09:56 +01:00
|
|
|
}
|
2004-03-21 03:03:37 +01:00
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
if (c == 'c')
|
|
|
|
{
|
|
|
|
torrent_handle h = get_active_torrent(handles);
|
|
|
|
if (h.is_valid()) h.clear_error();
|
2007-07-09 02:00:35 +02:00
|
|
|
}
|
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
// toggle displays
|
|
|
|
if (c == 't') print_trackers = !print_trackers;
|
|
|
|
if (c == 'i') print_peers = !print_peers;
|
|
|
|
if (c == 'l') print_log = !print_log;
|
|
|
|
if (c == 'd') print_downloads = !print_downloads;
|
|
|
|
if (c == 'f') print_file_progress = !print_file_progress;
|
|
|
|
if (c == 'h') show_pad_files = !show_pad_files;
|
|
|
|
if (c == 'a') print_piece_bar = !print_piece_bar;
|
|
|
|
if (c == 'g') show_dht_status = !show_dht_status;
|
|
|
|
// toggle columns
|
|
|
|
if (c == '1') print_ip = !print_ip;
|
|
|
|
if (c == '2') print_as = !print_as;
|
|
|
|
if (c == '3') print_timers = !print_timers;
|
|
|
|
if (c == '4') print_block = !print_block;
|
|
|
|
if (c == '5') print_peer_rate = !print_peer_rate;
|
|
|
|
if (c == '6') print_fails = !print_fails;
|
|
|
|
if (c == '7') print_send_bufs = !print_send_bufs;
|
|
|
|
}
|
|
|
|
if (c == 'q') break;
|
|
|
|
|
|
|
|
int terminal_width = 80;
|
2007-07-09 02:00:35 +02:00
|
|
|
|
|
|
|
#ifndef _WIN32
|
2009-02-26 08:09:56 +01:00
|
|
|
{
|
|
|
|
winsize size;
|
|
|
|
ioctl(STDOUT_FILENO, TIOCGWINSZ, (char*)&size);
|
|
|
|
terminal_width = size.ws_col;
|
|
|
|
}
|
2007-07-09 02:00:35 +02:00
|
|
|
#endif
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
// loop through the alert queue to see if anything has happened.
|
|
|
|
std::auto_ptr<alert> a;
|
|
|
|
a = ses.pop_alert();
|
|
|
|
std::string now = time_now_string();
|
|
|
|
while (a.get())
|
|
|
|
{
|
2009-05-13 03:02:06 +02:00
|
|
|
std::string event_string;
|
2009-02-26 08:09:56 +01:00
|
|
|
|
|
|
|
::print_alert(a.get(), event_string);
|
|
|
|
::handle_alert(ses, a.get(), handles);
|
|
|
|
|
2009-05-13 03:02:06 +02:00
|
|
|
events.push_back(event_string);
|
2009-02-26 08:09:56 +01:00
|
|
|
if (events.size() >= 20) events.pop_front();
|
|
|
|
|
2003-12-22 08:14:35 +01:00
|
|
|
a = ses.pop_alert();
|
2009-02-26 08:09:56 +01:00
|
|
|
}
|
2004-07-18 02:39:58 +02:00
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
session_status sess_stat = ses.status();
|
2008-07-12 10:19:17 +02:00
|
|
|
|
2009-05-13 03:02:06 +02:00
|
|
|
std::string out;
|
|
|
|
out = "[q] quit [i] toggle peers [d] toggle downloading pieces [p] toggle paused "
|
2009-02-26 08:09:56 +01:00
|
|
|
"[a] toggle piece bar [s] toggle download sequential [f] toggle files "
|
|
|
|
"[j] force recheck [space] toggle session pause [c] clear error [v] scrape [g] show DHT\n"
|
|
|
|
"[1] toggle IP [2] toggle AS [3] toggle timers [4] toggle block progress "
|
|
|
|
"[5] toggle peer rate [6] toggle failures [7] toggle send buffers\n";
|
2008-07-06 14:22:56 +02:00
|
|
|
|
2009-05-13 03:02:06 +02:00
|
|
|
char str[500];
|
2009-02-26 08:09:56 +01:00
|
|
|
int torrent_index = 0;
|
|
|
|
torrent_handle active_handle;
|
|
|
|
for (handles_t::iterator i = handles.begin();
|
|
|
|
i != handles.end(); ++torrent_index)
|
|
|
|
{
|
|
|
|
torrent_handle& h = i->second;
|
|
|
|
if (!h.is_valid())
|
|
|
|
{
|
|
|
|
handles.erase(i++);
|
|
|
|
continue;
|
2003-12-22 08:14:35 +01:00
|
|
|
}
|
2009-02-26 08:09:56 +01:00
|
|
|
else
|
2003-10-30 00:28:09 +01:00
|
|
|
{
|
2009-02-26 08:09:56 +01:00
|
|
|
++i;
|
|
|
|
}
|
2006-04-30 12:28:45 +02:00
|
|
|
|
2008-05-07 20:02:11 +02:00
|
|
|
#ifdef ANSI_TERMINAL_COLORS
|
2009-02-26 08:09:56 +01:00
|
|
|
char const* term = "\x1b[0m";
|
2008-05-07 20:02:11 +02:00
|
|
|
#else
|
2009-02-26 08:09:56 +01:00
|
|
|
char const* term = "";
|
2008-05-07 20:02:11 +02:00
|
|
|
#endif
|
2009-02-26 08:09:56 +01:00
|
|
|
if (active_torrent == torrent_index)
|
|
|
|
{
|
|
|
|
term = "\x1b[0m\x1b[7m";
|
2009-05-13 03:02:06 +02:00
|
|
|
out += esc("7");
|
|
|
|
out += "*";
|
2009-02-26 08:09:56 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-05-13 03:02:06 +02:00
|
|
|
out += " ";
|
2009-02-26 08:09:56 +01:00
|
|
|
}
|
2008-05-07 20:02:11 +02:00
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
int queue_pos = h.queue_position();
|
2009-05-13 03:02:06 +02:00
|
|
|
if (queue_pos == -1) out += "- ";
|
|
|
|
else
|
|
|
|
{
|
|
|
|
snprintf(str, sizeof(str), "%-3d", queue_pos);
|
|
|
|
out += str;
|
|
|
|
}
|
2008-04-24 05:28:48 +02:00
|
|
|
|
2009-05-13 03:02:06 +02:00
|
|
|
if (h.is_paused()) out += esc("34");
|
|
|
|
else out += esc("37");
|
2008-04-24 05:28:48 +02:00
|
|
|
|
2009-05-15 18:21:36 +02:00
|
|
|
std::string name = h.name();
|
|
|
|
if (name.size() > 40) name.resize(40);
|
|
|
|
snprintf(str, sizeof(str), "%-40s %s ", name.c_str(), term);
|
2009-05-13 03:02:06 +02:00
|
|
|
out += str;
|
2003-10-31 05:02:51 +01:00
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
torrent_status s = h.status();
|
2008-05-20 09:57:44 +02:00
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
bool paused = h.is_paused();
|
|
|
|
bool auto_managed = h.is_auto_managed();
|
|
|
|
bool sequential_download = h.is_sequential_download();
|
2009-05-13 03:02:06 +02:00
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
if (!s.error.empty())
|
|
|
|
{
|
2009-05-13 03:02:06 +02:00
|
|
|
out += esc("31");
|
|
|
|
out += "error ";
|
|
|
|
out += s.error;
|
|
|
|
out += esc("0");
|
|
|
|
out += "\n";
|
2009-02-26 08:09:56 +01:00
|
|
|
continue;
|
2008-05-07 11:28:49 +02:00
|
|
|
}
|
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
int seeds = 0;
|
|
|
|
int downloaders = 0;
|
|
|
|
|
|
|
|
if (s.num_complete >= 0) seeds = s.num_complete;
|
|
|
|
else seeds = s.list_seeds;
|
|
|
|
|
|
|
|
if (s.num_incomplete >= 0) downloaders = s.num_incomplete;
|
|
|
|
else downloaders = s.list_peers - s.list_seeds;
|
|
|
|
|
2010-01-17 22:10:28 +01:00
|
|
|
if (s.state != torrent_status::queued_for_checking && s.state != torrent_status::checking_files)
|
2009-02-26 08:09:56 +01:00
|
|
|
{
|
2010-01-17 22:10:28 +01:00
|
|
|
snprintf(str, sizeof(str), "%-13s down: (%s%s%s) up: %s%s%s (%s%s%s) swarm: %4d:%4d"
|
|
|
|
" bw queue: (%d|%d) all-time (Rx: %s%s%s Tx: %s%s%s) seed rank: %x%s\n"
|
|
|
|
, (paused && !auto_managed)?"paused":(paused && auto_managed)?"queued":state_str[s.state]
|
|
|
|
, esc("32"), add_suffix(s.total_download).c_str(), term
|
|
|
|
, esc("31"), add_suffix(s.upload_rate, "/s").c_str(), term
|
|
|
|
, esc("31"), add_suffix(s.total_upload).c_str(), term
|
|
|
|
, downloaders, seeds
|
|
|
|
, s.up_bandwidth_queue, s.down_bandwidth_queue
|
|
|
|
, esc("32"), add_suffix(s.all_time_download).c_str(), term
|
|
|
|
, esc("31"), add_suffix(s.all_time_upload).c_str(), term
|
|
|
|
, s.seed_rank, esc("0"));
|
|
|
|
out += str;
|
|
|
|
|
|
|
|
if (torrent_index != active_torrent && s.state == torrent_status::seeding) continue;
|
|
|
|
char const* progress_bar_color = "33"; // yellow
|
|
|
|
if (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
|
|
|
|
}
|
|
|
|
|
2010-01-31 22:30:43 +01:00
|
|
|
snprintf(str, sizeof(str), " %-10s: %s%-11"PRId64"%s Bytes %6.2f%% %s\n"
|
2010-01-17 22:10:28 +01:00
|
|
|
, sequential_download?"sequential":"progress"
|
|
|
|
, esc("32"), s.total_done, esc("0")
|
|
|
|
, s.progress_ppm / 10000.f
|
2010-01-31 22:30:43 +01:00
|
|
|
, progress_bar(s.progress_ppm / 1000, terminal_width - 43, progress_bar_color).c_str());
|
2010-01-17 22:10:28 +01:00
|
|
|
out += str;
|
2008-09-20 19:42:25 +02:00
|
|
|
}
|
2010-01-17 22:10:28 +01:00
|
|
|
else
|
2009-02-26 08:09:56 +01:00
|
|
|
{
|
2010-01-17 22:10:28 +01:00
|
|
|
snprintf(str, sizeof(str), "%-13s %s\n"
|
|
|
|
, state_str[s.state]
|
2010-01-31 22:30:43 +01:00
|
|
|
, progress_bar(s.progress_ppm / 1000, terminal_width - 43 - 20, "35").c_str());
|
2010-01-17 22:10:28 +01:00
|
|
|
out += str;
|
2009-02-26 08:09:56 +01:00
|
|
|
}
|
2009-05-13 03:02:06 +02:00
|
|
|
|
2010-01-17 22:10:28 +01:00
|
|
|
if (print_piece_bar && s.progress_ppm < 1000000 && s.progress > 0)
|
2009-05-13 03:02:06 +02:00
|
|
|
{
|
|
|
|
out += " ";
|
|
|
|
out += piece_bar(s.pieces, terminal_width - 7);
|
|
|
|
out += "\n";
|
|
|
|
}
|
|
|
|
|
2010-01-17 22:10:28 +01:00
|
|
|
if (s.state != torrent_status::queued_for_checking && s.state != torrent_status::checking_files)
|
|
|
|
{
|
|
|
|
boost::posix_time::time_duration t = s.next_announce;
|
|
|
|
snprintf(str, sizeof(str)
|
|
|
|
, " peers: %s%d%s (%s%d%s) seeds: %s%d%s distributed copies: %s%4.2f%s "
|
2009-05-13 03:02:06 +02:00
|
|
|
"sparse regions: %d download: %s%s%s next announce: %s%02d:%02d:%02d%s "
|
|
|
|
"tracker: %s%s%s\n"
|
2010-01-17 22:10:28 +01:00
|
|
|
, esc("37"), s.num_peers, esc("0")
|
|
|
|
, esc("37"), s.connect_candidates, esc("0")
|
|
|
|
, esc("37"), s.num_seeds, esc("0")
|
|
|
|
, esc("37"), s.distributed_copies, esc("0")
|
|
|
|
, s.sparse_regions
|
|
|
|
, esc("32"), add_suffix(s.download_rate, "/s").c_str(), esc("0")
|
|
|
|
, esc("37"), t.hours(), t.minutes(), t.seconds(), esc("0")
|
|
|
|
, esc("36"), s.current_tracker.c_str(), esc("0"));
|
|
|
|
out += str;
|
|
|
|
}
|
2009-02-26 08:09:56 +01:00
|
|
|
|
|
|
|
if (torrent_index != active_torrent) continue;
|
|
|
|
active_handle = h;
|
|
|
|
}
|
2008-09-20 19:42:25 +02:00
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
cache_status cs = ses.get_cache_status();
|
|
|
|
if (cs.blocks_read < 1) cs.blocks_read = 1;
|
|
|
|
if (cs.blocks_written < 1) cs.blocks_written = 1;
|
|
|
|
|
2009-05-13 03:02:06 +02:00
|
|
|
snprintf(str, sizeof(str), "==== conns: %d down: %s%s%s (%s%s%s) up: %s%s%s (%s%s%s) "
|
|
|
|
"tcp/ip: %s%s%s %s%s%s DHT: %s%s%s %s%s%s tracker: %s%s%s %s%s%s ====\n"
|
|
|
|
, sess_stat.num_peers
|
|
|
|
, esc("32"), add_suffix(sess_stat.download_rate, "/s").c_str(), esc("0")
|
|
|
|
, esc("32"), add_suffix(sess_stat.total_download).c_str(), esc("0")
|
|
|
|
, esc("31"), add_suffix(sess_stat.upload_rate, "/s").c_str(), esc("0")
|
|
|
|
, esc("31"), add_suffix(sess_stat.total_upload).c_str(), esc("0")
|
|
|
|
, esc("32"), add_suffix(sess_stat.ip_overhead_download_rate, "/s").c_str(), esc("0")
|
|
|
|
, esc("31"), add_suffix(sess_stat.ip_overhead_upload_rate, "/s").c_str(), esc("0")
|
|
|
|
, esc("32"), add_suffix(sess_stat.dht_download_rate, "/s").c_str(), esc("0")
|
|
|
|
, esc("31"), add_suffix(sess_stat.dht_upload_rate, "/s").c_str(), esc("0")
|
|
|
|
, esc("32"), add_suffix(sess_stat.tracker_download_rate, "/s").c_str(), esc("0")
|
|
|
|
, esc("31"), add_suffix(sess_stat.tracker_upload_rate, "/s").c_str(), esc("0"));
|
|
|
|
out += str;
|
|
|
|
|
|
|
|
snprintf(str, sizeof(str), "==== waste: %s fail: %s unchoked: %d / %d "
|
2009-09-06 18:47:48 +02:00
|
|
|
"bw queues: %8d (%d) | %8d (%d) cache: w: %"PRId64"%% r: %"PRId64"%% size: %s (%s) / %s dq: %"PRId64" ===\n"
|
2009-05-13 03:02:06 +02:00
|
|
|
, add_suffix(sess_stat.total_redundant_bytes).c_str()
|
|
|
|
, add_suffix(sess_stat.total_failed_bytes).c_str()
|
|
|
|
, sess_stat.num_unchoked, sess_stat.allowed_upload_slots
|
|
|
|
, sess_stat.up_bandwidth_bytes_queue
|
|
|
|
, sess_stat.up_bandwidth_queue
|
|
|
|
, sess_stat.down_bandwidth_bytes_queue
|
|
|
|
, sess_stat.down_bandwidth_queue
|
|
|
|
, (cs.blocks_written - cs.writes) * 100 / cs.blocks_written
|
|
|
|
, cs.blocks_read_hit * 100 / cs.blocks_read
|
|
|
|
, add_suffix(cs.cache_size * 16 * 1024).c_str()
|
|
|
|
, add_suffix(cs.read_cache_size * 16 * 1024).c_str()
|
2009-07-23 06:38:52 +02:00
|
|
|
, add_suffix(cs.total_used_buffers * 16 * 1024).c_str()
|
|
|
|
, cs.queued_bytes);
|
2009-05-13 03:02:06 +02:00
|
|
|
out += str;
|
|
|
|
|
|
|
|
snprintf(str, sizeof(str), "==== optimistic unchoke: %d unchoke counter: %d ====\n"
|
|
|
|
, sess_stat.optimistic_unchoke_counter, sess_stat.unchoke_counter);
|
|
|
|
out += str;
|
2009-02-26 08:09:56 +01:00
|
|
|
|
2009-08-30 08:25:17 +02:00
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
2009-02-26 08:09:56 +01:00
|
|
|
if (show_dht_status)
|
|
|
|
{
|
2009-05-15 17:52:15 +02:00
|
|
|
snprintf(str, sizeof(str), "DHT nodes: %d DHT cached nodes: %d total DHT size: %"PRId64"\n"
|
2009-05-13 03:02:06 +02:00
|
|
|
, sess_stat.dht_nodes, sess_stat.dht_node_cache, sess_stat.dht_global_nodes);
|
|
|
|
out += str;
|
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
for (std::vector<dht_lookup>::iterator i = sess_stat.active_requests.begin()
|
|
|
|
, end(sess_stat.active_requests.end()); i != end; ++i)
|
2008-05-07 11:28:49 +02:00
|
|
|
{
|
2009-10-09 04:34:25 +02:00
|
|
|
snprintf(str, sizeof(str)
|
|
|
|
, " %s in flight: %d [limit: %d] timeouts %d responses %d left %d\n"
|
2009-09-29 19:06:08 +02:00
|
|
|
, i->type, i->outstanding_requests, i->branch_factor, i->timeouts
|
2009-10-09 04:34:25 +02:00
|
|
|
, i->responses, i->nodes_left);
|
2009-05-13 03:02:06 +02:00
|
|
|
out += str;
|
2009-02-26 08:09:56 +01:00
|
|
|
}
|
|
|
|
}
|
2009-08-30 08:25:17 +02:00
|
|
|
#endif
|
2008-05-07 11:28:49 +02:00
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
if (active_handle.is_valid())
|
|
|
|
{
|
|
|
|
torrent_handle h = active_handle;
|
|
|
|
torrent_status s = h.status();
|
|
|
|
|
|
|
|
if ((print_downloads && s.state != torrent_status::seeding)
|
|
|
|
|| print_peers)
|
|
|
|
h.get_peer_info(peers);
|
2008-05-07 11:28:49 +02:00
|
|
|
|
2009-05-13 03:02:06 +02:00
|
|
|
out += "====== ";
|
|
|
|
out += h.name();
|
|
|
|
out += " ======\n";
|
2008-05-07 20:02:11 +02:00
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
if (print_peers && !peers.empty())
|
|
|
|
print_peer_info(out, peers);
|
2004-02-25 00:55:42 +01:00
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
if (print_trackers)
|
|
|
|
{
|
|
|
|
std::vector<announce_entry> tr = h.trackers();
|
|
|
|
ptime now = time_now();
|
|
|
|
for (std::vector<announce_entry>::iterator i = tr.begin()
|
|
|
|
, end(tr.end()); i != end; ++i)
|
2008-11-29 09:38:40 +01:00
|
|
|
{
|
2009-05-14 21:49:33 +02:00
|
|
|
snprintf(str, sizeof(str), "%2d %-55s fails: %-3d %s %s\n"
|
2009-05-13 03:02:06 +02:00
|
|
|
, i->tier, i->url.c_str(), i->fails, i->verified?"OK ":"- "
|
2009-05-14 21:49:33 +02:00
|
|
|
, i->updating?"updating"
|
|
|
|
:!i->verified?""
|
|
|
|
:to_string(total_seconds(i->next_announce - now), 8).c_str());
|
2009-05-13 03:02:06 +02:00
|
|
|
out += str;
|
2008-11-29 09:38:40 +01:00
|
|
|
}
|
2009-02-26 08:09:56 +01:00
|
|
|
}
|
2008-11-29 09:38:40 +01:00
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
if (print_downloads)
|
|
|
|
{
|
2009-05-14 21:49:33 +02:00
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
h.get_download_queue(queue);
|
|
|
|
std::sort(queue.begin(), queue.end(), bind(&partial_piece_info::piece_index, _1)
|
|
|
|
< bind(&partial_piece_info::piece_index, _2));
|
2008-02-08 11:22:05 +01:00
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
std::vector<cached_piece_info> pieces;
|
|
|
|
ses.get_cache_info(h.info_hash(), pieces);
|
2008-02-08 11:22:05 +01:00
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
for (std::vector<partial_piece_info>::iterator i = queue.begin();
|
|
|
|
i != queue.end(); ++i)
|
|
|
|
{
|
|
|
|
cached_piece_info* cp = 0;
|
|
|
|
std::vector<cached_piece_info>::iterator cpi = std::find_if(pieces.begin(), pieces.end()
|
|
|
|
, bind(&cached_piece_info::piece, _1) == i->piece_index);
|
|
|
|
if (cpi != pieces.end()) cp = &*cpi;
|
2008-02-08 11:22:05 +01:00
|
|
|
|
2009-05-22 05:40:06 +02:00
|
|
|
snprintf(str, sizeof(str), "%5d: [", i->piece_index);
|
2009-05-14 21:49:33 +02:00
|
|
|
out += str;
|
2009-02-26 08:09:56 +01:00
|
|
|
for (int j = 0; j < i->blocks_in_piece; ++j)
|
|
|
|
{
|
2010-01-31 22:30:43 +01:00
|
|
|
int index = peer_index(i->blocks[j].peer(), peers) % 36;
|
2009-05-14 21:49:33 +02:00
|
|
|
char chr = '+';
|
2009-02-26 08:09:56 +01:00
|
|
|
if (index >= 0)
|
2009-05-14 21:49:33 +02:00
|
|
|
chr = (index < 10)?'0' + index:'A' + index - 10;
|
|
|
|
|
|
|
|
char const* color = "";
|
2007-01-10 16:02:25 +01:00
|
|
|
|
2005-08-15 00:04:58 +02:00
|
|
|
#ifdef ANSI_TERMINAL_COLORS
|
2009-05-14 21:49:33 +02:00
|
|
|
if (cp && cp->blocks[j]) color = esc("36;7");
|
2009-02-26 08:09:56 +01:00
|
|
|
else if (i->blocks[j].bytes_progress > 0
|
|
|
|
&& i->blocks[j].state == block_info::requested)
|
2008-07-11 12:29:26 +02:00
|
|
|
{
|
2009-05-14 21:49:33 +02:00
|
|
|
if (i->blocks[j].num_peers > 1) color = esc("1;7");
|
|
|
|
else color = esc("33;7");
|
|
|
|
chr = '0' + (i->blocks[j].bytes_progress / float(i->blocks[j].block_size) * 10);
|
2009-02-26 08:09:56 +01:00
|
|
|
}
|
2009-05-14 21:49:33 +02:00
|
|
|
else if (i->blocks[j].state == block_info::finished) color = esc("32;7");
|
|
|
|
else if (i->blocks[j].state == block_info::writing) color = esc("35;7");
|
|
|
|
else if (i->blocks[j].state == block_info::requested) color = esc("0");
|
|
|
|
else { color = esc("0"); chr = ' '; }
|
2008-07-11 12:29:26 +02:00
|
|
|
#else
|
2009-05-14 21:49:33 +02:00
|
|
|
if (cp && cp->blocks[j]) chr = 'c';
|
|
|
|
else if (i->blocks[j].state == block_info::finished) chr = '#';
|
|
|
|
else if (i->blocks[j].state == block_info::writing) chr = '+';
|
|
|
|
else if (i->blocks[j].state == block_info::requested) chr = '-';
|
2009-05-29 03:27:08 +02:00
|
|
|
else chr = ' ';
|
2008-07-11 12:29:26 +02:00
|
|
|
#endif
|
2009-05-14 21:49:33 +02:00
|
|
|
snprintf(str, sizeof(str), "%s%c", color, chr);
|
|
|
|
out += str;
|
2008-07-11 12:29:26 +02:00
|
|
|
}
|
2009-05-14 21:49:33 +02:00
|
|
|
#ifdef ANSI_TERMINAL_COLORS
|
|
|
|
out += esc("0");
|
|
|
|
#endif
|
2009-05-23 09:47:20 +02:00
|
|
|
char const* piece_state[4] = {"", " slow", " medium", " fast"};
|
|
|
|
snprintf(str, sizeof(str), "]%s", piece_state[i->piece_state]);
|
2009-05-14 21:49:33 +02:00
|
|
|
out += str;
|
|
|
|
if (cp)
|
|
|
|
{
|
2009-05-22 05:40:06 +02:00
|
|
|
snprintf(str, sizeof(str), " %scache age: %-.1f"
|
2009-05-14 21:49:33 +02:00
|
|
|
, i->piece_state > 0?"| ":""
|
|
|
|
, total_milliseconds(time_now() - cp->last_use) / 1000.f);
|
|
|
|
out += str;
|
|
|
|
}
|
|
|
|
out += "\n";
|
2004-07-24 13:54:17 +02:00
|
|
|
}
|
2006-06-11 15:48:39 +02:00
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
for (std::vector<cached_piece_info>::iterator i = pieces.begin()
|
|
|
|
, end(pieces.end()); i != end; ++i)
|
2006-06-11 15:48:39 +02:00
|
|
|
{
|
2009-02-26 08:09:56 +01:00
|
|
|
if (i->kind != cached_piece_info::read_cache) continue;
|
2009-05-22 05:40:06 +02:00
|
|
|
snprintf(str, sizeof(str), "%5d: [", i->piece);
|
2009-05-14 21:49:33 +02:00
|
|
|
out += str;
|
2009-02-26 08:09:56 +01:00
|
|
|
for (std::vector<bool>::iterator k = i->blocks.begin()
|
|
|
|
, end(i->blocks.end()); k != end; ++k)
|
2006-06-11 15:48:39 +02:00
|
|
|
{
|
2009-05-14 21:49:33 +02:00
|
|
|
char const* color = "";
|
|
|
|
char chr = ' ';
|
2009-02-26 08:09:56 +01:00
|
|
|
#ifdef ANSI_TERMINAL_COLORS
|
2009-05-14 21:49:33 +02:00
|
|
|
color = *k?esc("33;7"):esc("0");
|
2009-02-26 08:09:56 +01:00
|
|
|
#else
|
2009-05-14 21:49:33 +02:00
|
|
|
chr = *k?'#':' ';
|
2009-02-26 08:09:56 +01:00
|
|
|
#endif
|
2009-05-14 21:49:33 +02:00
|
|
|
snprintf(str, sizeof(str), "%s%c", color, chr);
|
|
|
|
out += str;
|
2006-06-11 15:48:39 +02:00
|
|
|
}
|
2009-05-14 21:49:33 +02:00
|
|
|
#ifdef ANSI_TERMINAL_COLORS
|
|
|
|
out += esc("0");
|
|
|
|
#endif
|
2009-05-22 05:40:06 +02:00
|
|
|
snprintf(str, sizeof(str), "] cache age: %-.1f\n"
|
2009-05-14 21:49:33 +02:00
|
|
|
, total_milliseconds(time_now() - i->last_use) / 1000.f);
|
|
|
|
out += str;
|
2006-06-11 15:48:39 +02:00
|
|
|
}
|
2009-05-13 03:02:06 +02:00
|
|
|
out += "___________________________________\n";
|
2003-10-30 00:28:09 +01:00
|
|
|
}
|
2009-05-14 21:49:33 +02:00
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
if (print_file_progress
|
|
|
|
&& s.state != torrent_status::seeding
|
|
|
|
&& h.has_metadata())
|
2003-12-22 08:14:35 +01:00
|
|
|
{
|
2009-02-26 08:09:56 +01:00
|
|
|
std::vector<size_type> file_progress;
|
|
|
|
h.file_progress(file_progress);
|
|
|
|
torrent_info const& info = h.get_torrent_info();
|
|
|
|
for (int i = 0; i < info.num_files(); ++i)
|
2004-07-24 13:54:17 +02:00
|
|
|
{
|
2009-02-26 08:09:56 +01:00
|
|
|
bool pad_file = info.file_at(i).pad_file;
|
|
|
|
if (!show_pad_files && pad_file) continue;
|
2009-07-19 06:59:27 +02:00
|
|
|
int progress = info.file_at(i).size > 0
|
|
|
|
?file_progress[i] * 1000 / info.file_at(i).size:1000;
|
2009-05-14 21:49:33 +02:00
|
|
|
|
|
|
|
char const* color = (file_progress[i] == info.file_at(i).size)
|
|
|
|
?"32":"33";
|
|
|
|
|
|
|
|
snprintf(str, sizeof(str), "%s %s %-5.2f%% %s %s%s\n",
|
|
|
|
progress_bar(progress, 100, color).c_str()
|
|
|
|
, pad_file?esc("34"):""
|
2009-07-19 11:14:00 +02:00
|
|
|
, progress / 10.f
|
2009-05-14 21:49:33 +02:00
|
|
|
, add_suffix(file_progress[i]).c_str()
|
2009-10-26 02:29:39 +01:00
|
|
|
, filename(info.file_at(i).path).c_str()
|
2009-05-14 21:49:33 +02:00
|
|
|
, pad_file?esc("0"):"");
|
|
|
|
out += str;
|
2004-07-24 13:54:17 +02:00
|
|
|
}
|
2009-02-26 08:09:56 +01:00
|
|
|
|
2009-05-13 03:02:06 +02:00
|
|
|
out += "___________________________________\n";
|
2003-12-22 08:14:35 +01:00
|
|
|
}
|
2009-05-14 21:49:33 +02:00
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
}
|
2005-10-19 02:50:38 +02:00
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
if (print_log)
|
|
|
|
{
|
|
|
|
for (std::deque<std::string>::iterator i = events.begin();
|
|
|
|
i != events.end(); ++i)
|
2005-10-19 02:50:38 +02:00
|
|
|
{
|
2009-05-13 03:02:06 +02:00
|
|
|
out += "\n";
|
|
|
|
out += *i;
|
2005-10-19 02:50:38 +02:00
|
|
|
}
|
2003-10-30 00:28:09 +01:00
|
|
|
}
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
clear_home();
|
2009-05-13 03:02:06 +02:00
|
|
|
puts(out.c_str());
|
2009-02-26 08:09:56 +01:00
|
|
|
|
|
|
|
if (!monitor_dir.empty()
|
|
|
|
&& next_dir_scan < time_now())
|
|
|
|
{
|
|
|
|
scan_dir(monitor_dir, ses, handles, preferred_ratio
|
2010-01-18 06:29:35 +01:00
|
|
|
, allocation_mode, save_path, torrent_upload_limit
|
2009-02-26 08:09:56 +01:00
|
|
|
, torrent_download_limit);
|
|
|
|
next_dir_scan = time_now() + seconds(poll_interval);
|
2008-04-05 06:53:22 +02:00
|
|
|
}
|
2009-02-26 08:09:56 +01:00
|
|
|
}
|
2008-04-05 06:53:22 +02:00
|
|
|
|
2010-01-31 22:30:43 +01:00
|
|
|
// keep track of the number of resume data
|
|
|
|
// alerts to wait for
|
|
|
|
int num_resume_data = 0;
|
|
|
|
ses.pause();
|
|
|
|
for (handles_t::iterator i = handles.begin();
|
|
|
|
i != handles.end(); ++i)
|
|
|
|
{
|
|
|
|
torrent_handle& h = i->second;
|
|
|
|
if (!h.is_valid()) continue;
|
|
|
|
if (h.is_paused()) continue;
|
|
|
|
if (!h.has_metadata()) continue;
|
|
|
|
|
|
|
|
printf("saving resume data for %s\n", h.name().c_str());
|
|
|
|
// save_resume_data will generate an alert when it's done
|
|
|
|
h.save_resume_data();
|
|
|
|
++num_resume_data;
|
|
|
|
}
|
|
|
|
printf("waiting for resume data\n");
|
|
|
|
|
|
|
|
while (num_resume_data > 0)
|
|
|
|
{
|
|
|
|
alert const* a = ses.wait_for_alert(seconds(30));
|
|
|
|
if (a == 0)
|
|
|
|
{
|
|
|
|
printf(" aborting with %d outstanding "
|
|
|
|
"torrents to save resume data for", num_resume_data);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::auto_ptr<alert> holder = ses.pop_alert();
|
|
|
|
|
|
|
|
std::string log;
|
|
|
|
::print_alert(holder.get(), log);
|
|
|
|
printf("%s\n", log.c_str());
|
|
|
|
|
|
|
|
if (alert_cast<save_resume_data_failed_alert>(a))
|
|
|
|
{
|
|
|
|
--num_resume_data;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
save_resume_data_alert const* rd = alert_cast<save_resume_data_alert>(a);
|
|
|
|
if (!rd) continue;
|
|
|
|
--num_resume_data;
|
|
|
|
|
|
|
|
if (!rd->resume_data) continue;
|
|
|
|
|
|
|
|
torrent_handle h = rd->handle;
|
|
|
|
std::vector<char> out;
|
|
|
|
bencode(std::back_inserter(out), *rd->resume_data);
|
|
|
|
save_file(combine_path(h.save_path(), h.name() + ".resume"), out);
|
|
|
|
}
|
2009-05-13 03:02:06 +02:00
|
|
|
printf("saving session state\n");
|
2009-02-26 08:09:56 +01:00
|
|
|
{
|
|
|
|
entry session_state = ses.state();
|
2009-05-13 03:02:06 +02:00
|
|
|
|
|
|
|
std::vector<char> out;
|
|
|
|
bencode(std::back_inserter(out), session_state);
|
|
|
|
save_file(".ses_state", out);
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
2009-02-26 08:09:56 +01:00
|
|
|
|
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
2009-05-13 03:02:06 +02:00
|
|
|
printf("saving DHT state\n");
|
|
|
|
entry dht_state = ses.dht_state();
|
|
|
|
|
|
|
|
std::vector<char> out;
|
|
|
|
bencode(std::back_inserter(out), dht_state);
|
|
|
|
save_file(".dht_state", out);
|
2008-06-29 07:34:22 +02:00
|
|
|
#endif
|
2009-05-13 03:02:06 +02:00
|
|
|
printf("closing session");
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2005-10-16 11:15:46 +02:00
|
|
|
|