switch trackerRemove and trackerReplace rpc calls to use tracker id instead of announce urls as identifiers

This commit is contained in:
Daniel Lee
2010-09-14 06:23:48 +00:00
parent 0046f49fb5
commit ac53f4792f
8 changed files with 68 additions and 39 deletions

View File

@@ -1276,6 +1276,7 @@ printTrackersImpl( tr_benc * trackerStats )
tr_bool hasAnnounced; tr_bool hasAnnounced;
tr_bool hasScraped; tr_bool hasScraped;
const char * host; const char * host;
int64_t id;
tr_bool isBackup; tr_bool isBackup;
int64_t lastAnnouncePeerCount; int64_t lastAnnouncePeerCount;
const char * lastAnnounceResult; const char * lastAnnounceResult;
@@ -1300,6 +1301,7 @@ printTrackersImpl( tr_benc * trackerStats )
tr_bencDictFindBool( t, "hasAnnounced", &hasAnnounced ) && tr_bencDictFindBool( t, "hasAnnounced", &hasAnnounced ) &&
tr_bencDictFindBool( t, "hasScraped", &hasScraped ) && tr_bencDictFindBool( t, "hasScraped", &hasScraped ) &&
tr_bencDictFindStr ( t, "host", &host ) && tr_bencDictFindStr ( t, "host", &host ) &&
tr_bencDictFindInt ( t, "id", &id ) &&
tr_bencDictFindBool( t, "isBackup", &isBackup ) && tr_bencDictFindBool( t, "isBackup", &isBackup ) &&
tr_bencDictFindInt ( t, "announceState", &announceState ) && tr_bencDictFindInt ( t, "announceState", &announceState ) &&
tr_bencDictFindInt ( t, "scrapeState", &scrapeState ) && tr_bencDictFindInt ( t, "scrapeState", &scrapeState ) &&
@@ -1323,7 +1325,7 @@ printTrackersImpl( tr_benc * trackerStats )
const time_t now = time( NULL ); const time_t now = time( NULL );
printf( "\n" ); printf( "\n" );
printf( " Tracker #%d: %s\n", (int)(i+1), host ); printf( " Tracker %d: %s\n", (int)(id), host );
if( isBackup ) if( isBackup )
printf( " Backup on tier #%d\n", (int)tier ); printf( " Backup on tier #%d\n", (int)tier );
else else
@@ -2035,8 +2037,8 @@ processArgs( const char * host, int port, int argc, const char ** argv )
{ {
case 712: case 712:
{ {
tr_benc * trackers = tr_bencDictAddDict( args, "trackerRemove", 1 ); tr_benc * trackers = tr_bencDictAddList( args, "trackerRemove", 1 );
tr_bencDictAddInt( trackers, "id", atoi(optarg) ); tr_bencListAddInt( trackers, atoi(optarg) );
break; break;
} }
case 950: tr_bencDictAddReal( args, "seedRatioLimit", atof(optarg) ); case 950: tr_bencDictAddReal( args, "seedRatioLimit", atof(optarg) );

View File

@@ -105,8 +105,8 @@
"seedRatioLimit" | double torrent-level seeding ratio "seedRatioLimit" | double torrent-level seeding ratio
"seedRatioMode" | number which ratio to use. See tr_ratiolimit "seedRatioMode" | number which ratio to use. See tr_ratiolimit
"trackerAdd" | array strings of announce URLs to add "trackerAdd" | array strings of announce URLs to add
"trackerRemove" | array strings of announce URLs to remove "trackerRemove" | array ids of trackers to remove
"trackerReplace" | array pairs of old/new announce announce URLs "trackerReplace" | array pairs of <trackerId/new announce URLs>
"uploadLimit" | number maximum upload speed (KBps) "uploadLimit" | number maximum upload speed (KBps)
"uploadLimited" | boolean true if "uploadLimit" is honored "uploadLimited" | boolean true if "uploadLimit" is honored
@@ -616,8 +616,8 @@
------+---------+-----------+----------------+------------------------------- ------+---------+-----------+----------------+-------------------------------
10 | 2.10 | yes | session-get | new arg "cache-size" 10 | 2.10 | yes | session-get | new arg "cache-size"
| | yes | torrent-set | new arg "trackerAdd" | | yes | torrent-set | new arg "trackerAdd"
| | yes | torrent-set | new arg "trackerEdit"
| | yes | torrent-set | new arg "trackerRemove" | | yes | torrent-set | new arg "trackerRemove"
| | yes | torrent-set | new arg "trackerReplace"
| | yes | session-set | new arg "idle-seeding-limit" | | yes | session-set | new arg "idle-seeding-limit"
| | yes | session-set | new arg "idle-seeding-limit-enabled" | | yes | session-set | new arg "idle-seeding-limit-enabled"
| | yes | session-get | new arg "idle-seeding-limit" | | yes | session-get | new arg "idle-seeding-limit"

View File

@@ -847,7 +847,7 @@ addTrackerUrls( tr_torrent * tor, tr_benc * urls )
} }
static const char* static const char*
replaceTrackerUrls( tr_torrent * tor, tr_benc * urls ) replaceTrackers( tr_torrent * tor, tr_benc * urls )
{ {
int i; int i;
tr_benc * pair[2]; tr_benc * pair[2];
@@ -866,17 +866,16 @@ replaceTrackerUrls( tr_torrent * tor, tr_benc * urls )
while(((pair[0] = tr_bencListChild(urls,i))) && while(((pair[0] = tr_bencListChild(urls,i))) &&
((pair[1] = tr_bencListChild(urls,i+1)))) ((pair[1] = tr_bencListChild(urls,i+1))))
{ {
const char * oldval; int64_t pos;
const char * newval; const char * newval;
if( tr_bencGetStr( pair[0], &oldval ) if( tr_bencGetInt( pair[0], &pos )
&& tr_bencGetStr( pair[1], &newval ) && tr_bencGetStr( pair[1], &newval )
&& strcmp( oldval, newval )
&& tr_urlIsValid( newval, -1 ) && tr_urlIsValid( newval, -1 )
&& findAnnounceUrl( trackers, n, oldval, &i ) ) && pos < n )
{ {
tr_free( trackers[i].announce ); tr_free( trackers[pos].announce );
trackers[i].announce = tr_strdup( newval ); trackers[pos].announce = tr_strdup( newval );
changed = TRUE; changed = TRUE;
} }
@@ -893,10 +892,12 @@ replaceTrackerUrls( tr_torrent * tor, tr_benc * urls )
} }
static const char* static const char*
removeTrackerUrls( tr_torrent * tor, tr_benc * urls ) removeTrackers( tr_torrent * tor, tr_benc * ids )
{ {
int i; int i;
int n; int n;
int * tids;
int t = 0;
tr_benc * val; tr_benc * val;
tr_tracker_info * trackers; tr_tracker_info * trackers;
tr_bool changed = FALSE; tr_bool changed = FALSE;
@@ -905,20 +906,27 @@ removeTrackerUrls( tr_torrent * tor, tr_benc * urls )
/* make a working copy of the existing announce list */ /* make a working copy of the existing announce list */
n = inf->trackerCount; n = inf->trackerCount;
tids = tr_new0( int, n );
trackers = tr_new0( tr_tracker_info, n ); trackers = tr_new0( tr_tracker_info, n );
copyTrackers( trackers, inf->trackers, n ); copyTrackers( trackers, inf->trackers, n );
/* remove the ones specified in the urls list */ /* remove the ones specified in the urls list */
i = 0; i = 0;
while(( val = tr_bencListChild( urls, i++ ))) while(( val = tr_bencListChild( ids, i++ )))
{ {
int pos; int64_t pos;
const char * url; if( tr_bencGetInt( val, &pos ) && pos < n )
if( tr_bencGetStr( val, &url ) && findAnnounceUrl( trackers, n, url, &pos ) ) tids[t++] = pos;
{ }
tr_removeElementFromArray( trackers, pos, sizeof( tr_tracker_info ), n-- );
changed = TRUE; /* sort trackerIds because tr_removeElementFromArray changes indices as it removes */
} qsort( tids, t, sizeof(int), compareInt );
/* remove from largest trackerId to smallest */
while( t-- )
{
tr_removeElementFromArray( trackers, tids[t], sizeof( tr_tracker_info ), n-- );
changed = TRUE;
} }
if( !changed ) if( !changed )
@@ -927,6 +935,7 @@ removeTrackerUrls( tr_torrent * tor, tr_benc * urls )
errmsg = "error setting announce list"; errmsg = "error setting announce list";
freeTrackers( trackers, n ); freeTrackers( trackers, n );
tr_free( tids );
return errmsg; return errmsg;
} }
@@ -947,7 +956,7 @@ torrentSet( tr_session * session,
int64_t tmp; int64_t tmp;
double d; double d;
tr_benc * files; tr_benc * files;
tr_benc * urls; tr_benc * trackers;
tr_bool boolVal; tr_bool boolVal;
tr_torrent * tor = torrents[i]; tr_torrent * tor = torrents[i];
@@ -984,12 +993,12 @@ torrentSet( tr_session * session,
tr_torrentSetRatioLimit( tor, d ); tr_torrentSetRatioLimit( tor, d );
if( tr_bencDictFindInt( args_in, "seedRatioMode", &tmp ) ) if( tr_bencDictFindInt( args_in, "seedRatioMode", &tmp ) )
tr_torrentSetRatioMode( tor, tmp ); tr_torrentSetRatioMode( tor, tmp );
if( !errmsg && tr_bencDictFindList( args_in, "trackerAdd", &urls ) ) if( !errmsg && tr_bencDictFindList( args_in, "trackerAdd", &trackers ) )
errmsg = addTrackerUrls( tor, urls ); errmsg = addTrackerUrls( tor, trackers );
if( !errmsg && tr_bencDictFindList( args_in, "trackerRemove", &urls ) ) if( !errmsg && tr_bencDictFindList( args_in, "trackerRemove", &trackers ) )
errmsg = removeTrackerUrls( tor, urls ); errmsg = removeTrackers( tor, trackers );
if( !errmsg && tr_bencDictFindList( args_in, "trackerReplace", &urls ) ) if( !errmsg && tr_bencDictFindList( args_in, "trackerReplace", &trackers ) )
errmsg = replaceTrackerUrls( tor, urls ); errmsg = replaceTrackers( tor, trackers );
notify( session, TR_RPC_TORRENT_CHANGED, tor ); notify( session, TR_RPC_TORRENT_CHANGED, tor );
} }

View File

@@ -1296,7 +1296,7 @@ parseNumberSection( const char * str, int len, struct number_range * setme )
return success; return success;
} }
static int int
compareInt( const void * va, const void * vb ) compareInt( const void * va, const void * vb )
{ {
const int a = *(const int *)va; const int a = *(const int *)va;

View File

@@ -438,6 +438,8 @@ void tr_set_compare( const void * a, size_t aCount,
tr_set_func in_both_cb, tr_set_func in_both_cb,
void * userData ); void * userData );
int compareInt( const void * va, const void * vb );
void tr_sha1_to_hex( char * out, const uint8_t * sha1 ) TR_GNUC_NONNULL(1,2); void tr_sha1_to_hex( char * out, const uint8_t * sha1 ) TR_GNUC_NONNULL(1,2);
void tr_hex_to_sha1( uint8_t * out, const char * hex ) TR_GNUC_NONNULL(1,2); void tr_hex_to_sha1( uint8_t * out, const char * hex ) TR_GNUC_NONNULL(1,2);

View File

@@ -1035,11 +1035,9 @@ Details :: onEditTrackerClicked( )
QSet<int> ids; QSet<int> ids;
ids << trackerInfo.torrentId; ids << trackerInfo.torrentId;
QStringList urls; const QPair<int,QString> idUrl = qMakePair( trackerInfo.st.id, newval );
urls << trackerInfo.st.announce;
urls << newval;
mySession.torrentSet( ids, "trackerReplace", urls ); mySession.torrentSet( ids, "trackerReplace", idUrl );
getNewData( ); getNewData( );
} }
} }
@@ -1050,21 +1048,23 @@ Details :: onRemoveTrackerClicked( )
// make a map of torrentIds to announce URLs to remove // make a map of torrentIds to announce URLs to remove
QItemSelectionModel * selectionModel = myTrackerView->selectionModel( ); QItemSelectionModel * selectionModel = myTrackerView->selectionModel( );
QModelIndexList selectedRows = selectionModel->selectedRows( ); QModelIndexList selectedRows = selectionModel->selectedRows( );
QMap<int,QStringList> torrentId_to_urls; QMap<int,int> torrentId_to_trackerIds;
foreach( QModelIndex i, selectedRows ) foreach( QModelIndex i, selectedRows )
{ {
const TrackerInfo inf = myTrackerView->model()->data( i, TrackerModel::TrackerRole ).value<TrackerInfo>(); const TrackerInfo inf = myTrackerView->model()->data( i, TrackerModel::TrackerRole ).value<TrackerInfo>();
torrentId_to_urls[ inf.torrentId ].append( inf.st.announce ); torrentId_to_trackerIds.insertMulti( inf.torrentId, inf.st.id );
} }
// batch all of a tracker's torrents into one command // batch all of a tracker's torrents into one command
foreach( int id, torrentId_to_urls.keys( ) ) foreach( int id, torrentId_to_trackerIds.uniqueKeys( ) )
{ {
QSet<int> ids; QSet<int> ids;
ids << id; ids << id;
mySession.torrentSet( ids, "trackerRemove", torrentId_to_urls.value( id ) ); mySession.torrentSet( ids, "trackerRemove", torrentId_to_trackerIds.values( id ) );
getNewData( );
} }
selectionModel->clearSelection( );
getNewData( );
} }
QWidget * QWidget *

