Changeset 12133


Ignore:
Timestamp:
Mar 11, 2011, 3:11:37 PM (11 years ago)
Author:
jordan
Message:

(trunk libT) implement multiscrape.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/announcer.c

    r12131 r12133  
    330330    t->announceIntervalSec = DEFAULT_ANNOUNCE_INTERVAL_SEC;
    331331    t->announceMinIntervalSec = DEFAULT_ANNOUNCE_MIN_INTERVAL_SEC;
    332     t->scrapeAt = now + tr_cryptoWeakRandInt( 60*5 );
     332    t->scrapeAt = now + tr_cryptoWeakRandInt( 60*3 );
    333333    t->tor = tor;
    334334
     
    12221222
    12231223static void
    1224 tierScrape( tr_announcer * announcer, tr_tier * tier )
    1225 {
    1226     tr_scrape_request request;
    1227 
    1228     /* sanity clause */
    1229     assert( tier );
    1230     assert( !tier->isScraping );
    1231     assert( tier->currentTracker != NULL );
    1232     assert( tr_isTorrent( tier->tor ) );
    1233 
    1234     /* initialize the request */
    1235     request.url = tier->currentTracker->scrape;
    1236     request.info_hash_count = 1;
    1237     memcpy( request.info_hash[0], tier->tor->info.hash, SHA_DIGEST_LENGTH );
    1238     tier_build_log_name( tier, request.log_name, sizeof( request.log_name ) );
    1239 
    1240     /* start scraping */
    1241     dbgmsg( tier, "scraping \"%s\"", request.url );
    1242     tier->isScraping = TRUE;
    1243     tier->lastScrapeStartTime = tr_time( );
    1244     --announcer->slotsAvailable;
    1245     scrape_request_delegate( announcer, &request, on_scrape_done, NULL );
     1224multiscrape( tr_announcer * announcer, tr_ptrArray * tiers )
     1225{
     1226    int i;
     1227    int request_count = 0;
     1228    const time_t now = tr_time( );
     1229    const int tier_count = tr_ptrArraySize( tiers );
     1230    const int max_request_count = MIN( announcer->slotsAvailable, tier_count );
     1231    tr_scrape_request * requests = tr_new0( tr_scrape_request, max_request_count );
     1232
     1233    /* batch as many info_hashes into a request as we can */
     1234    for( i=0; i<tier_count; ++i )
     1235    {
     1236        int j;
     1237        tr_tier * tier = tr_ptrArrayNth( tiers, i );
     1238        char * url = tier->currentTracker->scrape;
     1239        const uint8_t * hash = tier->tor->info.hash;
     1240
     1241        /* if there's a request with this scrape URL and a free slot, use it */
     1242        for( j=0; j<request_count; ++j )
     1243        {
     1244            tr_scrape_request * req = &requests[j];
     1245
     1246            if( req->info_hash_count >= TR_MULTISCRAPE_MAX )
     1247                continue;
     1248            if( tr_strcmp0( req->url, url ) )
     1249                continue;
     1250
     1251            memcpy( req->info_hash[req->info_hash_count++], hash, SHA_DIGEST_LENGTH );
     1252            tier->isScraping = TRUE;
     1253            tier->lastScrapeStartTime = now;
     1254            break;
     1255        }
     1256
     1257        /* otherwise, if there's room for another request, build a new one */
     1258        if( ( j==request_count ) && ( request_count < max_request_count ) )
     1259        {
     1260            tr_scrape_request * req = &requests[request_count++];
     1261            req->url = url;
     1262            tier_build_log_name( tier, req->log_name, sizeof( req->log_name ) );
     1263
     1264            memcpy( req->info_hash[req->info_hash_count++], hash, SHA_DIGEST_LENGTH );
     1265            tier->isScraping = TRUE;
     1266            tier->lastScrapeStartTime = now;
     1267        }
     1268    }
     1269
     1270    /* send the requests we just built */
     1271    for( i=0; i<request_count; ++i )
     1272        scrape_request_delegate( announcer, &requests[i], on_scrape_done, NULL );
     1273
     1274    /* cleanup */
     1275    tr_free( requests );
    12461276}
    12471277
     
    13391369
    13401370        /* scrape some */
    1341         /* FIXME: multiscrape */
    1342         n = MIN( tr_ptrArraySize( &scrapeMe ), announcer->slotsAvailable );
    1343         for( i=0; i<n; ++i ) {
    1344             tr_tier * tier = tr_ptrArrayNth( &scrapeMe, i );
    1345             dbgmsg( tier, "scraping tier %d of %d", (i+1), n );
    1346             tierScrape( announcer, tier );
    1347         }
     1371        multiscrape( announcer, &scrapeMe );
    13481372
    13491373#if 0
Note: See TracChangeset for help on using the changeset viewer.