2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2008-2022 Transmission authors and contributors.
|
|
|
|
// 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"
|
|
|
|
|
2012-09-09 05:19:10 +00:00
|
|
|
#define BONJOUR_SERVICE_NAME_MAX_LENGTH 63
|
|
|
|
|
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];
|
2021-08-07 07:27:56 +00:00
|
|
|
if (serviceName.length > BONJOUR_SERVICE_NAME_MAX_LENGTH)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
|
|
|
[serviceName deleteCharactersInRange:NSMakeRange(BONJOUR_SERVICE_NAME_MAX_LENGTH, serviceName.length - BONJOUR_SERVICE_NAME_MAX_LENGTH)];
|
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
fService = [[NSNetService alloc] initWithDomain:@"" type:@"_http._tcp." name:serviceName port:port];
|
2021-08-07 07:27:56 +00:00
|
|
|
fService.delegate = self;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2008-12-06 01:18:25 +00:00
|
|
|
[fService publish];
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)stop
|
2008-12-06 01:18:25 +00:00
|
|
|
{
|
|
|
|
[fService stop];
|
|
|
|
fService = nil;
|
|
|
|
}
|
|
|
|
|
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
|