try to reduce the level of useless dht tracker requests by not announcing empty torrents we do not follow.

This commit is contained in:
Miguel Freitas 2014-01-06 21:59:40 -02:00
parent 4eab7e4d81
commit 8e80324c36
5 changed files with 30 additions and 7 deletions

View File

@ -513,6 +513,7 @@ namespace libtorrent
} }
void super_seeding(bool on); void super_seeding(bool on);
void set_following(bool on);
int get_piece_to_super_seed(bitfield const&); int get_piece_to_super_seed(bitfield const&);
// returns true if we have downloaded the given piece // returns true if we have downloaded the given piece
@ -1219,6 +1220,9 @@ namespace libtorrent
// if this is true, we're currently super seeding this // if this is true, we're currently super seeding this
// torrent. // torrent.
bool m_super_seeding:1; bool m_super_seeding:1;
// if this is true, we're currently following this user
bool m_following:1;
// this is set when we don't want to load seed_mode, // this is set when we don't want to load seed_mode,
// paused or auto_managed from the resume data // paused or auto_managed from the resume data

View File

@ -412,6 +412,7 @@ namespace libtorrent
#endif #endif
void super_seeding(bool on) const; void super_seeding(bool on) const;
void set_following(bool on) const;
sha1_hash info_hash() const; sha1_hash info_hash() const;

View File

