Changeset 10749


Ignore:
Timestamp:
Jun 14, 2010, 3:42:54 AM (12 years ago)
Author:
charles
Message:

(trunk libT) experimental commit

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/peer-mgr.c

    r10748 r10749  
    32733273{
    32743274    uint64_t score;
    3275     int from;
    3276     uint8_t salt;
    3277     int priority;
    3278     int seedProbability;
    3279     int wasRecentlyStarted;
    3280     time_t lastConnectionAt;
    3281     time_t lastConnectionAttemptAt;
    32823275    tr_torrent * tor;
    32833276    struct peer_atom * atom;
    32843277};
    32853278
    3286 static int
    3287 compareSeedProbabilities( int a, int b )
    3288 {
    3289     /* 1. smaller numbers are better
    3290        2. prefer leechers to unknown
    3291        3. prefer unknown to seeds (FIXME: this is a simplistic test) */
    3292     if( a == 100 ) a = 101;
    3293     else if( a == -1 ) a = 100;
    3294 
    3295     if( b == 100 ) b = 101;
    3296     else if( b == -1 ) b = 100;
    3297 
    3298     if( a > b ) return 1;
    3299     if( a < b ) return -1;
    3300     return 0;
    3301 }
    3302 
    33033279static tr_bool
    33043280torrentWasRecentlyStarted( const tr_torrent * tor )
     
    33073283}
    33083284
    3309 static int loud = FALSE;
    3310 
    3311 static inline uint64_t
     3285static uint64_t
    33123286addValToKey( uint64_t value, int width, uint64_t addme )
    33133287{
    3314 if(loud) fprintf( stderr, "old value %"PRIu64"... width %d, addme %d\n", value, width, (int)addme );
    33153288    value = (value << (uint64_t)width);
    3316 if(loud) fprintf( stderr, "mid value %"PRIu64"\n", value );
    33173289    value |= addme;
    3318 if(loud) fprintf( stderr, "new value %"PRIu64"\n", value );
    33193290    return value;
    33203291}
     
    33653336}
    33663337
    3367 static int
    3368 oldComparePeerCandidates( const void * va, const void * vb )
    3369 {
    3370     int i;
    3371     const struct peer_candidate * a = va;
    3372     const struct peer_candidate * b = vb;
    3373 
    3374     /* prefer peers we've connected to, or never tried, over peers we failed to connect to. */
    3375     const tr_bool a_failed = a->atom->lastConnectionAt < a->atom->lastConnectionAttemptAt;
    3376     const tr_bool b_failed = b->atom->lastConnectionAt < b->atom->lastConnectionAttemptAt;
    3377     if( a_failed != b_failed ) {
    3378 if(loud) fprintf( stderr, "old: a_failed %d\n", (int)a_failed );
    3379 if(loud) fprintf( stderr, "old: b_failed %d\n", (int)b_failed );
    3380         return a_failed ? 1 : -1;
    3381     }
    3382 
    3383     /* prefer the one we attempted least recently (to cycle through all peers) */
    3384     if( a->lastConnectionAttemptAt != b->lastConnectionAttemptAt ) {
    3385 if(loud) fprintf( stderr, "old: a->lastConnectionAttemptAt %u\n", (unsigned int)a->lastConnectionAttemptAt );
    3386 if(loud) fprintf( stderr, "old: b->lastConnectionAttemptAt %u\n", (unsigned int)b->lastConnectionAttemptAt );
    3387         return a->lastConnectionAttemptAt < b->lastConnectionAttemptAt ? -1 : 1;
    3388     }
    3389 
    3390     /* prefer peers belonging to a torrent of a higher priority */
    3391     if( a->priority != b->priority ) {
    3392 if(loud) fprintf( stderr, "old: a->priority %d\n", (int)a->priority );
    3393 if(loud) fprintf( stderr, "old: b->priority %d\n", (int)b->priority );
    3394         return a->priority > b->priority ? -1 : 1;
    3395     }
    3396 
    3397     /* prefer recently-started torrents */
    3398     if( a->wasRecentlyStarted != b->wasRecentlyStarted ) {
    3399 if(loud) fprintf( stderr, "old: a->wasRecentlyStarted %d\n", (int)a->wasRecentlyStarted );
    3400 if(loud) fprintf( stderr, "old: b->wasRecentlyStarted %d\n", (int)b->wasRecentlyStarted );
    3401         return a->wasRecentlyStarted ? -1 : 1;
    3402      }
    3403 
    3404     /* prefer peers that we might have a chance of uploading to */
    3405     if(( i = compareSeedProbabilities( a->seedProbability, b->seedProbability ))) {
    3406 if(loud) fprintf( stderr, "old: a->seedProbability %d\n", (int)a->seedProbability );
    3407 if(loud) fprintf( stderr, "old: b->seedProbability %d\n", (int)b->seedProbability );
    3408         return i;
    3409     }
    3410 
    3411     /* prefer peers that we got from more trusted sources */
    3412     if( a->from != b->from ) {
    3413 if(loud) fprintf( stderr, "old: a->from %d\n", (int)a->from );
    3414 if(loud) fprintf( stderr, "old: b->from %d\n", (int)b->from );
    3415         return a->from < b->from ? -1 : 1;
    3416     }
    3417 
    3418     /* salt */
    3419 if(loud) fprintf( stderr, "old: a->salt %d\n", (int)a->salt );
    3420 if(loud) fprintf( stderr, "old: b->salt %d\n", (int)b->salt );
    3421     if( a->salt != b->salt )
    3422         return a->salt < b->salt ? -1 : 1;
    3423 
    3424     return 0;
    3425 }
    3426 
    34273338/* sort an array of peer candidates */
    34283339static int
    34293340comparePeerCandidates( const void * va, const void * vb )
    34303341{
    3431     int ret;
    3432     int oldret;
    34333342    const struct peer_candidate * a = va;
    34343343    const struct peer_candidate * b = vb;
    34353344
    34363345    if( a->score != b->score )
    3437         ret = a->score < b->score ? -1 : 1;
    3438     else
    3439         ret = 0;
    3440    
    3441     oldret = oldComparePeerCandidates( va, vb );
    3442 
    3443     if( ret != oldret ) {
    3444         loud = TRUE;
    3445         oldret = oldComparePeerCandidates( va, vb );
    3446         fprintf( stderr, "ccc =========================\n" );
    3447         fprintf( stderr, "oldret %d\n", oldret );
    3448         fprintf( stderr, "ret %d\n", ret );
    3449         fprintf( stderr, "a score %"PRIu64"\n", a->score );
    3450         fprintf( stderr, "b score %"PRIu64"\n", b->score );
    3451         fprintf( stderr, "generating a score\n" );
    3452         getPeerCandidateScore( a->tor, a->atom, a->salt );
    3453         fprintf( stderr, "generating b bcore\n" );
    3454         getPeerCandidateScore( b->tor, b->atom, b->salt );
    3455         assert( 0 );
    3456     }
    3457 
    3458     return ret;
     3346        return a->score < b->score ? -1 : 1;
     3347
     3348    return 0;
    34593349}
    34603350
     
    35143404            if( isPeerCandidate( tor, atom, now ) )
    35153405            {
     3406                const uint8_t salt = tr_cryptoWeakRandInt( 1024 );
    35163407                walk->tor = tor;
    35173408                walk->atom = atom;
    3518                 walk->from = atom->from;
    3519                 walk->salt = tr_cryptoWeakRandInt( 4096 );
    3520                 walk->score = getPeerCandidateScore( tor, atom, walk->salt );
    3521                 walk->priority = tr_torrentGetPriority( tor );
    3522                 walk->seedProbability = atom->seedProbability;
    3523                 walk->wasRecentlyStarted = torrentWasRecentlyStarted( tor );
    3524                 walk->lastConnectionAt = atom->lastConnectionAt;
    3525                 walk->lastConnectionAttemptAt = atom->lastConnectionAttemptAt;
     3409                walk->score = getPeerCandidateScore( tor, atom, salt );
    35263410                ++walk;
    35273411            }
Note: See TracChangeset for help on using the changeset viewer.