From 98903d5a7370f6313887d106ef24c19a09c85588 Mon Sep 17 00:00:00 2001 From: Miguel Freitas Date: Wed, 18 Sep 2013 08:47:26 -0300 Subject: [PATCH] keep track of last "have" for each torrent --- libtorrent/include/libtorrent/piece_picker.hpp | 9 +++++++-- libtorrent/include/libtorrent/torrent.hpp | 7 +++++++ libtorrent/include/libtorrent/torrent_handle.hpp | 3 +++ libtorrent/src/piece_picker.cpp | 9 +++++++++ libtorrent/src/torrent.cpp | 1 + libtorrent/src/torrent_handle.cpp | 1 + src/twister.cpp | 10 ++++++++++ 7 files changed, 38 insertions(+), 2 deletions(-) diff --git a/libtorrent/include/libtorrent/piece_picker.hpp b/libtorrent/include/libtorrent/piece_picker.hpp index dc14c0e3..88468920 100644 --- a/libtorrent/include/libtorrent/piece_picker.hpp +++ b/libtorrent/include/libtorrent/piece_picker.hpp @@ -212,8 +212,8 @@ namespace libtorrent // sets all pieces to dont-have void init(int blocks_per_piece, int blocks_in_last_piece, int total_num_pieces); - void increase_num_pieces(int total_num_pieces); - int num_pieces() const { return int(m_piece_map.size()); } + void increase_num_pieces(int total_num_pieces); + int num_pieces() const { return int(m_piece_map.size()); } bool have_piece(int index) const { @@ -357,6 +357,8 @@ namespace libtorrent int num_have() const { return m_num_have; } + int last_have() const { return m_last_have; } + // the number of pieces we want and don't have int num_want_left() const { return num_pieces() - m_num_have - m_num_filtered; } @@ -611,6 +613,9 @@ namespace libtorrent // the number of pieces we have int m_num_have; + // the index of last have + int m_last_have; + // we have all pieces in the range [0, m_cursor) // m_cursor is the first piece we don't have int m_cursor; diff --git a/libtorrent/include/libtorrent/torrent.hpp b/libtorrent/include/libtorrent/torrent.hpp index 14597bc6..54e7103b 100644 --- a/libtorrent/include/libtorrent/torrent.hpp +++ b/libtorrent/include/libtorrent/torrent.hpp @@ -529,6 +529,13 @@ namespace libtorrent : m_torrent_file->num_pieces(); } + int last_have() const + { + return has_picker() + ? m_picker->last_have() + : -1; + } + // when we get a have message, this is called for that piece void peer_has(int index, peer_connection const* peer) { diff --git a/libtorrent/include/libtorrent/torrent_handle.hpp b/libtorrent/include/libtorrent/torrent_handle.hpp index e2aa5e02..c4574ead 100644 --- a/libtorrent/include/libtorrent/torrent_handle.hpp +++ b/libtorrent/include/libtorrent/torrent_handle.hpp @@ -558,6 +558,9 @@ namespace libtorrent // std::accumulate(pieces->begin(), pieces->end()); int num_pieces; + // index of last piece we have + int last_have; + // the number of bytes of the file we have // including pieces that may have been filtered // after we downloaded them diff --git a/libtorrent/src/piece_picker.cpp b/libtorrent/src/piece_picker.cpp index e86e78ad..9ac7e09e 100644 --- a/libtorrent/src/piece_picker.cpp +++ b/libtorrent/src/piece_picker.cpp @@ -71,6 +71,7 @@ namespace libtorrent , m_num_filtered(0) , m_num_have_filtered(0) , m_num_have(0) + , m_last_have(-1) , m_cursor(0) , m_reverse_cursor(0) , m_sparse_regions(1) @@ -104,6 +105,7 @@ namespace libtorrent m_num_filtered += m_num_have_filtered; m_num_have_filtered = 0; m_num_have = 0; + m_last_have = -1; m_dirty = true; for (std::vector::iterator i = m_piece_map.begin() , end(m_piece_map.end()); i != end; ++i) @@ -1224,6 +1226,11 @@ namespace libtorrent --m_num_have; p.set_not_have(); + if (index == m_last_have) { + while( !m_piece_map[m_last_have].have() && m_last_have >= 0 ) + m_last_have--; + } + if (m_dirty) return; if (p.priority(this) >= 0) add(index); } @@ -1288,6 +1295,8 @@ namespace libtorrent } ++m_num_have; p.set_have(); + if (index > m_last_have) + m_last_have = index; if (m_cursor == m_reverse_cursor - 1 && m_cursor == index) { diff --git a/libtorrent/src/torrent.cpp b/libtorrent/src/torrent.cpp index ca1897d8..ab8ce3ca 100644 --- a/libtorrent/src/torrent.cpp +++ b/libtorrent/src/torrent.cpp @@ -8537,6 +8537,7 @@ namespace libtorrent st->pieces.resize(num_pieces, true); } st->num_pieces = num_have(); + st->last_have = last_have(); st->num_seeds = num_seeds(); if ((flags & torrent_handle::query_distributed_copies) && m_picker.get()) { diff --git a/libtorrent/src/torrent_handle.cpp b/libtorrent/src/torrent_handle.cpp index aa606734..7f6087f9 100644 --- a/libtorrent/src/torrent_handle.cpp +++ b/libtorrent/src/torrent_handle.cpp @@ -104,6 +104,7 @@ namespace libtorrent , list_peers(0) , connect_candidates(0) , num_pieces(0) + , last_have(-1) , total_done(0) , total_wanted_done(0) , total_wanted(0) diff --git a/src/twister.cpp b/src/twister.cpp index acb216f5..880fb1a8 100644 --- a/src/twister.cpp +++ b/src/twister.cpp @@ -147,10 +147,20 @@ torrent_handle startTorrentUser(std::string const &username) m_userTorrent[username] = ses->add_torrent(tparams); m_userTorrent[username].force_dht_announce(); + torrent_status status = m_userTorrent[username].status(); } return m_userTorrent[username]; } +int lastPostKfromTorrent(std::string const &username) +{ + if( !m_userTorrent.count(username) ) + return -1; + + torrent_status status = m_userTorrent[username].status(); + return status.last_have; +} + void ThreadWaitExtIP() { RenameThread("wait-extip");