Skip to content

Commit

Permalink
[ntcore] Local topic: A bit more properties refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterJohnson committed Oct 25, 2024
1 parent a775593 commit 60c855e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
15 changes: 1 addition & 14 deletions ntcore/src/main/native/cpp/local/LocalStorageImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,7 @@ void StorageImpl::NetworkAnnounce(LocalTopic* topic, std::string_view typeStr,

// may be properties update, but need to compare to see if it actually
// changed to determine whether to update string / send event
wpi::json update = wpi::json::object();
// added/changed
for (auto&& prop : properties.items()) {
auto it = topic->properties.find(prop.key());
if (it == topic->properties.end() || *it != prop.value()) {
update[prop.key()] = prop.value();
}
}
// removed
for (auto&& prop : topic->properties.items()) {
if (properties.find(prop.key()) == properties.end()) {
update[prop.key()] = wpi::json();
}
}
wpi::json update = topic->CompareProperties(properties);
if (!update.empty()) {
topic->properties = properties;
PropertiesUpdated(topic, update, event, false);
Expand Down
26 changes: 22 additions & 4 deletions ntcore/src/main/native/cpp/local/LocalTopic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void LocalTopic::StartStopDataLog(LocalDataLogger* logger, int64_t timestamp,
datalogs.emplace_back(
logger->log,
logger->Start(name, typeStr,
LocalDataLoggerEntry::MakeMetadata(propertiesStr),
LocalDataLoggerEntry::MakeMetadata(m_propertiesStr),
timestamp),
logger->handle);
datalogType = type;
Expand All @@ -33,7 +33,7 @@ void LocalTopic::StartStopDataLog(LocalDataLogger* logger, int64_t timestamp,
void LocalTopic::UpdateDataLogProperties() {
if (!datalogs.empty()) {
auto now = Now();
auto metadata = LocalDataLoggerEntry::MakeMetadata(propertiesStr);
auto metadata = LocalDataLoggerEntry::MakeMetadata(m_propertiesStr);
for (auto&& datalog : datalogs) {
datalog.SetMetadata(metadata, now);
}
Expand Down Expand Up @@ -151,7 +151,25 @@ void LocalTopic::RefreshProperties(bool updateFlags) {
if (updateFlags) {
RefreshFlags();
}
propertiesStr = properties.dump();
m_propertiesStr = properties.dump();
}

wpi::json LocalTopic::CompareProperties(const wpi::json props) {
wpi::json update = wpi::json::object();
// added/changed
for (auto&& prop : props.items()) {
auto it = properties.find(prop.key());
if (it == properties.end() || *it != prop.value()) {
update[prop.key()] = prop.value();
}
}
// removed
for (auto&& prop : properties.items()) {
if (props.find(prop.key()) == props.end()) {
update[prop.key()] = wpi::json();
}
}
return update;
}

void LocalTopic::ResetIfDoesNotExist() {
Expand All @@ -165,7 +183,7 @@ void LocalTopic::ResetIfDoesNotExist() {
typeStr.clear();
m_flags = 0;
properties = wpi::json::object();
propertiesStr = "{}";
m_propertiesStr = "{}";
}

void LocalTopic::RefreshFlags() {
Expand Down
9 changes: 6 additions & 3 deletions ntcore/src/main/native/cpp/local/LocalTopic.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,16 @@ struct LocalTopic {

void RefreshProperties(bool updateFlags);

// returns update json
wpi::json CompareProperties(const wpi::json props);

TopicInfo GetTopicInfo() const {
TopicInfo info;
info.topic = handle;
info.name = name;
info.type = type;
info.type_str = typeStr;
info.properties = propertiesStr;
info.properties = m_propertiesStr;
return info;
}

Expand Down Expand Up @@ -101,8 +104,8 @@ struct LocalTopic {
// update flags from properties
void RefreshFlags();

unsigned int m_flags{0}; // for NT3 APIs
std::string propertiesStr{"{}"}; // cached string for GetTopicInfo() et al
unsigned int m_flags{0}; // for NT3 APIs
std::string m_propertiesStr{"{}"}; // cached string for GetTopicInfo() et al
};

} // namespace nt::local

0 comments on commit 60c855e

Please sign in to comment.