1/******************************************************************************
2 * $Id: BonjourController.m 13491 2012-09-10 02:01:07Z livings124 $
3 *
4 * Copyright (c) 2008-2012 Transmission authors and contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *****************************************************************************/
24
25#import "BonjourController.h"
26
27#define BONJOUR_SERVICE_NAME_MAX_LENGTH 63
28
29@implementation BonjourController
30
31BonjourController * fDefaultController = nil;
32+ (BonjourController *) defaultController
33{
34    static dispatch_once_t onceToken;
35    dispatch_once(&onceToken, ^{
36        fDefaultController = [[BonjourController alloc] init];
37    });
38    
39    return fDefaultController;
40}
41
42+ (BOOL) defaultControllerExists
43{
44    return fDefaultController != nil;
45}
46
47- (void) dealloc
48{
49    [fService release];
50    [super dealloc];
51}
52
53- (void) startWithPort: (int) port
54{
55    [self stop];
56    
57    NSMutableString * serviceName = [NSMutableString stringWithFormat: @"Transmission (%@ - %@)", NSUserName(), [[NSHost currentHost] localizedName]];
58    if ([serviceName length] > BONJOUR_SERVICE_NAME_MAX_LENGTH)
59        [serviceName deleteCharactersInRange: NSMakeRange(BONJOUR_SERVICE_NAME_MAX_LENGTH, [serviceName length] - BONJOUR_SERVICE_NAME_MAX_LENGTH)];
60    
61    fService = [[NSNetService alloc] initWithDomain: @"" type: @"_http._tcp." name: serviceName port: port];
62    [fService setDelegate: self];
63    
64    [fService publish];
65}
66
67- (void) stop
68{
69    [fService stop];
70    [fService release];
71    fService = nil;
72}
73
74- (void) netService: (NSNetService *) sender didNotPublish: (NSDictionary *) errorDict
75{
76    NSLog(@"Failed to publish the web interface service on port %ld, with error: %@", [sender port], errorDict);
77}
78
79- (void) netService: (NSNetService *) sender didNotResolve: (NSDictionary *) errorDict
80{
81    NSLog(@"Failed to resolve the web interface service on port %ld, with error: %@", [sender port], errorDict);
82}
83
84@end
85