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
|
2010-08-23 10:48:02 +02:00
|
|
|
#include "libtorrent/time.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>
|
|
|
|
|
2011-03-31 05:19:05 +02:00
|
|
|
bool sleep_and_input(int* c, int sleep)
|
2003-10-30 00:28:09 +01:00
|
|
|
{
|
2011-06-06 06:24:12 +02:00
|
|
|
for (int i = 0; i < 2; ++i)
|
2003-10-30 00:28:09 +01:00
|
|
|
{
|
2009-05-13 18:02:43 +02:00
|
|
|
if (_kbhit())
|
|
|
|
{
|
|
|
|
*c = _getch();
|
|
|
|
return true;
|
|
|
|
}
|
2011-06-06 06:24:12 +02:00
|
|
|
Sleep(sleep / 2);
|
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
|
|
|
};
|
|
|
|
|
2011-03-31 05:19:05 +02:00
|
|
|
bool sleep_and_input(int* 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;
|
2010-08-23 10:48:02 +02:00
|
|
|
libtorrent::ptime start = libtorrent::time_now_hires();
|
|
|
|
int ret = 0;
|
|
|
|
retry:
|
2003-10-31 12:17:45 +01:00
|
|
|
fd_set set;
|
|
|
|
FD_ZERO(&set);
|
|
|
|
FD_SET(0, &set);
|
2011-06-06 06:24:12 +02:00
|
|
|
timeval tv = {sleep/ 1000, (sleep % 1000) * 1000 };
|
2010-08-23 10:48:02 +02:00
|
|
|
ret = select(1, &set, 0, 0, &tv);
|
|
|
|
if (ret > 0)
|
2010-01-05 14:00:04 +01:00
|
|
|
{
|
2011-03-31 05:19:05 +02:00
|
|
|
*c = getc(stdin);
|
2010-01-05 14:00:04 +01:00
|
|
|
return true;
|
|
|
|
}
|
2010-08-23 10:48:02 +02:00
|
|
|
if (errno == EINTR)
|
|
|
|
{
|
2011-06-06 06:24:12 +02:00
|
|
|
if (total_milliseconds(libtorrent::time_now_hires() - start) < sleep)
|
2010-08-23 10:48:02 +02:00
|
|
|
goto retry;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret < 0 && errno != 0 && errno != ETIMEDOUT)
|
2011-06-06 06:24:12 +02:00
|
|
|
{
|
2010-08-23 10:48:02 +02:00
|
|
|
fprintf(stderr, "select failed: %s\n", strerror(errno));
|
2011-06-06 06:24:12 +02:00
|
|
|
libtorrent::sleep(500);
|
|
|
|
}
|
2010-08-23 10:48:02 +02:00
|
|
|
|
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;
|
2010-11-29 02:33:05 +01:00
|
|
|
bool print_utp_stats = false;
|
2008-04-05 06:53:22 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
2011-07-04 07:33:06 +02:00
|
|
|
// the number of times we've asked to save resume data
|
|
|
|
// without having received a response (successful or failure)
|
|
|
|
int num_outstanding_resume_data = 0;
|
|
|
|
|
2010-11-06 17:57:15 +01:00
|
|
|
enum {
|
2011-02-01 10:48:28 +01:00
|
|
|
torrents_all,
|
|
|
|
torrents_downloading,
|
|
|
|
torrents_not_paused,
|
|
|
|
torrents_seeding,
|
|
|
|
torrents_queued,
|
|
|
|
torrents_stopped,
|
|
|
|
torrents_checking,
|
2011-03-21 05:59:45 +01:00
|
|
|
torrents_feeds,
|
2011-02-01 10:48:28 +01:00
|
|
|
|
|
|
|
torrents_max
|
2010-11-06 17:57:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
int torrent_filter = torrents_not_paused;
|
|
|
|
|
2010-11-06 19:04:07 +01:00
|
|
|
struct torrent_entry
|
|
|
|
{
|
|
|
|
torrent_entry(libtorrent::torrent_handle h) : handle(h) {}
|
|
|
|
libtorrent::torrent_handle handle;
|
|
|
|
libtorrent::torrent_status status;
|
|
|
|
};
|
|
|
|
|
2011-02-01 10:48:28 +01:00
|
|
|
// maps filenames to torrent_handles
|
|
|
|
typedef std::multimap<std::string, libtorrent::torrent_handle> handles_t;
|
2010-11-06 19:04:07 +01:00
|
|
|
|
2011-02-01 10:48:28 +01:00
|
|
|
using libtorrent::torrent_status;
|
2010-11-06 17:57:15 +01:00
|
|
|
|
2011-02-02 04:38:02 +01:00
|
|
|
bool show_torrent(libtorrent::torrent_status const& st, int torrent_filter, int* counters)
|
2011-02-01 10:48:28 +01:00
|
|
|
{
|
2011-02-02 04:38:02 +01:00
|
|
|
++counters[torrents_all];
|
|
|
|
|
|
|
|
if (!st.paused
|
|
|
|
&& st.state != torrent_status::seeding
|
|
|
|
&& st.state != torrent_status::finished)
|
|
|
|
{
|
|
|
|
++counters[torrents_downloading];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!st.paused) ++counters[torrents_not_paused];
|
|
|
|
|
|
|
|
if (!st.paused
|
|
|
|
&& (st.state == torrent_status::seeding
|
|
|
|
|| st.state == torrent_status::finished))
|
|
|
|
{
|
|
|
|
++counters[torrents_seeding];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (st.paused && st.auto_managed)
|
|
|
|
{
|
|
|
|
++counters[torrents_queued];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (st.paused && !st.auto_managed)
|
|
|
|
{
|
|
|
|
++counters[torrents_stopped];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (st.state == torrent_status::checking_files
|
|
|
|
|| st.state == torrent_status::queued_for_checking)
|
|
|
|
{
|
|
|
|
++counters[torrents_checking];
|
|
|
|
}
|
|
|
|
|
2010-11-06 17:57:15 +01:00
|
|
|
switch (torrent_filter)
|
|
|
|
{
|
|
|
|
case torrents_all: return true;
|
|
|
|
case torrents_downloading:
|
|
|
|
return !st.paused
|
|
|
|
&& st.state != torrent_status::seeding
|
|
|
|
&& st.state != torrent_status::finished;
|
|
|
|
case torrents_not_paused: return !st.paused;
|
|
|
|
case torrents_seeding:
|
|
|
|
return !st.paused
|
|
|
|
&& (st.state == torrent_status::seeding
|
|
|
|
|| st.state == torrent_status::finished);
|
2011-02-01 10:48:28 +01:00
|
|
|
case torrents_queued: return st.paused && st.auto_managed;
|
|
|
|
case torrents_stopped: return st.paused && !st.auto_managed;
|
|
|
|
case torrents_checking: return st.state == torrent_status::checking_files
|
|
|
|
|| st.state == torrent_status::queued_for_checking;
|
2011-03-21 05:59:45 +01:00
|
|
|
case torrents_feeds: return false;
|
2010-11-06 17:57:15 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-02-01 10:48:28 +01:00
|
|
|
bool yes(libtorrent::torrent_status const&)
|
|
|
|
{ return true; }
|
|
|
|
|
|
|
|
bool compare_torrent(torrent_status const& lhs, torrent_status const& rhs)
|
|
|
|
{
|
|
|
|
if (lhs.queue_position != -1 && rhs.queue_position != -1)
|
|
|
|
{
|
|
|
|
// both are downloading, sort by queue pos
|
|
|
|
return lhs.queue_position < rhs.queue_position;
|
|
|
|
}
|
|
|
|
else if (lhs.queue_position == -1 && rhs.queue_position == -1)
|
|
|
|
{
|
|
|
|
// both are seeding, sort by seed-rank
|
|
|
|
return lhs.seed_rank > rhs.seed_rank;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (lhs.queue_position == -1) < (rhs.queue_position == -1);
|
|
|
|
}
|
|
|
|
|
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()
|
2010-07-22 18:49:40 +02:00
|
|
|
, peers.end(), boost::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;
|
2010-11-29 02:33:05 +01: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
|
2011-06-06 06:24:12 +02:00
|
|
|
out += "down (total | peak ) up (total | peak ) sent-req tmo bsy rcv 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)
|
|
|
|
{
|
2010-11-29 02:33:05 +01:00
|
|
|
snprintf(str, sizeof(str), "%-30s %-22s", (print_endpoint(i->ip) +
|
|
|
|
(i->connection_type == peer_info::bittorrent_utp ? " [uTP]" : "")).c_str()
|
2010-07-17 09:13:14 +02:00
|
|
|
, print_endpoint(i->local_endpoint).c_str());
|
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
|
|
|
|
2011-06-06 06:24:12 +02:00
|
|
|
char temp[10];
|
|
|
|
snprintf(temp, sizeof(temp), "%d/%d"
|
|
|
|
, i->download_queue_length
|
|
|
|
, i->target_dl_queue_length);
|
|
|
|
temp[7] = 0;
|
|
|
|
|
2009-05-15 18:21:36 +02:00
|
|
|
snprintf(str, sizeof(str)
|
2011-06-06 06:24:12 +02:00
|
|
|
, "%s%s (%s|%s) %s%s (%s|%s) %s%7s %4d%4d%4d %c%c%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")
|
|
|
|
|
2011-06-06 06:24:12 +02:00
|
|
|
, temp // sent requests and target number of outstanding reqs.
|
|
|
|
, i->timed_out_requests
|
|
|
|
, i->busy_requests
|
2009-05-15 18:21:36 +02:00
|
|
|
, 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':
|
2011-02-13 23:27:02 +01:00
|
|
|
(i->read_state == peer_info::bw_network)?'R':
|
|
|
|
(i->read_state == peer_info::bw_disk)?'D':'.'
|
2009-05-13 03:02:06 +02:00
|
|
|
, (i->write_state == peer_info::bw_limit)?'w':
|
2011-02-13 23:27:02 +01:00
|
|
|
(i->write_state == peer_info::bw_network)?'W':
|
|
|
|
(i->write_state == peer_info::bw_disk)?'D':'.'
|
2009-05-13 03:02:06 +02:00
|
|
|
, (i->flags & peer_info::snubbed)?'S':'.'
|
|
|
|
, (i->flags & peer_info::upload_only)?'U':'D'
|
2010-12-18 11:19:34 +01:00
|
|
|
, (i->flags & peer_info::endgame_mode)?'-':'.'
|
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
|
2010-11-29 02:33:05 +01:00
|
|
|
, (i->flags & peer_info::holepunched)?'h':'.'
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 = "";
|
2010-07-17 09:13:14 +02:00
|
|
|
std::string outgoing_interface = "";
|
2010-01-12 02:56:48 +01:00
|
|
|
int poll_interval = 5;
|
|
|
|
int max_connections_per_torrent = 50;
|
2011-08-07 08:34:27 +02:00
|
|
|
bool seed_mode = false;
|
2010-01-12 02:56:48 +01:00
|
|
|
|
2010-09-05 18:01:36 +02:00
|
|
|
bool share_mode = false;
|
2011-07-10 22:17:32 +02:00
|
|
|
bool disable_storage = false;
|
2010-09-05 18:01:36 +02:00
|
|
|
|
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
|
2011-02-09 05:54:22 +01:00
|
|
|
, std::set<libtorrent::torrent_handle>& non_files
|
2005-10-19 02:50:38 +02:00
|
|
|
, 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;
|
2011-08-07 08:34:27 +02:00
|
|
|
p.seed_mode = seed_mode;
|
2011-07-10 22:17:32 +02:00
|
|
|
if (disable_storage) p.storage = disabled_storage_constructor;
|
2010-09-05 18:01:36 +02:00
|
|
|
p.share_mode = share_mode;
|
2008-07-01 01:14:31 +02:00
|
|
|
lazy_entry resume_data;
|
|
|
|
|
2011-02-26 08:48:05 +01:00
|
|
|
std::string filename = combine_path(save_path, ".resume/" + to_hex(t->info_hash().to_string()) + ".resume");
|
2008-07-01 01:14:31 +02:00
|
|
|
|
|
|
|
std::vector<char> buf;
|
2011-01-19 11:07:51 +01:00
|
|
|
if (load_file(filename.c_str(), buf, ec) == 0)
|
2008-07-01 01:14:31 +02:00
|
|
|
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);
|
2010-03-29 02:31:30 +02:00
|
|
|
if (ec)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "failed to add torrent: %s\n", ec.message().c_str());
|
|
|
|
return;
|
|
|
|
}
|
2008-04-24 05:28:48 +02:00
|
|
|
|
2011-02-01 10:48:28 +01:00
|
|
|
if (monitored_dir)
|
|
|
|
{
|
|
|
|
handles.insert(std::pair<const std::string, torrent_handle>(
|
|
|
|
torrent, h));
|
|
|
|
}
|
2011-02-09 05:54:22 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
non_files.insert(h);
|
|
|
|
}
|
2005-10-19 02:50:38 +02:00
|
|
|
|
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);
|
2010-07-17 09:13:14 +02:00
|
|
|
h.use_interface(outgoing_interface.c_str());
|
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
|
2011-02-01 10:48:28 +01:00
|
|
|
, handles_t& files
|
2011-02-09 05:54:22 +01:00
|
|
|
, std::set<libtorrent::torrent_handle>& non_files
|
2005-10-19 02:50:38 +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-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
|
|
|
|
2011-02-01 10:48:28 +01:00
|
|
|
handles_t::iterator k = files.find(file);
|
|
|
|
if (k != files.end())
|
2005-10-19 02:50:38 +02:00
|
|
|
{
|
|
|
|
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.
|
2011-02-09 05:54:22 +01:00
|
|
|
add_torrent(ses, files, non_files, 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
|
|
|
|
|
2011-02-01 10:48:28 +01:00
|
|
|
for (handles_t::iterator i = files.begin(); !files.empty() && i != files.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
|
|
|
|
2011-02-01 10:48:28 +01:00
|
|
|
torrent_handle& h = i->second;
|
2005-10-19 02:50:38 +02:00
|
|
|
if (!h.is_valid())
|
|
|
|
{
|
2011-02-01 10:48:28 +01:00
|
|
|
files.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
|
2011-07-04 07:33:06 +02:00
|
|
|
if (h.need_save_resume_data())
|
|
|
|
{
|
|
|
|
h.save_resume_data();
|
|
|
|
++num_outstanding_resume_data;
|
|
|
|
}
|
2008-04-13 20:54:36 +02:00
|
|
|
|
2011-02-01 10:48:28 +01:00
|
|
|
files.erase(i++);
|
2005-10-19 02:50:38 +02:00
|
|
|
}
|
2005-10-16 11:15:46 +02:00
|
|
|
}
|
|
|
|
|
2011-02-01 10:48:28 +01:00
|
|
|
torrent_status const& get_active_torrent(std::vector<torrent_status> const& torrents)
|
2008-06-17 10:32:32 +02:00
|
|
|
{
|
2011-02-21 06:24:41 +01:00
|
|
|
if (active_torrent >= int(torrents.size())
|
2010-11-06 19:04:07 +01:00
|
|
|
|| active_torrent < 0) active_torrent = 0;
|
2011-02-01 10:48:28 +01:00
|
|
|
std::vector<torrent_status>::const_iterator i = torrents.begin();
|
2008-06-17 10:32:32 +02:00
|
|
|
std::advance(i, active_torrent);
|
2011-02-01 10:48:28 +01:00
|
|
|
return *i;
|
2008-06-17 10:32:32 +02:00
|
|
|
}
|
|
|
|
|
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);
|
2011-02-21 06:24:41 +01:00
|
|
|
if (written != int(v.size())) return -3;
|
2009-05-13 03:02:06 +02:00
|
|
|
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
|
2011-02-09 05:54:22 +01:00
|
|
|
, handles_t const& files, std::set<libtorrent::torrent_handle> const& non_files)
|
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();
|
2011-07-04 07:33:06 +02:00
|
|
|
++num_outstanding_resume_data;
|
2008-07-06 14:22:56 +02:00
|
|
|
}
|
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
|
|
|
{
|
2011-07-04 07:33:06 +02:00
|
|
|
--num_outstanding_resume_data;
|
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);
|
2011-02-26 08:48:05 +01:00
|
|
|
save_file(combine_path(h.save_path(), ".resume/" + to_hex(h.info_hash().to_string()) + ".resume"), out);
|
2011-07-14 05:45:24 +02:00
|
|
|
if (h.is_valid()
|
|
|
|
&& non_files.find(h) == non_files.end()
|
2011-02-09 05:54:22 +01:00
|
|
|
&& std::find_if(files.begin(), files.end()
|
|
|
|
, boost::bind(&handles_t::value_type::second, _1) == h) == files.end())
|
2008-09-26 02:26:35 +02:00
|
|
|
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
|
|
|
{
|
2011-07-04 07:33:06 +02:00
|
|
|
--num_outstanding_resume_data;
|
2008-11-17 02:19:46 +01:00
|
|
|
torrent_handle h = p->handle;
|
2011-07-14 05:45:24 +02:00
|
|
|
if (h.is_valid()
|
|
|
|
&& non_files.find(h) == non_files.end()
|
2011-04-02 09:39:02 +02:00
|
|
|
&& std::find_if(files.begin(), files.end()
|
|
|
|
, boost::bind(&handles_t::value_type::second, _1) == h) == files.end())
|
2008-11-17 02:19:46 +01:00
|
|
|
ses.remove_torrent(h);
|
|
|
|
}
|
2011-06-05 22:48:00 +02:00
|
|
|
else if (torrent_paused_alert* p = alert_cast<torrent_paused_alert>(a))
|
|
|
|
{
|
|
|
|
// write resume data for the finished torrent
|
|
|
|
// the alert handler for save_resume_data_alert
|
|
|
|
// will save it to disk
|
|
|
|
torrent_handle h = p->handle;
|
|
|
|
h.save_resume_data();
|
2011-07-04 07:33:06 +02:00
|
|
|
++num_outstanding_resume_data;
|
2011-06-05 22:48:00 +02:00
|
|
|
}
|
2008-07-06 14:22:56 +02:00
|
|
|
}
|
|
|
|
|
2011-07-10 10:57:27 +02:00
|
|
|
void print_piece(libtorrent::partial_piece_info* pp
|
|
|
|
, libtorrent::cached_piece_info* cs
|
|
|
|
, std::vector<libtorrent::peer_info> const& peers
|
|
|
|
, std::string& out)
|
|
|
|
{
|
|
|
|
using namespace libtorrent;
|
|
|
|
|
|
|
|
char str[1024];
|
|
|
|
assert(pp == 0 || cs == 0 || cs->piece == pp->piece_index);
|
|
|
|
int piece = pp ? pp->piece_index : cs->piece;
|
|
|
|
int num_blocks = pp ? pp->blocks_in_piece : cs->blocks.size();
|
|
|
|
|
|
|
|
snprintf(str, sizeof(str), "%5d: [", piece);
|
|
|
|
out += str;
|
|
|
|
char const* last_color = 0;
|
|
|
|
for (int j = 0; j < num_blocks; ++j)
|
|
|
|
{
|
|
|
|
int index = pp ? peer_index(pp->blocks[j].peer(), peers) % 36 : -1;
|
|
|
|
char chr = '+';
|
|
|
|
if (index >= 0)
|
|
|
|
chr = (index < 10)?'0' + index:'A' + index - 10;
|
|
|
|
|
|
|
|
char const* color = "";
|
|
|
|
|
|
|
|
if (pp == 0)
|
|
|
|
{
|
|
|
|
color = cs->blocks[j] ? esc("34;7") : esc("0");
|
|
|
|
chr = ' ';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifdef ANSI_TERMINAL_COLORS
|
|
|
|
if (cs && cs->blocks[j]) color = esc("36;7");
|
|
|
|
else if (pp->blocks[j].bytes_progress > 0
|
|
|
|
&& pp->blocks[j].state == block_info::requested)
|
|
|
|
{
|
|
|
|
if (pp->blocks[j].num_peers > 1) color = esc("1;7");
|
|
|
|
else color = esc("33;7");
|
|
|
|
chr = '0' + (pp->blocks[j].bytes_progress / float(pp->blocks[j].block_size) * 10);
|
|
|
|
}
|
|
|
|
else if (pp->blocks[j].state == block_info::finished) color = esc("32;7");
|
|
|
|
else if (pp->blocks[j].state == block_info::writing) color = esc("36;7");
|
|
|
|
else if (pp->blocks[j].state == block_info::requested) color = esc("0");
|
|
|
|
else { color = esc("0"); chr = ' '; }
|
|
|
|
#else
|
|
|
|
if (cs && cs->blocks[j]) chr = 'c';
|
|
|
|
else if (pp->blocks[j].state == block_info::finished) chr = '#';
|
|
|
|
else if (pp->blocks[j].state == block_info::writing) chr = '+';
|
|
|
|
else if (pp->blocks[j].state == block_info::requested) chr = '-';
|
|
|
|
else chr = ' ';
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
if (last_color == 0 || strcmp(last_color, color) != 0)
|
|
|
|
snprintf(str, sizeof(str), "%s%c", color, chr);
|
|
|
|
else
|
|
|
|
out += chr;
|
|
|
|
|
|
|
|
out += str;
|
|
|
|
}
|
|
|
|
#ifdef ANSI_TERMINAL_COLORS
|
|
|
|
out += esc("0");
|
|
|
|
#endif
|
|
|
|
char const* piece_state[4] = {"", " slow", " medium", " fast"};
|
|
|
|
snprintf(str, sizeof(str), "] %3d cache age: %-.1f %s\n"
|
|
|
|
, cs ? cs->next_to_hash : 0
|
|
|
|
, cs ? (total_milliseconds(time_now() - cs->last_use) / 1000.f) : 0.f
|
|
|
|
, pp ? piece_state[pp->piece_state] : "");
|
|
|
|
out += str;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2010-12-30 02:47:30 +01:00
|
|
|
fprintf(stderr, "usage: client_test [OPTIONS] [TORRENT|MAGNETURL|URL]\n\n"
|
2009-02-26 08:09:56 +01:00
|
|
|
"OPTIONS:\n"
|
2011-04-27 10:07:24 +02:00
|
|
|
"\n CLIENT OPTIONS\n"
|
2009-02-26 08:09:56 +01:00
|
|
|
" -f <log file> logs all events to the given file\n"
|
|
|
|
" -s <path> sets the save path for downloads\n"
|
|
|
|
" -m <path> sets the .torrent monitor directory\n"
|
|
|
|
" -t <seconds> sets the scan interval of the monitor dir\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"
|
2011-04-27 10:07:24 +02:00
|
|
|
" -q <num loops> automatically quit the client after <num loops> of refreshes\n"
|
|
|
|
" this is useful for scripting tests\n"
|
|
|
|
" -k enable high performance settings. This overwrites any other\n"
|
|
|
|
" previous command line options, so be sure to specify this first\n"
|
2011-08-07 08:34:27 +02:00
|
|
|
" -G Add torrents in seed-mode (i.e. assume all pieces\n"
|
|
|
|
" are present and check hashes on-demand)\n"
|
2011-04-27 10:07:24 +02:00
|
|
|
"\n BITTORRENT OPTIONS\n"
|
|
|
|
" -c <limit> sets the max number of connections\n"
|
|
|
|
" -T <limit> sets the max number of connections per torrent\n"
|
|
|
|
" -U <rate> sets per-torrent upload rate\n"
|
|
|
|
" -D <rate> sets per-torrent download rate\n"
|
|
|
|
" -d <rate> limits the download rate\n"
|
|
|
|
" -u <rate> limits the upload rate\n"
|
|
|
|
" -S <limit> limits the upload slots\n"
|
2010-01-31 22:30:43 +01:00
|
|
|
" -A <num pieces> allowed pieces set size\n"
|
2011-04-27 10:07:24 +02:00
|
|
|
" -H Don't start DHT\n"
|
|
|
|
" -n announce to trackers in all tiers\n"
|
|
|
|
" -W <num peers> Set the max number of peers to keep in the peer list\n"
|
|
|
|
" -r <ratio> sets the preferred share ratio\n"
|
2011-05-02 07:32:54 +02:00
|
|
|
" -B <seconds> sets the peer timeout\n"
|
|
|
|
" -Q enables share mode. Share mode attempts to maximize\n"
|
|
|
|
" share ratio rather than downloading\n"
|
2011-04-27 10:07:24 +02:00
|
|
|
"\n QUEING OPTIONS\n"
|
|
|
|
" -v <limit> Set the max number of active downloads\n"
|
|
|
|
" -^ <limit> Set the max number of active seeds\n"
|
|
|
|
"\n NETWORK OPTIONS\n"
|
|
|
|
" -p <port> sets the listen port\n"
|
|
|
|
" -o <limit> limits the number of simultaneous\n"
|
|
|
|
" half-open TCP connections to the\n"
|
|
|
|
" given number.\n"
|
|
|
|
" -w <seconds> sets the retry time for failed web seeds\n"
|
|
|
|
" -x <file> loads an emule IP-filter file\n"
|
2010-07-03 08:56:09 +02:00
|
|
|
" -P <host:port> Use the specified SOCKS5 proxy\n"
|
2010-07-25 03:31:15 +02:00
|
|
|
" -L <user:passwd> Use the specified username and password for the\n"
|
|
|
|
" proxy specified by -P\n"
|
2011-04-27 10:07:24 +02:00
|
|
|
" -h allow multiple connections from the same IP\n"
|
2010-11-29 02:33:05 +01:00
|
|
|
" -M Disable TCP/uTP bandwidth balancing\n"
|
2010-08-03 09:30:38 +02:00
|
|
|
" -N Do not attempt to use UPnP and NAT-PMP to forward ports\n"
|
2010-08-27 16:52:42 +02:00
|
|
|
" -Y Rate limit local peers\n"
|
2010-11-29 02:33:05 +01:00
|
|
|
" -y Disable TCP connections (disable outgoing TCP and reject\n"
|
|
|
|
" incoming TCP connections)\n"
|
2011-04-27 10:07:24 +02:00
|
|
|
" -b <IP> sets IP of the interface to bind the\n"
|
|
|
|
" listen socket to\n"
|
|
|
|
" -I <IP> sets the IP of the interface to bind\n"
|
|
|
|
" outgoing peer connections to\n"
|
|
|
|
#if TORRENT_USE_I2P
|
|
|
|
" -i <i2p-host> the hostname to an I2P SAM bridge to use\n"
|
|
|
|
#endif
|
2011-05-02 07:32:54 +02:00
|
|
|
" -l <limit> sets the listen socket queue size\n"
|
2011-04-27 10:07:24 +02:00
|
|
|
"\n DISK OPTIONS\n"
|
2011-08-22 02:51:14 +02:00
|
|
|
" -a <mode> sets the allocation mode. [sparse|full]\n"
|
2011-04-27 10:07:24 +02:00
|
|
|
" -R <num blocks> number of blocks per read cache line\n"
|
|
|
|
" -C <limit> sets the max cache size. Specified in 16kB blocks\n"
|
|
|
|
" -O Disallow disk job reordering\n"
|
|
|
|
" -j disable disk read-ahead\n"
|
|
|
|
" -z disable piece hash checks (used for benchmarking)\n"
|
2011-07-10 22:17:32 +02:00
|
|
|
" -0 disable disk I/O, read garbage and don't flush to disk\n"
|
2009-02-26 08:09:56 +01:00
|
|
|
"\n\n"
|
|
|
|
"TORRENT is a path to a .torrent file\n"
|
2010-12-30 02:47:30 +01:00
|
|
|
"MAGNETURL is a magnet link\n"
|
2011-04-27 10:07:24 +02:00
|
|
|
"URL is a url to a torrent file\n"
|
|
|
|
"\n"
|
|
|
|
"Example for running benchmark:\n\n"
|
2011-05-02 07:32:54 +02:00
|
|
|
" client_test -k -z -N -h -H -M -l 2000 -S 1000 -T 1000 -c 1000 test.torrent\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
|
|
|
|
2010-07-25 03:31:15 +02:00
|
|
|
proxy_settings ps;
|
|
|
|
|
2011-06-06 06:24:12 +02:00
|
|
|
int refresh_delay = 1000;
|
2010-05-03 07:51:01 +02:00
|
|
|
bool start_dht = true;
|
2010-08-03 09:30:38 +02:00
|
|
|
bool start_upnp = true;
|
2010-11-26 00:35:43 +01:00
|
|
|
int loop_limit = 0;
|
2009-05-13 18:02:43 +02:00
|
|
|
|
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.
|
2011-02-01 10:48:28 +01:00
|
|
|
std::vector<torrent_status> handles;
|
|
|
|
handles_t files;
|
2011-02-09 05:54:22 +01:00
|
|
|
// torrents that were not added via the monitor dir
|
|
|
|
std::set<torrent_handle> non_files;
|
|
|
|
|
2011-02-02 04:38:02 +01:00
|
|
|
int counters[torrents_max];
|
2011-02-01 10:48:28 +01:00
|
|
|
|
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;
|
2011-01-19 11:07:51 +01:00
|
|
|
error_code ec;
|
|
|
|
if (load_file(".ses_state", in, ec) == 0)
|
2009-05-13 03:02:06 +02:00
|
|
|
{
|
2010-03-04 17:42:39 +01:00
|
|
|
lazy_entry e;
|
2010-10-28 06:01:59 +02:00
|
|
|
if (lazy_bdecode(&in[0], &in[0] + in.size(), e, ec) == 0)
|
2010-03-04 17:42:39 +01:00
|
|
|
ses.load_state(e);
|
2009-05-13 03:02:06 +02:00
|
|
|
}
|
2008-04-05 06:53:22 +02:00
|
|
|
|
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
|
|
|
|
2010-06-05 23:03:01 +02:00
|
|
|
std::vector<add_torrent_params> magnet_links;
|
|
|
|
std::vector<std::string> torrents;
|
|
|
|
|
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
|
|
|
// 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;
|
2011-08-07 08:34:27 +02:00
|
|
|
p.seed_mode = seed_mode;
|
2011-07-10 22:17:32 +02:00
|
|
|
if (disable_storage) p.storage = disabled_storage_constructor;
|
2010-09-05 18:01:36 +02:00
|
|
|
p.share_mode = share_mode;
|
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;
|
2010-06-05 23:03:01 +02:00
|
|
|
magnet_links.push_back(p);
|
2009-02-26 08:09:56 +01:00
|
|
|
continue;
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
2009-02-26 08:09:56 +01:00
|
|
|
|
2010-06-05 23:03:01 +02:00
|
|
|
torrents.push_back(argv[i]);
|
2009-02-26 08:09:56 +01:00
|
|
|
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;
|
2010-10-09 21:09:38 +02:00
|
|
|
case 'o': settings.half_open_limit = 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;
|
2011-04-27 10:07:24 +02:00
|
|
|
case 'k': settings = high_performance_seed(); --i; break;
|
|
|
|
case 'j': settings.use_disk_read_ahead = false; --i; break;
|
|
|
|
case 'z': settings.disable_hash_checks = true; --i; break;
|
2009-02-26 08:09:56 +01:00
|
|
|
case 'r':
|
|
|
|
preferred_ratio = atoi(arg);
|
|
|
|
if (preferred_ratio != 0 && preferred_ratio < 1.f) preferred_ratio = 1.f;
|
|
|
|
break;
|
2011-05-02 07:32:54 +02:00
|
|
|
case 'B': settings.peer_timeout = atoi(arg); break;
|
2009-12-05 18:12:30 +01:00
|
|
|
case 'n': settings.announce_to_all_tiers = true; --i; break;
|
2011-08-07 08:34:27 +02:00
|
|
|
case 'G': seed_mode = true; --i; break;
|
2010-10-09 21:09:38 +02:00
|
|
|
case 'd': settings.download_rate_limit = atoi(arg) * 1000; break;
|
|
|
|
case 'u': settings.upload_rate_limit = atoi(arg) * 1000; break;
|
|
|
|
case 'S': settings.unchoke_slots_limit = atoi(arg); break;
|
2010-01-18 06:29:35 +01:00
|
|
|
case 'a':
|
|
|
|
if (strcmp(arg, "allocate") == 0) allocation_mode = storage_mode_allocate;
|
2011-08-22 02:51:14 +02:00
|
|
|
if (strcmp(arg, "sparse") == 0) allocation_mode = storage_mode_sparse;
|
2010-01-18 06:29:35 +01:00
|
|
|
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;
|
2010-11-29 02:33:05 +01:00
|
|
|
case 'Q': share_mode = true; --i; break;
|
2009-02-26 08:09:56 +01:00
|
|
|
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;
|
2010-05-03 07:51:01 +02:00
|
|
|
case 'H': start_dht = false; --i; break;
|
2011-05-02 07:32:54 +02:00
|
|
|
case 'l': settings.listen_queue_size = atoi(arg); break;
|
2010-10-01 06:07:38 +02:00
|
|
|
case 'W':
|
|
|
|
settings.max_peerlist_size = atoi(arg);
|
|
|
|
settings.max_paused_peerlist_size = atoi(arg) / 2;
|
|
|
|
break;
|
2009-02-26 08:09:56 +01:00
|
|
|
case 'x':
|
|
|
|
{
|
2010-06-06 04:31:20 +02:00
|
|
|
FILE* filter = fopen(arg, "r");
|
|
|
|
if (filter)
|
2009-02-26 08:09:56 +01:00
|
|
|
{
|
2010-06-06 04:31:20 +02:00
|
|
|
ip_filter fil;
|
|
|
|
unsigned int a,b,c,d,e,f,g,h, flags;
|
|
|
|
while (fscanf(filter, "%u.%u.%u.%u - %u.%u.%u.%u %u\n", &a, &b, &c, &d, &e, &f, &g, &h, &flags) == 9)
|
|
|
|
{
|
|
|
|
address_v4 start((a << 24) + (b << 16) + (c << 8) + d);
|
|
|
|
address_v4 last((e << 24) + (f << 16) + (g << 8) + h);
|
|
|
|
if (flags <= 127) flags = ip_filter::blocked;
|
|
|
|
else flags = 0;
|
|
|
|
fil.add_rule(start, last, flags);
|
|
|
|
}
|
|
|
|
ses.set_ip_filter(fil);
|
|
|
|
fclose(filter);
|
2009-02-26 08:09:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2010-10-09 21:09:38 +02:00
|
|
|
case 'c': settings.connections_limit = 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;
|
2010-11-29 02:33:05 +01:00
|
|
|
case 'M': settings.mixed_mode_algorithm = session_settings::prefer_tcp; --i; break;
|
|
|
|
case 'y': settings.enable_outgoing_tcp = false; settings.enable_incoming_tcp = false; --i; break;
|
2010-07-03 08:56:09 +02:00
|
|
|
case 'P':
|
|
|
|
{
|
2010-08-03 09:30:38 +02:00
|
|
|
char* port = (char*) strrchr(arg, ':');
|
2010-07-03 08:56:09 +02:00
|
|
|
if (port == 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "invalid proxy hostname, no port found\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*port++ = 0;
|
|
|
|
ps.hostname = arg;
|
|
|
|
ps.port = atoi(port);
|
|
|
|
if (ps.port == 0) {
|
|
|
|
fprintf(stderr, "invalid proxy port\n");
|
|
|
|
break;
|
|
|
|
}
|
2010-07-25 03:31:15 +02:00
|
|
|
if (ps.type == proxy_settings::none)
|
|
|
|
ps.type = proxy_settings::socks5;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'L':
|
|
|
|
{
|
|
|
|
char* pw = (char*) strchr(arg, ':');
|
|
|
|
if (pw == 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "invalid proxy username and password specified\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*pw++ = 0;
|
|
|
|
ps.username = arg;
|
|
|
|
ps.password = pw;
|
|
|
|
ps.type = proxy_settings::socks5_pw;
|
2010-07-03 08:56:09 +02:00
|
|
|
}
|
|
|
|
break;
|
2010-07-17 09:13:14 +02:00
|
|
|
case 'I': outgoing_interface = arg; break;
|
2010-08-03 09:30:38 +02:00
|
|
|
case 'N': start_upnp = false; --i; break;
|
2010-08-27 16:52:42 +02:00
|
|
|
case 'Y': settings.ignore_limits_on_local_network = false; --i; break;
|
2011-03-16 05:46:14 +01:00
|
|
|
case 'v': settings.active_downloads = atoi(arg);
|
|
|
|
settings.active_limit = (std::max)(atoi(arg) * 2, settings.active_limit);
|
|
|
|
break;
|
|
|
|
case '^':
|
|
|
|
settings.active_seeds = atoi(arg);
|
|
|
|
settings.active_limit = (std::max)(atoi(arg) * 2, settings.active_limit);
|
2011-01-20 06:49:36 +01:00
|
|
|
break;
|
2010-11-26 00:35:43 +01:00
|
|
|
case 'q': loop_limit = atoi(arg); break;
|
2011-07-10 22:17:32 +02:00
|
|
|
case '0': disable_storage = true; --i;
|
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
|
|
|
}
|
|
|
|
|
2011-01-31 04:13:22 +01:00
|
|
|
// create directory for resume files
|
|
|
|
create_directory(combine_path(save_path, ".resume/"), ec);
|
|
|
|
if (ec)
|
|
|
|
fprintf(stderr, "failed to create resume file directory: %s\n", ec.message().c_str());
|
|
|
|
|
2010-08-03 09:30:38 +02:00
|
|
|
ses.start_lsd();
|
|
|
|
if (start_upnp)
|
|
|
|
{
|
|
|
|
ses.start_upnp();
|
|
|
|
ses.start_natpmp();
|
|
|
|
}
|
|
|
|
|
2010-08-26 17:13:17 +02:00
|
|
|
ses.set_proxy(ps);
|
2010-07-25 03:31:15 +02:00
|
|
|
|
2011-03-07 08:02:30 +01:00
|
|
|
ses.listen_on(std::make_pair(listen_port, listen_port)
|
2011-02-21 06:24:41 +01:00
|
|
|
, ec, bind_to_interface.c_str());
|
|
|
|
if (ec)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "failed to listen on %s on ports %d-%d: %s\n"
|
|
|
|
, bind_to_interface.c_str(), listen_port, listen_port+1, ec.message().c_str());
|
|
|
|
}
|
2003-10-30 00:28:09 +01:00
|
|
|
|
2010-12-29 03:17:44 +01:00
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
2010-05-03 07:51:01 +02:00
|
|
|
if (start_dht)
|
|
|
|
{
|
|
|
|
settings.use_dht_as_fallback = false;
|
|
|
|
|
|
|
|
ses.add_dht_router(std::make_pair(
|
|
|
|
std::string("router.bittorrent.com"), 6881));
|
|
|
|
ses.add_dht_router(std::make_pair(
|
|
|
|
std::string("router.utorrent.com"), 6881));
|
|
|
|
ses.add_dht_router(std::make_pair(
|
|
|
|
std::string("router.bitcomet.com"), 6881));
|
|
|
|
|
|
|
|
ses.start_dht();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-04-27 10:07:24 +02:00
|
|
|
settings.user_agent = "client_test/" LIBTORRENT_VERSION;
|
|
|
|
settings.choking_algorithm = session_settings::auto_expand_choker;
|
|
|
|
settings.disk_cache_algorithm = session_settings::avoid_readback;
|
|
|
|
settings.volatile_read_cache = false;
|
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
ses.set_settings(settings);
|
|
|
|
|
2010-06-05 23:03:01 +02:00
|
|
|
for (std::vector<add_torrent_params>::iterator i = magnet_links.begin()
|
|
|
|
, end(magnet_links.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
error_code ec;
|
|
|
|
torrent_handle h = ses.add_torrent(*i, ec);
|
|
|
|
if (ec)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "failed to add torrent: %s\n", ec.message().c_str());
|
|
|
|
continue;
|
|
|
|
}
|
2011-02-09 05:54:22 +01:00
|
|
|
non_files.insert(h);
|
2010-06-05 23:03:01 +02:00
|
|
|
|
|
|
|
h.set_max_connections(max_connections_per_torrent);
|
|
|
|
h.set_max_uploads(-1);
|
|
|
|
h.set_ratio(preferred_ratio);
|
|
|
|
h.set_upload_limit(torrent_upload_limit);
|
|
|
|
h.set_download_limit(torrent_download_limit);
|
2010-07-17 09:13:14 +02:00
|
|
|
h.use_interface(outgoing_interface.c_str());
|
2010-06-05 23:03:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (std::vector<std::string>::iterator i = torrents.begin()
|
|
|
|
, end(torrents.end()); i != end; ++i)
|
|
|
|
{
|
2010-12-30 02:47:30 +01:00
|
|
|
if (std::strstr(i->c_str(), "http://") == i->c_str()
|
|
|
|
|| std::strstr(i->c_str(), "https://") == i->c_str()
|
|
|
|
|| std::strstr(i->c_str(), "magnet:") == i->c_str())
|
2010-06-05 23:03:01 +02:00
|
|
|
{
|
|
|
|
add_torrent_params p;
|
2011-08-07 08:34:27 +02:00
|
|
|
p.seed_mode = seed_mode;
|
2011-07-10 22:17:32 +02:00
|
|
|
if (disable_storage) p.storage = disabled_storage_constructor;
|
2010-09-05 18:01:36 +02:00
|
|
|
p.share_mode = share_mode;
|
2010-06-05 23:03:01 +02:00
|
|
|
p.save_path = save_path;
|
|
|
|
p.storage_mode = (storage_mode_t)allocation_mode;
|
2010-12-30 02:47:30 +01:00
|
|
|
p.url = *i;
|
|
|
|
|
2011-02-26 08:48:05 +01:00
|
|
|
std::vector<char> buf;
|
|
|
|
if (std::strstr(i->c_str(), "magnet:") == i->c_str())
|
|
|
|
{
|
|
|
|
std::string btih = url_has_argument(*i, "xt");
|
|
|
|
if (btih.empty()) continue;
|
|
|
|
|
|
|
|
if (btih.compare(0, 9, "urn:btih:") != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
sha1_hash info_hash;
|
|
|
|
if (btih.size() == 40 + 9) from_hex(&btih[9], 40, (char*)&info_hash[0]);
|
|
|
|
else info_hash.assign(base32decode(btih.substr(9)));
|
|
|
|
|
|
|
|
std::string filename = combine_path(save_path, ".resume/"
|
|
|
|
+ to_hex(info_hash.to_string()) + ".resume");
|
|
|
|
|
|
|
|
if (load_file(filename.c_str(), buf, ec) == 0)
|
|
|
|
p.resume_data = &buf;
|
|
|
|
}
|
|
|
|
|
2010-12-30 02:47:30 +01:00
|
|
|
printf("adding URL: %s\n", i->c_str());
|
2010-06-05 23:03:01 +02:00
|
|
|
error_code ec;
|
2010-12-30 02:47:30 +01:00
|
|
|
torrent_handle h = ses.add_torrent(p, ec);
|
2010-06-05 23:03:01 +02:00
|
|
|
if (ec)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s\n", ec.message().c_str());
|
|
|
|
continue;
|
|
|
|
}
|
2011-02-09 05:54:22 +01:00
|
|
|
non_files.insert(h);
|
2010-06-05 23:03:01 +02:00
|
|
|
|
|
|
|
h.set_max_connections(max_connections_per_torrent);
|
|
|
|
h.set_max_uploads(-1);
|
|
|
|
h.set_ratio(preferred_ratio);
|
|
|
|
h.set_upload_limit(torrent_upload_limit);
|
|
|
|
h.set_download_limit(torrent_download_limit);
|
2010-07-17 09:13:14 +02:00
|
|
|
h.use_interface(outgoing_interface.c_str());
|
2010-06-05 23:03:01 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if it's a torrent file, open it as usual
|
2011-02-09 05:54:22 +01:00
|
|
|
add_torrent(ses, files, non_files, i->c_str(), preferred_ratio
|
2010-06-05 23:03:01 +02:00
|
|
|
, allocation_mode, save_path, false
|
|
|
|
, torrent_upload_limit, torrent_download_limit);
|
|
|
|
}
|
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
// main loop
|
|
|
|
std::vector<peer_info> peers;
|
|
|
|
std::vector<partial_piece_info> queue;
|
|
|
|
|
2011-07-04 07:33:06 +02:00
|
|
|
int tick = 0;
|
|
|
|
|
2010-11-26 00:35:43 +01:00
|
|
|
while (loop_limit > 1 || loop_limit == 0)
|
2009-02-26 08:09:56 +01:00
|
|
|
{
|
2011-07-04 07:33:06 +02:00
|
|
|
++tick;
|
2011-02-01 10:48:28 +01:00
|
|
|
handles.clear();
|
2011-02-02 04:38:02 +01:00
|
|
|
memset(counters, 0, sizeof(counters));
|
|
|
|
ses.get_torrent_status(&handles, boost::bind(&show_torrent, _1, torrent_filter, (int*)counters));
|
2011-02-21 06:24:41 +01:00
|
|
|
if (active_torrent >= int(handles.size())) active_torrent = handles.size() - 1;
|
2011-05-14 23:00:45 +02:00
|
|
|
else if (active_torrent >= 0)
|
|
|
|
{
|
|
|
|
// ask for distributed copies for the selected torrent. Since this
|
|
|
|
// is a somewhat expensive operation, don't do it by default for
|
|
|
|
// all torrents
|
|
|
|
handles[active_torrent] = handles[active_torrent].handle.status(
|
2011-08-07 09:19:18 +02:00
|
|
|
torrent_handle::query_distributed_copies
|
|
|
|
| torrent_handle::query_pieces
|
|
|
|
| torrent_handle::query_verified_pieces);
|
2011-05-14 23:00:45 +02:00
|
|
|
}
|
2011-02-01 10:48:28 +01:00
|
|
|
|
2011-03-23 03:46:22 +01:00
|
|
|
std::vector<feed_handle> feeds;
|
|
|
|
ses.get_feeds(feeds);
|
|
|
|
|
|
|
|
counters[torrents_feeds] = feeds.size();
|
|
|
|
|
2011-02-01 10:48:28 +01:00
|
|
|
std::sort(handles.begin(), handles.end(), &compare_torrent);
|
|
|
|
|
2010-11-26 00:35:43 +01:00
|
|
|
if (loop_limit > 1) --loop_limit;
|
2011-03-31 05:19:05 +02:00
|
|
|
int c = 0;
|
2009-05-13 18:02:43 +02:00
|
|
|
while (sleep_and_input(&c, refresh_delay))
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2011-03-31 05:19:05 +02:00
|
|
|
if (c == EOF) { c = 'q'; break; }
|
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
|
2011-03-31 05:19:05 +02:00
|
|
|
int c = getc(stdin);
|
2008-05-07 11:28:49 +02:00
|
|
|
#endif
|
2011-03-31 05:19:05 +02:00
|
|
|
if (c == EOF) { c = 'q'; break; }
|
|
|
|
if (c != '[') continue;
|
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
|
2011-03-31 05:19:05 +02:00
|
|
|
if (c == EOF) break;
|
|
|
|
|
2010-11-06 17:57:15 +01:00
|
|
|
if (c == 68)
|
|
|
|
{
|
|
|
|
// arrow left
|
2011-02-01 10:48:28 +01:00
|
|
|
if (torrent_filter > 0)
|
|
|
|
{
|
|
|
|
--torrent_filter;
|
|
|
|
handles.clear();
|
2011-02-02 04:38:02 +01:00
|
|
|
memset(counters, 0, sizeof(counters));
|
|
|
|
ses.get_torrent_status(&handles, boost::bind(&show_torrent, _1, torrent_filter, (int*)counters));
|
2011-02-21 06:24:41 +01:00
|
|
|
if (active_torrent >= int(handles.size())) active_torrent = handles.size() - 1;
|
2011-02-01 10:48:28 +01:00
|
|
|
std::sort(handles.begin(), handles.end(), &compare_torrent);
|
|
|
|
}
|
2010-11-06 17:57:15 +01:00
|
|
|
}
|
|
|
|
else if (c == 67)
|
|
|
|
{
|
|
|
|
// arrow right
|
2011-02-01 10:48:28 +01:00
|
|
|
if (torrent_filter < torrents_max - 1)
|
|
|
|
{
|
|
|
|
++torrent_filter;
|
|
|
|
handles.clear();
|
2011-02-02 04:38:02 +01:00
|
|
|
memset(counters, 0, sizeof(counters));
|
|
|
|
ses.get_torrent_status(&handles, boost::bind(&show_torrent, _1, torrent_filter, (int*)counters));
|
2011-02-21 06:24:41 +01:00
|
|
|
if (active_torrent >= int(handles.size())) active_torrent = handles.size() - 1;
|
2011-02-01 10:48:28 +01:00
|
|
|
std::sort(handles.begin(), handles.end(), &compare_torrent);
|
|
|
|
}
|
2010-11-06 17:57:15 +01:00
|
|
|
}
|
|
|
|
else if (c == 65)
|
2009-02-26 08:09:56 +01:00
|
|
|
{
|
|
|
|
// arrow up
|
|
|
|
--active_torrent;
|
2011-02-01 10:48:28 +01:00
|
|
|
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;
|
2011-02-21 06:24:41 +01:00
|
|
|
if (active_torrent >= int(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;
|
2011-02-01 10:48:28 +01:00
|
|
|
std::vector<torrent_handle> torrents = ses.get_torrents();
|
|
|
|
for (std::vector<torrent_handle>::iterator i = torrents.begin();
|
|
|
|
i != torrents.end(); ++i)
|
2008-04-22 02:05:23 +02:00
|
|
|
{
|
2011-02-01 10:48:28 +01:00
|
|
|
i->get_full_peer_list(peers);
|
|
|
|
FILE* f = fopen(("peers_" + i->name()).c_str(), "w+");
|
2009-05-13 03:02:06 +02:00
|
|
|
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
|
|
|
|
2011-04-10 22:35:45 +02:00
|
|
|
if (c == 'D')
|
|
|
|
{
|
|
|
|
torrent_handle h = get_active_torrent(handles).handle;
|
|
|
|
if (h.is_valid())
|
|
|
|
{
|
|
|
|
printf("\n\nARE YOU SURE YOU WANT TO DELETE THE FILES FOR '%s'. THIS OPERATION CANNOT BE UNDONE. (y/N)"
|
|
|
|
, h.name().c_str());
|
|
|
|
char response = 'n';
|
|
|
|
scanf("%c", &response);
|
|
|
|
if (response == 'y')
|
|
|
|
{
|
|
|
|
// also delete the .torrent file from the torrent directory
|
|
|
|
handles_t::iterator i = std::find_if(files.begin(), files.end()
|
|
|
|
, boost::bind(&handles_t::value_type::second, _1) == h);
|
|
|
|
if (i != files.end())
|
|
|
|
{
|
|
|
|
files.erase(i);
|
|
|
|
error_code ec;
|
|
|
|
remove(combine_path(monitor_dir, i->first), ec);
|
|
|
|
if (ec) printf("failed to delete .torrent file: %s\n", ec.message().c_str());
|
|
|
|
}
|
2011-07-14 05:45:24 +02:00
|
|
|
if (h.is_valid())
|
|
|
|
ses.remove_torrent(h, session::delete_files);
|
2011-04-10 22:35:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-06 19:04:07 +01:00
|
|
|
if (c == 'j' && !handles.empty())
|
2009-02-26 08:09:56 +01:00
|
|
|
{
|
2010-11-06 19:04:07 +01:00
|
|
|
get_active_torrent(handles).handle.force_recheck();
|
2009-02-26 08:09:56 +01:00
|
|
|
}
|
|
|
|
|
2010-11-06 19:04:07 +01:00
|
|
|
if (c == 'r' && !handles.empty())
|
2009-02-26 08:09:56 +01:00
|
|
|
{
|
2010-11-06 19:04:07 +01:00
|
|
|
get_active_torrent(handles).handle.force_reannounce();
|
2009-02-26 08:09:56 +01:00
|
|
|
}
|
2008-09-07 11:59:54 +02:00
|
|
|
|
2010-11-06 19:04:07 +01:00
|
|
|
if (c == 's' && !handles.empty())
|
2009-02-26 08:09:56 +01:00
|
|
|
{
|
2011-02-01 10:48:28 +01:00
|
|
|
torrent_status const& ts = get_active_torrent(handles);
|
|
|
|
ts.handle.set_sequential_download(!ts.sequential_download);
|
2009-02-26 08:09:56 +01:00
|
|
|
}
|
|
|
|
|
2010-03-20 23:00:13 +01:00
|
|
|
if (c == 'R')
|
|
|
|
{
|
|
|
|
// save resume data for all torrents
|
2011-02-01 10:48:28 +01:00
|
|
|
for (std::vector<torrent_status>::iterator i = handles.begin()
|
2010-03-20 23:00:13 +01:00
|
|
|
, end(handles.end()); i != end; ++i)
|
|
|
|
{
|
2011-02-01 10:48:28 +01:00
|
|
|
if (i->need_save_resume)
|
2011-07-04 07:33:06 +02:00
|
|
|
{
|
2011-02-01 10:48:28 +01:00
|
|
|
i->handle.save_resume_data();
|
2011-07-04 07:33:06 +02:00
|
|
|
++num_outstanding_resume_data;
|
|
|
|
}
|
2010-03-20 23:00:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-06 19:04:07 +01:00
|
|
|
if (c == 'o' && !handles.empty())
|
2009-11-02 02:01:07 +01:00
|
|
|
{
|
2011-02-01 10:48:28 +01:00
|
|
|
torrent_status const& ts = get_active_torrent(handles);
|
|
|
|
int num_pieces = ts.num_pieces;
|
2010-11-06 19:04:07 +01:00
|
|
|
if (num_pieces > 300) num_pieces = 300;
|
|
|
|
for (int i = 0; i < num_pieces; ++i)
|
2009-11-02 02:01:07 +01:00
|
|
|
{
|
2011-02-01 10:48:28 +01:00
|
|
|
ts.handle.set_piece_deadline(i, (i+5) * 1000, torrent_handle::alert_when_available);
|
2009-11-02 02:01:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-06 19:04:07 +01:00
|
|
|
if (c == 'v' && !handles.empty())
|
2009-02-26 08:09:56 +01:00
|
|
|
{
|
2011-02-01 10:48:28 +01:00
|
|
|
torrent_status const& ts = get_active_torrent(handles);
|
|
|
|
ts.handle.scrape_tracker();
|
2009-02-26 08:09:56 +01:00
|
|
|
}
|
|
|
|
|
2010-11-06 19:04:07 +01:00
|
|
|
if (c == 'p' && !handles.empty())
|
2009-02-26 08:09:56 +01:00
|
|
|
{
|
2011-02-01 10:48:28 +01:00
|
|
|
torrent_status const& ts = get_active_torrent(handles);
|
|
|
|
if (!ts.auto_managed && ts.paused)
|
2004-03-21 03:03:37 +01:00
|
|
|
{
|
2011-02-01 10:48:28 +01:00
|
|
|
ts.handle.auto_managed(true);
|
2005-10-19 15:58:41 +02:00
|
|
|
}
|
2010-11-06 19:04:07 +01:00
|
|
|
else
|
|
|
|
{
|
2011-02-01 10:48:28 +01:00
|
|
|
ts.handle.auto_managed(false);
|
|
|
|
ts.handle.pause(torrent_handle::graceful_pause);
|
2010-11-06 19:04:07 +01:00
|
|
|
}
|
2009-02-26 08:09:56 +01:00
|
|
|
}
|
2004-03-21 03:03:37 +01:00
|
|
|
|
2010-11-06 19:04:07 +01:00
|
|
|
if (c == 'c' && !handles.empty())
|
2009-02-26 08:09:56 +01:00
|
|
|
{
|
2011-02-01 10:48:28 +01:00
|
|
|
torrent_status const& ts = get_active_torrent(handles);
|
|
|
|
ts.handle.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;
|
2010-11-29 02:33:05 +01:00
|
|
|
if (c == 'u') print_utp_stats = !print_utp_stats;
|
2009-02-26 08:09:56 +01:00
|
|
|
// 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;
|
2011-03-21 05:59:45 +01:00
|
|
|
if (c == 'y')
|
|
|
|
{
|
|
|
|
char url[2048];
|
|
|
|
puts("Enter RSS feed URL:\n");
|
|
|
|
scanf("%2048s", url);
|
|
|
|
feed_settings set;
|
|
|
|
set.url = url;
|
|
|
|
set.add_args.save_path = save_path;
|
|
|
|
feed_handle h = ses.add_feed(set);
|
|
|
|
h.update_feed();
|
|
|
|
}
|
2009-02-26 08:09:56 +01:00
|
|
|
}
|
|
|
|
if (c == 'q') break;
|
|
|
|
|
|
|
|
int terminal_width = 80;
|
2011-02-01 10:48:28 +01:00
|
|
|
int terminal_height = 50;
|
2007-07-09 02:00:35 +02:00
|
|
|
|
|
|
|
#ifndef _WIN32
|
2009-02-26 08:09:56 +01:00
|
|
|
{
|
|
|
|
winsize size;
|
2011-06-03 07:46:11 +02:00
|
|
|
int ret = ioctl(STDOUT_FILENO, TIOCGWINSZ, (char*)&size);
|
|
|
|
if (ret == 0)
|
|
|
|
{
|
|
|
|
terminal_width = size.ws_col;
|
|
|
|
terminal_height = size.ws_row;
|
|
|
|
|
|
|
|
if (terminal_width < 64)
|
|
|
|
terminal_width = 64;
|
|
|
|
if (terminal_height < 25)
|
|
|
|
terminal_height = 25;
|
|
|
|
}
|
2011-06-03 10:25:34 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
terminal_width = 190;
|
|
|
|
terminal_height = 100;
|
|
|
|
}
|
2009-02-26 08:09:56 +01:00
|
|
|
}
|
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.
|
2011-03-14 03:59:46 +01:00
|
|
|
std::deque<alert*> alerts;
|
|
|
|
ses.pop_alerts(&alerts);
|
2009-02-26 08:09:56 +01:00
|
|
|
std::string now = time_now_string();
|
2011-03-14 03:59:46 +01:00
|
|
|
for (std::deque<alert*>::iterator i = alerts.begin()
|
|
|
|
, end(alerts.end()); i != end; ++i)
|
2009-02-26 08:09:56 +01:00
|
|
|
{
|
2009-05-13 03:02:06 +02:00
|
|
|
std::string event_string;
|
2009-02-26 08:09:56 +01:00
|
|
|
|
2011-03-14 03:59:46 +01:00
|
|
|
::print_alert(*i, event_string);
|
2011-06-21 04:02:58 +02:00
|
|
|
TORRENT_TRY
|
|
|
|
{
|
|
|
|
::handle_alert(ses, *i, files, non_files);
|
|
|
|
} TORRENT_CATCH(std::exception& e) {}
|
2009-02-26 08:09:56 +01:00
|
|
|
|
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();
|
2011-03-14 03:59:46 +01:00
|
|
|
delete *i;
|
2009-02-26 08:09:56 +01:00
|
|
|
}
|
2011-03-14 03:59:46 +01:00
|
|
|
alerts.clear();
|
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
|
|
|
|
2011-07-04 07:33:06 +02:00
|
|
|
// in test mode, also quit when we loose the last peer
|
|
|
|
if (loop_limit > 1 && sess_stat.num_peers == 0 && tick > 30) break;
|
|
|
|
|
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 "
|
2010-03-20 23:00:13 +01:00
|
|
|
"[5] toggle peer rate [6] toggle failures [7] toggle send buffers [R] save resume data\n";
|
2008-07-06 14:22:56 +02:00
|
|
|
|
2011-03-21 05:59:45 +01:00
|
|
|
char const* filter_names[] = { "all", "downloading", "non-paused", "seeding", "queued", "stopped", "checking", "RSS"};
|
2011-02-21 06:24:41 +01:00
|
|
|
for (int i = 0; i < int(sizeof(filter_names)/sizeof(filter_names[0])); ++i)
|
2010-11-06 17:57:15 +01:00
|
|
|
{
|
2011-02-02 04:38:02 +01:00
|
|
|
char filter[200];
|
|
|
|
snprintf(filter, sizeof(filter), "%s[%s (%d)]%s", torrent_filter == i?esc("7"):""
|
|
|
|
, filter_names[i], counters[i], torrent_filter == i?esc("0"):"");
|
|
|
|
out += filter;
|
2010-11-06 17:57:15 +01:00
|
|
|
}
|
|
|
|
out += '\n';
|
|
|
|
|
2009-05-13 03:02:06 +02:00
|
|
|
char str[500];
|
2009-02-26 08:09:56 +01:00
|
|
|
int torrent_index = 0;
|
2011-02-01 10:48:28 +01:00
|
|
|
int lines_printed = 3;
|
2011-03-21 05:59:45 +01:00
|
|
|
|
|
|
|
if (torrent_filter == torrents_feeds)
|
|
|
|
{
|
|
|
|
for (std::vector<feed_handle>::iterator i = feeds.begin()
|
|
|
|
, end(feeds.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
if (lines_printed >= terminal_height - 15)
|
|
|
|
{
|
|
|
|
out += "...\n";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
feed_status st = i->get_feed_status();
|
|
|
|
if (st.url.size() > 70) st.url.resize(70);
|
|
|
|
snprintf(str, sizeof(str), "%-70s %c %4d (%2d) %s\n", st.url.c_str()
|
|
|
|
, st.updating? 'u' : '-'
|
|
|
|
, st.next_update
|
|
|
|
, int(st.items.size())
|
|
|
|
, st.error ? st.error.message().c_str() : "");
|
|
|
|
out += str;
|
|
|
|
++lines_printed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-01 10:48:28 +01:00
|
|
|
for (std::vector<torrent_status>::iterator i = handles.begin();
|
2009-02-26 08:09:56 +01:00
|
|
|
i != handles.end(); ++torrent_index)
|
|
|
|
{
|
2011-02-01 10:48:28 +01:00
|
|
|
if (lines_printed >= terminal_height - 15)
|
|
|
|
{
|
|
|
|
out += "...\n";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
torrent_status& s = *i;
|
|
|
|
if (!s.handle.is_valid())
|
2009-02-26 08:09:56 +01:00
|
|
|
{
|
2011-02-09 05:54:22 +01:00
|
|
|
i = handles.erase(i);
|
2009-02-26 08:09:56 +01:00
|
|
|
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
|
|
|
|
2011-02-01 10:48:28 +01:00
|
|
|
int queue_pos = s.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
|
|
|
|
2010-10-30 10:36:18 +02:00
|
|
|
if (s.paused) out += esc("34");
|
2009-05-13 03:02:06 +02:00
|
|
|
else out += esc("37");
|
2008-04-24 05:28:48 +02:00
|
|
|
|
2011-02-01 10:48:28 +01:00
|
|
|
std::string name = s.handle.name();
|
2009-05-15 18:21:36 +02:00
|
|
|
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
|
|
|
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";
|
2011-02-01 10:48:28 +01:00
|
|
|
++lines_printed;
|
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;
|
|
|
|
|
2011-08-06 22:45:21 +02:00
|
|
|
snprintf(str, sizeof(str), "%s%-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 %c%s\n"
|
|
|
|
, (!s.paused && !s.auto_managed)?"[F] ":""
|
|
|
|
, (s.paused && !s.auto_managed)?"paused":
|
|
|
|
(s.paused && s.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, s.need_save_resume?'S':' ', esc("0"));
|
|
|
|
out += str;
|
|
|
|
++lines_printed;
|
2010-01-17 22:10:28 +01:00
|
|
|
|
2011-08-06 22:45:21 +02:00
|
|
|
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
|
2008-09-20 19:42:25 +02:00
|
|
|
}
|
2011-08-06 22:45:21 +02:00
|
|
|
else if (s.current_tracker.empty())
|
2009-02-26 08:09:56 +01:00
|
|
|
{
|
2011-08-06 22:45:21 +02:00
|
|
|
progress_bar_color = "31"; // red
|
|
|
|
}
|
|
|
|
else if (sess_stat.has_incoming_connections)
|
|
|
|
{
|
|
|
|
progress_bar_color = "32"; // green
|
2009-02-26 08:09:56 +01:00
|
|
|
}
|
2011-08-06 22:45:21 +02:00
|
|
|
|
|
|
|
snprintf(str, sizeof(str), " %-10s: %s%-11"PRId64"%s Bytes %6.2f%% %s\n"
|
|
|
|
, s.sequential_download?"sequential":"progress"
|
|
|
|
, esc("32"), s.total_done, esc("0")
|
|
|
|
, s.progress_ppm / 10000.f
|
|
|
|
, progress_bar(s.progress_ppm / 1000, terminal_width - 43, progress_bar_color).c_str());
|
|
|
|
out += str;
|
2011-02-01 10:48:28 +01:00
|
|
|
++lines_printed;
|
2009-05-13 03:02:06 +02:00
|
|
|
|
2011-08-07 09:19:18 +02:00
|
|
|
if (print_piece_bar && (s.state != torrent_status::seeding || s.seed_mode))
|
2009-05-13 03:02:06 +02:00
|
|
|
{
|
|
|
|
out += " ";
|
|
|
|
out += piece_bar(s.pieces, terminal_width - 7);
|
|
|
|
out += "\n";
|
2011-02-01 10:48:28 +01:00
|
|
|
++lines_printed;
|
2011-08-07 09:19:18 +02:00
|
|
|
if (s.seed_mode)
|
|
|
|
{
|
|
|
|
out += " ";
|
|
|
|
out += piece_bar(s.verified_pieces, terminal_width - 7);
|
|
|
|
out += "\n";
|
|
|
|
++lines_printed;
|
|
|
|
}
|
2009-05-13 03:02:06 +02:00
|
|
|
}
|
|
|
|
|
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")
|
2011-07-20 07:14:25 +02:00
|
|
|
, esc("37"), int(t.hours()), int(t.minutes()), int(t.seconds()), esc("0")
|
2010-01-17 22:10:28 +01:00
|
|
|
, esc("36"), s.current_tracker.c_str(), esc("0"));
|
|
|
|
out += str;
|
2011-02-01 10:48:28 +01:00
|
|
|
++lines_printed;
|
2010-01-17 22:10:28 +01:00
|
|
|
}
|
2009-02-26 08:09:56 +01:00
|
|
|
}
|
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 "
|
2011-01-30 11:04:15 +01:00
|
|
|
"bw queues: %8d (%d) | %8d (%d) disk queues: %d | %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
|
2011-01-30 11:04:15 +01:00
|
|
|
, sess_stat.disk_write_queue
|
|
|
|
, sess_stat.disk_read_queue
|
2009-05-13 03:02:06 +02:00
|
|
|
, (cs.blocks_written - cs.writes) * 100 / cs.blocks_written
|
|
|
|
, cs.blocks_read_hit * 100 / cs.blocks_read
|
2011-02-16 07:15:17 +01:00
|
|
|
, add_suffix(boost::int64_t(cs.cache_size) * 16 * 1024).c_str()
|
|
|
|
, add_suffix(boost::int64_t(cs.read_cache_size) * 16 * 1024).c_str()
|
|
|
|
, add_suffix(boost::int64_t(cs.total_used_buffers) * 16 * 1024).c_str()
|
2009-07-23 06:38:52 +02:00
|
|
|
, cs.queued_bytes);
|
2009-05-13 03:02:06 +02:00
|
|
|
out += str;
|
|
|
|
|
2010-10-01 06:07:38 +02:00
|
|
|
snprintf(str, sizeof(str), "==== optimistic unchoke: %d unchoke counter: %d peerlist: %d ====\n"
|
|
|
|
, sess_stat.optimistic_unchoke_counter, sess_stat.unchoke_counter, sess_stat.peerlist_size);
|
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
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
2009-02-26 08:09:56 +01:00
|
|
|
if (show_dht_status)
|
|
|
|
{
|
2010-12-12 04:17:08 +01:00
|
|
|
snprintf(str, sizeof(str), "DHT nodes: %d DHT cached nodes: %d "
|
|
|
|
"total DHT size: %"PRId64" total observers: %d\n"
|
2010-11-06 08:12:57 +01:00
|
|
|
, sess_stat.dht_nodes, sess_stat.dht_node_cache, sess_stat.dht_global_nodes
|
|
|
|
, sess_stat.dht_total_allocations);
|
2009-05-13 03:02:06 +02:00
|
|
|
out += str;
|
|
|
|
|
2010-12-12 04:17:08 +01:00
|
|
|
int bucket = 0;
|
|
|
|
for (std::vector<dht_routing_bucket>::iterator i = sess_stat.dht_routing_table.begin()
|
|
|
|
, end(sess_stat.dht_routing_table.end()); i != end; ++i, ++bucket)
|
|
|
|
{
|
|
|
|
snprintf(str, sizeof(str)
|
|
|
|
, "%3d [%2d, %2d] active: %d\n"
|
|
|
|
, bucket, i->num_nodes, i->num_replacements, i->last_active);
|
|
|
|
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)
|
2010-12-12 04:28:06 +01:00
|
|
|
, " %10s [limit: %2d] "
|
|
|
|
"in-flight: %-2d "
|
|
|
|
"left: %-3d "
|
|
|
|
"1st-timeout: %-2d "
|
|
|
|
"timeouts: %-2d "
|
|
|
|
"responses: %-2d "
|
|
|
|
"last_sent: %-2d\n"
|
|
|
|
, i->type
|
|
|
|
, i->branch_factor
|
|
|
|
, i->outstanding_requests
|
|
|
|
, i->nodes_left
|
|
|
|
, i->first_timeout
|
|
|
|
, i->timeouts
|
|
|
|
, i->responses
|
|
|
|
, i->last_sent);
|
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
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
if (print_utp_stats)
|
|
|
|
{
|
|
|
|
snprintf(str, sizeof(str), "uTP idle: %d syn: %d est: %d fin: %d wait: %d\n"
|
|
|
|
, sess_stat.utp_stats.num_idle, sess_stat.utp_stats.num_syn_sent
|
|
|
|
, sess_stat.utp_stats.num_connected, sess_stat.utp_stats.num_fin_sent
|
|
|
|
, sess_stat.utp_stats.num_close_wait);
|
|
|
|
out += str;
|
|
|
|
}
|
|
|
|
|
2011-02-01 10:48:28 +01:00
|
|
|
torrent_status const* st = 0;
|
|
|
|
if (!handles.empty()) st = &get_active_torrent(handles);
|
|
|
|
if (st && st->handle.is_valid())
|
2009-02-26 08:09:56 +01:00
|
|
|
{
|
2011-02-01 10:48:28 +01:00
|
|
|
torrent_handle h = st->handle;
|
|
|
|
torrent_status const& s = *st;
|
2009-02-26 08:09:56 +01:00
|
|
|
|
|
|
|
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
|
|
|
{
|
2010-07-15 07:16:06 +02:00
|
|
|
snprintf(str, sizeof(str), "%2d %-55s fails: %-3d (%-3d) %s %s %5d \"%s\" %s\n"
|
2010-05-04 18:49:26 +02:00
|
|
|
, i->tier, i->url.c_str(), i->fails, i->fail_limit, i->verified?"OK ":"- "
|
2009-05-14 21:49:33 +02:00
|
|
|
, i->updating?"updating"
|
2010-04-27 20:22:59 +02:00
|
|
|
:!i->will_announce(now)?""
|
2010-04-13 06:37:39 +02:00
|
|
|
:to_string(total_seconds(i->next_announce - now), 8).c_str()
|
2010-07-15 07:16:06 +02:00
|
|
|
, i->min_announce > now ? total_seconds(i->min_announce - now) : 0
|
2010-04-13 06:37:39 +02:00
|
|
|
, i->last_error ? i->last_error.message().c_str() : ""
|
|
|
|
, i->message.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)
|
|
|
|
{
|
2011-07-10 10:57:27 +02:00
|
|
|
std::vector<cached_piece_info> pieces;
|
|
|
|
ses.get_cache_info(h.info_hash(), pieces);
|
2009-05-14 21:49:33 +02:00
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
h.get_download_queue(queue);
|
2011-07-10 10:57:27 +02:00
|
|
|
|
2010-07-22 18:49:40 +02:00
|
|
|
std::sort(queue.begin(), queue.end(), boost::bind(&partial_piece_info::piece_index, _1)
|
|
|
|
< boost::bind(&partial_piece_info::piece_index, _2));
|
2008-02-08 11:22:05 +01:00
|
|
|
|
2011-07-10 10:57:27 +02:00
|
|
|
std::sort(pieces.begin(), pieces.end(), boost::bind(&cached_piece_info::last_use, _1)
|
|
|
|
> boost::bind(&cached_piece_info::last_use, _2));
|
2008-02-08 11:22:05 +01:00
|
|
|
|
2011-07-10 10:57:27 +02:00
|
|
|
for (std::vector<cached_piece_info>::iterator i = pieces.begin();
|
|
|
|
i != pieces.end(); ++i)
|
2009-02-26 08:09:56 +01:00
|
|
|
{
|
2011-07-10 10:57:27 +02:00
|
|
|
partial_piece_info* pp = 0;
|
|
|
|
partial_piece_info tmp;
|
|
|
|
tmp.piece_index = i->piece;
|
|
|
|
std::vector<partial_piece_info>::iterator ppi
|
|
|
|
= std::lower_bound(queue.begin(), queue.end(), tmp
|
|
|
|
, boost::bind(&partial_piece_info::piece_index, _1)
|
|
|
|
< boost::bind(&partial_piece_info::piece_index, _2));
|
|
|
|
if (ppi != queue.end() && ppi->piece_index == i->piece) pp = &*ppi;
|
|
|
|
|
|
|
|
print_piece(pp, &*i, peers, out);
|
|
|
|
|
|
|
|
if (pp) queue.erase(ppi);
|
2004-07-24 13:54:17 +02:00
|
|
|
}
|
2006-06-11 15:48:39 +02:00
|
|
|
|
2011-07-10 10:57:27 +02:00
|
|
|
for (std::vector<partial_piece_info>::iterator i = queue.begin()
|
|
|
|
, end(queue.end()); i != end; ++i)
|
2006-06-11 15:48:39 +02:00
|
|
|
{
|
2011-07-10 10:57:27 +02:00
|
|
|
print_piece(&*i, 0, peers, out);
|
2006-06-11 15:48:39 +02:00
|
|
|
}
|
2011-07-10 10:57:27 +02:00
|
|
|
snprintf(str, sizeof(str), "%s %s: read cache %s %s: downloading %s %s: cached %s %s: flushed\n"
|
|
|
|
, esc("34;7"), esc("0") // read cache
|
|
|
|
, esc("33;7"), esc("0") // downloading
|
|
|
|
, esc("36;7"), esc("0") // cached
|
|
|
|
, esc("32;7"), esc("0")); // flushed
|
2011-07-10 11:07:40 +02:00
|
|
|
out += str;
|
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
|
2010-10-30 19:23:30 +02:00
|
|
|
&& s.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()
|
2010-11-25 00:49:22 +01:00
|
|
|
, filename(info.files().file_path(info.file_at(i))).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());
|
2011-06-02 07:44:46 +02:00
|
|
|
fflush(stdout);
|
2009-02-26 08:09:56 +01:00
|
|
|
|
|
|
|
if (!monitor_dir.empty()
|
|
|
|
&& next_dir_scan < time_now())
|
|
|
|
{
|
2011-02-09 05:59:43 +01:00
|
|
|
scan_dir(monitor_dir, ses, files, non_files, 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
|
2011-01-24 01:11:13 +01:00
|
|
|
int num_paused = 0;
|
|
|
|
int num_failed = 0;
|
|
|
|
|
2010-01-31 22:30:43 +01:00
|
|
|
ses.pause();
|
2011-01-24 01:11:13 +01:00
|
|
|
printf("saving resume data\n");
|
2011-02-01 10:48:28 +01:00
|
|
|
std::vector<torrent_status> temp;
|
|
|
|
ses.get_torrent_status(&temp, &yes, 0);
|
|
|
|
for (std::vector<torrent_status>::iterator i = temp.begin();
|
|
|
|
i != temp.end(); ++i)
|
2010-01-31 22:30:43 +01:00
|
|
|
{
|
2011-02-01 10:48:28 +01:00
|
|
|
torrent_status& st = *i;
|
2011-07-04 07:33:06 +02:00
|
|
|
if (!st.handle.is_valid())
|
|
|
|
{
|
|
|
|
printf(" skipping, invalid handle\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!st.has_metadata)
|
|
|
|
{
|
|
|
|
printf(" skipping %s, no metadata\n", st.handle.name().c_str());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!st.need_save_resume)
|
|
|
|
{
|
|
|
|
printf(" skipping %s, resume file up-to-date\n", st.handle.name().c_str());
|
|
|
|
continue;
|
|
|
|
}
|
2010-01-31 22:30:43 +01:00
|
|
|
|
|
|
|
// save_resume_data will generate an alert when it's done
|
2011-02-01 10:48:28 +01:00
|
|
|
st.handle.save_resume_data();
|
2011-07-04 07:33:06 +02:00
|
|
|
++num_outstanding_resume_data;
|
|
|
|
printf("\r%d ", num_outstanding_resume_data);
|
2010-01-31 22:30:43 +01:00
|
|
|
}
|
2011-07-04 07:33:06 +02:00
|
|
|
printf("\nwaiting for resume data [%d]\n", num_outstanding_resume_data);
|
2010-01-31 22:30:43 +01:00
|
|
|
|
2011-07-04 07:33:06 +02:00
|
|
|
while (num_outstanding_resume_data > 0)
|
2010-01-31 22:30:43 +01:00
|
|
|
{
|
2011-01-24 01:11:13 +01:00
|
|
|
alert const* a = ses.wait_for_alert(seconds(10));
|
|
|
|
if (a == 0) continue;
|
2010-01-31 22:30:43 +01:00
|
|
|
|
2011-03-14 03:59:46 +01:00
|
|
|
std::deque<alert*> alerts;
|
|
|
|
ses.pop_alerts(&alerts);
|
|
|
|
std::string now = time_now_string();
|
|
|
|
for (std::deque<alert*>::iterator i = alerts.begin()
|
|
|
|
, end(alerts.end()); i != end; ++i)
|
2011-01-24 01:11:13 +01:00
|
|
|
{
|
2011-03-14 03:59:46 +01:00
|
|
|
// make sure to delete each alert
|
|
|
|
std::auto_ptr<alert> a(*i);
|
2010-01-31 22:30:43 +01:00
|
|
|
|
2011-03-14 03:59:46 +01:00
|
|
|
torrent_paused_alert const* tp = alert_cast<torrent_paused_alert>(*i);
|
|
|
|
if (tp)
|
|
|
|
{
|
|
|
|
++num_paused;
|
|
|
|
printf("\rleft: %d failed: %d pause: %d "
|
2011-07-04 07:33:06 +02:00
|
|
|
, num_outstanding_resume_data, num_failed, num_paused);
|
2011-03-14 03:59:46 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (alert_cast<save_resume_data_failed_alert>(*i))
|
|
|
|
{
|
|
|
|
++num_failed;
|
2011-07-04 07:33:06 +02:00
|
|
|
--num_outstanding_resume_data;
|
2011-03-14 03:59:46 +01:00
|
|
|
printf("\rleft: %d failed: %d pause: %d "
|
2011-07-04 07:33:06 +02:00
|
|
|
, num_outstanding_resume_data, num_failed, num_paused);
|
2011-03-14 03:59:46 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-07-04 07:33:06 +02:00
|
|
|
save_resume_data_alert const* rd = alert_cast<save_resume_data_alert>(*i);
|
2011-03-14 03:59:46 +01:00
|
|
|
if (!rd) continue;
|
2011-07-04 07:33:06 +02:00
|
|
|
--num_outstanding_resume_data;
|
2011-01-24 01:11:13 +01:00
|
|
|
printf("\rleft: %d failed: %d pause: %d "
|
2011-07-04 07:33:06 +02:00
|
|
|
, num_outstanding_resume_data, num_failed, num_paused);
|
2010-01-31 22:30:43 +01:00
|
|
|
|
2011-03-14 03:59:46 +01:00
|
|
|
if (!rd->resume_data) continue;
|
2010-01-31 22:30:43 +01:00
|
|
|
|
2011-03-14 03:59:46 +01:00
|
|
|
torrent_handle h = rd->handle;
|
|
|
|
std::vector<char> out;
|
|
|
|
bencode(std::back_inserter(out), *rd->resume_data);
|
|
|
|
save_file(combine_path(h.save_path(), ".resume/" + to_hex(h.info_hash().to_string()) + ".resume"), out);
|
|
|
|
}
|
2010-01-31 22:30:43 +01:00
|
|
|
}
|
2011-03-14 03:59:46 +01:00
|
|
|
|
2011-02-20 23:49:55 +01:00
|
|
|
if (g_log_file) fclose(g_log_file);
|
2011-01-24 01:11:13 +01:00
|
|
|
printf("\nsaving session state\n");
|
2010-03-04 17:42:39 +01:00
|
|
|
{
|
|
|
|
entry session_state;
|
|
|
|
ses.save_state(session_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
|
|
|
|
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
|
|
|
|