Changeset 12160


Ignore:
Timestamp:
Mar 14, 2011, 2:40:39 AM (11 years ago)
Author:
jordan
Message:

(trunk libT) more tweakin'

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/announcer.c

    r12144 r12160  
    2323#include "announcer-common.h"
    2424#include "crypto.h"
    25 #include "net.h"
    2625#include "peer-mgr.h" /* tr_peerMgrCompactToPex() */
    2726#include "ptrarray.h"
     
    3130#include "torrent.h"
    3231#include "utils.h"
    33 #include "web.h"
    3432
    3533struct tr_tier;
     
    123121typedef struct tr_announcer
    124122{
    125    /* Since we can't poll a tr_torrent's fields after it's destroyed,
    126     * we pre-build the "stop" tr_announce_request when a torrent is
    127     * removed from Transmission */
    128     tr_ptrArray stops;
     123    tr_ptrArray stops; /* tr_announce_request */
    129124
    130125    tr_session * session;
     
    144139
    145140static inline time_t
    146 valPlusJitter( const int minPeriod )
     141jitterize( const int val )
    147142{
    148143    const double jitter = 0.1;
    149 
    150     assert( minPeriod > 0 );
    151 
    152     return tr_time()
    153         + minPeriod
    154         + tr_cryptoWeakRandInt( (int) ( minPeriod * jitter ) + 1 );
     144    assert( val > 0 );
     145    return val + tr_cryptoWeakRandInt((int)(1 + val * jitter));
    155146}
    156147
     
    162153{
    163154    tr_announcer * a;
    164 
    165     const time_t lpdAt = valPlusJitter( LPD_HOUSEKEEPING_INTERVAL_SECS / 3 );
    166155
    167156    assert( tr_isSession( session ) );
     
    172161    a->session = session;
    173162    a->slotsAvailable = MAX_CONCURRENT_TASKS;
    174     a->lpdUpkeepAt = lpdAt;
     163    a->lpdUpkeepAt = tr_time() + jitterize(5);
    175164    a->upkeepTimer = evtimer_new( session->event_base, onUpkeepTimer, a );
    176165    tr_timerAdd( a->upkeepTimer, UPKEEP_INTERVAL_SECS, 0 );
     
    204193typedef struct
    205194{
    206     char * hostname;
     195    char * key;
    207196    char * announce;
    208197    char * scrape;
     
    221210tr_tracker;
    222211
    223 /* format: hostname + ':' + port */
     212/* format: host+':'+ port */
    224213static char *
    225 getHostName( const char * url )
     214getKey( const char * url )
    226215{
    227216    int port = 0;
     
    238227{
    239228    tr_tracker * tracker = tr_new0( tr_tracker, 1  );
    240     tracker->hostname = getHostName( announce );
     229    tracker->key = getKey( announce );
    241230    tracker->announce = tr_strdup( announce );
    242231    tracker->scrape = tr_strdup( scrape );
     
    256245    tr_free( tracker->scrape );
    257246    tr_free( tracker->announce );
    258     tr_free( tracker->hostname );
     247    tr_free( tracker->key );
    259248    tr_free( tracker );
    260249}
     
    353342    tr_snprintf( buf, buflen, "[%s---%s]",
    354343       ( tier && tier->tor ) ? tr_torrentName( tier->tor ) : "?",
    355        ( tier && tier->currentTracker ) ? tier->currentTracker->hostname : "?" );
     344       ( tier && tier->currentTracker ) ? tier->currentTracker->key : "?" );
    356345}
    357346
     
    459448publishMessage( tr_tier * tier, const char * msg, int type )
    460449{
    461     if( tier && tier->tor && tier->tor->tiers )
     450    if( tier && tier->tor && tier->tor->tiers && tier->tor->tiers->callback )
    462451    {
    463452        tr_torrent_tiers * tiers = tier->tor->tiers;
     
    468457            event.tracker = tier->currentTracker->announce;
    469458
    470         if( tiers->callback != NULL )
    471             tiers->callback( tier->tor, &event, tiers->callbackData );
     459        tiers->callback( tier->tor, &event, tiers->callbackData );
    472460    }
    473461}
     
    515503                 const tr_pex * pex, int n )
    516504{
    517     tr_tracker_event e = TRACKER_EVENT_INIT;
    518     e.messageType = TR_TRACKER_PEERS;
    519     e.seedProbability = getSeedProbability( tier, seeds, leechers, n );
    520     e.pex = pex;
    521     e.pexCount = n;
    522 
    523     dbgmsg( tier, "got %d peers; seed probability %d", n, (int)e.seedProbability );
    524 
    525     if( tier->tor->tiers->callback != NULL )
     505    if( tier->tor->tiers->callback )
     506    {
     507        tr_tracker_event e = TRACKER_EVENT_INIT;
     508        e.messageType = TR_TRACKER_PEERS;
     509        e.seedProbability = getSeedProbability( tier, seeds, leechers, n );
     510        e.pex = pex;
     511        e.pexCount = n;
     512        dbgmsg( tier, "got %d peers; seed prob %d", n, (int)e.seedProbability );
     513
    526514        tier->tor->tiers->callback( tier->tor, &e, NULL );
     515    }
    527516}
    528517
     
    614603        return FALSE;
    615604
     605    /* return true if any tier can manual announce */
    616606    n = tr_ptrArraySize( &tor->tiers->tiers );
    617607    tiers = (const tr_tier**) tr_ptrArrayBase( &tor->tiers->tiers );
     
    633623    assert( tr_isTorrent( tor  ) );
    634624
     625    /* find the earliest manual announce time from all peers */
    635626    tiers = tor->tiers;
    636627    n = tr_ptrArraySize( &tiers->tiers );
     
    670661tier_announce_remove_trailing( tr_tier * tier, tr_announce_event e )
    671662{
    672     while( ( tier->announce_event_count > 0 ) && ( tier->announce_events[tier->announce_event_count-1] == e ) )
     663    while( ( tier->announce_event_count > 0 )
     664        && ( tier->announce_events[tier->announce_event_count-1] == e ) )
    673665        --tier->announce_event_count;
    674666}
    675667
    676668static void
    677 tier_announce_event_push( tr_tier * tier, tr_announce_event e, time_t announceAt )
     669tier_announce_event_push( tr_tier            * tier,
     670                          tr_announce_event    e,
     671                          time_t               announceAt )
    678672{
    679673    int i;
     
    682676
    683677    dbgmsg_tier_announce_queue( tier );
    684     dbgmsg( tier, "appending \"%s\" to announce queue", tr_announce_event_get_string( e ) );
     678    dbgmsg( tier, "queued \"%s\"", tr_announce_event_get_string( e ) );
    685679
    686680    if( tier->announce_event_count > 0 )
     
    742736    assert( tr_isTorrent( tor ) );
    743737
     738    /* walk through each tier and tell them to announce */
    744739    tiers = tor->tiers;
    745740    n = tr_ptrArraySize( &tiers->tiers );
     
    976971            if(( str = response->warning ))
    977972            {
    978                 tr_strlcpy( tier->lastAnnounceStr, str, sizeof( tier->lastAnnounceStr ) );
     973                tr_strlcpy( tier->lastAnnounceStr, str,
     974                            sizeof( tier->lastAnnounceStr ) );
    979975                dbgmsg( tier, "tracker gave \"%s\"", str );
    980976                publishWarning( tier, str );
     
    11181114{
    11191115    int i;
    1120     int n = tr_ptrArraySize( &tor->tiers->tiers );
     1116    const int n = tr_ptrArraySize( &tor->tiers->tiers );
    11211117    tr_tier ** tiers = (tr_tier**) tr_ptrArrayBase( &tor->tiers->tiers );
    11221118
     
    13371333announceMore( tr_announcer * announcer )
    13381334{
     1335    int i;
     1336    int n;
     1337    tr_torrent * tor;
     1338    tr_ptrArray announceMe = TR_PTR_ARRAY_INIT;
     1339    tr_ptrArray scrapeMe = TR_PTR_ARRAY_INIT;
     1340    const time_t now = tr_time( );
     1341
     1342    dbgmsg( NULL, "announceMore: slotsAvailable is %d", announcer->slotsAvailable );
     1343
     1344    if( announcer->slotsAvailable < 1 )
     1345        return;
     1346
     1347    /* build a list of tiers that need to be announced */
     1348    tor = NULL;
     1349    while(( tor = tr_torrentNext( announcer->session, tor ))) {
     1350        if( tor->tiers ) {
     1351            n = tr_ptrArraySize( &tor->tiers->tiers );
     1352            for( i=0; i<n; ++i ) {
     1353                tr_tier * tier = tr_ptrArrayNth( &tor->tiers->tiers, i );
     1354                if( tierNeedsToAnnounce( tier, now ) )
     1355                    tr_ptrArrayAppend( &announceMe, tier );
     1356                else if( tierNeedsToScrape( tier, now ) )
     1357                    tr_ptrArrayAppend( &scrapeMe, tier );
     1358            }
     1359        }
     1360    }
     1361
     1362    /* if there are more tiers than slots available, prioritize */
     1363    n = tr_ptrArraySize( &announceMe );
     1364    if( n > announcer->slotsAvailable )
     1365        qsort( tr_ptrArrayBase(&announceMe), n, sizeof(tr_tier*), compareTiers );
     1366
     1367    /* announce some */
     1368    n = MIN( tr_ptrArraySize( &announceMe ), announcer->slotsAvailable );
     1369    for( i=0; i<n; ++i ) {
     1370        tr_tier * tier = tr_ptrArrayNth( &announceMe, i );
     1371        dbgmsg( tier, "announcing tier %d of %d", i, n );
     1372        tierAnnounce( announcer, tier );
     1373    }
     1374
     1375    /* scrape some */
     1376    multiscrape( announcer, &scrapeMe );
     1377
     1378    /* cleanup */
     1379    tr_ptrArrayDestruct( &scrapeMe, NULL );
     1380    tr_ptrArrayDestruct( &announceMe, NULL );
     1381}
     1382
     1383static void
     1384dht_upkeep( tr_session * session )
     1385{
    13391386    tr_torrent * tor = NULL;
    13401387    const time_t now = tr_time( );
    13411388
    1342     dbgmsg( NULL, "announceMore: slotsAvailable is %d", announcer->slotsAvailable );
    1343 
    1344     if( announcer->slotsAvailable > 0 )
    1345     {
    1346         int i;
    1347         int n;
    1348         tr_ptrArray announceMe = TR_PTR_ARRAY_INIT;
    1349         tr_ptrArray scrapeMe = TR_PTR_ARRAY_INIT;
    1350 
    1351         /* build a list of tiers that need to be announced */
    1352         while(( tor = tr_torrentNext( announcer->session, tor ))) {
    1353             if( tor->tiers ) {
    1354                 n = tr_ptrArraySize( &tor->tiers->tiers );
    1355                 for( i=0; i<n; ++i ) {
    1356                     tr_tier * tier = tr_ptrArrayNth( &tor->tiers->tiers, i );
    1357                     if( tierNeedsToAnnounce( tier, now ) )
    1358                         tr_ptrArrayAppend( &announceMe, tier );
    1359                     else if( tierNeedsToScrape( tier, now ) )
    1360                         tr_ptrArrayAppend( &scrapeMe, tier );
    1361                 }
    1362             }
    1363         }
    1364 
    1365         /* if there are more tiers than slots available, prioritize */
    1366         n = tr_ptrArraySize( &announceMe );
    1367         if( n > announcer->slotsAvailable )
    1368             qsort( tr_ptrArrayBase( &announceMe ), n, sizeof( tr_tier * ), compareTiers );
    1369 
    1370         /* announce some */
    1371         n = MIN( tr_ptrArraySize( &announceMe ), announcer->slotsAvailable );
    1372         for( i=0; i<n; ++i ) {
    1373             tr_tier * tier = tr_ptrArrayNth( &announceMe, i );
    1374             dbgmsg( tier, "announcing tier %d of %d", i, n );
    1375             tierAnnounce( announcer, tier );
    1376         }
    1377 
    1378         /* scrape some */
    1379         multiscrape( announcer, &scrapeMe );
    1380 
    1381 #if 0
    1382 char timebuf[64];
    1383 tr_getLogTimeStr( timebuf, 64 );
    1384 fprintf( stderr, "[%s] announce.c has %d requests ready to send (announce: %d, scrape: %d)\n", timebuf, (int)(tr_ptrArraySize(&announceMe)+tr_ptrArraySize(&scrapeMe)), (int)tr_ptrArraySize(&announceMe), (int)tr_ptrArraySize(&scrapeMe) );
    1385 #endif
    1386 
    1387         /* cleanup */
    1388         tr_ptrArrayDestruct( &scrapeMe, NULL );
    1389         tr_ptrArrayDestruct( &announceMe, NULL );
    1390     }
    1391 
    1392     tor = NULL;
    1393     while(( tor = tr_torrentNext( announcer->session, tor ))) {
    1394         if( tor->dhtAnnounceAt <= now ) {
     1389    while(( tor = tr_torrentNext( session, tor )))
     1390    {
     1391        if( tor->dhtAnnounceAt <= now )
     1392        {
    13951393            if( tor->isRunning && tr_torrentAllowsDHT(tor) ) {
    1396                 int rc;
    1397                 rc = tr_dhtAnnounce(tor, AF_INET, 1);
     1394                const int rc = tr_dhtAnnounce(tor, AF_INET, 1);
    13981395                if(rc == 0)
    13991396                    /* The DHT is not ready yet. Try again soon. */
     
    14061403        }
    14071404
    1408         if( tor->dhtAnnounce6At <= now ) {
     1405        if( tor->dhtAnnounce6At <= now )
     1406        {
    14091407            if( tor->isRunning && tr_torrentAllowsDHT(tor) ) {
    1410                 int rc;
    1411                 rc = tr_dhtAnnounce(tor, AF_INET6, 1);
     1408                const int rc = tr_dhtAnnounce(tor, AF_INET6, 1);
    14121409                if(rc == 0)
    14131410                    tor->dhtAnnounce6At = now + 5 + tr_cryptoWeakRandInt( 5 );
     
    14331430    announceMore( announcer );
    14341431
     1432    dht_upkeep( announcer->session );
     1433
    14351434    /* LPD upkeep */
    14361435    if( announcer->lpdUpkeepAt <= now ) {
    14371436        const int seconds = LPD_HOUSEKEEPING_INTERVAL_SECS;
    1438         announcer->lpdUpkeepAt = valPlusJitter( seconds );
     1437        announcer->lpdUpkeepAt = now + jitterize( seconds );
    14391438        tr_lpdAnnounceMore( now, seconds );
    14401439    }
     
    14571456
    14581457tr_tracker_stat *
    1459 tr_announcerStats( const tr_torrent * torrent,
    1460                    int              * setmeTrackerCount )
     1458tr_announcerStats( const tr_torrent * torrent, int * setmeTrackerCount )
    14611459{
    14621460    int i;
     
    14941492
    14951493            st->id = tracker->id;
    1496             tr_strlcpy( st->host, tracker->hostname, sizeof( st->host ) );
     1494            tr_strlcpy( st->host, tracker->key, sizeof( st->host ) );
    14971495            tr_strlcpy( st->announce, tracker->announce, sizeof( st->announce ) );
    14981496            st->tier = i;
     
    15211519                    st->lastScrapeSucceeded = tier->lastScrapeSucceeded;
    15221520                    st->lastScrapeTimedOut = tier->lastScrapeTimedOut;
    1523                     tr_strlcpy( st->lastScrapeResult, tier->lastScrapeStr, sizeof( st->lastScrapeResult ) );
     1521                    tr_strlcpy( st->lastScrapeResult, tier->lastScrapeStr,
     1522                                sizeof( st->lastScrapeResult ) );
    15241523                }
    15251524
     
    15401539                if(( st->hasAnnounced = tier->lastAnnounceTime != 0 )) {
    15411540                    st->lastAnnounceTime = tier->lastAnnounceTime;
    1542                     tr_strlcpy( st->lastAnnounceResult, tier->lastAnnounceStr, sizeof( st->lastAnnounceResult ) );
     1541                    tr_strlcpy( st->lastAnnounceResult, tier->lastAnnounceStr,
     1542                                sizeof( st->lastAnnounceResult ) );
    15431543                    st->lastAnnounceSucceeded = tier->lastAnnounceSucceeded;
    15441544                    st->lastAnnounceTimedOut = tier->lastAnnounceTimedOut;
Note: See TracChangeset for help on using the changeset viewer.