-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSSYNetServicesSearcher.m
161 lines (135 loc) · 5.3 KB
/
SSYNetServicesSearcher.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
#import "SSYNetServicesSearcher.h"
#import "NSArray+SSYMutations.h"
#import "NSDictionary+SimpleMutations.h"
NSString* const SSYNetServicesSearcherDidFindDomainNotification = @"SSYNetServicesSearcherDFD" ;
NSString* const SSYNetServicesSearcherDidFindServiceNotification = @"SSYNetServicesSearcherDFS" ;
NSString* const SSYNetServicesSearcherDidFinishNotification = @"SSYNetServicesSearcherDFi" ;
NSString* const SSYNetServicesSearcherDidFailNotification = @"SSYNetServicesSearcherDFa" ;
@interface SSYNetServicesSearcher ()
@property (retain) NSDictionary* targets ;
@property SSYNetServicesSearcherState state ;
@property (retain) NSMutableArray* domains ;
@property (retain) NSMutableArray* services ;
@property (retain) NSMutableArray* targetTypesForCurrentDomain ;
@property (retain) NSNetServiceBrowser* browser ;
@property (retain) NSError* error ;
@end
@implementation SSYNetServicesSearcher
@synthesize targets ;
@synthesize state ;
@synthesize domains ;
@synthesize services ;
@synthesize targetTypesForCurrentDomain ;
@synthesize browser ;
@synthesize error ;
- (void) dealloc {
[targets release] ;
[browser release] ;
[domains release] ;
[services release] ;
[targetTypesForCurrentDomain release] ;
[error release] ;
[super dealloc];
}
- (id) initWithTargets:(NSDictionary*)targets_ {
self = [super init] ;
if (self != nil) {
[self setTargets:targets_] ;
domains = [[NSMutableArray alloc] init] ;
services =[[NSMutableArray alloc] init] ;
// Create browser
browser = [[NSNetServiceBrowser alloc] init] ;
[browser setDelegate:self] ;
// Begin search for domains
[browser searchForRegistrationDomains] ;
[self setState:SSYNetServicesSearcherStateSearchingForDomains] ;
}
return self;
}
/*
- (void)netServiceBrowserWillSearch:(NSNetServiceBrowser *)netServiceBrowser {
// New search has begun.
// I don't seem to have any use for this.
}
*/
- (void)searchForServicesInNextDomain {
if ([[self domains] count] < 1) {
[self setState:SSYNetServicesSearcherStateDone] ;
[[NSNotificationCenter defaultCenter] postNotificationName:SSYNetServicesSearcherDidFinishNotification
object:self] ;
}
else {
NSString* domain = [[self domains] objectAtIndex:0] ;
[[self domains] removeObjectAtIndex:0] ;
if ([[[self targets] allKeys] indexOfObject:domain] != NSNotFound) {
NSArray* types = [[self targets] objectForKey:domain] ;
if ([types count] < 1) {
[[self domains] removeObjectAtIndex:0] ;
[self searchForServicesInNextDomain] ;
}
else {
// This domain is one of our targets, and it has at least
// one more target type which we've not searched for yet.
// Get the next type and remove it from our targets
NSString* type = [types objectAtIndex:0] ;
types = [types arrayByRemovingObjectAtIndex:0] ;
[self setTargets:[[self targets] dictionaryBySettingValue:types
forKey:domain]] ;
// Search this domain for this type.
// One little thing they don't tell you in the NSNetServiceBrowser
// documentation is this little fact I found in a listserv archive:
// "NSNetServiceBrowsers can only do one thing at a time, really -
// if you want to search for services of a given type within the
// domain you just found, you should create a new NSNetServiceBrowser
// for that domain, and start -that- searching on the domain."
[browser stop] ;
[browser release] ;
browser = [[NSNetServiceBrowser alloc] init] ;
[browser setDelegate:self] ;
// Otherwise, it quits immediately with error NSNetServicesActivityInProgress
// Ok, we're ready to do the next search.
[browser searchForServicesOfType:type
inDomain:domain] ;
}
}
else {
[self searchForServicesInNextDomain] ;
}
}
}
- (void)netServiceBrowser:(NSNetServiceBrowser *)netServiceBrowser
didFindDomain:(NSString *)domainName
moreComing:(BOOL)moreDomainsComing {
[[self domains] addObject:domainName] ;
[[NSNotificationCenter defaultCenter] postNotificationName:SSYNetServicesSearcherDidFindDomainNotification
object:self] ;
if (!moreDomainsComing) {
[self setState:SSYNetServicesSearcherStateSearchingForServices] ;
[self searchForServicesInNextDomain] ;
}
}
- (void)netServiceBrowser:(NSNetServiceBrowser*)netServiceBrowser
didFindService:(NSNetService*)netService
moreComing:(BOOL)moreServicesComing {
[[self services] addObject:netService] ;
[[NSNotificationCenter defaultCenter] postNotificationName:SSYNetServicesSearcherDidFindServiceNotification
object:self] ;
if (!moreServicesComing) {
[self searchForServicesInNextDomain] ;
}
}
- (void)netServiceBrowser:(NSNetServiceBrowser*)netServiceBrowser
didNotSearch:(NSDictionary*)errorInfo {
NSError* error_ = [NSError errorWithDomain:NSNetServicesErrorDomain
code:[[errorInfo objectForKey:NSNetServicesErrorCode] integerValue]
userInfo:[NSDictionary dictionaryWithObject:[self domains]
forKey:@"Domains found so far"]] ;
[self setError:error_] ;
[self setState:SSYNetServicesSearcherStateFailed] ;
[[NSNotificationCenter defaultCenter] postNotificationName:SSYNetServicesSearcherDidFailNotification
object:self] ;
}
- (void)netServiceBrowserDidStopSearch:(NSNetServiceBrowser*)netServiceBrowser {
[self setState:SSYNetServicesSearcherStateStopped] ;
}
@end