From c5a1a89df03232d29e980ca2242c07c077efb254 Mon Sep 17 00:00:00 2001 From: Mitchell Livingston Date: Tue, 9 Jan 2007 03:46:21 +0000 Subject: [PATCH] when a download finishes, stop and make wait if the seeding queue is full --- macosx/Controller.h | 4 +- macosx/Controller.m | 199 +++++++++++++++++++++++++------------------- macosx/Torrent.m | 11 ++- 3 files changed, 122 insertions(+), 92 deletions(-) diff --git a/macosx/Controller.h b/macosx/Controller.h index 85b8e02bf..6b7079e84 100644 --- a/macosx/Controller.h +++ b/macosx/Controller.h @@ -152,6 +152,9 @@ - (void) updateControlTint: (NSNotification *) notification; - (void) updateUI: (NSTimer *) timer; + +- (void) updateTorrentsInQueue; + - (void) torrentFinishedDownloading: (NSNotification *) notification; - (void) updateTorrentHistory; @@ -173,7 +176,6 @@ - (void) setQuickRatioGlobal: (id) sender; - (void) torrentStoppedForRatio: (NSNotification *) notification; -- (void) updateTorrentsInQueue; - (void) changeAutoImport; - (void) checkAutoImportDirectory; diff --git a/macosx/Controller.m b/macosx/Controller.m index 3675236bd..2a64d2230 100644 --- a/macosx/Controller.m +++ b/macosx/Controller.m @@ -1135,14 +1135,97 @@ static void sleepCallBack(void * controller, io_service_t y, natural_t messageTy [fBadger updateBadgeWithCompleted: fCompleted uploadRate: uploadRate downloadRate: downloadRate]; } +- (void) updateTorrentsInQueue +{ + BOOL download = [fDefaults boolForKey: @"Queue"], + seed = [fDefaults boolForKey: @"QueueSeed"]; + + if (!download && !seed) + { + NSEnumerator * enumerator = [fTorrents objectEnumerator]; + Torrent * torrent; + while ((torrent = [enumerator nextObject])) + if (![torrent isActive] && [torrent waitingToStart]) + [torrent startTransfer]; + + [self updateUI: nil]; + [self applyFilter: nil]; + [self updateTorrentHistory]; + + return; + } + + //determine the number of downloads needed to start + int desiredDownloadActive = download ? [fDefaults integerForKey: @"QueueDownloadNumber"] : 0, + desiredSeedActive = seed ? [fDefaults integerForKey: @"QueueSeedNumber"] : 0; + + NSEnumerator * enumerator = [fTorrents objectEnumerator]; + Torrent * torrent; + while ((torrent = [enumerator nextObject])) + if ([torrent isActive] && ![torrent isError]) + { + if (![torrent isSeeding]) + desiredDownloadActive--; + else + desiredSeedActive--; + + if (desiredDownloadActive <= 0 && desiredSeedActive <= 0) + break; + } + + //sort torrents by order value + NSArray * sortedTorrents; + if ([fTorrents count] > 1 && (desiredDownloadActive > 0 || desiredSeedActive > 0)) + { + NSSortDescriptor * orderDescriptor = [[[NSSortDescriptor alloc] initWithKey: + @"orderValue" ascending: YES] autorelease]; + NSArray * descriptors = [[NSArray alloc] initWithObjects: orderDescriptor, nil]; + + sortedTorrents = [fTorrents sortedArrayUsingDescriptors: descriptors]; + [descriptors release]; + } + else + sortedTorrents = fTorrents; + + enumerator = [sortedTorrents objectEnumerator]; + while ((torrent = [enumerator nextObject])) + { + if (![torrent isActive] && [torrent waitingToStart]) + { + if ([torrent progress] < 1.0) + { + if (!download || desiredDownloadActive > 0) + { + [torrent startTransfer]; + if ([torrent isActive]) + desiredDownloadActive--; + [torrent update]; + } + } + else + { + if (!seed || desiredSeedActive > 0) + { + [torrent startTransfer]; + if ([torrent isActive]) + desiredSeedActive--; + [torrent update]; + } + } + } + } + + [self updateUI: nil]; + [self applyFilter: nil]; + [self updateTorrentHistory]; +} + - (void) torrentFinishedDownloading: (NSNotification *) notification { Torrent * torrent = [notification object]; [fInfoController updateInfoStats]; - [self updateTorrentsInQueue]; - if ([fDefaults boolForKey: @"PlayDownloadSound"]) { NSSound * sound; @@ -1156,6 +1239,33 @@ static void sleepCallBack(void * controller, io_service_t y, natural_t messageTy if (![fWindow isKeyWindow]) fCompleted++; + + //update queue if there is a seeding queue + if ([fDefaults boolForKey: @"QueueSeed"]) + { + int desiredSeedActive = [fDefaults integerForKey: @"QueueSeedNumber"]; + + NSEnumerator * enumerator = [fTorrents objectEnumerator]; + Torrent * otherTorrent; + while ((otherTorrent = [enumerator nextObject])) + if (otherTorrent != torrent && [otherTorrent isSeeding] && [otherTorrent isActive] && ![otherTorrent isError]) + { + desiredSeedActive--; + if (desiredSeedActive <= 0) + break; + } + + if (desiredSeedActive <= 0) + { + [torrent stopTransfer]; + [torrent setWaitToStart: YES]; + [torrent update]; + } + } + + [self updateUI: nil]; + [self applyFilter: nil]; + [self updateTorrentHistory]; } - (void) updateTorrentHistory @@ -1576,91 +1686,6 @@ static void sleepCallBack(void * controller, io_service_t y, natural_t messageTy notificationName: GROWL_SEEDING_COMPLETE iconData: nil priority: 0 isSticky: NO clickContext: nil]; } -- (void) updateTorrentsInQueue -{ - BOOL download = [fDefaults boolForKey: @"Queue"], - seed = [fDefaults boolForKey: @"QueueSeed"]; - - if (!download && !seed) - { - NSEnumerator * enumerator = [fTorrents objectEnumerator]; - Torrent * torrent; - while ((torrent = [enumerator nextObject])) - if (![torrent isActive] && [torrent waitingToStart]) - [torrent startTransfer]; - - [self updateUI: nil]; - [self applyFilter: nil]; - [self updateTorrentHistory]; - - return; - } - - //determine the number of downloads needed to start - int desiredDownloadActive = download ? [fDefaults integerForKey: @"QueueDownloadNumber"] : 0, - desiredSeedActive = seed ? [fDefaults integerForKey: @"QueueSeedNumber"] : 0; - - NSEnumerator * enumerator = [fTorrents objectEnumerator]; - Torrent * torrent; - while ((torrent = [enumerator nextObject])) - if ([torrent isActive] && ![torrent isError]) - { - if (![torrent isSeeding]) - desiredDownloadActive--; - else - desiredSeedActive--; - - if (desiredDownloadActive <= 0 && desiredSeedActive <= 0) - break; - } - - //sort torrents by order value - NSArray * sortedTorrents; - if ([fTorrents count] > 1 && (desiredDownloadActive > 0 || desiredSeedActive > 0)) - { - NSSortDescriptor * orderDescriptor = [[[NSSortDescriptor alloc] initWithKey: - @"orderValue" ascending: YES] autorelease]; - NSArray * descriptors = [[NSArray alloc] initWithObjects: orderDescriptor, nil]; - - sortedTorrents = [fTorrents sortedArrayUsingDescriptors: descriptors]; - [descriptors release]; - } - else - sortedTorrents = fTorrents; - - enumerator = [sortedTorrents objectEnumerator]; - while ((torrent = [enumerator nextObject])) - { - if (![torrent isActive] && [torrent waitingToStart]) - { - if ([torrent progress] < 1.0) - { - if (!download || desiredDownloadActive > 0) - { - [torrent startTransfer]; - if ([torrent isActive]) - desiredDownloadActive--; - [torrent update]; - } - } - else - { - if (!seed || desiredSeedActive > 0) - { - [torrent startTransfer]; - if ([torrent isActive]) - desiredSeedActive--; - [torrent update]; - } - } - } - } - - [self updateUI: nil]; - [self applyFilter: nil]; - [self updateTorrentHistory]; -} - -(void) watcher: (id) watcher receivedNotification: (NSString *) notification forPath: (NSString *) path { if ([notification isEqualToString: UKFileWatcherWriteNotification]) diff --git a/macosx/Torrent.m b/macosx/Torrent.m index 5936998ab..09fb5f4ba 100644 --- a/macosx/Torrent.m +++ b/macosx/Torrent.m @@ -290,11 +290,14 @@ static uint32_t kRed = BE(0xFF6450FF), //255, 100, 80 NSString * tempString; case TR_STATUS_PAUSE: - if (fFinishedSeeding) + if (fWaitToStart) + { + tempString = [self progress] < 1.0 + ? [NSLocalizedString(@"Waiting to start", "Torrent -> status string") stringByAppendingEllipsis] + : [NSLocalizedString(@"Waiting to seed", "Torrent -> status string") stringByAppendingEllipsis]; + } + else if (fFinishedSeeding) tempString = NSLocalizedString(@"Seeding complete", "Torrent -> status string"); - #warning differentiate seed waiting - else if (fWaitToStart) - tempString = [NSLocalizedString(@"Waiting to start", "Torrent -> status string") stringByAppendingEllipsis]; else tempString = NSLocalizedString(@"Paused", "Torrent -> status string");