* The BLIPConnection receivedRequest: delegate method now returns BOOL. If the method returns NO (or if the method isn't implemented in the delegate), that means it didn't handle the message at all; an error will be returned to the sender.
* If the connection closes unexpectedly due to an error, then the auto-generated responses to pending requests will contain that error. This makes it easier to display a meaningful error message in the handler for the request.
5 // Created by Jens Alfke on 1/22/08.
6 // Copyright 2008 Jens Alfke. All rights reserved.
9 #import "MYBonjourService.h"
10 #import "MYBonjourQuery.h"
11 #import "MYAddressLookup.h"
13 #import "ConcurrentOperation.h"
16 #import "ExceptionUtils.h"
20 NSString* const kBonjourServiceResolvedAddressesNotification = @"BonjourServiceResolvedAddresses";
23 @interface MYBonjourService ()
24 @property (copy) NSString *hostname;
25 @property UInt16 port;
29 @implementation MYBonjourService
32 - (id) initWithBrowser: (MYBonjourBrowser*)browser
33 name: (NSString*)serviceName
35 domain: (NSString*)domain
36 interface: (UInt32)interfaceIndex
43 _bonjourBrowser = browser;
44 _name = [serviceName copy];
46 _domain = [domain copy];
47 _fullName = [[[self class] fullNameOfService: _name ofType: _type inDomain: _domain] retain];
48 _interfaceIndex = interfaceIndex;
56 [_addressLookup stop];
57 [_addressLookup release];
67 @synthesize bonjourBrowser=_bonjourBrowser, name=_name, type=_type, domain=_domain,
68 fullName=_fullName, hostname=_hostname, port=_port, interfaceIndex=_interfaceIndex;
71 - (NSString*) description {
72 return $sprintf(@"%@[%@]", self.class,self.fullName);
76 - (NSComparisonResult) compare: (id)obj {
77 return [_name caseInsensitiveCompare: [obj name]];
80 - (BOOL) isEqual: (id)obj {
81 if ([obj isKindOfClass: [MYBonjourService class]]) {
82 MYBonjourService *service = obj;
83 return [_name caseInsensitiveCompare: [service name]] == 0
84 && $equal(_type, service->_type)
85 && $equal(_domain, service->_domain)
86 && _interfaceIndex == service->_interfaceIndex;
93 return _name.hash ^ _type.hash ^ _domain.hash;
98 LogTo(Bonjour,@"Added %@",self);
102 LogTo(Bonjour,@"Removed %@",self);
109 [_addressLookup stop];
113 - (NSString*) hostname {
114 if (!_startedResolve )
120 if (!_startedResolve )
127 #pragma mark TXT RECORD:
130 - (NSDictionary*) txtRecord {
132 _txtQuery = [[MYBonjourQuery alloc] initWithBonjourService: self
133 recordType: kDNSServiceType_TXT];
134 _txtQuery.continuous = YES;
140 - (void) txtRecordChanged {
141 // no-op (this is here for subclassers to override)
144 - (NSString*) txtStringForKey: (NSString*)key {
145 NSData *value = [self.txtRecord objectForKey: key];
148 if( ! [value isKindOfClass: [NSData class]] ) {
149 Warn(@"TXT dictionary has unexpected value type: %@",value.class);
152 NSString *str = [[NSString alloc] initWithData: value encoding: NSUTF8StringEncoding];
154 str = [[NSString alloc] initWithData: value encoding: NSWindowsCP1252StringEncoding];
155 return [str autorelease];
158 - (void) setTxtData: (NSData*)txtData {
159 NSDictionary *txtRecord = txtData ?[NSNetService dictionaryFromTXTRecordData: txtData] :nil;
160 if (!$equal(txtRecord,_txtRecord)) {
161 LogTo(Bonjour,@"%@ TXT = %@", self,txtRecord);
162 [self willChangeValueForKey: @"txtRecord"];
163 setObj(&_txtRecord, txtRecord);
164 [self didChangeValueForKey: @"txtRecord"];
165 [self txtRecordChanged];
170 - (void) queryDidUpdate: (MYBonjourQuery*)query {
171 if (query==_txtQuery)
172 [self setTxtData: query.recordData];
177 #pragma mark HOSTNAME/PORT RESOLUTION:
180 - (void) priv_resolvedHostname: (NSString*)hostname
182 txtRecord: (NSData*)txtData
184 LogTo(Bonjour, @"%@: hostname=%@, port=%u, txt=%u bytes",
185 self, hostname, port, txtData.length);
187 if (port!=_port || !$equal(hostname,_hostname)) {
188 self.hostname = hostname;
192 [self setTxtData: txtData];
195 - (void) gotResponse: (DNSServiceErrorType)errorCode {
196 [super gotResponse: errorCode];
197 [_addressLookup _serviceGotResponse];
201 static void resolveCallback(DNSServiceRef sdRef,
202 DNSServiceFlags flags,
203 uint32_t interfaceIndex,
204 DNSServiceErrorType errorCode,
205 const char *fullname,
206 const char *hosttarget,
209 const unsigned char *txtRecord,
212 MYBonjourService *service = context;
214 //LogTo(Bonjour, @"resolveCallback for %@ (err=%i)", service,errorCode);
216 NSData *txtData = nil;
218 txtData = [NSData dataWithBytes: txtRecord length: txtLen];
219 [service priv_resolvedHostname: [NSString stringWithUTF8String: hosttarget]
223 }catchAndReport(@"MYBonjourResolver query callback");
224 [service gotResponse: errorCode];
228 - (DNSServiceErrorType) createServiceRef: (DNSServiceRef*)sdRefPtr {
229 _startedResolve = YES;
230 return DNSServiceResolve(sdRefPtr,
231 kDNSServiceFlagsShareConnection,
233 _name.UTF8String, _type.UTF8String, _domain.UTF8String,
234 &resolveCallback, self);
238 - (MYAddressLookup*) addressLookup {
239 if (!_addressLookup) {
240 // Create the lookup the first time this is called:
241 _addressLookup = [[MYAddressLookup alloc] _initWithBonjourService: self];
242 _addressLookup.interfaceIndex = _interfaceIndex;
244 // (Re)start the lookup if it's expired:
245 if (_addressLookup && _addressLookup.timeToLive <= 0.0)
246 [_addressLookup start];
247 return _addressLookup;
251 - (MYBonjourQuery*) queryForRecord: (UInt16)recordType {
252 MYBonjourQuery *query = [[[MYBonjourQuery alloc] initWithBonjourService: self recordType: recordType]
254 return [query start] ?query :nil;
263 Copyright (c) 2008-2009, Jens Alfke <jens@mooseyard.com>. All rights reserved.
265 Redistribution and use in source and binary forms, with or without modification, are permitted
266 provided that the following conditions are met:
268 * Redistributions of source code must retain the above copyright notice, this list of conditions
269 and the following disclaimer.
270 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
271 and the following disclaimer in the documentation and/or other materials provided with the
274 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
275 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
276 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
277 BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
278 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
279 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
280 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
281 THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.