2003-10-23 01:00:57 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2003, Arvid Norberg
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <iterator>
|
|
|
|
#include <exception>
|
|
|
|
|
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
|
|
|
|
|
2006-06-29 01:27:44 +02:00
|
|
|
#include <boost/filesystem/operations.hpp>
|
2007-06-10 22:46:09 +02:00
|
|
|
#include <boost/filesystem/convenience.hpp>
|
2004-01-13 04:08:59 +01:00
|
|
|
#include <boost/filesystem/fstream.hpp>
|
2004-02-18 01:08:20 +01:00
|
|
|
#include <boost/filesystem/exception.hpp>
|
2004-01-17 21:04:19 +01:00
|
|
|
#include <boost/bind.hpp>
|
2005-10-16 11:15:46 +02:00
|
|
|
#include <boost/program_options.hpp>
|
|
|
|
#include <boost/regex.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"
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2005-08-15 00:04:58 +02:00
|
|
|
using boost::bind;
|
|
|
|
|
2004-09-12 15:53:00 +02:00
|
|
|
#ifdef _WIN32
|
2003-11-09 19:17:09 +01:00
|
|
|
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
# define for if (false) {} else for
|
|
|
|
#endif
|
|
|
|
|
2003-11-02 22:06:50 +01:00
|
|
|
#include <windows.h>
|
2003-10-30 00:28:09 +01:00
|
|
|
#include <conio.h>
|
|
|
|
|
|
|
|
bool sleep_and_input(char* c)
|
|
|
|
{
|
2004-01-17 21:04:19 +01:00
|
|
|
Sleep(500);
|
2007-03-02 02:16:59 +01:00
|
|
|
if (_kbhit())
|
2003-10-30 00:28:09 +01:00
|
|
|
{
|
2007-03-02 02:16:59 +01:00
|
|
|
*c = _getch();
|
2003-10-30 00:28:09 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
bool sleep_and_input(char* c)
|
|
|
|
{
|
|
|
|
// sets the terminal to single-character mode
|
|
|
|
// and resets when destructed
|
|
|
|
set_keypress s;
|
|
|
|
|
|
|
|
fd_set set;
|
|
|
|
FD_ZERO(&set);
|
|
|
|
FD_SET(0, &set);
|
2004-01-17 21:04:19 +01:00
|
|
|
timeval tv = {0, 500000};
|
2003-10-31 12:17:45 +01:00
|
|
|
if (select(1, &set, 0, 0, &tv) > 0)
|
|
|
|
{
|
|
|
|
*c = getc(stdin);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-10-16 11:15:46 +02:00
|
|
|
void clear_home()
|
2003-11-05 00:27:06 +01:00
|
|
|
{
|
2005-10-16 11:15:46 +02:00
|
|
|
std::cout << "\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-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;
|
|
|
|
bool sequential_download = false;
|
|
|
|
|
|
|
|
bool print_ip = true;
|
|
|
|
bool print_as = false;
|
|
|
|
bool print_timers = false;
|
|
|
|
bool print_block = false;
|
|
|
|
bool print_peer_rate = false;
|
|
|
|
bool print_fails = false;
|
|
|
|
bool print_send_bufs = true;
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
std::stringstream s;
|
|
|
|
s.flags(std::ios_base::right);
|
|
|
|
s.width(width);
|
2007-05-24 22:23:14 +02:00
|
|
|
s.fill(' ');
|
2007-04-15 19:44:06 +02:00
|
|
|
s << v;
|
|
|
|
return s.str();
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
int size = std::sprintf(&ret[0], "%*.*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
|
|
|
}
|
|
|
|
|
2005-05-25 12:01:01 +02:00
|
|
|
std::string pos_to_string(float v, int width, int precision = 4)
|
2004-01-12 04:05:10 +01:00
|
|
|
{
|
|
|
|
std::stringstream s;
|
2005-05-25 12:01:01 +02:00
|
|
|
s.precision(precision);
|
2004-01-12 04:05:10 +01:00
|
|
|
s.flags(std::ios_base::right);
|
|
|
|
s.width(width);
|
|
|
|
s.fill(' ');
|
|
|
|
s << fabs(v);
|
|
|
|
return s.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ratio(float a, float b)
|
|
|
|
{
|
|
|
|
std::stringstream s;
|
|
|
|
if (a > b)
|
|
|
|
{
|
|
|
|
if (b < 0.001f) s << " inf:1";
|
|
|
|
else s << pos_to_string(a/b, 4) << ":1";
|
|
|
|
}
|
|
|
|
else if (a < b)
|
|
|
|
{
|
|
|
|
if (a < 0.001f) s << " 1:inf";
|
|
|
|
else s << "1:" << pos_to_string(b/a, 4);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
s << " 1:1";
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.str();
|
|
|
|
}
|
|
|
|
|
2007-05-23 09:39:21 +02:00
|
|
|
std::string const& add_suffix(float val)
|
2003-10-31 05:02:51 +01:00
|
|
|
{
|
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;
|
2007-05-23 09:39:21 +02:00
|
|
|
if (fabs(val) < 1000.f)
|
|
|
|
{
|
2008-02-28 04:24:38 +01:00
|
|
|
std::string& ret = to_string(val, 4);
|
2007-05-23 09:39:21 +02:00
|
|
|
ret += prefix[i];
|
|
|
|
return ret;
|
|
|
|
}
|
2003-10-31 05:02:51 +01:00
|
|
|
}
|
2008-02-28 04:24:38 +01:00
|
|
|
std::string& ret = to_string(val, 4);
|
2007-05-23 09:39:21 +02:00
|
|
|
ret += "PB";
|
|
|
|
return ret;
|
2003-10-31 05:02:51 +01:00
|
|
|
}
|
|
|
|
|
2007-07-09 02:00:35 +02:00
|
|
|
std::string const& piece_bar(std::vector<bool> const& p, int width)
|
|
|
|
{
|
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 += "[";
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2007-05-23 09:39:21 +02:00
|
|
|
std::string const& progress_bar(float 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
|
|
|
|
2005-05-31 00:50:54 +02:00
|
|
|
int progress_chars = static_cast<int>(progress * width + .5f);
|
2005-08-15 00:04:58 +02:00
|
|
|
bar = esc(code);
|
2005-05-30 19:43:03 +02:00
|
|
|
std::fill_n(std::back_inserter(bar), progress_chars, '#');
|
2005-08-15 00:04:58 +02:00
|
|
|
bar += esc("0");
|
2005-05-30 19:43:03 +02:00
|
|
|
std::fill_n(std::back_inserter(bar), width - progress_chars, '-');
|
2007-05-23 09:39:21 +02:00
|
|
|
return bar;
|
2004-01-15 17:45:34 +01:00
|
|
|
}
|
|
|
|
|
2007-01-10 16:02:25 +01:00
|
|
|
int peer_index(libtorrent::tcp::endpoint addr, std::vector<libtorrent::peer_info> const& peers)
|
2005-08-15 00:04:58 +02:00
|
|
|
{
|
|
|
|
using namespace libtorrent;
|
|
|
|
std::vector<peer_info>::const_iterator i = std::find_if(peers.begin()
|
2007-04-29 22:49:30 +02:00
|
|
|
, peers.end(), bind(&peer_info::ip, _1) == addr);
|
2007-01-10 16:02:25 +01:00
|
|
|
if (i == peers.end()) return -1;
|
2005-08-15 00:04:58 +02:00
|
|
|
|
2007-01-10 16:02:25 +01:00
|
|
|
return i - peers.begin();
|
2005-08-15 00:04:58 +02:00
|
|
|
}
|
|
|
|
|
2004-07-24 13:54:17 +02:00
|
|
|
void print_peer_info(std::ostream& out, std::vector<libtorrent::peer_info> const& peers)
|
|
|
|
{
|
|
|
|
using namespace libtorrent;
|
2008-04-05 06:53:22 +02:00
|
|
|
if (print_ip) out << "IP ";
|
|
|
|
#ifndef TORRENT_DISABLE_GEO_IP
|
|
|
|
if (print_as) out << "AS ";
|
|
|
|
#endif
|
|
|
|
out << "down (total | peak ) up (total | peak ) sent-req recv flags source ";
|
|
|
|
if (print_fails) out << "fail hshf ";
|
2008-04-10 12:03:23 +02:00
|
|
|
if (print_send_bufs) out << "sndb quota rcvb ";
|
2008-04-05 06:53:22 +02:00
|
|
|
if (print_timers) out << "inactive wait ";
|
|
|
|
out << "disk rtt ";
|
|
|
|
if (print_block) out << "block-progress ";
|
2007-05-24 22:23:14 +02:00
|
|
|
#ifndef TORRENT_DISABLE_RESOLVE_COUNTRIES
|
2008-04-05 06:53:22 +02:00
|
|
|
out << "country ";
|
2007-05-24 22:23:14 +02:00
|
|
|
#endif
|
2008-04-05 06:53:22 +02:00
|
|
|
if (print_peer_rate) out << "peer-rate ";
|
|
|
|
out << "client \n";
|
2004-07-24 13:54:17 +02:00
|
|
|
|
|
|
|
for (std::vector<peer_info>::const_iterator i = peers.begin();
|
2005-05-25 12:01:01 +02:00
|
|
|
i != peers.end(); ++i)
|
2004-07-24 13:54:17 +02:00
|
|
|
{
|
2007-05-05 02:29:33 +02:00
|
|
|
if (i->flags & (peer_info::handshake))
|
2007-03-15 23:03:56 +01:00
|
|
|
continue;
|
|
|
|
|
2004-07-24 13:54:17 +02:00
|
|
|
out.fill(' ');
|
2008-04-05 06:53:22 +02:00
|
|
|
if (print_ip)
|
|
|
|
{
|
|
|
|
std::stringstream ip;
|
|
|
|
ip << i->ip.address().to_string() << ":" << i->ip.port();
|
|
|
|
out.width(22);
|
|
|
|
out << ip.str() << " ";
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef TORRENT_DISABLE_GEO_IP
|
|
|
|
if (print_as)
|
|
|
|
{
|
|
|
|
std::string as_name = i->inet_as_name;
|
|
|
|
if (as_name.size() > 42) as_name.resize(42);
|
|
|
|
out.width(42);
|
|
|
|
out << as_name << " ";
|
|
|
|
}
|
|
|
|
#endif
|
2004-07-24 13:54:17 +02:00
|
|
|
out.width(2);
|
2007-03-02 07:49:00 +01:00
|
|
|
out << esc("32") << (i->down_speed > 0 ? add_suffix(i->down_speed) + "/s " : " ")
|
2008-04-05 06:53:22 +02:00
|
|
|
<< "(" << (i->total_download > 0 ? add_suffix(i->total_download) : " ") << "|"
|
|
|
|
<< (i->download_rate_peak > 0 ? add_suffix(i->download_rate_peak) + "/s" : " ") << ") " << esc("0")
|
2007-03-02 07:49:00 +01:00
|
|
|
<< esc("31") << (i->up_speed > 0 ? add_suffix(i->up_speed) + "/s ": " ")
|
2008-04-05 06:53:22 +02:00
|
|
|
<< "(" << (i->total_upload > 0 ? add_suffix(i->total_upload) : " ") << "|"
|
|
|
|
<< (i->upload_rate_peak > 0 ? add_suffix(i->upload_rate_peak) + "/s" : " ") << ") " << esc("0")
|
2007-09-10 10:07:18 +02:00
|
|
|
<< to_string(i->download_queue_length, 3) << " ("
|
|
|
|
<< to_string(i->target_dl_queue_length, 3) << ") "
|
2007-05-25 21:42:10 +02:00
|
|
|
<< to_string(i->upload_queue_length, 3) << " "
|
2005-10-16 11:15:46 +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':'.')
|
2007-05-25 21:42:10 +02:00
|
|
|
<< ((i->flags & peer_info::local_connection)?'l':'r')
|
|
|
|
<< ((i->flags & peer_info::seed)?'s':'.')
|
|
|
|
<< ((i->flags & peer_info::on_parole)?'p':'.')
|
2007-08-22 07:02:45 +02:00
|
|
|
<< ((i->flags & peer_info::optimistic_unchoke)?'O':'.')
|
2008-01-14 00:46:43 +01:00
|
|
|
<< ((i->read_state == peer_info::bw_torrent)?'t':
|
|
|
|
(i->read_state == peer_info::bw_global)?'r':
|
|
|
|
(i->read_state == peer_info::bw_network)?'R':'.')
|
|
|
|
<< ((i->write_state == peer_info::bw_torrent)?'t':
|
|
|
|
(i->write_state == peer_info::bw_global)?'w':
|
|
|
|
(i->write_state == peer_info::bw_network)?'W':'.')
|
2007-06-06 02:41:20 +02:00
|
|
|
#ifndef TORRENT_DISABLE_ENCRYPTION
|
|
|
|
<< ((i->flags & peer_info::rc4_encrypted)?'E':
|
|
|
|
(i->flags & peer_info::plaintext_encrypted)?'e':'.')
|
2007-06-07 02:05:18 +02:00
|
|
|
#else
|
2008-01-10 23:13:23 +01:00
|
|
|
<< ".."
|
2007-06-06 02:41:20 +02:00
|
|
|
#endif
|
|
|
|
<< " "
|
2007-04-10 23:23:13 +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":"_")
|
2008-04-05 06:53:22 +02:00
|
|
|
<< ((i->source & peer_info::resume_data)?"R":"_") << " ";
|
|
|
|
if (print_fails)
|
2004-07-24 13:54:17 +02:00
|
|
|
{
|
2008-04-05 06:53:22 +02:00
|
|
|
out << to_string(i->failcount, 3) << " "
|
|
|
|
<< to_string(i->num_hashfails, 3) << " ";
|
2004-07-24 13:54:17 +02:00
|
|
|
}
|
2008-04-05 06:53:22 +02:00
|
|
|
if (print_send_bufs)
|
|
|
|
{
|
|
|
|
out << to_string(i->used_send_buffer, 6) << " ("<< add_suffix(i->send_buffer_size) << ") "
|
2008-04-10 12:03:23 +02:00
|
|
|
<< to_string(i->send_quota, 5) << " "
|
|
|
|
<< to_string(i->used_receive_buffer, 6) << " ("<< add_suffix(i->receive_buffer_size) << ") ";
|
2008-04-05 06:53:22 +02:00
|
|
|
}
|
|
|
|
if (print_timers)
|
2005-05-30 19:43:03 +02:00
|
|
|
{
|
2008-04-05 06:53:22 +02:00
|
|
|
out << to_string(total_seconds(i->last_active), 8) << " "
|
|
|
|
<< to_string(total_seconds(i->last_request), 4) << " ";
|
|
|
|
}
|
|
|
|
out << add_suffix(i->pending_disk_bytes) << " "
|
|
|
|
<< to_string(i->rtt, 4) << " ";
|
|
|
|
|
|
|
|
if (print_block)
|
|
|
|
{
|
|
|
|
if (i->downloading_piece_index >= 0)
|
|
|
|
{
|
|
|
|
out << progress_bar(
|
|
|
|
i->downloading_progress / float(i->downloading_total), 14);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
out << progress_bar(0.f, 14);
|
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
out << " ..";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
out << " " << i->country[0] << i->country[1];
|
|
|
|
}
|
2007-05-24 22:23:14 +02:00
|
|
|
#endif
|
2008-04-05 06:53:22 +02:00
|
|
|
if (print_peer_rate) out << " " << (i->remote_dl_rate > 0 ? add_suffix(i->remote_dl_rate) + "/s ": " ");
|
|
|
|
out << " ";
|
2007-06-07 02:05:18 +02:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
if (i->flags & peer_info::handshake)
|
|
|
|
{
|
|
|
|
out << esc("31") << " waiting for handshake" << esc("0") << "\n";
|
|
|
|
}
|
|
|
|
else if (i->flags & peer_info::connecting)
|
2005-11-02 17:28:39 +01:00
|
|
|
{
|
|
|
|
out << esc("31") << " connecting to peer" << esc("0") << "\n";
|
|
|
|
}
|
|
|
|
else if (i->flags & peer_info::queued)
|
|
|
|
{
|
|
|
|
out << esc("33") << " queued" << esc("0") << "\n";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
out << " " << i->client << "\n";
|
2005-11-02 17:28:39 +01:00
|
|
|
}
|
2004-07-24 13:54:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-19 02:50:38 +02:00
|
|
|
typedef std::multimap<std::string, libtorrent::torrent_handle> handles_t;
|
|
|
|
|
|
|
|
using boost::bind;
|
|
|
|
using boost::filesystem::path;
|
|
|
|
using boost::filesystem::exists;
|
|
|
|
using boost::filesystem::directory_iterator;
|
|
|
|
using boost::filesystem::extension;
|
|
|
|
|
|
|
|
|
|
|
|
// monitored_dir is true if this torrent is added because
|
|
|
|
// it was found in the directory that is monitored. If it
|
|
|
|
// is, it should be remembered so that it can be removed
|
|
|
|
// if it's no longer in that directory.
|
2005-10-16 11:15:46 +02:00
|
|
|
void add_torrent(libtorrent::session& ses
|
2005-10-19 02:50:38 +02:00
|
|
|
, handles_t& handles
|
|
|
|
, std::string const& torrent
|
2005-10-16 11:15:46 +02:00
|
|
|
, float preferred_ratio
|
2005-10-17 15:45:53 +02:00
|
|
|
, bool compact_mode
|
2005-10-19 02:50:38 +02:00
|
|
|
, path 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
|
|
|
|
, int torrent_download_limit) try
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
|
|
|
using namespace libtorrent;
|
|
|
|
|
2005-10-19 02:50:38 +02:00
|
|
|
std::ifstream in(torrent.c_str(), std::ios_base::binary);
|
2005-10-16 11:15:46 +02:00
|
|
|
in.unsetf(std::ios_base::skipws);
|
|
|
|
entry e = bdecode(std::istream_iterator<char>(in), std::istream_iterator<char>());
|
2007-12-16 00:35:25 +01:00
|
|
|
boost::intrusive_ptr<torrent_info> t(new torrent_info(e));
|
2005-10-16 18:58:41 +02:00
|
|
|
|
2007-12-16 00:35:25 +01:00
|
|
|
std::cout << t->name() << "\n";
|
2005-10-16 11:15:46 +02:00
|
|
|
|
|
|
|
entry resume_data;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
std::stringstream s;
|
2007-12-16 00:35:25 +01:00
|
|
|
s << t->name() << ".fastresume";
|
2005-10-16 11:15:46 +02:00
|
|
|
boost::filesystem::ifstream resume_file(save_path / s.str(), std::ios_base::binary);
|
|
|
|
resume_file.unsetf(std::ios_base::skipws);
|
|
|
|
resume_data = bdecode(
|
2005-10-19 02:50:38 +02:00
|
|
|
std::istream_iterator<char>(resume_file)
|
|
|
|
, std::istream_iterator<char>());
|
2005-10-16 11:15:46 +02:00
|
|
|
}
|
|
|
|
catch (invalid_encoding&) {}
|
|
|
|
catch (boost::filesystem::filesystem_error&) {}
|
|
|
|
|
2008-04-24 05:28:48 +02:00
|
|
|
add_torrent_params p;
|
|
|
|
p.ti = t;
|
|
|
|
p.save_path = save_path;
|
|
|
|
p.resume_data = &resume_data;
|
|
|
|
p.storage_mode = compact_mode ? storage_mode_compact : storage_mode_sparse;
|
|
|
|
p.paused = true;
|
|
|
|
p.duplicate_is_error = false;
|
|
|
|
p.auto_managed = true;
|
|
|
|
torrent_handle h = ses.add_torrent(p);
|
|
|
|
|
2005-10-19 02:50:38 +02:00
|
|
|
handles.insert(std::make_pair(
|
|
|
|
monitored_dir?std::string(torrent):std::string(), h));
|
|
|
|
|
2008-01-02 19:22:34 +01:00
|
|
|
h.set_max_connections(50);
|
2005-10-19 02:50:38 +02:00
|
|
|
h.set_max_uploads(-1);
|
|
|
|
h.set_ratio(preferred_ratio);
|
2008-01-02 19:22:34 +01:00
|
|
|
h.set_upload_limit(torrent_upload_limit);
|
2008-01-11 00:12:58 +01:00
|
|
|
h.set_download_limit(torrent_download_limit);
|
2007-05-24 22:23:14 +02:00
|
|
|
#ifndef TORRENT_DISABLE_RESOLVE_COUNTRIES
|
2007-01-29 08:39:33 +01:00
|
|
|
h.resolve_countries(true);
|
2007-05-24 22:23:14 +02:00
|
|
|
#endif
|
2005-10-19 02:50:38 +02:00
|
|
|
}
|
2006-11-02 12:47:26 +01:00
|
|
|
catch (std::exception&) {};
|
2005-10-19 02:50:38 +02:00
|
|
|
|
|
|
|
void scan_dir(path const& dir_path
|
|
|
|
, libtorrent::session& ses
|
|
|
|
, handles_t& handles
|
|
|
|
, float preferred_ratio
|
|
|
|
, bool compact_mode
|
2008-01-02 19:22:34 +01:00
|
|
|
, path 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;
|
|
|
|
|
|
|
|
for (directory_iterator i(dir_path), end; i != end; ++i)
|
|
|
|
{
|
|
|
|
if (extension(*i) != ".torrent") continue;
|
|
|
|
std::string file = i->string();
|
2005-10-16 18:58:41 +02:00
|
|
|
|
2005-10-19 02:50:38 +02:00
|
|
|
handles_t::iterator k = handles.find(file);
|
|
|
|
if (k != handles.end())
|
|
|
|
{
|
|
|
|
valid.insert(file);
|
|
|
|
continue;
|
|
|
|
}
|
2005-10-16 18:58:41 +02:00
|
|
|
|
2005-10-19 02:50:38 +02:00
|
|
|
// the file has been added to the dir, start
|
|
|
|
// downloading it.
|
2007-12-18 07:04:54 +01:00
|
|
|
add_torrent(ses, handles, file, preferred_ratio, compact_mode
|
2008-01-11 00:12:58 +01:00
|
|
|
, save_path, true, torrent_upload_limit, torrent_download_limit);
|
2005-10-19 02:50:38 +02:00
|
|
|
valid.insert(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove the torrents that are no longer in the directory
|
|
|
|
|
2007-09-27 18:14:24 +02:00
|
|
|
for (handles_t::iterator i = handles.begin(); !handles.empty() && i != handles.end();)
|
2005-10-19 02:50:38 +02:00
|
|
|
{
|
2007-09-27 18:14:24 +02:00
|
|
|
if (i->first.empty() || valid.find(i->first) != valid.end())
|
|
|
|
{
|
|
|
|
++i;
|
|
|
|
continue;
|
|
|
|
}
|
2005-10-16 18:58:41 +02:00
|
|
|
|
2005-10-19 02:50:38 +02:00
|
|
|
torrent_handle& h = i->second;
|
|
|
|
if (!h.is_valid())
|
|
|
|
{
|
2007-10-02 20:52:27 +02:00
|
|
|
handles.erase(i++);
|
2005-10-19 02:50:38 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
h.pause();
|
2008-04-13 20:54:36 +02:00
|
|
|
// the alert handler for save_resume_data_alert
|
|
|
|
// will save it to disk and remove the torrent
|
|
|
|
h.save_resume_data();
|
|
|
|
|
2007-10-02 20:52:27 +02:00
|
|
|
handles.erase(i++);
|
2005-10-19 02:50:38 +02:00
|
|
|
}
|
2005-10-16 11:15:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int ac, char* av[])
|
|
|
|
{
|
2007-04-23 19:14:40 +02:00
|
|
|
#if BOOST_VERSION < 103400
|
|
|
|
using boost::filesystem::no_check;
|
2005-10-19 02:50:38 +02:00
|
|
|
path::default_name_check(no_check);
|
2007-04-23 19:14:40 +02:00
|
|
|
#endif
|
2005-10-19 02:50:38 +02:00
|
|
|
|
2005-10-16 11:15:46 +02:00
|
|
|
int listen_port;
|
|
|
|
float preferred_ratio;
|
|
|
|
int download_limit;
|
|
|
|
int upload_limit;
|
2008-01-02 19:22:34 +01:00
|
|
|
int torrent_upload_limit;
|
2008-01-11 00:12:58 +01:00
|
|
|
int torrent_download_limit;
|
2007-01-07 19:01:12 +01:00
|
|
|
int upload_slots_limit;
|
2005-11-02 17:28:39 +01:00
|
|
|
int half_open_limit;
|
2005-10-16 11:15:46 +02:00
|
|
|
std::string save_path_str;
|
|
|
|
std::string log_level;
|
|
|
|
std::string ip_filter_file;
|
2005-10-17 15:45:53 +02:00
|
|
|
std::string allocation_mode;
|
2005-10-19 02:50:38 +02:00
|
|
|
std::string in_monitor_dir;
|
2006-01-06 21:20:20 +01:00
|
|
|
std::string bind_to_interface;
|
2006-07-27 20:07:51 +02:00
|
|
|
std::string proxy;
|
|
|
|
std::string proxy_login;
|
2007-04-25 20:26:35 +02:00
|
|
|
std::string proxy_type;
|
2005-10-19 02:50:38 +02:00
|
|
|
int poll_interval;
|
2007-11-27 04:37:47 +01:00
|
|
|
int wait_retry;
|
2008-02-28 08:34:07 +01:00
|
|
|
int bind_port_start = 0;
|
|
|
|
int bind_port_end = 0;
|
2005-10-16 11:15:46 +02:00
|
|
|
|
|
|
|
namespace po = boost::program_options;
|
2005-10-19 02:50:38 +02:00
|
|
|
try
|
|
|
|
{
|
2005-10-16 11:15:46 +02:00
|
|
|
|
2005-10-19 02:50:38 +02:00
|
|
|
po::options_description desc("supported options");
|
|
|
|
desc.add_options()
|
2005-10-16 11:15:46 +02:00
|
|
|
("help,h", "display this help message")
|
|
|
|
("port,p", po::value<int>(&listen_port)->default_value(6881)
|
|
|
|
, "set listening port")
|
|
|
|
("ratio,r", po::value<float>(&preferred_ratio)->default_value(0)
|
|
|
|
, "set the preferred upload/download ratio. 0 means infinite. Values "
|
2005-10-19 02:50:38 +02:00
|
|
|
"smaller than 1 are clamped to 1.")
|
2005-10-16 11:15:46 +02:00
|
|
|
("max-download-rate,d", po::value<int>(&download_limit)->default_value(0)
|
|
|
|
, "the maximum download rate given in kB/s. 0 means infinite.")
|
|
|
|
("max-upload-rate,u", po::value<int>(&upload_limit)->default_value(0)
|
|
|
|
, "the maximum upload rate given in kB/s. 0 means infinite.")
|
2008-01-02 19:22:34 +01:00
|
|
|
("max-torrent-upload-rate", po::value<int>(&torrent_upload_limit)->default_value(20)
|
|
|
|
, "the maximum upload rate for an individual torrent, given in kB/s. 0 means infinite.")
|
2008-01-13 11:10:44 +01:00
|
|
|
("max-torrent-download-rate", po::value<int>(&torrent_download_limit)->default_value(0)
|
2008-01-11 00:12:58 +01:00
|
|
|
, "the maximum download rate for an individual torrent, given in kB/s. 0 means infinite.")
|
2007-01-07 19:01:12 +01:00
|
|
|
("max-upload-slots", po::value<int>(&upload_slots_limit)->default_value(8)
|
|
|
|
, "the maximum number of upload slots. 0 means infinite.")
|
2005-10-16 11:15:46 +02:00
|
|
|
("save-path,s", po::value<std::string>(&save_path_str)->default_value("./")
|
|
|
|
, "the path where the downloaded file/folder should be placed.")
|
|
|
|
("log-level,l", po::value<std::string>(&log_level)->default_value("info")
|
2005-11-07 03:18:39 +01:00
|
|
|
, "sets the level at which events are logged [debug | info | warning | fatal].")
|
2005-10-16 11:15:46 +02:00
|
|
|
("ip-filter,f", po::value<std::string>(&ip_filter_file)->default_value("")
|
|
|
|
, "sets the path to the ip-filter file used to block access from certain "
|
|
|
|
"ips. ")
|
2007-12-18 07:04:54 +01:00
|
|
|
("allocation-mode,a", po::value<std::string>(&allocation_mode)->default_value("full")
|
2005-10-17 15:45:53 +02:00
|
|
|
, "sets mode used for allocating the downloaded files on disk. "
|
|
|
|
"Possible options are [full | compact]")
|
2005-10-19 02:50:38 +02:00
|
|
|
("input-file,i", po::value<std::vector<std::string> >()
|
2005-10-16 11:15:46 +02:00
|
|
|
, "adds an input .torrent file. At least one is required. arguments "
|
|
|
|
"without any flag are implicitly an input file. To start a torrentless "
|
|
|
|
"download, use <info-hash>@<tracker-url> instead of specifying a file.")
|
2005-10-19 02:50:38 +02:00
|
|
|
("monitor-dir,m", po::value<std::string>(&in_monitor_dir)
|
|
|
|
, "monitors the given directory, looking for .torrent files and "
|
|
|
|
"automatically starts downloading them. It will stop downloading "
|
|
|
|
"torrent files that are removed from the directory")
|
|
|
|
("poll-interval,t", po::value<int>(&poll_interval)->default_value(2)
|
|
|
|
, "if a directory is being monitored, this is the interval (given "
|
|
|
|
"in seconds) between two refreshes of the directory listing")
|
2007-11-27 04:37:47 +01:00
|
|
|
("wait-retry,w", po::value<int>(&wait_retry)->default_value(30)
|
|
|
|
, "if the download of a url seed failes, this is the interval (given "
|
|
|
|
"in seconds) to wait until the next retry")
|
2007-01-02 15:16:26 +01:00
|
|
|
("half-open-limit,o", po::value<int>(&half_open_limit)->default_value(-1)
|
2005-11-02 17:28:39 +01:00
|
|
|
, "Sets the maximum number of simultaneous half-open tcp connections")
|
2006-01-06 21:20:20 +01:00
|
|
|
("bind,b", po::value<std::string>(&bind_to_interface)->default_value("")
|
|
|
|
, "Sets the local interface to bind outbound and the listen "
|
|
|
|
"socket to")
|
2006-07-27 20:07:51 +02:00
|
|
|
("proxy-server,x", po::value<std::string>(&proxy)->default_value("")
|
|
|
|
, "Sets the http proxy to be used for tracker and web seeds "
|
|
|
|
"connections. The string is expected to be on the form: "
|
|
|
|
"<hostname>:<port>. If no port is specified, 8080 is assumed")
|
2007-01-02 15:16:26 +01:00
|
|
|
("proxy-login,n", po::value<std::string>(&proxy_login)->default_value("")
|
2006-07-27 20:07:51 +02:00
|
|
|
, "Sets the username and password used to authenticate with the http "
|
|
|
|
"proxy. The string should be given in the form: <username>:<password>")
|
2007-04-25 20:26:35 +02:00
|
|
|
("proxy-type", po::value<std::string>(&proxy_type)->default_value("socks5")
|
|
|
|
, "Sets the type of proxy to use [socks5 | http] ")
|
2008-02-28 08:34:07 +01:00
|
|
|
("bind-port-start", po::value<int>(&bind_port_start)->default_value(0)
|
|
|
|
, "The lower port number that outgoing connections will be bound to")
|
|
|
|
("bind-port-end", po::value<int>(&bind_port_end)->default_value(0)
|
|
|
|
, "The upper port number that outgoing connections will be bound to")
|
2005-10-19 02:50:38 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
po::positional_options_description p;
|
|
|
|
p.add("input-file", -1);
|
|
|
|
|
|
|
|
po::variables_map vm;
|
|
|
|
po::store(po::command_line_parser(ac, av).
|
|
|
|
options(desc).positional(p).run(), vm);
|
|
|
|
po::notify(vm);
|
|
|
|
|
|
|
|
// make sure the arguments stays within the usable limits
|
|
|
|
path monitor_dir(in_monitor_dir);
|
|
|
|
if (listen_port < 0 || listen_port > 65525) listen_port = 6881;
|
|
|
|
if (preferred_ratio != 0 && preferred_ratio < 1.f) preferred_ratio = 1.f;
|
|
|
|
upload_limit *= 1000;
|
2008-01-03 09:17:17 +01:00
|
|
|
torrent_upload_limit *= 1000;
|
2008-01-11 00:12:58 +01:00
|
|
|
torrent_download_limit *= 1000;
|
2005-10-19 02:50:38 +02:00
|
|
|
download_limit *= 1000;
|
|
|
|
if (download_limit <= 0) download_limit = -1;
|
|
|
|
if (upload_limit <= 0) upload_limit = -1;
|
2008-01-02 19:22:34 +01:00
|
|
|
if (torrent_upload_limit <= 0) torrent_upload_limit = -1;
|
2008-01-11 00:12:58 +01:00
|
|
|
if (torrent_download_limit <= 0) torrent_download_limit = -1;
|
2005-10-19 02:50:38 +02:00
|
|
|
if (poll_interval < 2) poll_interval = 2;
|
2007-11-27 04:37:47 +01:00
|
|
|
if (wait_retry < 0) wait_retry = 0;
|
2005-11-02 17:28:39 +01:00
|
|
|
if (half_open_limit < 1) half_open_limit = -1;
|
2007-01-07 19:01:12 +01:00
|
|
|
if (upload_slots_limit <= 0) upload_slots_limit = -1;
|
2005-10-19 02:50:38 +02:00
|
|
|
if (!monitor_dir.empty() && !exists(monitor_dir))
|
|
|
|
{
|
|
|
|
std::cerr << "The monitor directory doesn't exist: " << monitor_dir.string() << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
2005-10-16 11:15:46 +02:00
|
|
|
|
2005-10-19 02:50:38 +02:00
|
|
|
if (vm.count("help")
|
|
|
|
|| vm.count("input-file") + vm.count("monitor-dir") == 0)
|
|
|
|
{
|
|
|
|
std::cout << desc << "\n";
|
|
|
|
return 1;
|
|
|
|
}
|
2005-10-16 11:15:46 +02:00
|
|
|
|
2005-10-19 02:50:38 +02:00
|
|
|
bool compact_allocation_mode = (allocation_mode == "compact");
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2005-10-19 02:50:38 +02:00
|
|
|
using namespace libtorrent;
|
2005-10-16 11:15:46 +02:00
|
|
|
|
2005-10-19 02:50:38 +02:00
|
|
|
std::vector<std::string> input;
|
|
|
|
if (vm.count("input-file") > 0)
|
|
|
|
input = vm["input-file"].as< std::vector<std::string> >();
|
2005-10-16 11:15:46 +02:00
|
|
|
|
2006-05-21 01:24:19 +02:00
|
|
|
session_settings settings;
|
2007-04-25 20:26:35 +02:00
|
|
|
proxy_settings ps;
|
2006-07-27 20:07:51 +02:00
|
|
|
|
|
|
|
if (!proxy.empty())
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
std::size_t i = proxy.find(':');
|
2007-04-25 20:26:35 +02:00
|
|
|
ps.hostname = proxy.substr(0, i);
|
|
|
|
if (i == std::string::npos) ps.port = 8080;
|
|
|
|
else ps.port = boost::lexical_cast<int>(
|
2006-07-27 20:07:51 +02:00
|
|
|
proxy.substr(i + 1));
|
2007-04-25 20:26:35 +02:00
|
|
|
if (proxy_type == "socks5")
|
|
|
|
ps.type = proxy_settings::socks5;
|
|
|
|
else
|
|
|
|
ps.type = proxy_settings::http;
|
2006-07-27 20:07:51 +02:00
|
|
|
}
|
|
|
|
catch (std::exception&)
|
|
|
|
{
|
|
|
|
std::cerr << "Proxy hostname did not match the required format: "
|
|
|
|
<< proxy << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!proxy_login.empty())
|
|
|
|
{
|
|
|
|
std::size_t i = proxy_login.find(':');
|
|
|
|
if (i == std::string::npos)
|
|
|
|
{
|
|
|
|
std::cerr << "Proxy login did not match the required format: "
|
|
|
|
<< proxy_login << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
2007-04-25 20:26:35 +02:00
|
|
|
ps.username = proxy_login.substr(0, i);
|
|
|
|
ps.password = proxy_login.substr(i + 1);
|
|
|
|
if (proxy_type == "socks5")
|
|
|
|
ps.type = proxy_settings::socks5_pw;
|
|
|
|
else
|
|
|
|
ps.type = proxy_settings::http_pw;
|
2006-07-27 20:07:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-09-28 02:49:40 +02:00
|
|
|
settings.user_agent = "client_test/" LIBTORRENT_VERSION;
|
2007-11-27 04:37:47 +01:00
|
|
|
settings.urlseed_wait_retry = wait_retry;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2008-02-28 08:34:07 +01:00
|
|
|
settings.outgoing_ports.first = bind_port_start;
|
|
|
|
settings.outgoing_ports.second = bind_port_end;
|
|
|
|
|
2005-10-19 02:50:38 +02:00
|
|
|
std::deque<std::string> events;
|
2003-12-22 08:14:35 +01:00
|
|
|
|
2007-04-10 11:11:32 +02:00
|
|
|
ptime next_dir_scan = time_now();
|
2005-10-19 02:50:38 +02:00
|
|
|
|
|
|
|
// the string is the filename of the .torrent file, but only if
|
|
|
|
// it was added through the directory monitor. It is used to
|
|
|
|
// be able to remove torrents that were added via the directory
|
|
|
|
// monitor when they're not in the directory anymore.
|
|
|
|
handles_t handles;
|
2004-08-08 23:26:40 +02:00
|
|
|
session ses;
|
2008-04-05 06:53:22 +02:00
|
|
|
#ifndef TORRENT_DISABLE_GEO_IP
|
|
|
|
ses.load_asnum_db("GeoIPASNum.dat");
|
2008-04-11 10:46:43 +02:00
|
|
|
ses.load_country_db("GeoIP.dat");
|
2008-04-05 06:53:22 +02:00
|
|
|
#endif
|
2007-05-31 02:21:54 +02:00
|
|
|
// UPnP port mapping
|
2007-10-02 21:46:14 +02:00
|
|
|
ses.start_upnp();
|
2007-05-31 02:21:54 +02:00
|
|
|
// NAT-PMP port mapping
|
2007-10-02 21:46:14 +02:00
|
|
|
ses.start_natpmp();
|
2007-05-31 02:21:54 +02:00
|
|
|
// Local service discovery (finds peers on the local network)
|
2007-10-02 21:46:14 +02:00
|
|
|
ses.start_lsd();
|
|
|
|
ses.add_extension(&create_metadata_plugin);
|
|
|
|
ses.add_extension(&create_ut_pex_plugin);
|
2007-12-11 21:02:55 +01:00
|
|
|
ses.add_extension(&create_ut_metadata_plugin);
|
2007-12-18 07:04:54 +01:00
|
|
|
ses.add_extension(&create_smart_ban_plugin);
|
2003-12-07 06:53:04 +01:00
|
|
|
|
2007-03-16 22:45:29 +01:00
|
|
|
ses.set_max_uploads(upload_slots_limit);
|
|
|
|
ses.set_max_half_open_connections(half_open_limit);
|
|
|
|
ses.set_download_rate_limit(download_limit);
|
|
|
|
ses.set_upload_rate_limit(upload_limit);
|
|
|
|
ses.listen_on(std::make_pair(listen_port, listen_port + 10)
|
|
|
|
, bind_to_interface.c_str());
|
|
|
|
ses.set_settings(settings);
|
2007-04-25 20:26:35 +02:00
|
|
|
ses.set_tracker_proxy(ps);
|
|
|
|
ses.set_peer_proxy(ps);
|
|
|
|
ses.set_web_seed_proxy(ps);
|
|
|
|
|
2007-03-16 22:45:29 +01:00
|
|
|
if (log_level == "debug")
|
|
|
|
ses.set_severity_level(alert::debug);
|
|
|
|
else if (log_level == "warning")
|
|
|
|
ses.set_severity_level(alert::warning);
|
|
|
|
else if (log_level == "fatal")
|
|
|
|
ses.set_severity_level(alert::fatal);
|
|
|
|
else
|
|
|
|
ses.set_severity_level(alert::info);
|
|
|
|
|
2008-04-05 06:53:22 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
boost::filesystem::ifstream ses_state_file(".ses_state"
|
|
|
|
, std::ios_base::binary);
|
|
|
|
ses_state_file.unsetf(std::ios_base::skipws);
|
|
|
|
ses.load_state(bdecode(
|
|
|
|
std::istream_iterator<char>(ses_state_file)
|
|
|
|
, std::istream_iterator<char>()));
|
|
|
|
}
|
|
|
|
catch (std::exception&) {}
|
|
|
|
|
2006-09-01 05:06:00 +02:00
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
2007-10-02 21:46:14 +02:00
|
|
|
settings.use_dht_as_fallback = false;
|
2007-03-08 22:42:37 +01:00
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
boost::filesystem::ifstream dht_state_file(".dht_state"
|
|
|
|
, std::ios_base::binary);
|
|
|
|
dht_state_file.unsetf(std::ios_base::skipws);
|
2006-09-29 16:59:26 +02:00
|
|
|
entry dht_state;
|
2006-08-01 17:27:08 +02:00
|
|
|
try
|
|
|
|
{
|
2006-09-29 16:59:26 +02:00
|
|
|
dht_state = bdecode(
|
2006-08-01 17:27:08 +02:00
|
|
|
std::istream_iterator<char>(dht_state_file)
|
|
|
|
, std::istream_iterator<char>());
|
|
|
|
}
|
2006-09-27 19:20:18 +02:00
|
|
|
catch (std::exception&) {}
|
2006-09-29 16:59:26 +02:00
|
|
|
ses.start_dht(dht_state);
|
2006-09-27 19:20:18 +02:00
|
|
|
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));
|
2006-09-01 05:06:00 +02:00
|
|
|
#endif
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2005-07-06 02:58:23 +02:00
|
|
|
// look for ipfilter.dat
|
|
|
|
// poor man's parser
|
|
|
|
// reads emule ipfilter files.
|
|
|
|
// with the following format:
|
|
|
|
//
|
|
|
|
// <first-ip> - <last-ip> , <access> , <comment>
|
|
|
|
//
|
|
|
|
// first-ip is an ip address that defines the first
|
|
|
|
// address of the range
|
|
|
|
// last-ip is the last ip address in the range
|
|
|
|
// access is a number specifying the access control
|
|
|
|
// for this ip-range. Right now values > 127 = allowed
|
|
|
|
// and numbers <= 127 = blocked
|
|
|
|
// the rest of the line is ignored
|
|
|
|
//
|
|
|
|
// In the original spec ranges may not overlap, but
|
|
|
|
// here ranges may overlap, and it is the last added
|
|
|
|
// rule that has precedence for addresses that may fall
|
|
|
|
// into more than one range.
|
2005-10-19 20:00:17 +02:00
|
|
|
if (!ip_filter_file.empty())
|
2005-07-06 02:58:23 +02:00
|
|
|
{
|
2005-10-19 20:00:17 +02:00
|
|
|
std::ifstream in(ip_filter_file.c_str());
|
|
|
|
ip_filter filter;
|
|
|
|
while (in.good())
|
|
|
|
{
|
|
|
|
char line[300];
|
|
|
|
in.getline(line, 300);
|
|
|
|
int len = in.gcount();
|
|
|
|
if (len <= 0) continue;
|
|
|
|
if (line[0] == '#') continue;
|
|
|
|
int a, b, c, d;
|
|
|
|
char dummy;
|
2007-04-17 07:56:43 +02:00
|
|
|
std::stringstream ln(line);
|
|
|
|
ln >> a >> dummy >> b >> dummy >> c >> dummy >> d >> dummy;
|
2006-09-01 05:06:00 +02:00
|
|
|
address_v4 start((a << 24) + (b << 16) + (c << 8) + d);
|
2007-04-17 07:56:43 +02:00
|
|
|
ln >> a >> dummy >> b >> dummy >> c >> dummy >> d >> dummy;
|
2006-09-01 05:06:00 +02:00
|
|
|
address_v4 last((a << 24) + (b << 16) + (c << 8) + d);
|
2005-10-19 20:00:17 +02:00
|
|
|
int flags;
|
2007-04-17 07:56:43 +02:00
|
|
|
ln >> flags;
|
2005-10-19 20:00:17 +02:00
|
|
|
if (flags <= 127) flags = ip_filter::blocked;
|
|
|
|
else flags = 0;
|
2007-04-17 07:56:43 +02:00
|
|
|
if (ln.fail()) break;
|
2005-10-19 20:00:17 +02:00
|
|
|
filter.add_rule(start, last, flags);
|
|
|
|
}
|
|
|
|
ses.set_ip_filter(filter);
|
2005-07-06 02:58:23 +02:00
|
|
|
}
|
2005-10-16 11:15:46 +02:00
|
|
|
boost::filesystem::path save_path(save_path_str);
|
|
|
|
|
|
|
|
// load the torrents given on the commandline
|
|
|
|
boost::regex ex("([0-9A-Fa-f]{40})@(.+)");
|
|
|
|
for (std::vector<std::string>::const_iterator i = input.begin();
|
|
|
|
i != input.end(); ++i)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2005-10-16 11:15:46 +02:00
|
|
|
// first see if this is a torrentless download
|
2007-12-11 21:02:55 +01:00
|
|
|
if (i->substr(0, 7) == "magnet:")
|
|
|
|
{
|
|
|
|
std::cout << "adding MANGET link: " << *i << std::endl;
|
|
|
|
torrent_handle h = add_magnet_uri(ses, *i, save_path
|
|
|
|
, compact_allocation_mode ? storage_mode_compact
|
|
|
|
: storage_mode_sparse);
|
|
|
|
|
|
|
|
handles.insert(std::make_pair(std::string(), h));
|
|
|
|
|
2008-01-02 19:22:34 +01:00
|
|
|
h.set_max_connections(50);
|
2007-12-11 21:02:55 +01:00
|
|
|
h.set_max_uploads(-1);
|
|
|
|
h.set_ratio(preferred_ratio);
|
2008-01-02 19:22:34 +01:00
|
|
|
h.set_upload_limit(torrent_upload_limit);
|
2008-01-11 00:12:58 +01:00
|
|
|
h.set_download_limit(torrent_download_limit);
|
2007-12-11 21:02:55 +01:00
|
|
|
continue;
|
|
|
|
}
|
2005-10-16 11:15:46 +02:00
|
|
|
boost::cmatch what;
|
|
|
|
if (boost::regex_match(i->c_str(), what, ex))
|
2004-06-14 01:30:42 +02:00
|
|
|
{
|
2005-10-16 11:15:46 +02:00
|
|
|
sha1_hash info_hash = boost::lexical_cast<sha1_hash>(what[1]);
|
2004-06-14 01:30:42 +02:00
|
|
|
|
2008-04-24 05:28:48 +02:00
|
|
|
add_torrent_params p;
|
|
|
|
p.name = std::string(what[2]).c_str();
|
|
|
|
p.info_hash = info_hash;
|
|
|
|
p.save_path = save_path;
|
|
|
|
p.storage_mode = compact_allocation_mode ? storage_mode_compact : storage_mode_sparse;
|
|
|
|
p.paused = true;
|
|
|
|
p.duplicate_is_error = false;
|
|
|
|
p.auto_managed = true;
|
|
|
|
torrent_handle h = ses.add_torrent(p);
|
|
|
|
|
2005-10-19 02:50:38 +02:00
|
|
|
handles.insert(std::make_pair(std::string(), h));
|
|
|
|
|
2008-01-02 19:22:34 +01:00
|
|
|
h.set_max_connections(50);
|
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);
|
2004-06-14 01:30:42 +02:00
|
|
|
continue;
|
|
|
|
}
|
2005-10-16 11:15:46 +02:00
|
|
|
// if it's a torrent file, open it as usual
|
2005-10-17 15:45:53 +02:00
|
|
|
add_torrent(ses, handles, i->c_str(), preferred_ratio
|
2008-01-02 19:22:34 +01:00
|
|
|
, compact_allocation_mode ? storage_mode_compact : storage_mode_sparse
|
2008-01-11 00:12:58 +01:00
|
|
|
, save_path, false, torrent_upload_limit, torrent_download_limit);
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
catch (std::exception& e)
|
|
|
|
{
|
|
|
|
std::cout << e.what() << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-16 11:15:46 +02:00
|
|
|
// main loop
|
2003-10-30 00:28:09 +01:00
|
|
|
std::vector<peer_info> peers;
|
2003-11-02 22:06:50 +01:00
|
|
|
std::vector<partial_piece_info> queue;
|
2003-10-30 00:28:09 +01:00
|
|
|
|
|
|
|
for (;;)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2003-10-30 00:28:09 +01:00
|
|
|
char c;
|
|
|
|
if (sleep_and_input(&c))
|
|
|
|
{
|
2008-04-22 02:05:23 +02:00
|
|
|
if (c == 'm')
|
|
|
|
{
|
|
|
|
std::cout << "saving peers for torrents" << std::endl;
|
|
|
|
|
|
|
|
std::vector<peer_list_entry> peers;
|
|
|
|
for (handles_t::iterator i = handles.begin();
|
|
|
|
i != handles.end(); ++i)
|
|
|
|
{
|
|
|
|
i->second.get_full_peer_list(peers);
|
|
|
|
std::ofstream f(("peers_" + i->second.name()).c_str());
|
|
|
|
for (std::vector<peer_list_entry>::iterator k = peers.begin()
|
|
|
|
, end(peers.end()); k != end; ++k)
|
|
|
|
{
|
2008-04-22 19:17:01 +02:00
|
|
|
f << k->ip.address()
|
|
|
|
#ifndef TORRENT_DISABLE_GEO_IP
|
|
|
|
<< "\t" << ses.as_for_ip(k->ip.address())
|
|
|
|
#endif
|
|
|
|
<< std::endl;
|
2008-04-22 02:05:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-02 21:46:24 +01:00
|
|
|
if (c == 'q')
|
|
|
|
{
|
2008-04-13 20:54:36 +02:00
|
|
|
// keep track of the number of resume data
|
|
|
|
// alerts to wait for
|
|
|
|
int num_resume_data = 0;
|
2005-10-19 02:50:38 +02:00
|
|
|
for (handles_t::iterator i = handles.begin();
|
2005-05-25 12:01:01 +02:00
|
|
|
i != handles.end(); ++i)
|
2004-01-13 04:08:59 +01:00
|
|
|
{
|
2005-10-19 02:50:38 +02:00
|
|
|
torrent_handle& h = i->second;
|
2005-10-16 11:15:46 +02:00
|
|
|
if (!h.is_valid() || !h.has_metadata()) continue;
|
2004-06-14 01:30:42 +02:00
|
|
|
|
2008-04-13 20:54:36 +02:00
|
|
|
// pause
|
|
|
|
std::cout << "pausing " << h.name() << std::endl;
|
2005-07-06 02:58:23 +02:00
|
|
|
h.pause();
|
2008-04-13 20:54:36 +02:00
|
|
|
// save_resume_data will generate an alert when it's done
|
|
|
|
h.save_resume_data();
|
|
|
|
++num_resume_data;
|
|
|
|
}
|
|
|
|
std::cout << "waiting for resume data" << std::endl;
|
2005-10-16 11:15:46 +02:00
|
|
|
|
2008-04-13 20:54:36 +02:00
|
|
|
while (num_resume_data > 0)
|
|
|
|
{
|
|
|
|
alert const* a = ses.wait_for_alert(seconds(10));
|
|
|
|
if (a == 0)
|
|
|
|
{
|
|
|
|
std::cout << " aborting with " << num_resume_data << " outstanding "
|
|
|
|
"torrents to save resume data for" << std::endl;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::auto_ptr<alert> holder = ses.pop_alert();
|
|
|
|
save_resume_data_alert const* rd = dynamic_cast<save_resume_data_alert const*>(a);
|
|
|
|
if (rd == 0)
|
|
|
|
{
|
|
|
|
std::cout << a->msg() << std::endl;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
torrent_handle h = rd->handle;
|
|
|
|
boost::filesystem::ofstream out(h.save_path()
|
|
|
|
/ (h.get_torrent_info().name() + ".fastresume"), std::ios_base::binary);
|
2004-01-13 04:08:59 +01:00
|
|
|
out.unsetf(std::ios_base::skipws);
|
2008-04-13 20:54:36 +02:00
|
|
|
bencode(std::ostream_iterator<char>(out), *rd->resume_data);
|
|
|
|
std::cout << "fast resume data saved for " << h.name() << std::endl;
|
|
|
|
--num_resume_data;
|
2004-01-13 04:08:59 +01:00
|
|
|
}
|
2004-01-02 21:46:24 +01:00
|
|
|
break;
|
|
|
|
}
|
2004-01-14 17:22:49 +01:00
|
|
|
|
2008-01-31 18:52:29 +01:00
|
|
|
if (c == 'r')
|
2004-01-14 17:22:49 +01:00
|
|
|
{
|
2005-10-19 15:58:41 +02:00
|
|
|
// force reannounce on all torrents
|
2005-10-16 11:15:46 +02:00
|
|
|
std::for_each(handles.begin(), handles.end()
|
2005-10-19 02:50:38 +02:00
|
|
|
, bind(&torrent_handle::force_reannounce
|
2005-10-19 15:58:41 +02:00
|
|
|
, bind(&handles_t::value_type::second, _1)));
|
|
|
|
}
|
2004-03-21 03:03:37 +01:00
|
|
|
|
2008-01-31 18:52:29 +01:00
|
|
|
if (c == 's')
|
|
|
|
{
|
|
|
|
// toggle torrents between sequential and rarest first mode
|
|
|
|
sequential_download = !sequential_download;
|
|
|
|
std::for_each(handles.begin(), handles.end()
|
|
|
|
, bind(&torrent_handle::set_sequential_download
|
|
|
|
, bind(&handles_t::value_type::second, _1), sequential_download));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c == 'p')
|
2004-03-21 03:03:37 +01:00
|
|
|
{
|
2005-10-19 15:58:41 +02:00
|
|
|
// pause all torrents
|
2005-10-16 11:15:46 +02:00
|
|
|
std::for_each(handles.begin(), handles.end()
|
2005-10-19 02:50:38 +02:00
|
|
|
, bind(&torrent_handle::pause
|
2005-10-19 15:58:41 +02:00
|
|
|
, bind(&handles_t::value_type::second, _1)));
|
|
|
|
}
|
2004-03-21 03:03:37 +01:00
|
|
|
|
2008-01-31 18:52:29 +01:00
|
|
|
if (c == 'u')
|
2004-03-21 03:03:37 +01:00
|
|
|
{
|
2005-10-19 15:58:41 +02:00
|
|
|
// unpause all torrents
|
2005-10-16 11:15:46 +02:00
|
|
|
std::for_each(handles.begin(), handles.end()
|
2005-10-19 02:50:38 +02:00
|
|
|
, bind(&torrent_handle::resume
|
2005-10-19 15:58:41 +02:00
|
|
|
, bind(&handles_t::value_type::second, _1)));
|
|
|
|
}
|
2004-03-21 03:03:37 +01:00
|
|
|
|
2008-04-05 06:53:22 +02:00
|
|
|
// toggle displays
|
2004-07-24 13:54:17 +02:00
|
|
|
if (c == 'i') print_peers = !print_peers;
|
|
|
|
if (c == 'l') print_log = !print_log;
|
|
|
|
if (c == 'd') print_downloads = !print_downloads;
|
2006-06-11 15:48:39 +02:00
|
|
|
if (c == 'f') print_file_progress = !print_file_progress;
|
2007-07-09 02:00:35 +02:00
|
|
|
if (c == 'a') print_piece_bar = !print_piece_bar;
|
2008-04-05 06:53:22 +02: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;
|
2007-07-09 02:00:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int terminal_width = 80;
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
{
|
|
|
|
winsize size;
|
|
|
|
ioctl(STDOUT_FILENO, TIOCGWINSZ, (char*)&size);
|
|
|
|
terminal_width = size.ws_col;
|
2003-10-30 00:28:09 +01:00
|
|
|
}
|
2007-07-09 02:00:35 +02:00
|
|
|
#endif
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2004-03-23 23:58:18 +01:00
|
|
|
// loop through the alert queue to see if anything has happened.
|
2003-12-22 08:14:35 +01:00
|
|
|
std::auto_ptr<alert> a;
|
|
|
|
a = ses.pop_alert();
|
2007-04-10 11:11:32 +02:00
|
|
|
std::string now = time_now_string();
|
2003-12-22 08:14:35 +01:00
|
|
|
while (a.get())
|
|
|
|
{
|
2007-03-16 22:04:58 +01:00
|
|
|
std::stringstream event_string;
|
|
|
|
if (a->severity() == alert::fatal)
|
|
|
|
event_string << esc("31"); // red
|
|
|
|
else if (a->severity() == alert::warning)
|
|
|
|
event_string << esc("33"); // yellow
|
|
|
|
|
|
|
|
event_string << now << ": ";
|
2004-03-28 19:45:37 +02:00
|
|
|
if (torrent_finished_alert* p = dynamic_cast<torrent_finished_alert*>(a.get()))
|
2004-02-01 01:50:18 +01:00
|
|
|
{
|
2008-01-02 19:22:34 +01:00
|
|
|
p->handle.set_max_connections(30);
|
2004-07-18 02:39:58 +02:00
|
|
|
|
2006-05-28 21:03:54 +02:00
|
|
|
// write resume data for the finished torrent
|
|
|
|
torrent_handle h = p->handle;
|
2008-04-13 20:54:36 +02:00
|
|
|
h.save_resume_data();
|
|
|
|
|
|
|
|
event_string << h.name() << ": " << a->msg();
|
2004-02-01 01:50:18 +01:00
|
|
|
}
|
2004-03-28 19:45:37 +02:00
|
|
|
else if (peer_error_alert* p = dynamic_cast<peer_error_alert*>(a.get()))
|
|
|
|
{
|
2007-03-16 22:04:58 +01:00
|
|
|
event_string << identify_client(p->pid) << ": " << a->msg();
|
2004-03-28 19:45:37 +02:00
|
|
|
}
|
|
|
|
else if (invalid_request_alert* p = dynamic_cast<invalid_request_alert*>(a.get()))
|
|
|
|
{
|
2007-03-16 22:04:58 +01:00
|
|
|
event_string << identify_client(p->pid) << ": " << a->msg();
|
2004-03-28 19:45:37 +02:00
|
|
|
}
|
2006-12-31 00:02:21 +01:00
|
|
|
else if (tracker_warning_alert* p = dynamic_cast<tracker_warning_alert*>(a.get()))
|
|
|
|
{
|
2007-03-16 22:04:58 +01:00
|
|
|
event_string << "tracker message: " << p->msg();
|
2006-12-31 00:02:21 +01:00
|
|
|
}
|
2007-02-01 08:33:04 +01:00
|
|
|
else if (tracker_reply_alert* p = dynamic_cast<tracker_reply_alert*>(a.get()))
|
|
|
|
{
|
2007-03-16 22:04:58 +01:00
|
|
|
event_string << p->msg() << " (" << p->num_peers << ")";
|
2007-02-01 08:33:04 +01:00
|
|
|
}
|
2007-03-07 01:10:47 +01:00
|
|
|
else if (url_seed_alert* p = dynamic_cast<url_seed_alert*>(a.get()))
|
|
|
|
{
|
2007-03-16 22:04:58 +01:00
|
|
|
event_string << "web seed '" << p->url << "': " << p->msg();
|
2007-03-07 01:10:47 +01:00
|
|
|
}
|
2007-04-17 07:56:43 +02:00
|
|
|
else if (peer_blocked_alert* p = dynamic_cast<peer_blocked_alert*>(a.get()))
|
|
|
|
{
|
|
|
|
event_string << "(" << p->ip << ") " << p->msg();
|
|
|
|
}
|
2008-04-13 20:54:36 +02:00
|
|
|
else if (save_resume_data_alert* p = dynamic_cast<save_resume_data_alert*>(a.get()))
|
|
|
|
{
|
|
|
|
torrent_handle h = p->handle;
|
|
|
|
if (p->resume_data)
|
|
|
|
{
|
|
|
|
boost::filesystem::ofstream out(h.save_path() / (h.name() + ".fastresume"), std::ios_base::binary);
|
|
|
|
out.unsetf(std::ios_base::skipws);
|
|
|
|
bencode(std::ostream_iterator<char>(out), *p->resume_data);
|
|
|
|
if (h.is_paused()) ses.remove_torrent(h);
|
|
|
|
}
|
|
|
|
event_string << "(" << h.name() << ") " << p->msg();
|
|
|
|
}
|
2007-06-10 22:46:09 +02:00
|
|
|
else if (torrent_alert* p = dynamic_cast<torrent_alert*>(a.get()))
|
|
|
|
{
|
|
|
|
std::string name;
|
|
|
|
try { name = p->handle.name(); } catch (std::exception&) {}
|
|
|
|
event_string << "(" << name << ") " << p->msg();
|
|
|
|
}
|
2004-03-28 19:45:37 +02:00
|
|
|
else
|
|
|
|
{
|
2007-03-16 22:04:58 +01:00
|
|
|
event_string << a->msg();
|
2004-03-28 19:45:37 +02:00
|
|
|
}
|
2007-03-16 22:04:58 +01:00
|
|
|
event_string << esc("0");
|
|
|
|
events.push_back(event_string.str());
|
2004-03-28 19:45:37 +02:00
|
|
|
|
2007-03-16 22:04:58 +01:00
|
|
|
if (events.size() >= 20) events.pop_front();
|
2003-12-22 08:14:35 +01:00
|
|
|
a = ses.pop_alert();
|
|
|
|
}
|
|
|
|
|
2005-08-15 00:04:58 +02:00
|
|
|
session_status sess_stat = ses.status();
|
|
|
|
|
2003-12-08 22:59:48 +01:00
|
|
|
std::stringstream out;
|
2005-10-19 02:50:38 +02:00
|
|
|
for (handles_t::iterator i = handles.begin();
|
2006-04-30 12:28:45 +02:00
|
|
|
i != handles.end();)
|
2003-10-30 00:28:09 +01:00
|
|
|
{
|
2005-10-19 02:50:38 +02:00
|
|
|
torrent_handle& h = i->second;
|
|
|
|
if (!h.is_valid())
|
2004-03-03 14:47:12 +01:00
|
|
|
{
|
2006-04-30 12:28:45 +02:00
|
|
|
handles.erase(i++);
|
2004-03-03 14:47:12 +01:00
|
|
|
continue;
|
|
|
|
}
|
2006-04-30 12:28:45 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
2008-04-24 05:28:48 +02:00
|
|
|
out << "- ";
|
|
|
|
if (h.is_paused()) out << esc("34");
|
|
|
|
else out << esc("37");
|
|
|
|
out << std::setw(40) << std::setiosflags(std::ios::left);
|
|
|
|
|
|
|
|
std::string name = h.name();
|
|
|
|
if (name.size() > 40) name.resize(40);
|
|
|
|
out << name;
|
|
|
|
|
2006-12-06 00:41:13 +01:00
|
|
|
out << esc("0") << " ";
|
2008-04-24 05:28:48 +02:00
|
|
|
|
2005-10-19 02:50:38 +02:00
|
|
|
torrent_status s = h.status();
|
2003-10-31 05:02:51 +01:00
|
|
|
|
2008-04-24 05:28:48 +02:00
|
|
|
bool paused = h.is_paused();
|
|
|
|
bool auto_managed = h.is_auto_managed();
|
2008-04-24 06:17:54 +02:00
|
|
|
out << std::setw(13) << std::setiosflags(std::ios::left);
|
|
|
|
if (paused && !auto_managed) out << "paused";
|
|
|
|
else if (paused && auto_managed) out << "queued";
|
2008-04-24 05:28:48 +02:00
|
|
|
else
|
2003-10-31 05:02:51 +01:00
|
|
|
{
|
2005-10-16 11:15:46 +02:00
|
|
|
static char const* state_str[] =
|
2008-04-24 06:17:54 +02:00
|
|
|
{"checking (q)", "checking", "connecting", "dl metadata"
|
2005-10-17 15:45:53 +02:00
|
|
|
, "downloading", "finished", "seeding", "allocating"};
|
2008-04-24 06:17:54 +02:00
|
|
|
out << state_str[s.state];
|
2005-07-06 20:40:01 +02:00
|
|
|
}
|
2004-07-24 13:54:17 +02:00
|
|
|
|
2007-02-12 06:46:29 +01:00
|
|
|
if ((print_downloads && s.state != torrent_status::seeding)
|
|
|
|
|| print_peers)
|
|
|
|
h.get_peer_info(peers);
|
2003-12-14 06:56:12 +01:00
|
|
|
|
2008-04-24 06:17:54 +02:00
|
|
|
out << "download: " << "(" << esc("32") << add_suffix(s.total_download) << esc("0") << ") "
|
|
|
|
"upload: " << esc("31") << (s.upload_rate > 0 ? add_suffix(s.upload_rate) + "/s ": " ") << esc("0")
|
|
|
|
<< "(" << esc("31") << add_suffix(s.total_upload) << esc("0") << ") "
|
|
|
|
<< "ratio: " << ratio(s.total_payload_download, s.total_payload_upload)
|
|
|
|
<< " bw queue: (" << s.up_bandwidth_queue << " | " << s.down_bandwidth_queue << ") "
|
|
|
|
"all-time (" << s.active_time - s.seeding_time << "/" << s.active_time << ")"
|
|
|
|
" (Rx: " << esc("32") << add_suffix(s.all_time_download) << esc("0")
|
|
|
|
<< " Tx: " << esc("31") << add_suffix(s.all_time_upload) << esc("0") << ") " << s.seed_cycles << "\n";
|
|
|
|
|
2005-07-06 20:40:01 +02:00
|
|
|
if (s.state != torrent_status::seeding)
|
|
|
|
{
|
2005-08-15 00:04:58 +02:00
|
|
|
char const* progress_bar_color = "33"; // yellow
|
|
|
|
if (s.state == torrent_status::checking_files
|
|
|
|
|| s.state == torrent_status::downloading_metadata)
|
|
|
|
{
|
|
|
|
progress_bar_color = "35"; // magenta
|
|
|
|
}
|
|
|
|
else if (s.current_tracker.empty())
|
|
|
|
{
|
|
|
|
progress_bar_color = "31"; // red
|
|
|
|
}
|
|
|
|
else if (sess_stat.has_incoming_connections)
|
|
|
|
{
|
|
|
|
progress_bar_color = "32"; // green
|
|
|
|
}
|
2008-04-24 06:17:54 +02:00
|
|
|
out << " progress: " << esc("32") << s.total_done << esc("0") << " Bytes ";
|
2005-07-06 20:40:01 +02:00
|
|
|
out.precision(4);
|
|
|
|
out.width(5);
|
|
|
|
out.fill(' ');
|
|
|
|
out << (s.progress*100) << "% ";
|
2008-04-24 06:17:54 +02:00
|
|
|
out << progress_bar(s.progress, terminal_width - 37, progress_bar_color) << "\n";
|
|
|
|
if (print_piece_bar && s.progress < 1.f && s.pieces)
|
|
|
|
out << " " << piece_bar(*s.pieces, terminal_width - 5) << "\n";
|
|
|
|
out << " peers: " << esc("37") << s.num_peers << esc("0") << " (" << esc("37") << s.connect_candidates << esc("0") << ") "
|
|
|
|
<< "seeds: " << esc("37") << s.num_seeds << esc("0") << " "
|
|
|
|
<< "distributed copies: " << esc("37") << s.distributed_copies << esc("0")
|
|
|
|
// << " magnet-link: " << make_magnet_uri(h) << "\n"
|
|
|
|
<< " download: " << esc("32") << (s.download_rate > 0 ? add_suffix(s.download_rate) + "/s ": " ") << esc("0");
|
2005-07-06 20:40:01 +02:00
|
|
|
boost::posix_time::time_duration t = s.next_announce;
|
2008-04-24 06:17:54 +02:00
|
|
|
out << " next announce: " << esc("37")
|
|
|
|
<< to_string(t.hours(), 2) << ":"
|
|
|
|
<< to_string(t.minutes(), 2) << ":"
|
2007-04-15 19:44:06 +02:00
|
|
|
<< to_string(t.seconds(), 2) << esc("0") << " ";
|
2008-04-24 06:17:54 +02:00
|
|
|
out << "tracker: " << esc("36") << s.current_tracker << esc("0") << "\n";
|
2005-07-06 20:40:01 +02:00
|
|
|
}
|
2003-12-08 02:37:30 +01:00
|
|
|
|
2005-07-06 20:40:01 +02:00
|
|
|
if (print_peers && !peers.empty())
|
2004-07-24 13:54:17 +02:00
|
|
|
print_peer_info(out, peers);
|
2004-02-25 00:55:42 +01:00
|
|
|
|
2005-07-06 20:40:01 +02:00
|
|
|
if (print_downloads && s.state != torrent_status::seeding)
|
2003-11-02 22:06:50 +01:00
|
|
|
{
|
2005-10-19 02:50:38 +02:00
|
|
|
h.get_download_queue(queue);
|
2007-06-10 22:46:09 +02:00
|
|
|
std::sort(queue.begin(), queue.end(), bind(&partial_piece_info::piece_index, _1)
|
|
|
|
< bind(&partial_piece_info::piece_index, _2));
|
2008-02-08 11:22:05 +01:00
|
|
|
|
|
|
|
std::vector<cached_piece_info> pieces;
|
|
|
|
ses.get_cache_info(h.info_hash(), pieces);
|
|
|
|
|
2004-07-24 13:54:17 +02:00
|
|
|
for (std::vector<partial_piece_info>::iterator i = queue.begin();
|
2005-06-16 17:41:04 +02:00
|
|
|
i != queue.end(); ++i)
|
2003-11-02 22:06:50 +01:00
|
|
|
{
|
2008-02-08 11:22:05 +01:00
|
|
|
cached_piece_info* cp = 0;
|
|
|
|
std::vector<cached_piece_info>::iterator cpi = std::find_if(pieces.begin(), pieces.end()
|
|
|
|
, bind(&cached_piece_info::piece, _1) == i->piece_index);
|
|
|
|
if (cpi != pieces.end()) cp = &*cpi;
|
|
|
|
|
2007-05-24 22:23:14 +02:00
|
|
|
out << to_string(i->piece_index, 4) << ": [";
|
2004-07-24 13:54:17 +02:00
|
|
|
for (int j = 0; j < i->blocks_in_piece; ++j)
|
|
|
|
{
|
2007-06-10 22:46:09 +02:00
|
|
|
int index = peer_index(i->blocks[j].peer, peers);
|
2007-01-10 18:39:24 +01:00
|
|
|
char str[] = "+";
|
2007-01-10 16:02:25 +01:00
|
|
|
if (index >= 0)
|
|
|
|
str[0] = (index < 10)?'0' + index:'A' + index - 10;
|
|
|
|
|
2005-08-15 00:04:58 +02:00
|
|
|
#ifdef ANSI_TERMINAL_COLORS
|
2008-02-08 11:22:05 +01:00
|
|
|
if (cp && cp->blocks[j]) out << esc("36;7") << str << esc("0");
|
|
|
|
else if (i->blocks[j].bytes_progress > 0
|
2007-07-08 22:45:42 +02:00
|
|
|
&& i->blocks[j].state == block_info::requested)
|
|
|
|
{
|
|
|
|
if (i->blocks[j].num_peers > 1)
|
|
|
|
out << esc("1;7");
|
|
|
|
else
|
|
|
|
out << esc("33;7");
|
|
|
|
out << to_string(i->blocks[j].bytes_progress / float(i->blocks[j].block_size) * 10, 1) << esc("0");
|
|
|
|
}
|
2007-06-10 22:46:09 +02:00
|
|
|
else if (i->blocks[j].state == block_info::finished) out << esc("32;7") << str << esc("0");
|
|
|
|
else if (i->blocks[j].state == block_info::writing) out << esc("35;7") << str << esc("0");
|
|
|
|
else if (i->blocks[j].state == block_info::requested) out << str;
|
2007-05-09 22:06:53 +02:00
|
|
|
else out << " ";
|
2005-08-15 00:04:58 +02:00
|
|
|
#else
|
2008-02-08 11:22:05 +01:00
|
|
|
if (cp && cp->blocks[j]) out << "c";
|
|
|
|
else if (i->blocks[j].state == block_info::finished) out << "#";
|
2007-06-13 19:36:26 +02:00
|
|
|
else if (i->blocks[j].state == block_info::writing) out << "+";
|
|
|
|
else if (i->blocks[j].state == block_info::requested) out << str;
|
2007-05-09 22:06:53 +02:00
|
|
|
else out << " ";
|
2005-08-15 00:04:58 +02:00
|
|
|
#endif
|
2004-07-24 13:54:17 +02:00
|
|
|
}
|
2007-04-27 02:27:37 +02:00
|
|
|
char* piece_state[4] = {"", "slow", "medium", "fast"};
|
2008-02-10 01:58:25 +01:00
|
|
|
out << "] " << piece_state[i->piece_state];
|
2008-04-24 05:28:48 +02:00
|
|
|
if (cp) out << (i->piece_state > 0?" | ":"") << "cache age: " << (total_milliseconds(time_now() - cp->last_use) / 1000.f);
|
2008-02-10 01:58:25 +01:00
|
|
|
out << "\n";
|
2003-11-02 22:06:50 +01:00
|
|
|
}
|
2004-02-25 00:55:42 +01:00
|
|
|
|
2004-07-24 13:54:17 +02:00
|
|
|
out << "___________________________________\n";
|
|
|
|
}
|
2006-06-11 15:48:39 +02:00
|
|
|
|
|
|
|
if (print_file_progress
|
|
|
|
&& s.state != torrent_status::seeding
|
|
|
|
&& h.has_metadata())
|
|
|
|
{
|
|
|
|
std::vector<float> file_progress;
|
|
|
|
h.file_progress(file_progress);
|
|
|
|
torrent_info const& info = h.get_torrent_info();
|
|
|
|
for (int i = 0; i < info.num_files(); ++i)
|
|
|
|
{
|
|
|
|
if (file_progress[i] == 1.f)
|
2007-03-15 23:03:56 +01:00
|
|
|
out << progress_bar(file_progress[i], 40, "32") << " "
|
2006-06-11 15:48:39 +02:00
|
|
|
<< info.file_at(i).path.leaf() << "\n";
|
|
|
|
else
|
2007-03-15 23:03:56 +01:00
|
|
|
out << progress_bar(file_progress[i], 40, "33") << " "
|
2006-06-11 15:48:39 +02:00
|
|
|
<< info.file_at(i).path.leaf() << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
out << "___________________________________\n";
|
|
|
|
}
|
|
|
|
|
2003-10-30 00:28:09 +01:00
|
|
|
}
|
2004-01-14 17:22:49 +01:00
|
|
|
|
2008-02-08 11:22:05 +01:00
|
|
|
cache_status cs = ses.get_cache_status();
|
2008-02-22 05:11:04 +01:00
|
|
|
if (cs.blocks_read < 1) cs.blocks_read = 1;
|
|
|
|
if (cs.blocks_written < 1) cs.blocks_written = 1;
|
2008-02-08 11:22:05 +01:00
|
|
|
|
2008-01-17 18:40:46 +01:00
|
|
|
out << "==== conns: " << sess_stat.num_peers
|
|
|
|
<< " down: " << esc("32") << add_suffix(sess_stat.download_rate) << "/s" << esc("0")
|
2008-01-06 21:16:46 +01:00
|
|
|
<< " (" << esc("32") << add_suffix(sess_stat.total_download) << esc("0") << ") "
|
|
|
|
" up: " << esc("31") << add_suffix(sess_stat.upload_rate) << "/s " << esc("0")
|
2008-01-13 12:18:18 +01:00
|
|
|
<< " (" << esc("31") << add_suffix(sess_stat.total_upload) << esc("0") << ")"
|
|
|
|
" unchoked: " << sess_stat.num_unchoked << " / " << sess_stat.allowed_upload_slots
|
2008-01-17 18:40:46 +01:00
|
|
|
<< " bw queues: (" << sess_stat.up_bandwidth_queue
|
2008-02-08 11:22:05 +01:00
|
|
|
<< " | " << sess_stat.down_bandwidth_queue << ") "
|
|
|
|
" write cache hits: " << ((cs.blocks_written - cs.writes) * 100 / cs.blocks_written) << "% "
|
2008-02-22 05:11:04 +01:00
|
|
|
" read cache hits: " << (cs.blocks_read_hit * 100 / cs.blocks_read) << "% "
|
|
|
|
" cache size: " << add_suffix(cs.cache_size * 16 * 1024)
|
|
|
|
<< " (" << add_suffix(cs.read_cache_size * 16 * 1024) << ")"
|
|
|
|
" ====" << std::endl;
|
2008-04-05 06:53:22 +02:00
|
|
|
out << "[q] quit [i] toggle peers [d] toggle downloading pieces [p] pause all "
|
|
|
|
"[u] unpause all [a] toggle piece bar [s] toggle download sequential [f] toggle files\n"
|
|
|
|
"[1] toggle IP [2] toggle AS [3] toggle timers [4] toggle block progress "
|
|
|
|
"[5] toggle peer rate [6] toggle failures [7] toggle send buffers";
|
2008-01-06 21:16:46 +01:00
|
|
|
|
2004-07-24 13:54:17 +02:00
|
|
|
if (print_log)
|
2003-12-22 08:14:35 +01:00
|
|
|
{
|
2004-07-24 13:54:17 +02:00
|
|
|
for (std::deque<std::string>::iterator i = events.begin();
|
2005-08-15 00:04:58 +02:00
|
|
|
i != events.end(); ++i)
|
2004-07-24 13:54:17 +02:00
|
|
|
{
|
2008-04-05 06:53:22 +02:00
|
|
|
out << "\n" << *i;
|
2004-07-24 13:54:17 +02:00
|
|
|
}
|
2003-12-22 08:14:35 +01:00
|
|
|
}
|
2004-01-14 17:22:49 +01:00
|
|
|
|
2005-10-16 11:15:46 +02:00
|
|
|
clear_home();
|
2004-01-21 01:59:38 +01:00
|
|
|
puts(out.str().c_str());
|
2005-10-19 02:50:38 +02:00
|
|
|
|
|
|
|
if (!monitor_dir.empty()
|
2007-04-10 11:11:32 +02:00
|
|
|
&& next_dir_scan < time_now())
|
2005-10-19 02:50:38 +02:00
|
|
|
{
|
|
|
|
scan_dir(monitor_dir, ses, handles, preferred_ratio
|
2008-01-11 00:12:58 +01:00
|
|
|
, compact_allocation_mode, save_path, torrent_upload_limit
|
|
|
|
, torrent_download_limit);
|
2007-04-10 11:11:32 +02:00
|
|
|
next_dir_scan = time_now() + seconds(poll_interval);
|
2005-10-19 02:50:38 +02:00
|
|
|
}
|
2003-10-30 00:28:09 +01:00
|
|
|
}
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2008-04-13 20:54:36 +02:00
|
|
|
std::cout << "saving session state" << std::endl;
|
2008-04-05 06:53:22 +02:00
|
|
|
{
|
|
|
|
entry session_state = ses.state();
|
|
|
|
boost::filesystem::ofstream out(".ses_state"
|
|
|
|
, std::ios_base::binary);
|
|
|
|
out.unsetf(std::ios_base::skipws);
|
|
|
|
bencode(std::ostream_iterator<char>(out), session_state);
|
|
|
|
}
|
|
|
|
|
2006-09-01 05:06:00 +02:00
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
2008-04-13 20:54:36 +02:00
|
|
|
std::cout << "saving DHT state" << std::endl;
|
2007-10-02 21:46:14 +02:00
|
|
|
dht_state = ses.dht_state();
|
2006-08-01 17:27:08 +02:00
|
|
|
boost::filesystem::ofstream out(".dht_state"
|
|
|
|
, std::ios_base::binary);
|
|
|
|
out.unsetf(std::ios_base::skipws);
|
|
|
|
bencode(std::ostream_iterator<char>(out), dht_state);
|
2006-09-01 05:06:00 +02:00
|
|
|
#endif
|
2008-04-13 20:54:36 +02:00
|
|
|
std::cout << "closing session" << std::endl;
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
catch (std::exception& e)
|
|
|
|
{
|
|
|
|
std::cout << e.what() << "\n";
|
|
|
|
}
|
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
|
|
|
|