Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coalesce operational instance adds in MTROperationalBrowser. #37906

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/darwin/Framework/CHIP/MTROperationalBrowser.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
#import <Matter/MTRDeviceControllerFactory.h>
#import <dns_sd.h>

class MTROperationalBrowser
{
class MTROperationalBrowser {
public:
// Should be created at a point when the dispatch queue is available.
MTROperationalBrowser(MTRDeviceControllerFactory * aFactory, dispatch_queue_t aQueue);
Expand All @@ -40,7 +39,7 @@ class MTROperationalBrowser

private:
static void OnBrowse(DNSServiceRef aServiceRef, DNSServiceFlags aFlags, uint32_t aInterfaceId, DNSServiceErrorType aError,
const char * aName, const char * aType, const char * aDomain, void * aContext);
const char * aName, const char * aType, const char * aDomain, void * aContext);

void EnsureBrowse();
void StopBrowse();
Expand All @@ -58,4 +57,7 @@ class MTROperationalBrowser
// Count of controllers that are currently active; we aim to have a browse
// going while this is nonzero;
size_t mActiveControllerCount = 0;

// Queued-up instance names to notify about.
NSMutableSet<NSString *> * mAddedInstanceNames = nil;
};
34 changes: 24 additions & 10 deletions src/darwin/Framework/CHIP/MTROperationalBrowser.mm
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@
return;
}

mAddedInstanceNames = [[NSMutableSet alloc] init];

mInitialized = true;

for (auto domain : kBrowseDomains) {
Expand All @@ -100,6 +102,7 @@
{
ChipLogProgress(Controller, "%p stopping persistent operational browse", this);
if (mInitialized) {
mAddedInstanceNames = nil;
DNSServiceRefDeallocate(mBrowseRef);
mInitialized = false;
}
Expand All @@ -126,22 +129,33 @@
return;
}

chip::PeerId peerId;
CHIP_ERROR err = chip::Dnssd::ExtractIdFromInstanceName(aName, &peerId);
if (err != CHIP_NO_ERROR) {
ChipLogError(Controller, "Invalid instance name: '%s'\n", aName);
auto * newInstanceName = [NSString stringWithUTF8String:aName];
if (!(aFlags & kDNSServiceFlagsAdd)) {
MTR_LOG("Matter operational instance advertisement removed: '%@'\n", newInstanceName);
// Make sure that we don't notify about things that appear and then disappear.
[self->mAddedInstanceNames removeObject:newInstanceName];
return;
}

if (!(aFlags & kDNSServiceFlagsAdd)) {
// We mostly only care about new things appearing, but log it when things
// disappear.
MTR_LOG("Matter operational instance advertisement removed: '%s'\n", aName);
[self->mAddedInstanceNames addObject:newInstanceName];

if (aFlags & kDNSServiceFlagsMoreComing) {
// Just wait for those other notifications, so we coalesce everything.
return;
}

ChipLogProgress(Controller, "Notifying controller factory about new operational instance: '%s'", aName);
[self->mDeviceControllerFactory operationalInstanceAdded:peerId];
for (NSString * instanceName in self->mAddedInstanceNames) {
chip::PeerId peerId;
CHIP_ERROR err = chip::Dnssd::ExtractIdFromInstanceName(instanceName.UTF8String, &peerId);
if (err != CHIP_NO_ERROR) {
ChipLogError(Controller, "Invalid instance name: '%@'\n", instanceName);
continue;
}

ChipLogProgress(Controller, "Notifying controller factory about new operational instance: '%@'", instanceName);
[self->mDeviceControllerFactory operationalInstanceAdded:peerId];
}
[self->mAddedInstanceNames removeAllObjects];
}

MTROperationalBrowser::~MTROperationalBrowser()
Expand Down
Loading