clean up piece picker interface to make piece_pos private again

This commit is contained in:
Arvid Norberg 2015-02-08 22:15:59 +00:00
parent 1e80629770
commit 7395a91bf4
3 changed files with 35 additions and 24 deletions

View File

@ -108,8 +108,6 @@ namespace libtorrent
{ {
public: public:
struct piece_pos;
enum enum
{ {
// the number of priority levels // the number of priority levels
@ -374,11 +372,15 @@ namespace libtorrent
// returns information about the given piece // returns information about the given piece
void piece_info(int index, piece_picker::downloading_piece& st) const; void piece_info(int index, piece_picker::downloading_piece& st) const;
piece_pos const& piece_stats(int index) const struct piece_stats_t
{ {
TORRENT_ASSERT(index >= 0 && index < int(m_piece_map.size())); int peer_count;
return m_piece_map[index]; int priority;
} bool have;
bool downloading;
};
piece_stats_t piece_stats(int index) const;
// if a piece had a hash-failure, it must be restored and // if a piece had a hash-failure, it must be restored and
// made available for redownloading // made available for redownloading
@ -480,9 +482,6 @@ namespace libtorrent
std::pair<int, int> expand_piece(int piece, int whole_pieces std::pair<int, int> expand_piece(int piece, int whole_pieces
, bitfield const& have, int options) const; , bitfield const& have, int options) const;
public:
// TODO: 2 this type should not be public
struct piece_pos struct piece_pos
{ {
piece_pos() {} piece_pos() {}
@ -697,8 +696,6 @@ namespace libtorrent
{ return index == p.index && peer_count == p.peer_count; } { return index == p.index && peer_count == p.peer_count; }
}; };
private:
#ifndef TORRENT_DEBUG_REFCOUNTS #ifndef TORRENT_DEBUG_REFCOUNTS
#if TORRENT_OPTIMIZE_MEMORY_USAGE #if TORRENT_OPTIMIZE_MEMORY_USAGE
BOOST_STATIC_ASSERT(sizeof(piece_pos) == sizeof(char) * 4); BOOST_STATIC_ASSERT(sizeof(piece_pos) == sizeof(char) * 4);

View File

@ -169,6 +169,19 @@ namespace libtorrent
st.finished = 0; st.finished = 0;
} }
piece_picker::piece_stats_t piece_picker::piece_stats(int index) const
{
TORRENT_ASSERT(index >= 0 && index < int(m_piece_map.size()));
piece_pos const& pp = m_piece_map[index];
piece_stats_t ret = {
pp.peer_count + m_seeds,
pp.priority(this),
pp.have(),
pp.downloading()
};
return ret;
}
piece_picker::dlpiece_iter piece_picker::add_download_piece(int piece) piece_picker::dlpiece_iter piece_picker::add_download_piece(int piece)
{ {
TORRENT_ASSERT(piece >= 0); TORRENT_ASSERT(piece >= 0);

View File

@ -9855,25 +9855,25 @@ namespace libtorrent
bool prio_updated = false; bool prio_updated = false;
for (int i = 0; i < num_pieces; ++i) for (int i = 0; i < num_pieces; ++i)
{ {
piece_picker::piece_pos const& pp = m_picker->piece_stats(i); piece_picker::piece_stats_t ps = m_picker->piece_stats(i);
if (pp.peer_count == 0) continue; if (ps.peer_count == 0) continue;
if (pp.filtered() && (pp.have() || pp.downloading())) if (ps.priority == 0 && (ps.have || ps.downloading))
{ {
m_picker->set_piece_priority(i, 1); m_picker->set_piece_priority(i, 1);
prio_updated = true; prio_updated = true;
continue; continue;
} }
// don't count pieces we already have or are downloading // don't count pieces we already have or are trying to download
if (!pp.filtered() || pp.have()) continue; if (ps.priority > 0 || ps.have) continue;
if (int(pp.peer_count) > rarest_rarity) continue; if (int(ps.peer_count) > rarest_rarity) continue;
if (int(pp.peer_count) == rarest_rarity) if (int(ps.peer_count) == rarest_rarity)
{ {
rarest_pieces.push_back(i); rarest_pieces.push_back(i);
continue; continue;
} }
rarest_pieces.clear(); rarest_pieces.clear();
rarest_rarity = pp.peer_count; rarest_rarity = ps.peer_count;
rarest_pieces.push_back(i); rarest_pieces.push_back(i);
} }
@ -9886,14 +9886,16 @@ namespace libtorrent
// if there's only a single peer that doesn't have the rarest piece // if there's only a single peer that doesn't have the rarest piece
// it's impossible for us to download one piece and upload it // it's impossible for us to download one piece and upload it
// twice. i.e. we cannot get a positive share ratio // twice. i.e. we cannot get a positive share ratio
if (num_peers - rarest_rarity < settings().get_int(settings_pack::share_mode_target)) return; if (num_peers - rarest_rarity
< settings().get_int(settings_pack::share_mode_target))
return;
// we might be able to do better than a share ratio of 2 if there are // we might be able to do better than a share ratio of 2 if there are
// enough downloaders of the pieces we already have. // enough downloaders of the pieces we already have.
// TODO: go through the pieces we have and count the total number // TODO: 2 go through the pieces we have and count the total number
// of downloaders we have. Only count peers that are interested in us // of downloaders we have. Only count peers that are interested in us
// since some peers might not send have messages for pieces we have // since some peers might not send have messages for pieces we have.
// it num_interested == 0, we need to pick a new piece // if num_interested == 0, we need to pick a new piece
// now, pick one of the rarest pieces to download // now, pick one of the rarest pieces to download
int pick = random() % rarest_pieces.size(); int pick = random() % rarest_pieces.size();
@ -9901,7 +9903,6 @@ namespace libtorrent
m_picker->set_piece_priority(rarest_pieces[pick], 1); m_picker->set_piece_priority(rarest_pieces[pick], 1);
update_gauge(); update_gauge();
update_peer_interest(was_finished); update_peer_interest(was_finished);
update_want_peers(); update_want_peers();
} }