Ignore:
Timestamp:
Jul 16, 2010, 3:12:57 AM (11 years ago)
Author:
livings124
Message:

First go at #2560 "inactive seeding time limit". libtransmission and Mac support are currently implemented.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/torrent.c

    r10999 r11013  
    125125}
    126126
    127 /***
    128 ****  PER-TORRENT UL / DL SPEEDS
    129 ***/
    130 
    131 void
    132 tr_torrentSetSpeedLimit_Bps( tr_torrent * tor, tr_direction dir, int Bps )
    133 {
    134     assert( tr_isTorrent( tor ) );
    135     assert( tr_isDirection( dir ) );
    136     assert( Bps >= 0 );
    137 
    138     if( tr_bandwidthSetDesiredSpeed_Bps( tor->bandwidth, dir, Bps ) )
    139         tr_torrentSetDirty( tor );
    140 }
    141 void
    142 tr_torrentSetSpeedLimit_KBps( tr_torrent * tor, tr_direction dir, int KBps )
    143 {
    144     tr_torrentSetSpeedLimit_Bps( tor, dir, toSpeedBytes( KBps ) );
    145 }
    146 
    147 int
    148 tr_torrentGetSpeedLimit_Bps( const tr_torrent * tor, tr_direction dir )
    149 {
    150     assert( tr_isTorrent( tor ) );
    151     assert( tr_isDirection( dir ) );
    152 
    153     return tr_bandwidthGetDesiredSpeed_Bps( tor->bandwidth, dir );
    154 }
    155 int
    156 tr_torrentGetSpeedLimit_KBps( const tr_torrent * tor, tr_direction dir )
    157 {
    158     return toSpeedKBps( tr_torrentGetSpeedLimit_Bps( tor, dir ) );
    159 }
    160 
    161 void
    162 tr_torrentUseSpeedLimit( tr_torrent * tor, tr_direction dir, tr_bool do_use )
    163 {
    164     assert( tr_isTorrent( tor ) );
    165     assert( tr_isDirection( dir ) );
    166 
    167     if( tr_bandwidthSetLimited( tor->bandwidth, dir, do_use ) )
    168         tr_torrentSetDirty( tor );
    169 }
    170 
    171 tr_bool
    172 tr_torrentUsesSpeedLimit( const tr_torrent * tor, tr_direction dir )
    173 {
    174     assert( tr_isTorrent( tor ) );
    175     assert( tr_isDirection( dir ) );
    176 
    177     return tr_bandwidthIsLimited( tor->bandwidth, dir );
    178 }
    179 
    180 void
    181 tr_torrentUseSessionLimits( tr_torrent * tor, tr_bool doUse )
    182 {
    183     tr_bool changed;
    184 
    185     assert( tr_isTorrent( tor ) );
    186 
    187     changed = tr_bandwidthHonorParentLimits( tor->bandwidth, TR_UP, doUse );
    188     changed |= tr_bandwidthHonorParentLimits( tor->bandwidth, TR_DOWN, doUse );
    189 
    190     if( changed )
    191         tr_torrentSetDirty( tor );
    192 }
    193 
    194 tr_bool
    195 tr_torrentUsesSessionLimits( const tr_torrent * tor )
    196 {
    197     assert( tr_isTorrent( tor ) );
    198 
    199     return tr_bandwidthAreParentLimitsHonored( tor->bandwidth, TR_UP );
    200 }
    201 
    202 /***
    203 ****
    204 ***/
    205 
    206 void
    207 tr_torrentSetRatioMode( tr_torrent *  tor, tr_ratiolimit mode )
    208 {
    209     assert( tr_isTorrent( tor ) );
    210     assert( mode==TR_RATIOLIMIT_GLOBAL || mode==TR_RATIOLIMIT_SINGLE || mode==TR_RATIOLIMIT_UNLIMITED  );
    211 
    212     if( mode != tor->ratioLimitMode )
    213     {
    214         tor->ratioLimitMode = mode;
    215 
    216         tr_torrentSetDirty( tor );
    217     }
    218 }
    219 
    220 tr_ratiolimit
    221 tr_torrentGetRatioMode( const tr_torrent * tor )
    222 {
    223     assert( tr_isTorrent( tor ) );
    224 
    225     return tor->ratioLimitMode;
    226 }
    227 
    228 void
    229 tr_torrentSetRatioLimit( tr_torrent * tor, double desiredRatio )
    230 {
    231     assert( tr_isTorrent( tor ) );
    232 
    233     if( (int)(desiredRatio*100.0) != (int)(tor->desiredRatio*100.0) )
    234     {
    235         tor->desiredRatio = desiredRatio;
    236 
    237         tr_torrentSetDirty( tor );
    238     }
    239 }
    240 
    241 double
    242 tr_torrentGetRatioLimit( const tr_torrent * tor )
    243 {
    244     assert( tr_isTorrent( tor ) );
    245 
    246     return tor->desiredRatio;
    247 }
    248 
    249127tr_bool
    250128tr_torrentIsPieceTransferAllowed( const tr_torrent  * tor,
     
    264142
    265143    return allowed;
     144}
     145
     146/***
     147****  PER-TORRENT UL / DL SPEEDS
     148***/
     149
     150void
     151tr_torrentSetSpeedLimit_Bps( tr_torrent * tor, tr_direction dir, int Bps )
     152{
     153    assert( tr_isTorrent( tor ) );
     154    assert( tr_isDirection( dir ) );
     155    assert( Bps >= 0 );
     156
     157    if( tr_bandwidthSetDesiredSpeed_Bps( tor->bandwidth, dir, Bps ) )
     158        tr_torrentSetDirty( tor );
     159}
     160void
     161tr_torrentSetSpeedLimit_KBps( tr_torrent * tor, tr_direction dir, int KBps )
     162{
     163    tr_torrentSetSpeedLimit_Bps( tor, dir, toSpeedBytes( KBps ) );
     164}
     165
     166int
     167tr_torrentGetSpeedLimit_Bps( const tr_torrent * tor, tr_direction dir )
     168{
     169    assert( tr_isTorrent( tor ) );
     170    assert( tr_isDirection( dir ) );
     171
     172    return tr_bandwidthGetDesiredSpeed_Bps( tor->bandwidth, dir );
     173}
     174int
     175tr_torrentGetSpeedLimit_KBps( const tr_torrent * tor, tr_direction dir )
     176{
     177    return toSpeedKBps( tr_torrentGetSpeedLimit_Bps( tor, dir ) );
     178}
     179
     180void
     181tr_torrentUseSpeedLimit( tr_torrent * tor, tr_direction dir, tr_bool do_use )
     182{
     183    assert( tr_isTorrent( tor ) );
     184    assert( tr_isDirection( dir ) );
     185
     186    if( tr_bandwidthSetLimited( tor->bandwidth, dir, do_use ) )
     187        tr_torrentSetDirty( tor );
     188}
     189
     190tr_bool
     191tr_torrentUsesSpeedLimit( const tr_torrent * tor, tr_direction dir )
     192{
     193    assert( tr_isTorrent( tor ) );
     194    assert( tr_isDirection( dir ) );
     195
     196    return tr_bandwidthIsLimited( tor->bandwidth, dir );
     197}
     198
     199void
     200tr_torrentUseSessionLimits( tr_torrent * tor, tr_bool doUse )
     201{
     202    tr_bool changed;
     203
     204    assert( tr_isTorrent( tor ) );
     205
     206    changed = tr_bandwidthHonorParentLimits( tor->bandwidth, TR_UP, doUse );
     207    changed |= tr_bandwidthHonorParentLimits( tor->bandwidth, TR_DOWN, doUse );
     208
     209    if( changed )
     210        tr_torrentSetDirty( tor );
     211}
     212
     213tr_bool
     214tr_torrentUsesSessionLimits( const tr_torrent * tor )
     215{
     216    assert( tr_isTorrent( tor ) );
     217
     218    return tr_bandwidthAreParentLimitsHonored( tor->bandwidth, TR_UP );
     219}
     220
     221/***
     222****
     223***/
     224
     225void
     226tr_torrentSetRatioMode( tr_torrent *  tor, tr_ratiolimit mode )
     227{
     228    assert( tr_isTorrent( tor ) );
     229    assert( mode==TR_RATIOLIMIT_GLOBAL || mode==TR_RATIOLIMIT_SINGLE || mode==TR_RATIOLIMIT_UNLIMITED  );
     230
     231    if( mode != tor->ratioLimitMode )
     232    {
     233        tor->ratioLimitMode = mode;
     234
     235        tr_torrentSetDirty( tor );
     236    }
     237}
     238
     239tr_ratiolimit
     240tr_torrentGetRatioMode( const tr_torrent * tor )
     241{
     242    assert( tr_isTorrent( tor ) );
     243
     244    return tor->ratioLimitMode;
     245}
     246
     247void
     248tr_torrentSetRatioLimit( tr_torrent * tor, double desiredRatio )
     249{
     250    assert( tr_isTorrent( tor ) );
     251
     252    if( (int)(desiredRatio*100.0) != (int)(tor->desiredRatio*100.0) )
     253    {
     254        tor->desiredRatio = desiredRatio;
     255
     256        tr_torrentSetDirty( tor );
     257    }
     258}
     259
     260double
     261tr_torrentGetRatioLimit( const tr_torrent * tor )
     262{
     263    assert( tr_isTorrent( tor ) );
     264
     265    return tor->desiredRatio;
    266266}
    267267
     
    324324}
    325325
    326 void
    327 tr_torrentCheckSeedRatio( tr_torrent * tor )
    328 {
    329     assert( tr_isTorrent( tor ) );
     326/***
     327****
     328***/
     329
     330void
     331tr_torrentSetInactiveMode( tr_torrent *  tor, tr_inactvelimit mode )
     332{
     333    assert( tr_isTorrent( tor ) );
     334    assert( mode==TR_INACTIVELIMIT_GLOBAL || mode==TR_INACTIVELIMIT_SINGLE || mode==TR_INACTIVELIMIT_UNLIMITED  );
     335
     336    if( mode != tor->inactiveLimitMode )
     337    {
     338        tor->inactiveLimitMode = mode;
     339
     340        tr_torrentSetDirty( tor );
     341    }
     342}
     343
     344tr_ratiolimit
     345tr_torrentGetInactiveMode( const tr_torrent * tor )
     346{
     347    assert( tr_isTorrent( tor ) );
     348
     349    return tor->inactiveLimitMode;
     350}
     351
     352void
     353tr_torrentSetInactiveLimit( tr_torrent * tor, uint64_t inactiveMinutes)
     354{
     355    assert( tr_isTorrent( tor ) );
     356
     357    if( inactiveMinutes > 0 )
     358    {
     359        tor->inactiveLimitMinutes = inactiveMinutes;
     360
     361        tr_torrentSetDirty( tor );
     362    }
     363}
     364
     365uint64_t
     366tr_torrentGetInactiveLimit( const tr_torrent * tor )
     367{
     368    assert( tr_isTorrent( tor ) );
     369
     370    return tor->inactiveLimitMinutes;
     371}
     372
     373tr_bool
     374tr_torrentGetSeedInactive( const tr_torrent * tor, uint64_t * inactiveMinutes )
     375{
     376    tr_bool isLimited;
     377
     378    switch( tr_torrentGetInactiveMode( tor ) )
     379    {
     380        case TR_INACTIVELIMIT_SINGLE:
     381            isLimited = TRUE;
     382            if( inactiveMinutes )
     383                *inactiveMinutes = tr_torrentGetInactiveLimit( tor );
     384            break;
     385
     386        case TR_INACTIVELIMIT_GLOBAL:
     387            isLimited = tr_sessionIsInactivityLimited( tor->session );
     388            if( isLimited && inactiveMinutes )
     389                *inactiveMinutes = tr_sessionGetInactiveLimit( tor->session );
     390            break;
     391
     392        default: /* TR_INACTIVELIMIT_UNLIMITED */
     393            isLimited = FALSE;
     394            break;
     395    }
     396
     397    return isLimited;
     398}
     399
     400static tr_bool
     401tr_torrentIsSeedInactiveLimitDone( tr_torrent * tor )
     402{
     403    uint64_t inactiveMinutes;
     404#warning can this use the idleSecs from tr_stat?
     405    return tr_torrentGetSeedInactive( tor, &inactiveMinutes )
     406        && difftime(tr_time(), MAX(tor->startDate, tor->activityDate)) >= inactiveMinutes * 60;
     407}
     408
     409/***
     410****
     411***/
     412
     413void
     414tr_torrentCheckSeedLimit( tr_torrent * tor )
     415{
     416    assert( tr_isTorrent( tor ) );
     417
     418    if( !tor->isRunning )
     419        return;
    330420
    331421    /* if we're seeding and reach our seed ratio limit, stop the torrent */
    332     if( tor->isRunning && tr_torrentIsSeedRatioDone( tor ) )
     422    if( tr_torrentIsSeedRatioDone( tor ) )
    333423    {
    334424        tr_torinf( tor, "Seed ratio reached; pausing torrent" );
     
    340430            tor->ratio_limit_hit_func( tor, tor->ratio_limit_hit_func_user_data );
    341431    }
    342 }
    343 
     432    /* if we're seeding and reach our inactiviy limit, stop the torrent */
     433    else if( tr_torrentIsSeedInactiveLimitDone( tor ) )
     434    {
     435        tr_torinf( tor, "Seeding inactivity limit reached; pausing torrent" );
     436
     437        tor->isStopping = TRUE;
     438
     439        /* maybe notify the client */
     440        if( tor->inactive_limit_hit_func != NULL )
     441            tor->inactive_limit_hit_func( tor, tor->inactive_limit_hit_func_user_data );
     442    }
     443}
    344444
    345445/***
     
    709809    }
    710810
     811    if( !( loaded & TR_FR_INACTIVELIMIT ) )
     812    {
     813        tr_torrentSetInactiveMode( tor, TR_INACTIVELIMIT_GLOBAL );
     814        tr_torrentSetInactiveLimit( tor, tr_sessionGetInactiveLimit( tor->session ) );
     815    }
     816
    711817    {
    712818        tr_torrent * it = NULL;
     
    10031109
    10041110    if (s->activity == TR_STATUS_DOWNLOAD || s->activity == TR_STATUS_SEED)
    1005         s->idleSecs = difftime(tr_time(), tor->anyDate);
     1111        s->idleSecs = difftime(tr_time(), MAX(s->startDate, s->activityDate));
    10061112    else
    10071113        s->idleSecs = -1;
     
    10361142                            s->downloadedEver ? s->downloadedEver : s->haveValid );
    10371143
     1144    #warning account for inactivity limit?
    10381145    seedRatioApplies = tr_torrentGetSeedRatioBytes( tor, &seedRatioBytesLeft,
    10391146                                                         &seedRatioBytesGoal );
     
    10631170
    10641171        case TR_STATUS_SEED: {
     1172            #warning do something for inactivity?
    10651173            if( !seedRatioApplies )
    10661174                s->eta = TR_ETA_NOT_AVAIL;
     
    10851193    }
    10861194
     1195    #warning (maybe) do something for inactivity?
    10871196    /* s->haveValid is here to make sure a torrent isn't marked 'finished'
    10881197     * when the user hits "uncheck all" prior to starting the torrent... */
     
    16721781{
    16731782    tr_torrentSetRatioLimitHitCallback( torrent, NULL, NULL );
     1783}
     1784
     1785void
     1786tr_torrentSetInactiveLimitHitCallback( tr_torrent                        * tor,
     1787                                       tr_torrent_inactive_limit_hit_func  func,
     1788                                       void                              * user_data )
     1789{
     1790    assert( tr_isTorrent( tor ) );
     1791
     1792    tor->inactive_limit_hit_func = func;
     1793    tor->inactive_limit_hit_func_user_data = user_data;
     1794}
     1795
     1796void
     1797tr_torrentClearInactivityLimitHitCallback( tr_torrent * torrent )
     1798{
     1799    tr_torrentSetInactiveLimitHitCallback( torrent, NULL, NULL );
    16741800}
    16751801
Note: See TracChangeset for help on using the changeset viewer.