View File

@@ -451,6 +451,21 @@ Session :: torrentSet( const QSet<int>& ids, const QString& key, const QList<int
tr_bencFree( &top ); tr_bencFree( &top );
} }
void
Session :: torrentSet( const QSet<int>& ids, const QString& key, const QPair<int,QString>& value )
{
tr_benc top;
tr_bencInitDict( &top, 2 );
tr_bencDictAddStr( &top, "method", "torrent-set" );
tr_benc * args( tr_bencDictAddDict( &top, "arguments", 2 ) );
addOptionalIds( args, ids );
tr_benc * list( tr_bencDictAddList( args, key.toUtf8().constData(), 2 ) );
tr_bencListAddInt( list, value.first );
tr_bencListAddStr( list, value.second.toUtf8().constData() );
exec( &top );
tr_bencFree( &top );
}
void void
Session :: torrentSetLocation( const QSet<int>& ids, const QString& location, bool doMove ) Session :: torrentSetLocation( const QSet<int>& ids, const QString& location, bool doMove )
{ {

View File

@@ -97,6 +97,7 @@ class Session: public QObject
void torrentSet( const QSet<int>& ids, const QString& key, double val ); void torrentSet( const QSet<int>& ids, const QString& key, double val );
void torrentSet( const QSet<int>& ids, const QString& key, const QList<int>& val ); void torrentSet( const QSet<int>& ids, const QString& key, const QList<int>& val );
void torrentSet( const QSet<int>& ids, const QString& key, const QStringList& val ); void torrentSet( const QSet<int>& ids, const QString& key, const QStringList& val );
void torrentSet( const QSet<int>& ids, const QString& key, const QPair<int,QString>& val);
void torrentSetLocation( const QSet<int>& ids, const QString& path, bool doMove ); void torrentSetLocation( const QSet<int>& ids, const QString& path, bool doMove );