transmission/macosx/TrackerNode.mm

248 lines
7.6 KiB
Plaintext
Raw Normal View History

/******************************************************************************
2012-01-14 17:12:04 +00:00
* Copyright (c) 2009-2012 Transmission authors and contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*****************************************************************************/
#import "TrackerNode.h"
#import "NSApplicationAdditions.h"
#import "NSStringAdditions.h"
@implementation TrackerNode
2021-01-12 19:06:30 +00:00
{
tr_tracker_stat fStat;
}
- (instancetype)initWithTrackerStat:(tr_tracker_stat*)stat torrent:(Torrent*)torrent
{
if ((self = [super init]))
{
fStat = *stat;
2021-01-12 19:06:30 +00:00
_torrent = torrent; //weak reference
}
return self;
}
- (NSString*)description
2010-01-09 00:57:30 +00:00
{
return [@"Tracker: " stringByAppendingString:self.fullAnnounceAddress];
2010-01-09 00:57:30 +00:00
}
- (id)copyWithZone:(NSZone*)zone
{
//this object is essentially immutable after initial setup
2017-07-29 16:14:22 +00:00
return self;
}
- (BOOL)isEqual:(id)object
{
if (self == object)
{
return YES;
}
if (![object isKindOfClass:[self class]])
{
return NO;
}
auto other = static_cast<decltype(self)>(object);
if (self.torrent != other.torrent)
{
return NO;
}
return self.tier == other.tier && [self.fullAnnounceAddress isEqualToString:other.fullAnnounceAddress];
}
- (NSString*)host
{
2017-07-08 08:06:32 +00:00
return @(fStat.host);
}
- (NSString*)fullAnnounceAddress
{
2017-07-08 08:06:32 +00:00
return @(fStat.announce);
}
- (NSInteger)tier
{
return fStat.tier;
}
- (NSUInteger)identifier
{
return fStat.id;
}
- (NSInteger)totalSeeders
{
return fStat.seederCount;
}
- (NSInteger)totalLeechers
{
return fStat.leecherCount;
}
- (NSInteger)totalDownloaded
{
return fStat.downloadCount;
}
- (NSString*)lastAnnounceStatusString
{
NSString* dateString;
2009-10-09 22:29:34 +00:00
if (fStat.hasAnnounced)
{
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateStyle = NSDateFormatterFullStyle;
dateFormatter.timeStyle = NSDateFormatterShortStyle;
dateFormatter.doesRelativeDateFormatting = YES;
dateString = [dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:fStat.lastAnnounceTime]];
}
else
{
dateString = NSLocalizedString(@"N/A", "Tracker last announce");
}
NSString* baseString;
2010-02-06 00:45:28 +00:00
if (fStat.hasAnnounced && fStat.lastAnnounceTimedOut)
{
baseString = [NSLocalizedString(@"Announce timed out", "Tracker last announce") stringByAppendingFormat:@": %@", dateString];
}
2010-02-06 00:45:28 +00:00
else if (fStat.hasAnnounced && !fStat.lastAnnounceSucceeded)
{
baseString = NSLocalizedString(@"Announce error", "Tracker last announce");
NSString* errorString = @(fStat.lastAnnounceResult);
if ([errorString isEqualToString:@""])
{
baseString = [baseString stringByAppendingFormat:@": %@", dateString];
}
else
{
baseString = [baseString stringByAppendingFormat:@": %@ - %@", errorString, dateString];
}
}
else
{
baseString = [NSLocalizedString(@"Last Announce", "Tracker last announce") stringByAppendingFormat:@": %@", dateString];
if (fStat.hasAnnounced && fStat.lastAnnounceSucceeded && fStat.lastAnnouncePeerCount > 0)
2009-09-28 03:19:11 +00:00
{
NSString* peerString;
2010-01-31 21:25:49 +00:00
if (fStat.lastAnnouncePeerCount == 1)
{
2010-01-05 04:34:31 +00:00
peerString = NSLocalizedString(@"got 1 peer", "Tracker last announce");
}
2010-01-05 04:34:31 +00:00
else
{
peerString = [NSString stringWithFormat:NSLocalizedString(@"got %d peers", "Tracker last announce"), fStat.lastAnnouncePeerCount];
}
baseString = [baseString stringByAppendingFormat:@" (%@)", peerString];
2009-09-28 03:19:11 +00:00
}
}
return baseString;
}
- (NSString*)nextAnnounceStatusString
{
2009-10-09 22:29:34 +00:00
switch (fStat.announceState)
{
case TR_TRACKER_ACTIVE:
return [NSLocalizedString(@"Announce in progress", "Tracker next announce") stringByAppendingEllipsis];
case TR_TRACKER_WAITING:
{
NSTimeInterval const nextAnnounceTimeLeft = fStat.nextAnnounceTime - [NSDate date].timeIntervalSince1970;
static NSDateComponentsFormatter* formatter;
2020-12-23 03:00:06 +00:00
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
formatter = [NSDateComponentsFormatter new];
formatter.unitsStyle = NSDateComponentsFormatterUnitsStyleAbbreviated;
formatter.zeroFormattingBehavior = NSDateComponentsFormatterZeroFormattingBehaviorDropLeading;
formatter.collapsesLargestUnit = YES;
});
NSString* timeString = [formatter stringFromTimeInterval:nextAnnounceTimeLeft];
return [NSString stringWithFormat:NSLocalizedString(@"Next announce in %@", "Tracker next announce"), timeString];
}
case TR_TRACKER_QUEUED:
return [NSLocalizedString(@"Announce is queued", "Tracker next announce") stringByAppendingEllipsis];
case TR_TRACKER_INACTIVE:
return fStat.isBackup ? NSLocalizedString(@"Tracker will be used as a backup", "Tracker next announce") :
NSLocalizedString(@"Announce not scheduled", "Tracker next announce");
default:
NSAssert1(NO, @"unknown announce state: %d", fStat.announceState);
return nil;
2009-10-09 22:29:34 +00:00
}
}
- (NSString*)lastScrapeStatusString
{
NSString* dateString;
if (fStat.hasScraped)
{
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateStyle = NSDateFormatterFullStyle;
dateFormatter.timeStyle = NSDateFormatterShortStyle;
dateFormatter.doesRelativeDateFormatting = YES;
dateString = [dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:fStat.lastScrapeTime]];
}
else
{
2009-09-28 14:51:13 +00:00
dateString = NSLocalizedString(@"N/A", "Tracker last scrape");
}
NSString* baseString;
if (fStat.hasScraped && fStat.lastScrapeTimedOut)
{
baseString = [NSLocalizedString(@"Scrape timed out", "Tracker last scrape") stringByAppendingFormat:@": %@", dateString];
}
else if (fStat.hasScraped && !fStat.lastScrapeSucceeded)
{
baseString = NSLocalizedString(@"Scrape error", "Tracker last scrape");
NSString* errorString = @(fStat.lastScrapeResult);
if ([errorString isEqualToString:@""])
{
baseString = [baseString stringByAppendingFormat:@": %@", dateString];
}
else
{
baseString = [baseString stringByAppendingFormat:@": %@ - %@", errorString, dateString];
}
}
else
{
baseString = [NSLocalizedString(@"Last Scrape", "Tracker last scrape") stringByAppendingFormat:@": %@", dateString];
}
return baseString;
}
@end