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

Mdns: Allow multiple operational advertisements #8610

Merged
merged 18 commits into from
Aug 4, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions src/lib/mdns/Advertiser_ImplMinimalMdns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ CHIP_ERROR AdvertiserMinMdns::Start(chip::Inet::InetLayer * inetLayer, uint16_t

mCommissionInstanceName1 = GetRandU32();
mCommissionInstanceName2 = GetRandU32();
// Re-set the server in the response sender in case this has been swapped in the
// GlobalMinimalMdnsServer (used for testing).
mResponseSender.SetServer(&GlobalMinimalMdnsServer::Server());
andy31415 marked this conversation as resolved.
Show resolved Hide resolved

ReturnErrorOnFailure(GlobalMinimalMdnsServer::Instance().StartServer(inetLayer, port));

Expand Down
23 changes: 16 additions & 7 deletions src/lib/mdns/MinimalMdnsServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,22 @@ class GlobalMinimalMdnsServer : public mdns::Minimal::ServerDelegate
public:
static constexpr size_t kMaxEndPoints = 30;

#ifdef MDNS_MINIMAL_TEST_SERVER
using ServerType = mdns::Minimal::test::CheckOnlyServer;
#else
using ServerType = mdns::Minimal::Server<kMaxEndPoints>;
#endif

GlobalMinimalMdnsServer() { mServer.SetDelegate(this); }

static GlobalMinimalMdnsServer & Instance();
static ServerType & Server() { return Instance().mServer; }
static mdns::Minimal::ServerBase & Server()
{
if (Instance().mReplacementServer != nullptr)
{
return *Instance().mReplacementServer;
}
else
{
return Instance().mServer;
}
}

/// Calls Server().Listen() on all available interfaces
CHIP_ERROR StartServer(chip::Inet::InetLayer * inetLayer, uint16_t port);
Expand All @@ -108,10 +114,13 @@ class GlobalMinimalMdnsServer : public mdns::Minimal::ServerDelegate
}
}

void SetReplacementServer(mdns::Minimal::ServerBase * server) { mReplacementServer = server; }

private:
ServerType mServer;
MdnsPacketDelegate * mQueryDelegate = nullptr;
MdnsPacketDelegate * mResponseDelegate = nullptr;
mdns::Minimal::ServerBase * mReplacementServer = nullptr;
MdnsPacketDelegate * mQueryDelegate = nullptr;
MdnsPacketDelegate * mResponseDelegate = nullptr;
};

} // namespace Mdns
Expand Down
2 changes: 2 additions & 0 deletions src/lib/mdns/minimal/ResponseSender.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ class ResponseSender : public ResponderDelegate
// Implementation of ResponderDelegate
void AddResponse(const ResourceRecord & record) override;

void SetServer(ServerBase * server) { mServer = server; }

private:
CHIP_ERROR FlushReply();
CHIP_ERROR PrepareNewReplyPacket();
Expand Down
13 changes: 5 additions & 8 deletions src/lib/mdns/minimal/tests/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,16 @@ chip_test_suite("tests") {
"TestRecordData.cpp",
"TestResponseSender.cpp",
]
if (chip_mdns == "minimal") {
test_sources += [ "TestAdvertiser.cpp" ]
}

cflags = [ "-Wconversion" ]

public_deps = [
"${chip_root}/src/lib/core",
"${nlunit_test_root}:nlunit-test",
"${chip_root}/src/lib/mdns",
"${chip_root}/src/lib/mdns/minimal",
]

if (current_os != "zephyr") {
defines = [ "MDNS_MINIMAL_TEST_SERVER=true" ]
public_deps += [ "${chip_root}/src/lib/mdns:minimal_mdns_tests" ]
test_sources += [ "TestAdvertiser.cpp" ]
} else {
public_deps += [ "${chip_root}/src/lib/mdns/minimal" ]
}
}
10 changes: 6 additions & 4 deletions src/lib/mdns/minimal/tests/TestAdvertiser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ using namespace chip::Mdns;
using namespace mdns::Minimal;
using namespace mdns::Minimal::test;

namespace {
CheckOnlyServer server;

const QNamePart kDnsSdQueryParts[] = { "_services", "_dns-sd", "_udp", "local" };
const FullQName kDnsSdQueryName = FullQName(kDnsSdQueryParts);
const QNamePart kMatterOperationalQueryParts[3] = { "_matter", "_tcp", "local" };
Expand All @@ -66,7 +67,6 @@ const QNamePart kTxtRecordEmptyParts[] = { "=" };
const FullQName kTxtRecordEmptyName = FullQName(kTxtRecordEmptyParts);

Inet::IPPacketInfo packetInfo;
} // namespace

CHIP_ERROR SendQuery(FullQName qname)
{
Expand All @@ -89,8 +89,7 @@ CHIP_ERROR SendQuery(FullQName qname)

void OperationalAdverts(nlTestSuite * inSuite, void * inContext)
{
auto & mdnsAdvertiser = chip::Mdns::ServiceAdvertiser::Instance();
CheckOnlyServer & server = GlobalMinimalMdnsServer::Server();
auto & mdnsAdvertiser = chip::Mdns::ServiceAdvertiser::Instance();
server.SetTestSuite(inSuite);

// Start a single operational advertiser
Expand Down Expand Up @@ -234,6 +233,9 @@ const nlTest sTests[] = {
int TestAdvertiser(void)
{
chip::Platform::MemoryInit();
GlobalMinimalMdnsServer::Instance().SetReplacementServer(&server);
auto & mdnsAdvertiser = chip::Mdns::ServiceAdvertiser::Instance();
mdnsAdvertiser.Start(nullptr, CHIP_PORT);
nlTestSuite theSuite = { "AdvertiserImplMinimal", sTests, nullptr, nullptr };
nlTestRunner(&theSuite, nullptr);
return nlTestRunnerStats(&theSuite);
Expand Down