Changeset 3455


Ignore:
Timestamp:
Oct 18, 2007, 3:55:55 AM (15 years ago)
Author:
charles
Message:

added debugging messages to the tracker cleanup. better error handling for esoteric http errors.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/tracker.c

    r3451 r3455  
    114114};
    115115
     116/**
     117***
     118**/
     119
     120static void
     121myDebug( const char * file, int line, const tr_tracker * t, const char * fmt, ... )
     122{   
     123    FILE * fp = tr_getLog( );
     124    if( fp != NULL )
     125    {
     126        va_list args;
     127        struct evbuffer * buf = evbuffer_new( );
     128        char timestr[64];
     129        evbuffer_add_printf( buf, "[%s] ", tr_getLogTimeStr( timestr, sizeof(timestr) ) );
     130        if( t != NULL )
     131            evbuffer_add_printf( buf, "%s ", t->name );
     132        va_start( args, fmt );
     133        evbuffer_add_vprintf( buf, fmt, args );
     134        va_end( args );
     135        evbuffer_add_printf( buf, " (%s:%d)\n", file, line );
     136
     137        fwrite( EVBUFFER_DATA(buf), 1, EVBUFFER_LENGTH(buf), fp );
     138        evbuffer_free( buf );
     139    }
     140}
     141
     142#define dbgmsg(t, fmt...) myDebug(__FILE__, __LINE__, t, ##fmt )
     143
     144
    116145/***
    117146****  Connections that know how to clean up after themselves
     
    295324        tr_free( t->redirect );
    296325    }
    297 
    298     tr_timerFree( &t->scrapeTimer );
    299326
    300327    tr_free( t->addresses );
     
    660687}
    661688
    662 static int onReannounceNow( void * vtor );
     689static int onRetry( void * vt );
     690static int onReannounce( void * vt );
    663691
    664692static void
    665693onStoppedResponse( struct evhttp_request * req UNUSED, void * handle UNUSED )
    666694{
     695    dbgmsg( NULL, "got a response to some `stop' message" );
    667696}
    668697
     
    673702    tr_tracker * t;
    674703    int err = 0;
     704    int responseCode;
    675705
    676706    t = findTrackerFromHash( torrent_hash );
     
    678708        return;
    679709
     710    dbgmsg( t, "got response from tracker: \"%s\"",
     711            ( req && req->response_code_line ) ?  req->response_code_line
     712                                               : "(null)" );
     713
    680714    tr_inf( "Torrent \"%s\" tracker response: %s",
    681715            t->name,
     
    693727            benc_val_t * tmp;
    694728
    695             if(( tmp = tr_bencDictFind( &benc, "failure reason" )))
     729            if(( tmp = tr_bencDictFind( &benc, "failure reason" ))) {
     730                dbgmsg( t, "got failure message [%s]", tmp->val.s.s );
    696731                publishErrorMessage( t, tmp->val.s.s );
    697 
    698             if(( tmp = tr_bencDictFind( &benc, "warning message" )))
     732            }
     733
     734            if(( tmp = tr_bencDictFind( &benc, "warning message" ))) {
     735                dbgmsg( t, "got warning message [%s]", tmp->val.s.s );
    699736                publishWarning( t, tmp->val.s.s );
    700 
    701             if(( tmp = tr_bencDictFind( &benc, "interval" )))
     737            }
     738
     739            if(( tmp = tr_bencDictFind( &benc, "interval" ))) {
     740                dbgmsg( t, "setting interval to %d", tmp->val.i );
    702741                t->announceIntervalSec = tmp->val.i;
    703 
    704             if(( tmp = tr_bencDictFind( &benc, "min interval" )))
     742            }
     743
     744            if(( tmp = tr_bencDictFind( &benc, "min interval" ))) {
     745                dbgmsg( t, "setting min interval to %d", tmp->val.i );
    705746                t->announceMinIntervalSec = tmp->val.i;
     747            }
    706748
    707749            if(( tmp = tr_bencDictFind( &benc, "tracker id" )))
     
    756798    }
    757799
    758     /* set reannounce times */
    759     tr_timerFree( &t->reannounceTimer );
    760     t->reannounceTimer = NULL;
    761     t->manualAnnounceAllowedAt = ~(time_t)0;
    762     if( t->isRunning )
    763     {
    764         tr_dbg( "torrent '%s' reannouncing in %d seconds",
    765                 t->name, t->announceIntervalSec );
    766 
     800    /**
     801    ***
     802    **/
     803
     804    responseCode = req ? req->response_code : 503;
     805
     806    if( 200<=responseCode && responseCode<=299 )
     807    {
     808        dbgmsg( t, "request succeeded. reannouncing in %d seconds", t->announceIntervalSec );
     809
     810        t->manualAnnounceAllowedAt = time(NULL)
     811                                   + t->announceMinIntervalSec;
    767812        t->reannounceTimer = tr_timerNew( t->handle,
    768                                           onReannounceNow, t,
     813                                          onReannounce, t,
    769814                                          t->announceIntervalSec * 1000 );
    770 
    771         t->manualAnnounceAllowedAt = time(NULL) + t->announceMinIntervalSec;
     815    }
     816    else if( 300<=responseCode && responseCode<=399 )
     817    {
     818        dbgmsg( t, "got a redirect; retrying immediately" );
     819
     820        /* it's a redirect... updateAddresses() has already
     821         * parsed the redirect, all that's left is to retry */
     822        onRetry( t );
     823    }
     824    else if( 400<=responseCode && responseCode<=499 )
     825    {
     826        dbgmsg( t, "got a 4xx error." );
     827
     828        /* The request could not be understood by the server due to
     829         * malformed syntax. The client SHOULD NOT repeat the
     830         * request without modifications. */
     831        publishErrorMessage( t, req->response_code_line );
     832        t->manualAnnounceAllowedAt = ~(time_t)0;
     833        t->reannounceTimer = NULL;
     834    }
     835    else if( 500<=responseCode && responseCode<=599 )
     836    {
     837        dbgmsg( t, "got a 5xx error... retrying in 15 seconds." );
     838
     839        /* Response status codes beginning with the digit "5" indicate
     840         * cases in which the server is aware that it has erred or is
     841         * incapable of performing the request.  So we pause a bit and
     842         * try again. */
     843        publishWarning( t, req->response_code_line );
     844        t->manualAnnounceAllowedAt = ~(time_t)0;
     845        t->reannounceTimer = tr_timerNew( t->handle, onRetry, t, 15 * 1000 );
     846    }
     847    else
     848    {
     849        dbgmsg( t, "unhandled condition... retrying in 120 seconds." );
     850
     851        /* WTF did we get?? */
     852        publishErrorMessage( t, req->response_code_line );
     853        t->manualAnnounceAllowedAt = ~(time_t)0;
     854        t->reannounceTimer = tr_timerNew( t->handle, onRetry, t, 120 * 1000 );
    772855    }
    773856}
     
    813896            req = evhttp_request_new( onTrackerResponse, torrentHashNew(t) );
    814897        }
     898        dbgmsg( t, "sending \"%s\" request to tracker", eventName ? eventName : "reannounce" );
     899
    815900        addCommonHeaders( t, req );
    816901        tr_evhttp_make_request( t->handle, evcon,
     
    822907
    823908static int
    824 onReannounceNow( void * vt )
     909onReannounce( void * vt )
    825910{
    826911    tr_tracker * t = vt;
    827912    sendTrackerRequest( t, "" );
     913    t->reannounceTimer = NULL;
     914    return FALSE;
     915}
     916
     917static int
     918onRetry( void * vt )
     919{
     920    tr_tracker * t = vt;
     921    sendTrackerRequest( t, t->lastRequest );
    828922    t->reannounceTimer = NULL;
    829923    return FALSE;
Note: See TracChangeset for help on using the changeset viewer.