Changeset 10352


Ignore:
Timestamp:
Mar 10, 2010, 10:19:31 PM (11 years ago)
Author:
charles
Message:

(trunk libT) #3026 "Recent T doesn't honor weekend speed limit setting" -- fixed in trunk for 2.00

Location:
trunk/libtransmission
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/session.c

    r10303 r10352  
    10951095}
    10961096
     1097enum
     1098{
     1099    MINUTES_PER_HOUR = 60,
     1100    MINUTES_PER_DAY = MINUTES_PER_HOUR * 24,
     1101    MINUTES_PER_WEEK = MINUTES_PER_DAY * 7
     1102};
     1103
    10971104static void
    1098 turtleFindNextChange( struct tr_turtle_info * t )
     1105turtleUpdateTable( struct tr_turtle_info * t )
    10991106{
    11001107    int day;
    1101     struct tm tm;
    1102     time_t today_began_at;
    1103     time_t next_begin;
    1104     time_t next_end;
    1105     const time_t now = tr_time( );
    1106     const int SECONDS_PER_DAY = 86400;
    1107 
    1108     tr_localtime_r( &now, &tm );
    1109     tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
    1110     today_began_at = mktime( &tm );
    1111 
    1112     next_begin = today_began_at + ( t->beginMinute * 60 );
    1113     if( next_begin <= now )
    1114         next_begin += SECONDS_PER_DAY;
    1115 
    1116     next_end = today_began_at + ( t->endMinute * 60 );
    1117     if( next_end <= now )
    1118         next_end += SECONDS_PER_DAY;
    1119 
    1120     if( next_begin < next_end ) {
    1121         t->_nextChangeAt = next_begin;
    1122         t->_nextChangeValue = TRUE;
    1123     } else {
    1124         t->_nextChangeAt = next_end;
    1125         t->_nextChangeValue = FALSE;
    1126     }
    1127 
    1128     /* if the next change is today, look for today in t->days.
    1129        if the next change is tomorrow to turn limits OFF, look for today in t->days.
    1130        if the next change is tomorrow to turn limits ON, look for tomorrow in t->days. */
    1131     if( t->_nextChangeValue && (( t->_nextChangeAt >= today_began_at + SECONDS_PER_DAY )))
    1132         day = ( tm.tm_wday + 1 ) % 7;
    1133     else
    1134         day = tm.tm_wday;
    1135     t->_nextChangeAllowed = ( t->days & (1<<day) ) != 0;
    1136 
    1137     if( t->isClockEnabled && t->_nextChangeAllowed ) {
    1138         char buf[128];
    1139         tr_localtime_r( &t->_nextChangeAt, &tm );
    1140         strftime( buf, sizeof( buf ), "%a %b %d %T %Y", &tm );
    1141         tr_inf( "Turtle clock updated: at %s we'll turn limits %s", buf, (t->_nextChangeValue?"on":"off") );
     1108    tr_bitfield * b = &t->minutes;
     1109
     1110    tr_bitfieldClear( b );
     1111
     1112    for( day=0; day<7; ++day )
     1113    {
     1114        if( t->days & (1<<day) )
     1115        {
     1116            int i;
     1117            const time_t begin = t->beginMinute;
     1118            time_t end = t->endMinute;
     1119
     1120            if( end <= begin )
     1121                end += MINUTES_PER_DAY;
     1122
     1123            for( i=begin; i<end; ++i )
     1124                tr_bitfieldAdd( b, (i+day*MINUTES_PER_DAY) % MINUTES_PER_WEEK );
     1125        }
    11421126    }
    11431127}
     
    11531137    updateBandwidth( session, TR_UP );
    11541138    updateBandwidth( session, TR_DOWN );
    1155     turtleFindNextChange( t );
    11561139
    11571140    if( t->callback != NULL )
     
    11751158}
    11761159
     1160static tr_bool
     1161testTurtleTime( const struct tr_turtle_info * t )
     1162{
     1163    struct tm tm;
     1164    size_t minute_of_the_week;
     1165    const time_t now = tr_time( );
     1166
     1167    tr_localtime_r( &now, &tm );
     1168
     1169    minute_of_the_week = tm.tm_wday * MINUTES_PER_DAY
     1170                       + tm.tm_hour * MINUTES_PER_HOUR
     1171                       + tm.tm_min;
     1172
     1173    if( minute_of_the_week >= MINUTES_PER_WEEK ) /* leap minutes? */
     1174        minute_of_the_week = MINUTES_PER_WEEK - 1;
     1175
     1176    return tr_bitfieldHasFast( &t->minutes, minute_of_the_week );
     1177}
     1178
    11771179static void
    11781180turtleCheckClock( tr_session * session, struct tr_turtle_info * t, tr_bool byUser )
    11791181{
    1180     const time_t now = tr_time( );
    1181     const tr_bool hit = ( t->testedAt < t->_nextChangeAt ) && ( t->_nextChangeAt <= tr_time( ));
    1182 
    1183     t->testedAt = now;
    1184 
    1185     if( hit )
     1182    if( t->isClockEnabled )
    11861183    {
    1187         const tr_bool enabled = t->_nextChangeValue;
    1188 
    1189         if( t->isClockEnabled && t->_nextChangeAllowed )
     1184        const tr_bool hit = testTurtleTime( t );
     1185
     1186        if( hit != t->isEnabled )
    11901187        {
    1191             tr_inf( "Time to turn %s turtle mode!", (enabled?"on":"off") );
    1192             useAltSpeed( session, t, enabled, byUser );
     1188            tr_inf( "Time to turn %s turtle mode!", (hit?"on":"off") );
     1189            useAltSpeed( session, t, hit, byUser );
    11931190        }
    1194 
    1195         turtleFindNextChange( t );
    11961191    }
    11971192}
     
    12031198turtleBootstrap( tr_session * session, struct tr_turtle_info * turtle )
    12041199{
    1205     turtleFindNextChange( turtle );
    1206 
    12071200    turtle->changedByUser = FALSE;
    12081201
     1202    tr_bitfieldConstruct( &turtle->minutes, MINUTES_PER_WEEK );
     1203
     1204    turtleUpdateTable( turtle );
     1205
    12091206    if( turtle->isClockEnabled )
    1210         turtle->isEnabled = !turtle->_nextChangeValue;
     1207        turtle->isEnabled = testTurtleTime( turtle );
    12111208
    12121209    altSpeedToggled( session );
     
    12891286    tr_dbg( "Refreshing the turtle mode clock due to user changes" );
    12901287
    1291     t->testedAt = 0;
    1292     turtleFindNextChange( t );
    1293 
    1294     if( t->isClockEnabled && t->_nextChangeAllowed )
    1295         useAltSpeed( s, t, !t->_nextChangeValue, TRUE );
     1288    turtleUpdateTable( t );
     1289
     1290    if( t->isClockEnabled )
     1291        useAltSpeed( s, t, testTurtleTime( t ), TRUE );
    12961292}
    12971293
     
    15931589    tr_bencFree( &session->removedTorrents );
    15941590    tr_bandwidthFree( session->bandwidth );
     1591    tr_bitfieldDestruct( &session->turtle.minutes );
    15951592    tr_lockFree( session->lock );
    15961593    if( session->metainfoLookup ) {
  • trunk/libtransmission/session.h

    r10276 r10352  
    2929
    3030#include "bencode.h"
     31#include "bitfield.h"
    3132
    3233typedef enum { TR_NET_OK, TR_NET_ERROR, TR_NET_WAIT } tr_tristate_t;
     
    8384    tr_bool changedByUser;
    8485
    85     /* this is the next time the clock will set turtle mode */
    86     time_t _nextChangeAt;
    87 
    88     /* the clock will set turtle mode to this flag. */
    89     tr_bool _nextChangeValue;
    90 
    91     /* When clock mode is on, only toggle turtle mode if this is true.
    92      * This flag is used to filter out changes that fall on days when
    93      * clock mode is disabled. */
    94     tr_bool _nextChangeAllowed;
    95 
    96     /* The last time the clock tested to see if _nextChangeAt was reached */
    97     time_t testedAt;
     86    tr_bitfield minutes;
    9887};
    9988
Note: See TracChangeset for help on using the changeset viewer.