mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-03 03:01:44 +00:00
[ntcore] Add subscriber option to exclude single publisher
This enables limited self-exclusion for entries seeing their own changes or similarly with a pub/sub pair.
This commit is contained in:
@@ -282,8 +282,9 @@ struct LSImpl {
|
||||
void CheckReset(TopicData* topic);
|
||||
|
||||
bool SetValue(TopicData* topic, const Value& value, unsigned int eventFlags,
|
||||
bool isDuplicate);
|
||||
void NotifyValue(TopicData* topic, unsigned int eventFlags, bool isDuplicate);
|
||||
bool isDuplicate, const PublisherData* publisher);
|
||||
void NotifyValue(TopicData* topic, unsigned int eventFlags, bool isDuplicate,
|
||||
const PublisherData* publisher);
|
||||
|
||||
void SetFlags(TopicData* topic, unsigned int flags);
|
||||
void SetPersistent(TopicData* topic, bool value);
|
||||
@@ -489,7 +490,8 @@ void LSImpl::CheckReset(TopicData* topic) {
|
||||
}
|
||||
|
||||
bool LSImpl::SetValue(TopicData* topic, const Value& value,
|
||||
unsigned int eventFlags, bool isDuplicate) {
|
||||
unsigned int eventFlags, bool isDuplicate,
|
||||
const PublisherData* publisher) {
|
||||
DEBUG4("SetValue({}, {}, {}, {})", topic->name, value.time(), eventFlags,
|
||||
isDuplicate);
|
||||
if (topic->type != NT_UNASSIGNED && topic->type != value.type()) {
|
||||
@@ -499,7 +501,7 @@ bool LSImpl::SetValue(TopicData* topic, const Value& value,
|
||||
// TODO: notify option even if older value
|
||||
topic->type = value.type();
|
||||
topic->lastValue = value;
|
||||
NotifyValue(topic, eventFlags, isDuplicate);
|
||||
NotifyValue(topic, eventFlags, isDuplicate, publisher);
|
||||
}
|
||||
if (!isDuplicate && topic->datalogType == value.type()) {
|
||||
for (auto&& datalog : topic->datalogs) {
|
||||
@@ -510,13 +512,15 @@ bool LSImpl::SetValue(TopicData* topic, const Value& value,
|
||||
}
|
||||
|
||||
void LSImpl::NotifyValue(TopicData* topic, unsigned int eventFlags,
|
||||
bool isDuplicate) {
|
||||
bool isDuplicate, const PublisherData* publisher) {
|
||||
bool isNetwork = (eventFlags & NT_EVENT_VALUE_REMOTE) != 0;
|
||||
for (auto&& subscriber : topic->localSubscribers) {
|
||||
if (subscriber->active &&
|
||||
(subscriber->config.keepDuplicates || !isDuplicate) &&
|
||||
((isNetwork && subscriber->config.fromRemote) ||
|
||||
(!isNetwork && subscriber->config.fromLocal))) {
|
||||
(!isNetwork && subscriber->config.fromLocal)) &&
|
||||
(!publisher ||
|
||||
(publisher && (subscriber->config.excludePub != publisher->handle)))) {
|
||||
subscriber->pollStorage.emplace_back(topic->lastValue);
|
||||
subscriber->handle.Set();
|
||||
if (!subscriber->valueListeners.empty()) {
|
||||
@@ -1246,7 +1250,8 @@ bool LSImpl::PublishLocalValue(PublisherData* publisher, const Value& value,
|
||||
publisher->topic->lastValueNetwork = value;
|
||||
m_network->SetValue(publisher->handle, value);
|
||||
}
|
||||
return SetValue(publisher->topic, value, NT_EVENT_VALUE_LOCAL, isDuplicate);
|
||||
return SetValue(publisher->topic, value, NT_EVENT_VALUE_LOCAL, isDuplicate,
|
||||
publisher);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
@@ -1260,6 +1265,9 @@ bool LSImpl::SetEntryValue(NT_Handle pubentryHandle, const Value& value) {
|
||||
if (!publisher) {
|
||||
if (auto entry = m_entries.Get(pubentryHandle)) {
|
||||
publisher = PublishEntry(entry, value.type());
|
||||
if (entry->subscriber->config.excludeSelf) {
|
||||
entry->subscriber->config.excludePub = publisher->handle;
|
||||
}
|
||||
}
|
||||
if (!publisher) {
|
||||
return false;
|
||||
@@ -1366,7 +1374,7 @@ void LocalStorage::NetworkSetValue(NT_Topic topicHandle, const Value& value) {
|
||||
std::scoped_lock lock{m_mutex};
|
||||
if (auto topic = m_impl->m_topics.Get(topicHandle)) {
|
||||
if (m_impl->SetValue(topic, value, NT_EVENT_VALUE_REMOTE,
|
||||
value == topic->lastValue)) {
|
||||
value == topic->lastValue, nullptr)) {
|
||||
topic->lastValueNetwork = value;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
#include "PubSubOptions.h"
|
||||
|
||||
#include "ntcore_c.h"
|
||||
#include "ntcore_cpp.h"
|
||||
|
||||
using namespace nt;
|
||||
@@ -48,6 +49,12 @@ nt::PubSubOptions::PubSubOptions(std::span<const PubSubOption> options) {
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case NT_PUBSUB_EXCLUDEPUB:
|
||||
excludePub = option.value;
|
||||
break;
|
||||
case NT_PUBSUB_EXCLUDESELF:
|
||||
excludeSelf = option.value != 0;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -26,6 +26,8 @@ class PubSubOptions {
|
||||
bool keepDuplicates = false;
|
||||
bool fromRemote = true;
|
||||
bool fromLocal = true;
|
||||
unsigned int excludePub = 0;
|
||||
bool excludeSelf = false;
|
||||
};
|
||||
|
||||
} // namespace nt
|
||||
|
||||
@@ -96,6 +96,8 @@ enum NT_PubSubOptionType {
|
||||
NT_PUBSUB_POLLSTORAGE, /* polling storage for subscription */
|
||||
NT_PUBSUB_KEEPDUPLICATES, /* preserve duplicate values */
|
||||
NT_PUBSUB_LOCALREMOTE, /* local, remote, or any value changes */
|
||||
NT_PUBSUB_EXCLUDEPUB, /* exclude value changes made by given publisher */
|
||||
NT_PUBSUB_EXCLUDESELF, /* exclude value changes made by entry publisher */
|
||||
};
|
||||
|
||||
/** Event notification flags. */
|
||||
@@ -290,7 +292,7 @@ struct NT_PubSubOption {
|
||||
* Option value. 1 (true) or 0 (false) for immediate and logging options,
|
||||
* time between updates, in milliseconds, for periodic option. For
|
||||
* local/remote option, 1=local only, 2=remote only, 0 or 3=both local and
|
||||
* remote.
|
||||
* remote. For exclude publisher, publisher handle.
|
||||
*/
|
||||
unsigned int value;
|
||||
};
|
||||
|
||||
@@ -358,6 +358,28 @@ class PubSubOption {
|
||||
return PubSubOption{NT_PUBSUB_LOCALREMOTE, 0u};
|
||||
}
|
||||
|
||||
/**
|
||||
* Don't queue value updates for the given publisher. Only has an effect on
|
||||
* subscriptions. Only one exclusion may be set.
|
||||
*
|
||||
* @param publisher publisher handle
|
||||
* @return option
|
||||
*/
|
||||
static constexpr PubSubOption ExcludePublisher(NT_Publisher publisher) {
|
||||
return PubSubOption{NT_PUBSUB_EXCLUDEPUB, publisher};
|
||||
}
|
||||
|
||||
/**
|
||||
* Don't queue value updates for the internal publisher for an entry. Only has
|
||||
* an effect on entries.
|
||||
*
|
||||
* @param enabled True to enable, false to disable
|
||||
* @return option
|
||||
*/
|
||||
static constexpr PubSubOption ExcludeSelf(bool enabled) {
|
||||
return PubSubOption{NT_PUBSUB_EXCLUDESELF, enabled ? 1u : 0u};
|
||||
}
|
||||
|
||||
NT_PubSubOptionType type;
|
||||
unsigned int value;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user