[ntcore] Various fixes (#4469)

* Fix C++ Publisher and Subscriber move assignment 
* Fix Publisher comment typo.
* Publish: check that properties is an object.
Print a warning, but still publish, just with empty properties.
Add error print for unassigned type publish.
* Return boolean from SetProperties
* Document exception-throw in Java for invalid JSON.
This commit is contained in:
Peter Johnson
2022-10-14 20:50:55 -07:00
committed by GitHub
parent d9d6c425e7
commit 574cb41c18
10 changed files with 48 additions and 17 deletions

View File

@@ -759,6 +759,9 @@ void LSImpl::SetRetained(TopicData* topic, bool value) {
void LSImpl::SetProperties(TopicData* topic, const wpi::json& update,
bool sendNetwork) {
if (!update.is_object()) {
return;
}
DEBUG4("SetProperties({},{})", topic->name, sendNetwork);
for (auto&& change : update.items()) {
if (change.value().is_null()) {
@@ -924,8 +927,13 @@ PublisherData* LSImpl::AddLocalPublisher(TopicData* topic,
if (properties.is_null()) {
topic->properties = wpi::json::object();
} else {
} else if (properties.is_object()) {
topic->properties = properties;
} else {
WPI_WARNING(m_logger,
"ignoring non-object properties when publishing '{}'",
topic->name);
topic->properties = wpi::json::object();
}
if (topic->properties.empty()) {
@@ -1812,14 +1820,17 @@ wpi::json LocalStorage::GetTopicProperties(NT_Topic topicHandle) {
}
}
void LocalStorage::SetTopicProperties(NT_Topic topicHandle,
bool LocalStorage::SetTopicProperties(NT_Topic topicHandle,
const wpi::json& update) {
if (!update.is_object()) {
return;
return false;
}
std::scoped_lock lock{m_mutex};
if (auto topic = m_impl->m_topics.Get(topicHandle)) {
m_impl->SetProperties(topic, update, true);
return true;
} else {
return {};
}
}
@@ -1871,15 +1882,21 @@ NT_Publisher LocalStorage::Publish(NT_Topic topicHandle, NT_Type type,
std::string_view typeStr,
const wpi::json& properties,
wpi::span<const PubSubOption> options) {
if (type == NT_UNASSIGNED || typeStr.empty()) {
return 0;
}
std::scoped_lock lock{m_mutex};
// Get the topic
auto* topic = m_impl->m_topics.Get(topicHandle);
if (!topic) {
WPI_ERROR(m_impl->m_logger, "trying to publish invalid topic handle ({})",
topicHandle);
return 0;
}
if (type == NT_UNASSIGNED || typeStr.empty()) {
WPI_ERROR(
m_impl->m_logger,
"cannot publish '{}' with an unassigned type or empty type string",
topic->name);
return 0;
}

View File

@@ -84,7 +84,7 @@ class LocalStorage final : public net::ILocalStorage {
wpi::json GetTopicProperties(NT_Topic topic);
void SetTopicProperties(NT_Topic topic, const wpi::json& update);
bool SetTopicProperties(NT_Topic topic, const wpi::json& update);
TopicInfo GetTopicInfo(NT_Topic topic);

View File

@@ -298,8 +298,7 @@ NT_Bool NT_SetTopicProperties(NT_Topic topic, const char* properties) {
} catch (wpi::json::parse_error&) {
return false;
}
nt::SetTopicProperties(topic, j);
return true;
return nt::SetTopicProperties(topic, j);
}
NT_Subscriber NT_Subscribe(NT_Topic topic, NT_Type type, const char* typeStr,

View File

@@ -293,9 +293,11 @@ wpi::json GetTopicProperties(NT_Topic topic) {
}
}
void SetTopicProperties(NT_Topic topic, const wpi::json& properties) {
bool SetTopicProperties(NT_Topic topic, const wpi::json& properties) {
if (auto ii = InstanceImpl::GetTyped(topic, Handle::kTopic)) {
ii->localStorage.SetTopicProperties(topic, properties);
return ii->localStorage.SetTopicProperties(topic, properties);
} else {
return {};
}
}