2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2009-2022 Transmission authors and contributors.
|
|
|
|
// It may be used under the MIT (SPDX: MIT) license.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2009-09-26 04:02:39 +00:00
|
|
|
|
|
|
|
#import "TrackerNode.h"
|
2009-09-28 01:57:35 +00:00
|
|
|
#import "NSStringAdditions.h"
|
2009-09-26 04:02:39 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
@interface TrackerNode ()
|
|
|
|
|
|
|
|
@property(nonatomic, readonly) tr_tracker_view fStat;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2009-09-26 04:02:39 +00:00
|
|
|
@implementation TrackerNode
|
2012-01-20 02:41:49 +00:00
|
|
|
|
2021-12-08 16:55:52 +00:00
|
|
|
- (instancetype)initWithTrackerView:(tr_tracker_view const*)stat torrent:(Torrent*)torrent
|
2009-09-26 04:02:39 +00:00
|
|
|
{
|
|
|
|
if ((self = [super init]))
|
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
_fStat = *stat;
|
2021-01-12 19:06:30 +00:00
|
|
|
_torrent = torrent; //weak reference
|
2009-09-26 04:02:39 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2009-09-26 04:02:39 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (NSString*)description
|
2010-01-09 00:57:30 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
return [@"Tracker: " stringByAppendingString:self.fullAnnounceAddress];
|
2010-01-09 00:57:30 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (id)copyWithZone:(NSZone*)zone
|
2009-09-28 01:57:35 +00:00
|
|
|
{
|
|
|
|
//this object is essentially immutable after initial setup
|
2017-07-29 16:14:22 +00:00
|
|
|
return self;
|
2009-09-28 01:57:35 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (BOOL)isEqual:(id)object
|
2012-01-14 02:28:54 +00:00
|
|
|
{
|
|
|
|
if (self == object)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2012-01-14 02:28:54 +00:00
|
|
|
return YES;
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
if (![object isKindOfClass:[self class]])
|
|
|
|
{
|
2012-01-14 02:28:54 +00:00
|
|
|
return NO;
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
|
|
|
|
2021-09-24 12:56:57 +00:00
|
|
|
auto other = static_cast<decltype(self)>(object);
|
2021-08-07 07:27:56 +00:00
|
|
|
if (self.torrent != other.torrent)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2012-01-14 16:06:39 +00:00
|
|
|
return NO;
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
return self.tier == other.tier && [self.fullAnnounceAddress isEqualToString:other.fullAnnounceAddress];
|
2012-01-14 02:28:54 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (NSString*)host
|
2009-09-26 04:02:39 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
return @(self.fStat.host);
|
2009-09-26 04:02:39 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (NSString*)fullAnnounceAddress
|
2009-09-28 02:55:16 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
return @(self.fStat.announce);
|
2009-09-28 02:55:16 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (NSInteger)tier
|
2009-09-29 03:02:44 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
return self.fStat.tier;
|
2009-09-29 03:02:44 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (NSUInteger)identifier
|
2010-01-10 02:34:00 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
return self.fStat.id;
|
2010-01-10 02:34:00 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (NSInteger)totalSeeders
|
2009-09-28 14:13:20 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
return self.fStat.seederCount;
|
2009-09-28 14:13:20 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (NSInteger)totalLeechers
|
2009-09-28 14:13:20 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
return self.fStat.leecherCount;
|
2009-09-28 14:13:20 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (NSInteger)totalDownloaded
|
2009-09-28 14:13:20 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
return self.fStat.downloadCount;
|
2009-09-28 14:13:20 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (NSString*)lastAnnounceStatusString
|
2009-09-28 01:57:35 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSString* dateString;
|
2022-02-22 16:04:20 +00:00
|
|
|
if (self.fStat.hasAnnounced)
|
2009-09-28 01:57:35 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
|
2021-08-07 07:27:56 +00:00
|
|
|
dateFormatter.dateStyle = NSDateFormatterFullStyle;
|
|
|
|
dateFormatter.timeStyle = NSDateFormatterShortStyle;
|
|
|
|
dateFormatter.doesRelativeDateFormatting = YES;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
dateString = [dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:self.fStat.lastAnnounceTime]];
|
2009-09-28 01:57:35 +00:00
|
|
|
}
|
|
|
|
else
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2009-09-28 01:57:35 +00:00
|
|
|
dateString = NSLocalizedString(@"N/A", "Tracker last announce");
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSString* baseString;
|
2022-02-22 16:04:20 +00:00
|
|
|
if (self.fStat.hasAnnounced && self.fStat.lastAnnounceTimedOut)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
|
|
|
baseString = [NSLocalizedString(@"Announce timed out", "Tracker last announce") stringByAppendingFormat:@": %@", dateString];
|
|
|
|
}
|
2022-02-22 16:04:20 +00:00
|
|
|
else if (self.fStat.hasAnnounced && !self.fStat.lastAnnounceSucceeded)
|
2009-11-21 18:23:54 +00:00
|
|
|
{
|
|
|
|
baseString = NSLocalizedString(@"Announce error", "Tracker last announce");
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
NSString* errorString = @(self.fStat.lastAnnounceResult);
|
2021-08-15 09:41:48 +00:00
|
|
|
if ([errorString isEqualToString:@""])
|
|
|
|
{
|
|
|
|
baseString = [baseString stringByAppendingFormat:@": %@", dateString];
|
|
|
|
}
|
2009-11-21 18:23:54 +00:00
|
|
|
else
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
|
|
|
baseString = [baseString stringByAppendingFormat:@": %@ - %@", errorString, dateString];
|
|
|
|
}
|
2009-11-21 18:23:54 +00:00
|
|
|
}
|
2009-09-28 01:57:35 +00:00
|
|
|
else
|
2009-09-28 02:27:38 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
baseString = [NSLocalizedString(@"Last Announce", "Tracker last announce") stringByAppendingFormat:@": %@", dateString];
|
2022-02-22 16:04:20 +00:00
|
|
|
if (self.fStat.hasAnnounced && self.fStat.lastAnnounceSucceeded && self.fStat.lastAnnouncePeerCount > 0)
|
2009-09-28 03:19:11 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSString* peerString;
|
2022-02-22 16:04:20 +00:00
|
|
|
if (self.fStat.lastAnnouncePeerCount == 1)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2010-01-05 04:34:31 +00:00
|
|
|
peerString = NSLocalizedString(@"got 1 peer", "Tracker last announce");
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2010-01-05 04:34:31 +00:00
|
|
|
else
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2022-11-14 18:30:03 +00:00
|
|
|
peerString = [NSString localizedStringWithFormat:NSLocalizedString(@"got %lu peers", "Tracker last announce"),
|
|
|
|
(size_t)self.fStat.lastAnnouncePeerCount];
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
|
|
|
baseString = [baseString stringByAppendingFormat:@" (%@)", peerString];
|
2009-09-28 03:19:11 +00:00
|
|
|
}
|
2009-09-28 02:27:38 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2009-11-21 18:23:54 +00:00
|
|
|
return baseString;
|
2009-09-28 01:57:35 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (NSString*)nextAnnounceStatusString
|
2009-09-28 01:57:35 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
switch (self.fStat.announceState)
|
2009-10-09 22:29:34 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
case TR_TRACKER_ACTIVE:
|
|
|
|
return [NSLocalizedString(@"Announce in progress", "Tracker next announce") stringByAppendingEllipsis];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
case TR_TRACKER_WAITING:
|
2014-10-17 05:12:00 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
NSTimeInterval const nextAnnounceTimeLeft = self.fStat.nextAnnounceTime - [NSDate date].timeIntervalSince1970;
|
2021-08-15 09:41:48 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
});
|
2021-08-15 09:41:48 +00:00
|
|
|
|
|
|
|
NSString* timeString = [formatter stringFromTimeInterval:nextAnnounceTimeLeft];
|
|
|
|
return [NSString stringWithFormat:NSLocalizedString(@"Next announce in %@", "Tracker next announce"), timeString];
|
2014-10-17 05:12:00 +00:00
|
|
|
}
|
2021-08-15 09:41:48 +00:00
|
|
|
case TR_TRACKER_QUEUED:
|
|
|
|
return [NSLocalizedString(@"Announce is queued", "Tracker next announce") stringByAppendingEllipsis];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
case TR_TRACKER_INACTIVE:
|
2022-02-22 16:04:20 +00:00
|
|
|
return self.fStat.isBackup ? NSLocalizedString(@"Tracker will be used as a backup", "Tracker next announce") :
|
2022-03-30 21:52:23 +00:00
|
|
|
NSLocalizedString(@"Announce not scheduled", "Tracker next announce");
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
default:
|
2022-02-22 16:04:20 +00:00
|
|
|
NSAssert1(NO, @"unknown announce state: %d", self.fStat.announceState);
|
2021-08-15 09:41:48 +00:00
|
|
|
return nil;
|
2009-10-09 22:29:34 +00:00
|
|
|
}
|
2009-09-28 01:57:35 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (NSString*)lastScrapeStatusString
|
2009-09-28 01:57:35 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSString* dateString;
|
2022-02-22 16:04:20 +00:00
|
|
|
if (self.fStat.hasScraped)
|
2009-09-28 01:57:35 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
|
2021-08-07 07:27:56 +00:00
|
|
|
dateFormatter.dateStyle = NSDateFormatterFullStyle;
|
|
|
|
dateFormatter.timeStyle = NSDateFormatterShortStyle;
|
|
|
|
dateFormatter.doesRelativeDateFormatting = YES;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
dateString = [dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:self.fStat.lastScrapeTime]];
|
2009-09-28 01:57:35 +00:00
|
|
|
}
|
|
|
|
else
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2009-09-28 14:51:13 +00:00
|
|
|
dateString = NSLocalizedString(@"N/A", "Tracker last scrape");
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSString* baseString;
|
2022-02-22 16:04:20 +00:00
|
|
|
if (self.fStat.hasScraped && self.fStat.lastScrapeTimedOut)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
|
|
|
baseString = [NSLocalizedString(@"Scrape timed out", "Tracker last scrape") stringByAppendingFormat:@": %@", dateString];
|
|
|
|
}
|
2022-02-22 16:04:20 +00:00
|
|
|
else if (self.fStat.hasScraped && !self.fStat.lastScrapeSucceeded)
|
2009-11-21 18:23:54 +00:00
|
|
|
{
|
|
|
|
baseString = NSLocalizedString(@"Scrape error", "Tracker last scrape");
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
NSString* errorString = @(self.fStat.lastScrapeResult);
|
2021-08-15 09:41:48 +00:00
|
|
|
if ([errorString isEqualToString:@""])
|
|
|
|
{
|
|
|
|
baseString = [baseString stringByAppendingFormat:@": %@", dateString];
|
|
|
|
}
|
2009-11-21 18:23:54 +00:00
|
|
|
else
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
|
|
|
baseString = [baseString stringByAppendingFormat:@": %@ - %@", errorString, dateString];
|
|
|
|
}
|
2009-11-21 18:23:54 +00:00
|
|
|
}
|
2009-09-28 01:57:35 +00:00
|
|
|
else
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
|
|
|
baseString = [NSLocalizedString(@"Last Scrape", "Tracker last scrape") stringByAppendingFormat:@": %@", dateString];
|
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2009-11-21 18:23:54 +00:00
|
|
|
return baseString;
|
2009-09-28 01:57:35 +00:00
|
|
|
}
|
|
|
|
|
2009-09-26 04:02:39 +00:00
|
|
|
@end
|