-
Notifications
You must be signed in to change notification settings - Fork 1
/
HHServiceBrowser.m
executable file
·180 lines (124 loc) · 5.4 KB
/
HHServiceBrowser.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//
// HHServiceBrowser.m
// Part of Hejsan-Hoppsan-Services : http://www.github.com/tolo/HHServices
//
// Created by Tobias on 2011-11-02.
// Copyright (c) 2011 Leafnode AB. All rights reserved.
//
#import "HHServiceBrowser.h"
#import "HHServiceSupport+Private.h"
@interface HHServiceBrowser ()
// Redefinition of read only properties to readwrite
@property (nonatomic, retain, readwrite) NSString* type;
@property (nonatomic, retain, readwrite) NSString* domain;
@property (nonatomic, retain) HHService* resolver;
- (void) browserReceviedResult:(DNSServiceErrorType)error serviceName:(NSString*)serviceName serviceDomain:(NSString*)serviceDomain add:(BOOL)add moreComing:(BOOL)moreComing;
@end
#pragma mark -
#pragma mark Callbacks
static void browseCallBack(DNSServiceRef sdRef, DNSServiceFlags flags, uint32_t interfaceIndex, DNSServiceErrorType errorCode,
const char *serviceName, const char *regtype, const char *replyDomain, void *context) {
ContextWrapper* contextWrapper = (ContextWrapper*)context;
HHServiceBrowser* serviceBrowser = contextWrapper.contextRetained;
if( serviceBrowser ) {
if( errorCode == kDNSServiceErr_NoError ) {
BOOL add = flags & kDNSServiceFlagsAdd;
BOOL moreComing = flags & kDNSServiceFlagsMoreComing;
NSString* newName = serviceName ? [[NSString alloc] initWithCString:serviceName encoding:NSUTF8StringEncoding] : nil;
NSString* newDomain = replyDomain ? [[NSString alloc] initWithCString:replyDomain encoding:NSUTF8StringEncoding] : nil;
dispatch_async(serviceBrowser.mainDispatchQueue, ^{
[serviceBrowser browserReceviedResult:errorCode serviceName:newName serviceDomain:newDomain add:add moreComing:moreComing];
[newName release];
[newDomain release];
});
} else {
[serviceBrowser dnsServiceError:errorCode];
}
}
[contextWrapper releaseContext];
}
#pragma mark -
#pragma mark ServiceBrowser
@implementation HHServiceBrowser
@synthesize type, domain, includeP2P;
@synthesize delegate, resolver;
#pragma mark -
#pragma mark Internal methods
- (void) browserReceviedResult:(DNSServiceErrorType)error serviceName:(NSString*)serviceName serviceDomain:(NSString*)serviceDomain add:(BOOL)add moreComing:(BOOL)moreComing {
self.lastError = error;
if (error == kDNSServiceErr_NoError) {
HHService* service = [[[HHService alloc] initWithName:serviceName type:self.type domain:serviceDomain includeP2P:self.includeP2P] autorelease];
if( add ) [self.delegate serviceBrowser:self didFindService:service moreComing:moreComing];
else [self.delegate serviceBrowser:self didRemoveService:service moreComing:moreComing];
}
}
#pragma mark -
#pragma mark Creation and destruction
- (id) initWithType:(NSString*)svcType domain:(NSString*)svcDomain {
return [self initWithType:svcType domain:svcDomain includeP2P:YES];
}
- (id) initWithType:(NSString*)svcType domain:(NSString*)svcDomain includeP2P:(BOOL)_includeP2P {
if( (self = [super init]) ) {
includeP2P = _includeP2P;
if( [svcType hasSuffix:@"."] ) svcType = [svcType substringToIndex:svcType.length-1];
self.type = svcType;
self.domain = svcDomain;
}
return self;
}
- (void) dealloc {
self.type = nil;
self.domain = nil;
self.resolver = nil;
[super dealloc];
}
#pragma mark -
#pragma mark ServicePublisher
- (BOOL) beginBrowse {
const char* _type = [self.type cStringUsingEncoding:NSUTF8StringEncoding];
const char* _domain = [self.domain cStringUsingEncoding:NSUTF8StringEncoding];
DNSServiceFlags flags = 0;
#if TARGET_OS_IPHONE == 1
flags = (uint32_t)(includeP2P ? kDNSServiceFlagsIncludeP2P : 0);
#endif
DNSServiceRef browseRef = NULL;
DNSServiceErrorType err = DNSServiceBrowse(&browseRef, flags, kDNSServiceInterfaceIndexAny, _type, _domain,
browseCallBack, [self setCurrentCallbackContextWithSelf]);
if( err == kDNSServiceErr_NoError ) {
[self HHLogDebug:@"Beginning browse"];
return [super setServiceRef:browseRef];
} else {
[self HHLogDebug:@"Error starting browse"];
[self dnsServiceError:err];
return NO;
}
}
- (void) endBrowse {
[self.resolver endResolve];
self.resolver = nil;
[super resetServiceRef];
}
- (HHService*) resolverForService:(NSString*)name {
return [[[HHService alloc] initWithName:name type:self.type domain:self.domain includeP2P:self.includeP2P] autorelease];
}
- (BOOL) resolveService:(NSString*)name delegate:(id<HHServiceDelegate>)resolveDelegate {
if( self.resolver ) [self.resolver endResolve];
self.resolver = [self resolverForService:name];
self.resolver.delegate = resolveDelegate;
return [self.resolver beginResolve];
}
#pragma mark -
#pragma mark Equals & hashcode etc
- (NSString*) identityString {
return [NSString stringWithFormat:@"%@|%@", self.type, self.domain];
}
- (NSUInteger)hash {
return [self.identityString hash];
}
- (BOOL)isEqual:(id)anObject {
return [anObject isKindOfClass:[self class]] && [self.identityString isEqual:((HHServiceBrowser *)anObject).identityString];
}
- (NSString*) description {
return [NSString stringWithFormat:@"HHServiceBrowser[%@, %@]", self.type, self.domain];
}
@end