2023-11-01 21:11:11 +00:00
|
|
|
// This file Copyright © Transmission authors and contributors.
|
2022-01-20 18:27:56 +00:00
|
|
|
// It may be used under the MIT (SPDX: MIT) license.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2008-12-06 01:18:25 +00:00
|
|
|
|
|
|
|
#import "BonjourController.h"
|
|
|
|
|
2022-10-19 19:28:21 +00:00
|
|
|
static NSUInteger const kBonjourServiceNameMaxLength = 63;
|
2012-09-09 05:19:10 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
@interface BonjourController ()
|
|
|
|
|
|
|
|
@property(nonatomic) NSNetService* fService;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2008-12-06 01:18:25 +00:00
|
|
|
@implementation BonjourController
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
BonjourController* fDefaultController = nil;
|
|
|
|
|
|
|
|
+ (BonjourController*)defaultController
|
2008-12-06 01:18:25 +00:00
|
|
|
{
|
2012-03-13 03:20:09 +00:00
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
2008-12-06 01:18:25 +00:00
|
|
|
fDefaultController = [[BonjourController alloc] init];
|
2012-03-13 03:20:09 +00:00
|
|
|
});
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2008-12-06 01:18:25 +00:00
|
|
|
return fDefaultController;
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
+ (BOOL)defaultControllerExists
|
2012-09-10 02:01:07 +00:00
|
|
|
{
|
|
|
|
return fDefaultController != nil;
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)startWithPort:(int)port
|
2008-12-06 01:18:25 +00:00
|
|
|
{
|
|
|
|
[self stop];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSMutableString* serviceName = [NSMutableString
|
|
|
|
stringWithFormat:@"Transmission (%@ - %@)", NSUserName(), [NSHost currentHost].localizedName];
|
2022-10-19 19:28:21 +00:00
|
|
|
if (serviceName.length > kBonjourServiceNameMaxLength)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2022-10-19 19:28:21 +00:00
|
|
|
[serviceName deleteCharactersInRange:NSMakeRange(kBonjourServiceNameMaxLength, serviceName.length - kBonjourServiceNameMaxLength)];
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fService = [[NSNetService alloc] initWithDomain:@"" type:@"_http._tcp." name:serviceName port:port];
|
|
|
|
self.fService.delegate = self;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
[self.fService publish];
|
2008-12-06 01:18:25 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)stop
|
2008-12-06 01:18:25 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
[self.fService stop];
|
|
|
|
self.fService = nil;
|
2008-12-06 01:18:25 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)netService:(NSNetService*)sender didNotPublish:(NSDictionary*)errorDict
|
2008-12-06 01:18:25 +00:00
|
|
|
{
|
2021-08-07 07:27:56 +00:00
|
|
|
NSLog(@"Failed to publish the web interface service on port %ld, with error: %@", sender.port, errorDict);
|
2008-12-06 01:18:25 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)netService:(NSNetService*)sender didNotResolve:(NSDictionary*)errorDict
|
2008-12-06 01:18:25 +00:00
|
|
|
{
|
2021-08-07 07:27:56 +00:00
|
|
|
NSLog(@"Failed to resolve the web interface service on port %ld, with error: %@", sender.port, errorDict);
|
2008-12-06 01:18:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|