Skip to content

Commit

Permalink
[ntcore] Add hidden subscribe option (wpilibsuite#6376)
Browse files Browse the repository at this point in the history
This allows creating subscribers that aren't communicated with the network.
  • Loading branch information
PeterJohnson authored Feb 17, 2024
1 parent 0cdab55 commit a8a352e
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ enum Kind {
disableRemote,
disableLocal,
excludePublisher,
excludeSelf
excludeSelf,
hidden
}

PubSubOption(Kind kind, boolean value) {
Expand Down Expand Up @@ -149,6 +150,18 @@ public static PubSubOption excludeSelf(boolean enabled) {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public PubSubOptions(PubSubOption... options) {
case excludeSelf:
excludeSelf = option.m_bValue;
break;
case hidden:
hidden = option.m_bValue;
break;
default:
break;
}
Expand All @@ -58,7 +61,8 @@ public PubSubOptions(PubSubOption... options) {
boolean prefixMatch,
boolean disableRemote,
boolean disableLocal,
boolean excludeSelf) {
boolean excludeSelf,
boolean hidden) {
this.pollStorage = pollStorage;
this.periodic = periodic;
this.excludePublisher = excludePublisher;
Expand All @@ -69,6 +73,7 @@ public PubSubOptions(PubSubOption... options) {
this.disableRemote = disableRemote;
this.disableLocal = disableLocal;
this.excludeSelf = excludeSelf;
this.hidden = hidden;
}

/** Default value of periodic. */
Expand Down Expand Up @@ -123,4 +128,11 @@ public PubSubOptions(PubSubOption... options) {

/** 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;
}
20 changes: 12 additions & 8 deletions ntcore/src/main/native/cpp/LocalStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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);
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion ntcore/src/main/native/cpp/jni/NetworkTablesJNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions ntcore/src/main/native/cpp/ntcore_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
8 changes: 8 additions & 0 deletions ntcore/src/main/native/include/ntcore_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

/**
Expand Down
8 changes: 8 additions & 0 deletions ntcore/src/main/native/include/ntcore_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

/**
Expand Down

0 comments on commit a8a352e

Please sign in to comment.