From 7fe3323cedbc6259242ab58f467d6ac10a414c65 Mon Sep 17 00:00:00 2001 From: Magnus Jonsson Date: Thu, 5 Aug 2004 13:56:26 +0000 Subject: [PATCH] added num_seeds and distributed_copies to torrent_status --- docs/manual.rst | 15 +++++++++++++++ examples/client_test.cpp | 19 ++++++++++--------- include/libtorrent/piece_picker.hpp | 2 ++ include/libtorrent/torrent.hpp | 1 + include/libtorrent/torrent_handle.hpp | 16 ++++++++++++++++ src/piece_picker.cpp | 12 ++++++++++++ src/torrent.cpp | 12 +++++++++++- 7 files changed, 67 insertions(+), 10 deletions(-) diff --git a/docs/manual.rst b/docs/manual.rst index a6fe60a68..87d16485d 100755 --- a/docs/manual.rst +++ b/docs/manual.rst @@ -1082,6 +1082,9 @@ It contains the following fields:: const std::vector* pieces; size_type total_done; + + int num_seeds; + float distributed_copies; }; ``progress`` is a value in the range [0, 1], that represents the progress of the @@ -1151,6 +1154,18 @@ be slightly smaller than the other rates, but if projected over a long time this does not necessarily has to be downloaded during this session (that's ``total_download_payload``). +``num_seeds`` is the number of peers that are seeding that this torrent +currently is connected to. + +``distributed_copies`` is the number of distributed copies of the torrent. +Note that one copy may be spread out among many peers. The whole number part +tells how many copies there are currently of the rarest piece(s) among the +peers this torrent is connected to. The fractional part tells the share of +pieces that have more copies than the rarest piece(s). For example: 2.5 would +mean that the rarest pieces have only 2 copies among the peers this torrent is +connected to, and that there are as many pieces that have 2 copies as there are +pieces that have more than two copies. + peer_info diff --git a/examples/client_test.cpp b/examples/client_test.cpp index bba615306..4b2fd92e9 100755 --- a/examples/client_test.cpp +++ b/examples/client_test.cpp @@ -199,7 +199,7 @@ std::string add_suffix(float val) { if (fabs(val) < 1000.f) return to_string(val, i==0?7:6) + prefix[i]; - val /= 1000.f; + val /= 1024.f; } return to_string(val, 6) + "PB"; } @@ -282,8 +282,7 @@ int main(int argc, char* argv[]) session ses(fingerprint("LT", 0, 1, 0, 0)); ses.listen_on(std::make_pair(6881, 6889)); - ses.set_upload_rate_limit(31 * 1024); -// ses.set_download_rate_limit(50000); + ses.set_upload_rate_limit(512 * 1024); ses.set_http_settings(settings); ses.set_severity_level(alert::debug); // ses.set_severity_level(alert::info); @@ -301,7 +300,7 @@ int main(int argc, char* argv[]) handles.push_back(ses.add_torrent(argv[i+1], info_hash, save_path)); handles.back().set_max_connections(60); handles.back().set_max_uploads(7); - handles.back().set_ratio(1.02f); + handles.back().set_ratio(1.1f); ++i; continue; @@ -328,8 +327,8 @@ int main(int argc, char* argv[]) handles.push_back(ses.add_torrent(e, save_path, resume_data)); handles.back().set_max_connections(100); - handles.back().set_max_uploads(3); - handles.back().set_ratio(0.f); + handles.back().set_max_uploads(-1); + handles.back().set_ratio(1.02f); } catch (std::exception& e) { @@ -484,10 +483,12 @@ int main(int argc, char* argv[]) out << progress_bar(s.progress, 49); out << "\n"; out << "total downloaded: " << s.total_done << " Bytes\n"; - out << "peers: " << (int)peers.size() << " " - << "d:" << add_suffix(s.download_rate) << "/s " + out << "peers: " << s.num_peers << " " + << "seeds: " << s.num_seeds << " " + << "distributed copies: " << s.distributed_copies << "\n"; + out << "download:" << add_suffix(s.download_rate) << "/s " << "(" << add_suffix(s.total_download) << ") " - << "u:" << add_suffix(s.upload_rate) << "/s " + << "upload:" << add_suffix(s.upload_rate) << "/s " << "(" << add_suffix(s.total_upload) << ") " << "ratio: " << ratio(s.total_payload_download, s.total_payload_upload) << "\n"; out << "info-hash: " << i->info_hash() << "\n"; diff --git a/include/libtorrent/piece_picker.hpp b/include/libtorrent/piece_picker.hpp index 84364adc9..ebf179449 100755 --- a/include/libtorrent/piece_picker.hpp +++ b/include/libtorrent/piece_picker.hpp @@ -199,6 +199,8 @@ namespace libtorrent int blocks_in_last_piece() const { return m_blocks_in_last_piece; } + float distributed_copies() const; + private: struct piece_pos diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index 027270172..bc5c7dff6 100755 --- a/include/libtorrent/torrent.hpp +++ b/include/libtorrent/torrent.hpp @@ -169,6 +169,7 @@ namespace libtorrent // the number of peers that belong to this torrent int num_peers() const { return (int)m_connections.size(); } + int num_seeds() const; typedef std::map::iterator peer_iterator; typedef std::map::const_iterator const_peer_iterator; diff --git a/include/libtorrent/torrent_handle.hpp b/include/libtorrent/torrent_handle.hpp index f8f0fdd0b..115e3b02f 100755 --- a/include/libtorrent/torrent_handle.hpp +++ b/include/libtorrent/torrent_handle.hpp @@ -85,6 +85,8 @@ namespace libtorrent , num_peers(0) , pieces(0) , total_done(0) + , num_seeds(0) + , distributed_copies(0.f) {} enum state_t @@ -136,6 +138,20 @@ namespace libtorrent // the number of bytes of the file we have size_type total_done; + + // the number of peers this torrent is connected to + // that are seeding. + int num_seeds; + + // the number of distributed copies of the file. + // note that one copy may be spread out among many peers. + // + // the whole number part tells how many copies + // there are of the rarest piece(s) + // + // the fractional part tells the fraction of pieces that + // have more copies than the rarest piece(s). + float distributed_copies; }; struct partial_piece_info diff --git a/src/piece_picker.cpp b/src/piece_picker.cpp index cc60d1b33..e08d018f1 100755 --- a/src/piece_picker.cpp +++ b/src/piece_picker.cpp @@ -247,6 +247,18 @@ namespace libtorrent } #endif + float piece_picker::distributed_copies() const + { + for(int count=0;count<(int)m_piece_info.size();count++) + { + if(!m_piece_info[count].empty()) { + float fraction_above_count = + 1.f - float(m_piece_info[count].size())/float(m_piece_map.size()); + return count+fraction_above_count; + } + } + return 0.f; + } void piece_picker::move(bool downloading, int peer_count, int elem_index) { diff --git a/src/torrent.cpp b/src/torrent.cpp index d4bcf5133..b65c072ae 100755 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -45,6 +45,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include +#include #ifdef _MSC_VER #pragma warning(pop) @@ -872,7 +873,7 @@ namespace libtorrent torrent_status st; - st.num_peers = (int)m_connections.size(); + st.num_peers = num_peers(); st.paused = m_paused; st.total_done = bytes_done(); @@ -934,9 +935,18 @@ namespace libtorrent else st.state = torrent_status::downloading; + st.num_seeds = num_seeds(); + st.distributed_copies = m_picker->distributed_copies(); return st; } + int torrent::num_seeds() const + { + return (int)count_if(m_connections.begin(), m_connections.end(), + boost::bind(&peer_connection::is_seed, + boost::bind(&std::map::value_type::second, _1))); + } + bool torrent::received_metadata(char const* buf, int size, int offset, int total_size) { INVARIANT_CHECK;