Skip to content
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
55 changes: 55 additions & 0 deletions include/thingset++/ThingSetServer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "thingset++/Eui.hpp"
#include "thingset++/StringLiteral.hpp"
#include "thingset++/ThingSetProperty.hpp"
#include "thingset++/ThingSetRegistry.hpp"
#include "thingset++/ThingSetRequestContext.hpp"
#include "thingset++/ThingSetServerTransport.hpp"
#include "thingset++/ThingSetStatus.hpp"
Expand Down Expand Up @@ -130,6 +131,60 @@ class ThingSetServer : public _ThingSetServer
return encoder.flush();
}

/// @brief Broadcasts every property tagged with the given subset as a single report.
/// @tparam SubsetType Type of the subset enum.
/// @param subset The subset to publish. All registered nodes whose subset mask
/// contains every bit of subset are included.
/// @return True if publishing succeeded.
template <typename SubsetType>
requires std::is_enum_v<SubsetType>
bool publish(SubsetType subset)
{
#ifdef ENABLE_ENHANCED_REPORTING
bool enhanced = true;
#else
bool enhanced = false;
#endif
Encoder encoder = _transport.getPublishingEncoder(enhanced);

if (enhanced) {
if (!encoder.encode(ThingSet::Eui::getValue())) {
return false;
}
}

if (!encoder.encode(0)) {
return false;
}

size_t count = 0;
for (auto *n : ThingSetRegistry::nodesInSubset(subset)) {
(void)n;
++count;
}

if (!encoder.encodeMapStart(count)) {
return false;
}

for (ThingSetNode *node : ThingSetRegistry::nodesInSubset(subset)) {
void *target = nullptr;
if (!node->tryCastTo(ThingSetNodeType::encodable, &target)) {
return false;
}
auto *encodable = reinterpret_cast<ThingSetEncodable *>(target);
if (!encoder.encode(node->getId()) || !encodable->encode(encoder)) {
return false;
}
}

if (!encoder.encodeMapEnd()) {
return false;
}

return encoder.flush();
}

private:
int requestCallback(Identifier &, uint8_t *request, size_t requestLen, uint8_t *response, size_t responseLen)
{
Expand Down
61 changes: 61 additions & 0 deletions tests/TestAsioIpPublishSubscribe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <thingset++/ThingSetListener.hpp>
#include <gtest/gtest.h>
#include <asio.hpp>
#include <map>
#include <thread>

using namespace ThingSet;
Expand Down Expand Up @@ -64,3 +65,63 @@ TEST(AsioIpPublishSubscribe, Name)

ASSERT_EQ(5, receiveCount);
}

TEST(AsioIpPublishSubscribe, Subset)
{
// Four flavours of property: tagged live, tagged live|persisted (union),
// tagged persisted-only, and untagged. publish(Subset::live) must include
// the first two and exclude the rest.
ThingSetReadWriteProperty<float, Subset::live | Subset::persisted> bothSubsets { 0x900, 0, "bothSubsets", 1.5f };
ThingSetReadWriteProperty<uint32_t, Subset::live> liveOnly { 0x901, 0, "liveOnly", 7u };
ThingSetReadWriteProperty<uint32_t, Subset::persisted> persistedOnly { 0x902, 0, "persistedOnly", 99u };
ThingSetReadWriteProperty<uint32_t> untagged { 0x903, 0, "untagged", 42u };

io_context serverContext(1);
ThingSetAsyncSocketServerTransport serverTransport(serverContext);
auto server = ThingSetServerBuilder::build(serverTransport);
server.listen();

io_context clientContext(1);
ThingSetAsyncSocketSubscriptionTransport subscriptionTransport(clientContext);
auto listener = ThingSetListenerBuilder::build(subscriptionTransport);

std::map<uint16_t, size_t> hits;
listener.subscribe([&](auto, auto id) {
hits[id]++;
// Stop once the last id from the third publish has arrived
if (hits[0x901] == 3) {
clientContext.stop();
}
});

std::thread serverThread([&]() {
for (int i = 0; i < 3; i++) {
server.publish(Subset::live);
std::this_thread::sleep_for(std::chrono::milliseconds(25));
}
});
std::thread clientThread([&]() {
std::this_thread::sleep_for(std::chrono::milliseconds(125));
clientContext.run_for(std::chrono::seconds(5));
serverContext.stop();
});
clientThread.join();
serverThread.join();

EXPECT_EQ(3u, hits[0x900]); // live | persisted matches live filter (union)
EXPECT_EQ(3u, hits[0x901]); // live matches
EXPECT_EQ(0u, hits[0x902]); // persisted-only excluded
EXPECT_EQ(0u, hits[0x903]); // untagged excluded
}

TEST(AsioIpPublishSubscribe, SubsetEmpty)
{
// No properties tagged Subset::live in this scope, so publish must still
// succeed and produce an empty map.
io_context serverContext(1);
ThingSetAsyncSocketServerTransport serverTransport(serverContext);
auto server = ThingSetServerBuilder::build(serverTransport);
server.listen();

EXPECT_TRUE(server.publish(Subset::live));
}
Loading