Changeset 6223


Ignore:
Timestamp:
Jun 19, 2008, 2:53:45 AM (13 years ago)
Author:
livings124
Message:

refactor some of the speed limit scheduling code, so that the timer goes off only when the speed has to change instead of every 5 seconds (so at most twice a day instead of 17280 times a day)

Location:
trunk/macosx
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/macosx/Controller.h

    r6221 r6223  
    6363    DragOverlayWindow               * fOverlayWindow;
    6464    IBOutlet TorrentTableView       * fTableView;
     65
     66    io_connect_t                    fRootPort;
     67    NSTimer                         * fTimer;
    6568   
    6669    IBOutlet NSMenuItem             * fOpenIgnoreDownloadFolder;
     
    6871    IBOutlet NSBox                  * fBottomTigerLine;
    6972    IBOutlet NSButton               * fActionButton, * fSpeedLimitButton;
    70     NSTimer                         * fSpeedLimitTimer;
    7173    IBOutlet NSTextField            * fTotalTorrentsField;
    7274   
     
    99101    IBOutlet NSTextField            * fURLSheetTextField;
    100102    IBOutlet NSButton               * fURLSheetOpenButton;
    101 
    102     io_connect_t                    fRootPort;
    103     NSTimer                         * fTimer;
    104103   
    105104    IBOutlet SUUpdater              * fUpdater;
     
    108107    Badger                          * fBadger;
    109108    IBOutlet NSMenu                 * fDockMenu;
     109   
     110    NSTimer                         * fSpeedLimitTimer;
    110111   
    111112    NSMutableArray                  * fAutoImportedNames;
     
    219220- (void) toggleSpeedLimit: (id) sender;
    220221- (void) autoSpeedLimitChange: (NSNotification *) notification;
    221 - (void) autoSpeedLimit;
     222- (void) autoSpeedLimit: (NSTimer *) timer;
     223- (void) setAutoSpeedLimitTimer: (BOOL) nextIsOn;
    222224
    223225- (void) setLimitGlobalEnabled: (id) sender;
  • trunk/macosx/Controller.m

    r6221 r6223  
    129129#define SEARCH_FILTER_MAX_WIDTH 95.0
    130130
    131 #define UPDATE_UI_SECONDS           1.0
    132 #define AUTO_SPEED_LIMIT_SECONDS    5.0
     131#define UPDATE_UI_SECONDS   1.0
    133132
    134133#define DOCK_SEEDING_TAG        101
     
    486485        [self showInfo: nil];
    487486   
    488     //timer to auto toggle speed limit
     487    //set up the speed limit
    489488    [self autoSpeedLimitChange: nil];
    490     fSpeedLimitTimer = [NSTimer scheduledTimerWithTimeInterval: AUTO_SPEED_LIMIT_SECONDS target: self
    491                         selector: @selector(autoSpeedLimit) userInfo: nil repeats: YES];
    492     [[NSRunLoop currentRunLoop] addTimer: fSpeedLimitTimer forMode: NSModalPanelRunLoopMode];
    493     [[NSRunLoop currentRunLoop] addTimer: fSpeedLimitTimer forMode: NSEventTrackingRunLoopMode];
    494489}
    495490
     
    559554    [fTimer invalidate];
    560555    [fSpeedLimitTimer invalidate];
     556   
    561557    if (fAutoImportTimer)
    562558    {   
     
    781777            }
    782778            else //this shouldn't happen
    783                 NSLog(@"Unknown error when attempting to open \"%@\"", torrentPath);
     779                NSLog(@"Unknown error code (%d) when attempting to open \"%@\"", result, torrentPath);
    784780           
    785781            tr_ctorFree(ctor);
     
    23502346- (void) autoSpeedLimitChange: (NSNotification *) notification
    23512347{
     2348    //clear timer here in case its not being reset
     2349    [fSpeedLimitTimer invalidate];
     2350    fSpeedLimitTimer = nil;
     2351   
    23522352    if (![fDefaults boolForKey: @"SpeedLimitAuto"])
    23532353        return;
    23542354 
    23552355    NSCalendarDate * onDate = [NSCalendarDate dateWithTimeIntervalSinceReferenceDate:
    2356                         [[fDefaults objectForKey: @"SpeedLimitAutoOnDate"] timeIntervalSinceReferenceDate]],
    2357         * offDate = [NSCalendarDate dateWithTimeIntervalSinceReferenceDate:
    2358                         [[fDefaults objectForKey: @"SpeedLimitAutoOffDate"] timeIntervalSinceReferenceDate]],
    2359         * nowDate = [NSCalendarDate calendarDate];
     2356                                [[fDefaults objectForKey: @"SpeedLimitAutoOnDate"] timeIntervalSinceReferenceDate]],
     2357                    * offDate = [NSCalendarDate dateWithTimeIntervalSinceReferenceDate:
     2358                                [[fDefaults objectForKey: @"SpeedLimitAutoOffDate"] timeIntervalSinceReferenceDate]],
     2359                    * nowDate = [NSCalendarDate calendarDate];
    23602360   
    23612361    //check if should be on if within range
     
    23732373    if ([fDefaults boolForKey: @"SpeedLimit"] != shouldBeOn)
    23742374        [self toggleSpeedLimit: nil];
    2375 }
    2376 
    2377 - (void) autoSpeedLimit
    2378 {
    2379     if (![fDefaults boolForKey: @"SpeedLimitAuto"])
     2375   
     2376    //no need to check if both times are equal
     2377    if (onTime == offTime)
    23802378        return;
    23812379   
    2382     //only toggle if within first few seconds of minutes
    2383     NSCalendarDate * nowDate = [NSCalendarDate calendarDate];
    2384     if ([nowDate secondOfMinute] > AUTO_SPEED_LIMIT_SECONDS)
    2385         return;
    2386    
    2387     NSCalendarDate * offDate = [NSCalendarDate dateWithTimeIntervalSinceReferenceDate:
    2388                         [[fDefaults objectForKey: @"SpeedLimitAutoOffDate"] timeIntervalSinceReferenceDate]];
    2389    
    2390     BOOL toggle;
    2391     if ([fDefaults boolForKey: @"SpeedLimit"])
     2380    [self setAutoSpeedLimitTimer: !shouldBeOn];
     2381}
     2382
     2383- (void) autoSpeedLimit: (NSTimer *) timer
     2384{
     2385    //check if should toggle (for cases where users might have manually
     2386    NSCalendarDate * onDate = [NSCalendarDate dateWithTimeIntervalSinceReferenceDate:
     2387                                [[fDefaults objectForKey: @"SpeedLimitAutoOnDate"] timeIntervalSinceReferenceDate]],
     2388                    * offDate = [NSCalendarDate dateWithTimeIntervalSinceReferenceDate:
     2389                                [[fDefaults objectForKey: @"SpeedLimitAutoOffDate"] timeIntervalSinceReferenceDate]],
     2390                    * nowDate = [NSCalendarDate calendarDate];
     2391   
     2392    BOOL currentlyLimiting = [fDefaults boolForKey: @"SpeedLimit"], toggle;
     2393    if (currentlyLimiting)
    23922394        toggle = [nowDate hourOfDay] == [offDate hourOfDay] && [nowDate minuteOfHour] == [offDate minuteOfHour];
    23932395    else
    2394     {
    2395         NSCalendarDate * onDate = [NSCalendarDate dateWithTimeIntervalSinceReferenceDate:
    2396                         [[fDefaults objectForKey: @"SpeedLimitAutoOnDate"] timeIntervalSinceReferenceDate]];
    2397         toggle = ([nowDate hourOfDay] == [onDate hourOfDay] && [nowDate minuteOfHour] == [onDate minuteOfHour])
    2398                     && !([onDate hourOfDay] == [offDate hourOfDay] && [onDate minuteOfHour] == [offDate minuteOfHour]);
    2399     }
     2396        toggle = [nowDate hourOfDay] == [onDate hourOfDay] && [nowDate minuteOfHour] == [onDate minuteOfHour];
    24002397   
    24012398    if (toggle)
     
    24082405            description: NSLocalizedString(@"Bandwidth settings changed", "Growl notification description")
    24092406            notificationName: GROWL_AUTO_SPEED_LIMIT iconData: nil priority: 0 isSticky: NO clickContext: nil];
    2410     }
     2407           
     2408            currentlyLimiting != currentlyLimiting;
     2409    }
     2410   
     2411    [self setAutoSpeedLimitTimer: !currentlyLimiting];
     2412}
     2413
     2414- (void) setAutoSpeedLimitTimer: (BOOL) nextIsOn
     2415{
     2416    NSCalendarDate * timerDate = [NSCalendarDate dateWithTimeIntervalSinceReferenceDate: [[fDefaults objectForKey:
     2417                                    nextIsOn ? @"SpeedLimitAutoOnDate" : @"SpeedLimitAutoOffDate"] timeIntervalSinceReferenceDate]],
     2418                    * nowDate = [NSCalendarDate calendarDate];
     2419   
     2420    //create date with combination of the current date and the date to go off
     2421    NSDateComponents * components = [[NSDateComponents alloc] init];
     2422    [components setDay: [nowDate dayOfMonth]];
     2423    [components setMonth: [nowDate monthOfYear]];
     2424    [components setYear: [nowDate yearOfCommonEra]];
     2425    [components setHour: [timerDate hourOfDay]];
     2426    [components setMinute: [timerDate minuteOfHour]];
     2427    [components setSecond: 0];
     2428   
     2429    NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
     2430    NSDate * dateToUse = [calendar dateFromComponents: components];
     2431    [calendar release];
     2432    [components release];
     2433   
     2434    //check if should be the next day
     2435    int timerTime = [timerDate hourOfDay] * 60 + [timerDate minuteOfHour],
     2436        nowTime = [nowDate hourOfDay] * 60 + [nowDate minuteOfHour];
     2437    if (timerTime < nowTime)
     2438        dateToUse = [dateToUse addTimeInterval: 60 * 60 * 24]; //60 sec * 60 min * 24 hr
     2439   
     2440    fSpeedLimitTimer = [[NSTimer alloc] initWithFireDate: dateToUse interval: 0 target: self selector: @selector(autoSpeedLimit:)
     2441                        userInfo: nil repeats: NO];
     2442   
     2443    [[NSRunLoop currentRunLoop] addTimer: fSpeedLimitTimer forMode: NSDefaultRunLoopMode];
     2444    [[NSRunLoop currentRunLoop] addTimer: fSpeedLimitTimer forMode: NSModalPanelRunLoopMode];
     2445    [[NSRunLoop currentRunLoop] addTimer: fSpeedLimitTimer forMode: NSEventTrackingRunLoopMode];
     2446    [fSpeedLimitTimer release];
    24112447}
    24122448
Note: See TracChangeset for help on using the changeset viewer.