# HG changeset patch # User Jens Alfke # Date 1242007250 25200 # Node ID 8efb48eabd087f4a57b5c04e4b33e692892f035a # Parent c1cf9df64c70bf3fbfe39653ad4b0a98b875b2ae Fixed MYAddressLookup to allocate an NSSet, and to send correct KV notifications. (Based on Jim Roepke's patch, but outsourcing the KV grunge to CollectionUtils.) diff -r c1cf9df64c70 -r 8efb48eabd08 Bonjour/MYAddressLookup.m --- a/Bonjour/MYAddressLookup.m Tue May 05 22:19:33 2009 -0700 +++ b/Bonjour/MYAddressLookup.m Sun May 10 19:00:50 2009 -0700 @@ -25,7 +25,7 @@ return nil; } _hostname = [hostname copy]; - _addresses = [[NSMutableArray alloc] init]; + _addresses = [[NSMutableSet alloc] init]; } return self; } @@ -62,10 +62,10 @@ if (address) { if (flags & kDNSServiceFlagsAdd) { LogTo(DNS,@"%@ got %@ [TTL = %u]", self, address, ttl); - [_addresses addObject: address]; + kvAddToSet(self, @"addresses", _addresses, address); } else { LogTo(DNS,@"%@ lost %@ [TTL = %u]", self, address, ttl); - [_addresses removeObject: address]; + kvRemoveFromSet(self, @"addresses", _addresses, address); } [address release]; } @@ -96,7 +96,7 @@ - (DNSServiceErrorType) createServiceRef: (DNSServiceRef*)sdRefPtr { - [_addresses removeAllObjects]; + kvSetSet(self, @"addresses", _addresses, nil); return DNSServiceGetAddrInfo(sdRefPtr, kDNSServiceFlagsShareConnection, _interfaceIndex, 0, diff -r c1cf9df64c70 -r 8efb48eabd08 Bonjour/MYBonjourBrowser.m --- a/Bonjour/MYBonjourBrowser.m Tue May 05 22:19:33 2009 -0700 +++ b/Bonjour/MYBonjourBrowser.m Sun May 10 19:00:50 2009 -0700 @@ -200,6 +200,7 @@ + #pragma mark - #pragma mark TESTING: @@ -221,8 +222,12 @@ self = [super init]; if (self != nil) { _browser = [[MYBonjourBrowser alloc] initWithServiceType: @"_presence._tcp"]; - [_browser addObserver: self forKeyPath: @"services" options: NSKeyValueObservingOptionNew context: NULL]; - [_browser addObserver: self forKeyPath: @"browsing" options: NSKeyValueObservingOptionNew context: NULL]; + [_browser addObserver: self forKeyPath: @"services" + options: NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew + context: NULL]; + [_browser addObserver: self forKeyPath: @"browsing" + options: NSKeyValueObservingOptionNew + context: NULL]; [_browser start]; MYBonjourRegistration *myReg = _browser.myRegistration; @@ -243,7 +248,7 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { - LogTo(Bonjour,@"Observed change in %@: %@",keyPath,change); + Log(@"Observed change in %@: %@",keyPath,change); if( $equal(keyPath,@"services") ) { if( [[change objectForKey: NSKeyValueChangeKindKey] intValue]==NSKeyValueChangeInsertion ) { NSSet *newServices = [change objectForKey: NSKeyValueChangeNewKey]; @@ -252,8 +257,18 @@ Log(@"##### %@ : at %@:%hu, TXT=%@", service, hostname, service.port, service.txtRecord); service.addressLookup.continuous = YES; + [service.addressLookup addObserver: self + forKeyPath: @"addresses" + options: NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew + context: NULL]; [service queryForRecord: kDNSServiceType_NULL]; } + } else if( [[change objectForKey: NSKeyValueChangeKindKey] intValue]==NSKeyValueChangeRemoval ) { + NSSet *oldServices = [change objectForKey: NSKeyValueChangeOldKey]; + for( MYBonjourService *service in oldServices ) { + Log(@"##### REMOVED: %@", service); + [service.addressLookup removeObserver: self forKeyPath: @"addresses"]; + } } } } diff -r c1cf9df64c70 -r 8efb48eabd08 MYNetwork.xcodeproj/project.pbxproj --- a/MYNetwork.xcodeproj/project.pbxproj Tue May 05 22:19:33 2009 -0700 +++ b/MYNetwork.xcodeproj/project.pbxproj Sun May 10 19:00:50 2009 -0700 @@ -351,23 +351,23 @@ 270461220DE49055003D9D3F /* MYUtilities */ = { isa = PBXGroup; children = ( + 270461880DE49634003D9D3F /* CollectionUtils.h */, + 270461870DE49634003D9D3F /* CollectionUtils.m */, 278C1BB50F9F975700954AE1 /* ConcurrentOperation.h */, 278C1BB60F9F975700954AE1 /* ConcurrentOperation.m */, - 270462C10DE4A64B003D9D3F /* MYUtilitiesTest_main.m */, - 270462C00DE4A639003D9D3F /* MYUtilities_Prefix.pch */, - 270461880DE49634003D9D3F /* CollectionUtils.h */, - 270461870DE49634003D9D3F /* CollectionUtils.m */, 270461360DE4918D003D9D3F /* ExceptionUtils.h */, 270461350DE4918D003D9D3F /* ExceptionUtils.m */, + 27E0DBEC0DF3450F00E7F648 /* GoogleToolboxSubset */, 2704612B0DE49088003D9D3F /* Logging.h */, 2704612A0DE49088003D9D3F /* Logging.m */, + 274122DD0F9CDD1600F21842 /* MYUtilities_Debug.xcconfig */, + 270462C00DE4A639003D9D3F /* MYUtilities_Prefix.pch */, + 274122DE0F9CDD1600F21842 /* MYUtilities_Release.xcconfig */, + 270462C10DE4A64B003D9D3F /* MYUtilitiesTest_main.m */, 270461450DE491A6003D9D3F /* Target.h */, 270461460DE491A6003D9D3F /* Target.m */, 270461290DE49088003D9D3F /* Test.h */, 270461280DE49088003D9D3F /* Test.m */, - 274122DD0F9CDD1600F21842 /* MYUtilities_Debug.xcconfig */, - 274122DE0F9CDD1600F21842 /* MYUtilities_Release.xcconfig */, - 27E0DBEC0DF3450F00E7F648 /* GoogleToolboxSubset */, ); name = MYUtilities; sourceTree = MYUtilities;