Bonjour/MYBonjourBrowser.m
changeset 48 2b4ad2067074
parent 31 1d6924779df7
child 50 63baa74c903f
     1.1 --- a/Bonjour/MYBonjourBrowser.m	Wed Apr 29 13:29:31 2009 -0700
     1.2 +++ b/Bonjour/MYBonjourBrowser.m	Sat May 16 14:24:06 2009 -0700
     1.3 @@ -200,6 +200,7 @@
     1.4  
     1.5  
     1.6  
     1.7 +
     1.8  #pragma mark -
     1.9  #pragma mark TESTING:
    1.10  
    1.11 @@ -221,8 +222,12 @@
    1.12      self = [super init];
    1.13      if (self != nil) {
    1.14          _browser = [[MYBonjourBrowser alloc] initWithServiceType: @"_presence._tcp"];
    1.15 -        [_browser addObserver: self forKeyPath: @"services" options: NSKeyValueObservingOptionNew context: NULL];
    1.16 -        [_browser addObserver: self forKeyPath: @"browsing" options: NSKeyValueObservingOptionNew context: NULL];
    1.17 +        [_browser addObserver: self forKeyPath: @"services" 
    1.18 +                      options: NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew 
    1.19 +                      context: NULL];
    1.20 +        [_browser addObserver: self forKeyPath: @"browsing" 
    1.21 +                      options: NSKeyValueObservingOptionNew
    1.22 +                      context: NULL];
    1.23          [_browser start];
    1.24          
    1.25          MYBonjourRegistration *myReg = _browser.myRegistration;
    1.26 @@ -243,7 +248,7 @@
    1.27  
    1.28  - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
    1.29  {
    1.30 -    LogTo(Bonjour,@"Observed change in %@: %@",keyPath,change);
    1.31 +    Log(@"Observed change in %@: %@",keyPath,change);
    1.32      if( $equal(keyPath,@"services") ) {
    1.33          if( [[change objectForKey: NSKeyValueChangeKindKey] intValue]==NSKeyValueChangeInsertion ) {
    1.34              NSSet *newServices = [change objectForKey: NSKeyValueChangeNewKey];
    1.35 @@ -252,8 +257,18 @@
    1.36                  Log(@"##### %@ : at %@:%hu, TXT=%@", 
    1.37                        service, hostname, service.port, service.txtRecord);
    1.38                  service.addressLookup.continuous = YES;
    1.39 +                [service.addressLookup addObserver: self
    1.40 +                                        forKeyPath: @"addresses"
    1.41 +                                           options: NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew
    1.42 +                                           context: NULL];
    1.43                  [service queryForRecord: kDNSServiceType_NULL];
    1.44              }
    1.45 +        } else if( [[change objectForKey: NSKeyValueChangeKindKey] intValue]==NSKeyValueChangeRemoval ) {
    1.46 +            NSSet *oldServices = [change objectForKey: NSKeyValueChangeOldKey];
    1.47 +            for( MYBonjourService *service in oldServices ) {
    1.48 +                Log(@"##### REMOVED: %@", service);
    1.49 +                [service.addressLookup removeObserver: self forKeyPath: @"addresses"];
    1.50 +            }
    1.51          }
    1.52      }
    1.53  }