Skip to content

Commit 4706914

Browse files
Coalesce operational instance adds in MTROperationalBrowser. (#37906)
This way we will only notify once if we get Add notifications all together on multiple interfaces.
1 parent 7d7d638 commit 4706914

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

src/darwin/Framework/CHIP/MTROperationalBrowser.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020
#import <Matter/MTRDeviceControllerFactory.h>
2121
#import <dns_sd.h>
2222

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

4140
private:
4241
static void OnBrowse(DNSServiceRef aServiceRef, DNSServiceFlags aFlags, uint32_t aInterfaceId, DNSServiceErrorType aError,
43-
const char * aName, const char * aType, const char * aDomain, void * aContext);
42+
const char * aName, const char * aType, const char * aDomain, void * aContext);
4443

4544
void EnsureBrowse();
4645
void StopBrowse();
@@ -58,4 +57,7 @@ class MTROperationalBrowser
5857
// Count of controllers that are currently active; we aim to have a browse
5958
// going while this is nonzero;
6059
size_t mActiveControllerCount = 0;
60+
61+
// Queued-up instance names to notify about.
62+
NSMutableSet<NSString *> * mAddedInstanceNames = nil;
6163
};

src/darwin/Framework/CHIP/MTROperationalBrowser.mm

+24-10
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@
8585
return;
8686
}
8787

88+
mAddedInstanceNames = [[NSMutableSet alloc] init];
89+
8890
mInitialized = true;
8991

9092
for (auto domain : kBrowseDomains) {
@@ -100,6 +102,7 @@
100102
{
101103
ChipLogProgress(Controller, "%p stopping persistent operational browse", this);
102104
if (mInitialized) {
105+
mAddedInstanceNames = nil;
103106
DNSServiceRefDeallocate(mBrowseRef);
104107
mInitialized = false;
105108
}
@@ -126,22 +129,33 @@
126129
return;
127130
}
128131

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

136-
if (!(aFlags & kDNSServiceFlagsAdd)) {
137-
// We mostly only care about new things appearing, but log it when things
138-
// disappear.
139-
MTR_LOG("Matter operational instance advertisement removed: '%s'\n", aName);
140+
[self->mAddedInstanceNames addObject:newInstanceName];
141+
142+
if (aFlags & kDNSServiceFlagsMoreComing) {
143+
// Just wait for those other notifications, so we coalesce everything.
140144
return;
141145
}
142146

143-
ChipLogProgress(Controller, "Notifying controller factory about new operational instance: '%s'", aName);
144-
[self->mDeviceControllerFactory operationalInstanceAdded:peerId];
147+
for (NSString * instanceName in self->mAddedInstanceNames) {
148+
chip::PeerId peerId;
149+
CHIP_ERROR err = chip::Dnssd::ExtractIdFromInstanceName(instanceName.UTF8String, &peerId);
150+
if (err != CHIP_NO_ERROR) {
151+
ChipLogError(Controller, "Invalid instance name: '%@'\n", instanceName);
152+
continue;
153+
}
154+
155+
ChipLogProgress(Controller, "Notifying controller factory about new operational instance: '%@'", instanceName);
156+
[self->mDeviceControllerFactory operationalInstanceAdded:peerId];
157+
}
158+
[self->mAddedInstanceNames removeAllObjects];
145159
}
146160

147161
MTROperationalBrowser::~MTROperationalBrowser()

0 commit comments

Comments
 (0)