From a8a352ed8ced982c14b083eb4c8ec691c2137483 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Sat, 17 Feb 2024 00:40:14 -0800 Subject: [PATCH] [ntcore] Add hidden subscribe option (#6376) This allows creating subscribers that aren't communicated with the network. --- .../wpi/first/networktables/PubSubOption.java | 15 +++++++++++++- .../first/networktables/PubSubOptions.java | 14 ++++++++++++- ntcore/src/main/native/cpp/LocalStorage.cpp | 20 +++++++++++-------- .../main/native/cpp/jni/NetworkTablesJNI.cpp | 4 +++- ntcore/src/main/native/cpp/ntcore_c.cpp | 1 + ntcore/src/main/native/include/ntcore_c.h | 8 ++++++++ ntcore/src/main/native/include/ntcore_cpp.h | 8 ++++++++ 7 files changed, 59 insertions(+), 11 deletions(-) diff --git a/ntcore/src/main/java/edu/wpi/first/networktables/PubSubOption.java b/ntcore/src/main/java/edu/wpi/first/networktables/PubSubOption.java index dd96eec8ef..b9d885533e 100644 --- a/ntcore/src/main/java/edu/wpi/first/networktables/PubSubOption.java +++ b/ntcore/src/main/java/edu/wpi/first/networktables/PubSubOption.java @@ -15,7 +15,8 @@ public class PubSubOption { disableRemote, disableLocal, excludePublisher, - excludeSelf + excludeSelf, + hidden } PubSubOption(Kind kind, boolean value) { @@ -149,6 +150,18 @@ public class PubSubOption { return new PubSubOption(Kind.excludeSelf, enabled); } + /** + * For subscriptions, don't share the existence of the subscription with the network. Note this + * means updates will not be received from the network unless another subscription overlaps with + * this one, and the subscription will not appear in metatopics. + * + * @param enabled True to enable, false to disable + * @return option + */ + public static PubSubOption hidden(boolean enabled) { + return new PubSubOption(Kind.hidden, enabled); + } + final Kind m_kind; final boolean m_bValue; final int m_iValue; diff --git a/ntcore/src/main/java/edu/wpi/first/networktables/PubSubOptions.java b/ntcore/src/main/java/edu/wpi/first/networktables/PubSubOptions.java index 18557b93db..ec9158af39 100644 --- a/ntcore/src/main/java/edu/wpi/first/networktables/PubSubOptions.java +++ b/ntcore/src/main/java/edu/wpi/first/networktables/PubSubOptions.java @@ -42,6 +42,9 @@ public class PubSubOptions { case excludeSelf: excludeSelf = option.m_bValue; break; + case hidden: + hidden = option.m_bValue; + break; default: break; } @@ -58,7 +61,8 @@ public class PubSubOptions { boolean prefixMatch, boolean disableRemote, boolean disableLocal, - boolean excludeSelf) { + boolean excludeSelf, + boolean hidden) { this.pollStorage = pollStorage; this.periodic = periodic; this.excludePublisher = excludePublisher; @@ -69,6 +73,7 @@ public class PubSubOptions { this.disableRemote = disableRemote; this.disableLocal = disableLocal; this.excludeSelf = excludeSelf; + this.hidden = hidden; } /** Default value of periodic. */ @@ -123,4 +128,11 @@ public class PubSubOptions { /** For entries, don't queue (for readQueue) value updates for the entry's internal publisher. */ public boolean excludeSelf; + + /** + * For subscriptions, don't share the existence of the subscription with the network. Note this + * means updates will not be received from the network unless another subscription overlaps with + * this one, and the subscription will not appear in metatopics. + */ + public boolean hidden; } diff --git a/ntcore/src/main/native/cpp/LocalStorage.cpp b/ntcore/src/main/native/cpp/LocalStorage.cpp index 26e898a483..7d1c3e4dc0 100644 --- a/ntcore/src/main/native/cpp/LocalStorage.cpp +++ b/ntcore/src/main/native/cpp/LocalStorage.cpp @@ -611,7 +611,7 @@ LocalStorage::SubscriberData* LocalStorage::Impl::AddLocalSubscriber( "published as '{}')", topic->name, config.typeStr, topic->typeStr); } - if (m_network) { + if (m_network && !subscriber->config.hidden) { DEBUG4("-> NetworkSubscribe({})", topic->name); m_network->Subscribe(subscriber->handle, {{topic->name}}, config); } @@ -640,7 +640,7 @@ LocalStorage::Impl::RemoveLocalSubscriber(NT_Subscriber subHandle) { listener.getSecond()->subscriber = nullptr; } } - if (m_network) { + if (m_network && !subscriber->config.hidden) { m_network->Unsubscribe(subscriber->handle); } } @@ -676,7 +676,7 @@ LocalStorage::MultiSubscriberData* LocalStorage::Impl::AddMultiSubscriber( } } } - if (m_network) { + if (m_network && !subscriber->options.hidden) { DEBUG4("-> NetworkSubscribe"); m_network->Subscribe(subscriber->handle, subscriber->prefixes, subscriber->options); @@ -696,7 +696,7 @@ LocalStorage::Impl::RemoveMultiSubscriber(NT_MultiSubscriber subHandle) { listener.getSecond()->multiSubscriber = nullptr; } } - if (m_network) { + if (m_network && !subscriber->options.hidden) { m_network->Unsubscribe(subscriber->handle); } } @@ -1128,12 +1128,16 @@ void LocalStorage::Impl::StartNetwork(net::NetworkInterface* network) { } } for (auto&& subscriber : m_subscribers) { - network->Subscribe(subscriber->handle, {{subscriber->topic->name}}, - subscriber->config); + if (!subscriber->config.hidden) { + network->Subscribe(subscriber->handle, {{subscriber->topic->name}}, + subscriber->config); + } } for (auto&& subscriber : m_multiSubscribers) { - network->Subscribe(subscriber->handle, subscriber->prefixes, - subscriber->options); + if (!subscriber->options.hidden) { + network->Subscribe(subscriber->handle, subscriber->prefixes, + subscriber->options); + } } } diff --git a/ntcore/src/main/native/cpp/jni/NetworkTablesJNI.cpp b/ntcore/src/main/native/cpp/jni/NetworkTablesJNI.cpp index fdec2b90e3..ae10656bb9 100644 --- a/ntcore/src/main/native/cpp/jni/NetworkTablesJNI.cpp +++ b/ntcore/src/main/native/cpp/jni/NetworkTablesJNI.cpp @@ -139,6 +139,7 @@ static nt::PubSubOptions FromJavaPubSubOptions(JNIEnv* env, jobject joptions) { FIELD(disableRemote, "Z"); FIELD(disableLocal, "Z"); FIELD(excludeSelf, "Z"); + FIELD(hidden, "Z"); #undef FIELD @@ -154,7 +155,8 @@ static nt::PubSubOptions FromJavaPubSubOptions(JNIEnv* env, jobject joptions) { FIELD(bool, Boolean, prefixMatch), FIELD(bool, Boolean, disableRemote), FIELD(bool, Boolean, disableLocal), - FIELD(bool, Boolean, excludeSelf)}; + FIELD(bool, Boolean, excludeSelf), + FIELD(bool, Boolean, hidden)}; #undef GET #undef FIELD diff --git a/ntcore/src/main/native/cpp/ntcore_c.cpp b/ntcore/src/main/native/cpp/ntcore_c.cpp index c2561c8833..cac7a3391e 100644 --- a/ntcore/src/main/native/cpp/ntcore_c.cpp +++ b/ntcore/src/main/native/cpp/ntcore_c.cpp @@ -126,6 +126,7 @@ static PubSubOptions ConvertToCpp(const NT_PubSubOptions* in) { out.disableRemote = in->disableRemote; out.disableLocal = in->disableLocal; out.excludeSelf = in->excludeSelf; + out.hidden = in->hidden; return out; } diff --git a/ntcore/src/main/native/include/ntcore_c.h b/ntcore/src/main/native/include/ntcore_c.h index 6259a76e9d..67efd76611 100644 --- a/ntcore/src/main/native/include/ntcore_c.h +++ b/ntcore/src/main/native/include/ntcore_c.h @@ -367,6 +367,14 @@ struct NT_PubSubOptions { * internal publisher. */ NT_Bool excludeSelf; + + /** + * For subscriptions, don't share the existence of the subscription with the + * network. Note this means updates will not be received from the network + * unless another subscription overlaps with this one, and the subscription + * will not appear in metatopics. + */ + NT_Bool hidden; }; /** diff --git a/ntcore/src/main/native/include/ntcore_cpp.h b/ntcore/src/main/native/include/ntcore_cpp.h index fe50f45437..f241f63811 100644 --- a/ntcore/src/main/native/include/ntcore_cpp.h +++ b/ntcore/src/main/native/include/ntcore_cpp.h @@ -374,6 +374,14 @@ struct PubSubOptions { * internal publisher. */ bool excludeSelf = false; + + /** + * For subscriptions, don't share the existence of the subscription with the + * network. Note this means updates will not be received from the network + * unless another subscription overlaps with this one, and the subscription + * will not appear in metatopics. + */ + bool hidden = false; }; /**