@ -387,6 +387,7 @@ namespace libtorrent
, m_got_tracker_response(false) , m_got_tracker_response(false)
, m_connections_initialized(false) , m_connections_initialized(false)
, m_super_seeding(false) , m_super_seeding(false)
, m_following(false)
, m_override_resume_data(p.flags & add_torrent_params::flag_override_resume_data) , m_override_resume_data(p.flags & add_torrent_params::flag_override_resume_data)
#ifndef TORRENT_DISABLE_RESOLVE_COUNTRIES #ifndef TORRENT_DISABLE_RESOLVE_COUNTRIES
, m_resolving_country(false) , m_resolving_country(false)
@ -827,6 +828,10 @@ namespace libtorrent
// because the info-hash is just the URL hash // because the info-hash is just the URL hash
if (!m_torrent_file->is_valid() && !m_url.empty()) return false; if (!m_torrent_file->is_valid() && !m_url.empty()) return false;
// try to reduce the level of useless dht tracker requests by not
// announcing empty torrents we do not follow.
if (!m_following && last_have() == -1 ) return false;
// don't announce private torrents // don't announce private torrents
if (m_torrent_file->is_valid() && m_torrent_file->priv()) return false; if (m_torrent_file->is_valid() && m_torrent_file->priv()) return false;
if (m_trackers.empty()) return true; if (m_trackers.empty()) return true;
@ -3509,6 +3514,11 @@ namespace libtorrent
} }
} }
void torrent::set_following(bool on)
{
m_following = on;
}
int torrent::get_piece_to_super_seed(bitfield const& bits) int torrent::get_piece_to_super_seed(bitfield const& bits)
{ {
// return a piece with low availability that is not in // return a piece with low availability that is not in

View File

@ -975,6 +975,12 @@ namespace libtorrent
TORRENT_ASYNC_CALL1(super_seeding, on); TORRENT_ASYNC_CALL1(super_seeding, on);
} }
void torrent_handle::set_following(bool on) const
{
INVARIANT_CHECK;
TORRENT_ASYNC_CALL1(set_following, on);
}
void torrent_handle::resolve_countries(bool r) void torrent_handle::resolve_countries(bool r)
{ {
INVARIANT_CHECK; INVARIANT_CHECK;

View File

@ -72,7 +72,7 @@ sha1_hash dhtTargetHash(std::string const &username, std::string const &resource
return hasher(buf.data(), buf.size()).final(); return hasher(buf.data(), buf.size()).final();
} }
torrent_handle startTorrentUser(std::string const &username) torrent_handle startTorrentUser(std::string const &username, bool following)
{ {
bool userInTxDb = usernameExists(username); // keep this outside cs_twister to avoid deadlock bool userInTxDb = usernameExists(username); // keep this outside cs_twister to avoid deadlock
if( !userInTxDb ) if( !userInTxDb )
@ -96,6 +96,8 @@ torrent_handle startTorrentUser(std::string const &username)
m_userTorrent[username] = ses->add_torrent(tparams); m_userTorrent[username] = ses->add_torrent(tparams);
m_userTorrent[username].force_dht_announce(); m_userTorrent[username].force_dht_announce();
} }
if( following )
m_userTorrent[username].set_following(true);
return m_userTorrent[username]; return m_userTorrent[username];
} }
@ -298,7 +300,7 @@ void ThreadWaitExtIP()
} }
// now restart the user torrents // now restart the user torrents
BOOST_FOREACH(string username, torrentsToStart) { BOOST_FOREACH(string username, torrentsToStart) {
startTorrentUser(username); startTorrentUser(username, true);
} }
} }
@ -601,7 +603,7 @@ void ThreadSessionAlerts()
neighborCheck.count(ih) ) { neighborCheck.count(ih) ) {
// Do something! // Do something!
if( dd->m_resource == "tracker" ) { if( dd->m_resource == "tracker" ) {
startTorrentUser(dd->m_username); startTorrentUser(dd->m_username, false);
} else { } else {
printf("Neighbor of special resource - do something!\n"); printf("Neighbor of special resource - do something!\n");
} }
@ -1285,7 +1287,7 @@ Value newpostmsg(const Array& params, bool fHelp)
if( !acceptSignedPost(buf.data(),buf.size(),strUsername,k,errmsg,NULL) ) if( !acceptSignedPost(buf.data(),buf.size(),strUsername,k,errmsg,NULL) )
throw JSONRPCError(RPC_INVALID_PARAMS,errmsg); throw JSONRPCError(RPC_INVALID_PARAMS,errmsg);
torrent_handle h = startTorrentUser(strUsername); torrent_handle h = startTorrentUser(strUsername, true);
if( h.is_valid() ) { if( h.is_valid() ) {
// if member of torrent post it directly // if member of torrent post it directly
h.add_piece(k,buf.data(),buf.size()); h.add_piece(k,buf.data(),buf.size());
@ -1371,7 +1373,7 @@ Value newdirectmsg(const Array& params, bool fHelp)
m_users[strFrom].m_directmsg[strTo].push_back(stoDM); m_users[strFrom].m_directmsg[strTo].push_back(stoDM);
} }
torrent_handle h = startTorrentUser(strFrom); torrent_handle h = startTorrentUser(strFrom, true);
h.add_piece(k,buf.data(),buf.size()); h.add_piece(k,buf.data(),buf.size());
hexcapePost(v); hexcapePost(v);
@ -1413,7 +1415,7 @@ Value newrtmsg(const Array& params, bool fHelp)
if( !acceptSignedPost(buf.data(),buf.size(),strUsername,k,errmsg,NULL) ) if( !acceptSignedPost(buf.data(),buf.size(),strUsername,k,errmsg,NULL) )
throw JSONRPCError(RPC_INVALID_PARAMS,errmsg); throw JSONRPCError(RPC_INVALID_PARAMS,errmsg);
torrent_handle h = startTorrentUser(strUsername); torrent_handle h = startTorrentUser(strUsername, true);
if( h.is_valid() ) { if( h.is_valid() ) {
// if member of torrent post it directly // if member of torrent post it directly
h.add_piece(k,buf.data(),buf.size()); h.add_piece(k,buf.data(),buf.size());
@ -1618,7 +1620,7 @@ Value follow(const Array& params, bool fHelp)
for( unsigned int u = 0; u < users.size(); u++ ) { for( unsigned int u = 0; u < users.size(); u++ ) {
string username = users[u].get_str(); string username = users[u].get_str();
torrent_handle h = startTorrentUser(username); torrent_handle h = startTorrentUser(username, true);
if( h.is_valid() ) { if( h.is_valid() ) {
LOCK(cs_twister); LOCK(cs_twister);