diff -r 50dc5502ef46 -r 5e4855a592ee Bonjour/MYAddressLookup.m --- a/Bonjour/MYAddressLookup.m Sun May 10 19:05:52 2009 -0700 +++ b/Bonjour/MYAddressLookup.m Fri Jul 24 14:06:28 2009 -0700 @@ -7,6 +7,7 @@ // #import "MYAddressLookup.h" +#import "MYBonjourService.h" #import "IPAddress.h" #import "ExceptionUtils.h" #import "Test.h" @@ -14,6 +15,11 @@ #import +@interface MYAddressLookup () +@property (copy) NSString *hostname; +@end + + @implementation MYAddressLookup - (id) initWithHostname: (NSString*)hostname @@ -30,10 +36,21 @@ return self; } + +- (id) _initWithBonjourService: (MYBonjourService*)service { + self = [super init]; + if (self) { + _service = [service retain]; + _addresses = [[NSMutableSet alloc] init]; + } + return self; +} + - (void) dealloc { [_hostname release]; [_addresses release]; + [_service release]; [super dealloc]; } @@ -44,7 +61,7 @@ } -@synthesize port=_port, interfaceIndex=_interfaceIndex, addresses=_addresses; +@synthesize hostname=_hostname, port=_port, interfaceIndex=_interfaceIndex, addresses=_addresses; - (NSTimeInterval) timeToLive { @@ -52,6 +69,38 @@ } +- (BOOL) start { + if (_hostname) { + return [super start]; + } else { + // Service doesn't know its hostname yet; wait till it does: + LogTo(DNS,@"MYAddressLookup requesting hostname of %@ ...", _service); + Assert(_service); + return [_service start]; + } +} + + +// Called by my _service's gotResponse method: +- (void) _serviceGotResponse { + Assert(_service); + DNSServiceErrorType err = _service.error; + if (err) { + [self cancel]; + self.error = err; + } else { + NSString *hostname = _service.hostname; + UInt16 port = _service.port; + if (port!=_port || !$equal(hostname,_hostname)) { + self.hostname = hostname; + self.port = port; + if (_hostname) + [self start]; + } + } +} + + - (void) priv_resolvedAddress: (const struct sockaddr*)sockaddr ttl: (uint32_t)ttl flags: (DNSServiceFlags)flags @@ -85,7 +134,7 @@ { MYAddressLookup *lookup = context; @try{ - //LogTo(Bonjour, @"lookupCallback for %s (err=%i)", hostname,errorCode); + LogTo(DNS, @"lookupCallback for %s (err=%i)", hostname,errorCode); if (errorCode) [lookup setError: errorCode]; else @@ -96,10 +145,12 @@ - (DNSServiceErrorType) createServiceRef: (DNSServiceRef*)sdRefPtr { + Assert(_hostname); kvSetSet(self, @"addresses", _addresses, nil); return DNSServiceGetAddrInfo(sdRefPtr, kDNSServiceFlagsShareConnection, - _interfaceIndex, 0, + _interfaceIndex, + kDNSServiceProtocol_IPv4, _hostname.UTF8String, &lookupCallback, self); }