mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
[ntcore] Pass pub/sub options as a unified PubSubOptions struct (#4794)
In Java, PubSubOption is still used for passing options, but this simplifies C++ use substantially, as it allows aggregate construction.
This commit is contained in:
@@ -107,11 +107,13 @@ struct TopicData {
|
||||
VectorSet<NT_Listener> listeners;
|
||||
};
|
||||
|
||||
struct PubSubConfig : public PubSubOptions {
|
||||
struct PubSubConfig : public PubSubOptionsImpl {
|
||||
PubSubConfig() = default;
|
||||
PubSubConfig(NT_Type type, std::string_view typeStr,
|
||||
std::span<const PubSubOption> options)
|
||||
: PubSubOptions{options}, type{type}, typeStr{typeStr} {}
|
||||
const PubSubOptions& options)
|
||||
: PubSubOptionsImpl{options}, type{type}, typeStr{typeStr} {
|
||||
prefixMatch = false;
|
||||
}
|
||||
|
||||
NT_Type type{NT_UNASSIGNED};
|
||||
std::string typeStr;
|
||||
@@ -141,7 +143,7 @@ struct SubscriberData {
|
||||
: handle{handle},
|
||||
topic{topic},
|
||||
config{std::move(config)},
|
||||
pollStorage{config.pollStorageSize} {}
|
||||
pollStorage{config.pollStorage} {}
|
||||
|
||||
void UpdateActive();
|
||||
|
||||
@@ -180,8 +182,8 @@ struct MultiSubscriberData {
|
||||
|
||||
MultiSubscriberData(NT_MultiSubscriber handle,
|
||||
std::span<const std::string_view> prefixes,
|
||||
PubSubOptions options)
|
||||
: handle{handle}, options{std::move(options)} {
|
||||
const PubSubOptionsImpl& options)
|
||||
: handle{handle}, options{options} {
|
||||
this->options.prefixMatch = true;
|
||||
this->prefixes.reserve(prefixes.size());
|
||||
for (auto&& prefix : prefixes) {
|
||||
@@ -194,7 +196,7 @@ struct MultiSubscriberData {
|
||||
// invariants
|
||||
wpi::SignalObject<NT_MultiSubscriber> handle;
|
||||
std::vector<std::string> prefixes;
|
||||
PubSubOptions options;
|
||||
PubSubOptionsImpl options;
|
||||
|
||||
// value listeners
|
||||
VectorSet<NT_Listener> valueListeners;
|
||||
@@ -517,10 +519,10 @@ void LSImpl::NotifyValue(TopicData* topic, unsigned int eventFlags,
|
||||
for (auto&& subscriber : topic->localSubscribers) {
|
||||
if (subscriber->active &&
|
||||
(subscriber->config.keepDuplicates || !isDuplicate) &&
|
||||
((isNetwork && subscriber->config.fromRemote) ||
|
||||
(!isNetwork && subscriber->config.fromLocal)) &&
|
||||
(!publisher ||
|
||||
(publisher && (subscriber->config.excludePub != publisher->handle)))) {
|
||||
((isNetwork && !subscriber->config.disableRemote) ||
|
||||
(!isNetwork && !subscriber->config.disableLocal)) &&
|
||||
(!publisher || (publisher && (subscriber->config.excludePublisher !=
|
||||
publisher->handle)))) {
|
||||
subscriber->pollStorage.emplace_back(topic->lastValue);
|
||||
subscriber->handle.Set();
|
||||
if (!subscriber->valueListeners.empty()) {
|
||||
@@ -1090,10 +1092,8 @@ void LSImpl::AddListener(NT_Listener listenerHandle,
|
||||
return;
|
||||
}
|
||||
// subscribe to make sure topic updates are received
|
||||
PubSubOptions options;
|
||||
options.topicsOnly = (eventMask & NT_EVENT_VALUE_ALL) == 0;
|
||||
options.prefixMatch = true;
|
||||
auto sub = AddMultiSubscriber(prefixes, options);
|
||||
auto sub = AddMultiSubscriber(
|
||||
prefixes, {.topicsOnly = (eventMask & NT_EVENT_VALUE_ALL) == 0});
|
||||
AddListenerImpl(listenerHandle, sub, eventMask, true);
|
||||
}
|
||||
|
||||
@@ -1266,7 +1266,7 @@ bool LSImpl::SetEntryValue(NT_Handle pubentryHandle, const Value& value) {
|
||||
if (auto entry = m_entries.Get(pubentryHandle)) {
|
||||
publisher = PublishEntry(entry, value.type());
|
||||
if (entry->subscriber->config.excludeSelf) {
|
||||
entry->subscriber->config.excludePub = publisher->handle;
|
||||
entry->subscriber->config.excludePublisher = publisher->handle;
|
||||
}
|
||||
}
|
||||
if (!publisher) {
|
||||
@@ -1642,7 +1642,7 @@ TopicInfo LocalStorage::GetTopicInfo(NT_Topic topicHandle) {
|
||||
|
||||
NT_Subscriber LocalStorage::Subscribe(NT_Topic topicHandle, NT_Type type,
|
||||
std::string_view typeStr,
|
||||
std::span<const PubSubOption> options) {
|
||||
const PubSubOptions& options) {
|
||||
std::scoped_lock lock{m_mutex};
|
||||
|
||||
// Get the topic
|
||||
@@ -1669,8 +1669,7 @@ void LocalStorage::Unsubscribe(NT_Subscriber subHandle) {
|
||||
}
|
||||
|
||||
NT_MultiSubscriber LocalStorage::SubscribeMultiple(
|
||||
std::span<const std::string_view> prefixes,
|
||||
std::span<const PubSubOption> options) {
|
||||
std::span<const std::string_view> prefixes, const PubSubOptions& options) {
|
||||
std::scoped_lock lock{m_mutex};
|
||||
|
||||
if (m_impl->m_multiSubscribers.size() >= kMaxMultiSubscribers) {
|
||||
@@ -1679,9 +1678,7 @@ NT_MultiSubscriber LocalStorage::SubscribeMultiple(
|
||||
return 0;
|
||||
}
|
||||
|
||||
PubSubOptions opts{options};
|
||||
opts.prefixMatch = true;
|
||||
return m_impl->AddMultiSubscriber(prefixes, opts)->handle;
|
||||
return m_impl->AddMultiSubscriber(prefixes, options)->handle;
|
||||
}
|
||||
|
||||
void LocalStorage::UnsubscribeMultiple(NT_MultiSubscriber subHandle) {
|
||||
@@ -1692,7 +1689,7 @@ void LocalStorage::UnsubscribeMultiple(NT_MultiSubscriber subHandle) {
|
||||
NT_Publisher LocalStorage::Publish(NT_Topic topicHandle, NT_Type type,
|
||||
std::string_view typeStr,
|
||||
const wpi::json& properties,
|
||||
std::span<const PubSubOption> options) {
|
||||
const PubSubOptions& options) {
|
||||
std::scoped_lock lock{m_mutex};
|
||||
|
||||
// Get the topic
|
||||
@@ -1742,7 +1739,7 @@ void LocalStorage::Unpublish(NT_Handle pubentryHandle) {
|
||||
|
||||
NT_Entry LocalStorage::GetEntry(NT_Topic topicHandle, NT_Type type,
|
||||
std::string_view typeStr,
|
||||
std::span<const PubSubOption> options) {
|
||||
const PubSubOptions& options) {
|
||||
std::scoped_lock lock{m_mutex};
|
||||
|
||||
// Get the topic
|
||||
|
||||
@@ -93,24 +93,23 @@ class LocalStorage final : public net::ILocalStorage {
|
||||
|
||||
NT_Subscriber Subscribe(NT_Topic topic, NT_Type type,
|
||||
std::string_view typeStr,
|
||||
std::span<const PubSubOption> options);
|
||||
const PubSubOptions& options);
|
||||
|
||||
void Unsubscribe(NT_Subscriber sub);
|
||||
|
||||
NT_MultiSubscriber SubscribeMultiple(
|
||||
std::span<const std::string_view> prefixes,
|
||||
std::span<const PubSubOption> options);
|
||||
std::span<const std::string_view> prefixes, const PubSubOptions& options);
|
||||
|
||||
void UnsubscribeMultiple(NT_MultiSubscriber subHandle);
|
||||
|
||||
NT_Publisher Publish(NT_Topic topic, NT_Type type, std::string_view typeStr,
|
||||
const wpi::json& properties,
|
||||
std::span<const PubSubOption> options);
|
||||
const PubSubOptions& options);
|
||||
|
||||
void Unpublish(NT_Handle pubentry);
|
||||
|
||||
NT_Entry GetEntry(NT_Topic topic, NT_Type type, std::string_view typeStr,
|
||||
std::span<const PubSubOption> options);
|
||||
const PubSubOptions& options);
|
||||
|
||||
void ReleaseEntry(NT_Entry entry);
|
||||
|
||||
|
||||
@@ -1,62 +0,0 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
#include "PubSubOptions.h"
|
||||
|
||||
#include "ntcore_c.h"
|
||||
#include "ntcore_cpp.h"
|
||||
|
||||
using namespace nt;
|
||||
|
||||
nt::PubSubOptions::PubSubOptions(std::span<const PubSubOption> options) {
|
||||
for (auto&& option : options) {
|
||||
switch (option.type) {
|
||||
case NT_PUBSUB_PERIODIC:
|
||||
periodicMs = option.value;
|
||||
break;
|
||||
case NT_PUBSUB_SENDALL:
|
||||
sendAll = option.value != 0;
|
||||
if (sendAll) {
|
||||
pollStorageSize = 20;
|
||||
}
|
||||
break;
|
||||
case NT_PUBSUB_TOPICSONLY:
|
||||
topicsOnly = option.value != 0;
|
||||
break;
|
||||
case NT_PUBSUB_KEEPDUPLICATES:
|
||||
keepDuplicates = option.value != 0;
|
||||
break;
|
||||
case NT_PUBSUB_POLLSTORAGE:
|
||||
pollStorageSize = static_cast<size_t>(option.value);
|
||||
break;
|
||||
case NT_PUBSUB_LOCALREMOTE:
|
||||
switch (static_cast<int>(option.value)) {
|
||||
case 0:
|
||||
case 3:
|
||||
fromLocal = true;
|
||||
fromRemote = true;
|
||||
break;
|
||||
case 1:
|
||||
fromLocal = true;
|
||||
fromRemote = false;
|
||||
break;
|
||||
case 2:
|
||||
fromLocal = false;
|
||||
fromRemote = true;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case NT_PUBSUB_EXCLUDEPUB:
|
||||
excludePub = option.value;
|
||||
break;
|
||||
case NT_PUBSUB_EXCLUDESELF:
|
||||
excludeSelf = option.value != 0;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,30 +4,33 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <span>
|
||||
|
||||
#include "ntcore_cpp.h"
|
||||
|
||||
namespace nt {
|
||||
|
||||
// options built from array of PubSubOption
|
||||
class PubSubOptions {
|
||||
// internal helper class for PubSubOptions
|
||||
class PubSubOptionsImpl : public PubSubOptions {
|
||||
public:
|
||||
PubSubOptions() = default;
|
||||
explicit PubSubOptions(std::span<const PubSubOption> options);
|
||||
constexpr PubSubOptionsImpl() : PubSubOptionsImpl{kDefaultPubSubOptions} {}
|
||||
|
||||
/*implicit*/ constexpr PubSubOptionsImpl( // NOLINT
|
||||
const PubSubOptions& options)
|
||||
: PubSubOptions{options} {
|
||||
if (periodic == 0) {
|
||||
periodic = kDefaultPeriodic;
|
||||
}
|
||||
periodicMs = static_cast<unsigned int>(periodic * 1000);
|
||||
if (pollStorage == 0) {
|
||||
if (sendAll) {
|
||||
pollStorage = 20;
|
||||
} else {
|
||||
pollStorage = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static constexpr unsigned int kDefaultPeriodicMs = 100;
|
||||
|
||||
unsigned int periodicMs = kDefaultPeriodicMs;
|
||||
size_t pollStorageSize = 1;
|
||||
bool sendAll = false;
|
||||
bool topicsOnly = false;
|
||||
bool prefixMatch = false;
|
||||
bool keepDuplicates = false;
|
||||
bool fromRemote = true;
|
||||
bool fromLocal = true;
|
||||
unsigned int excludePub = 0;
|
||||
bool excludeSelf = false;
|
||||
};
|
||||
|
||||
} // namespace nt
|
||||
|
||||
@@ -39,6 +39,7 @@ static JClass eventCls;
|
||||
static JClass floatCls;
|
||||
static JClass logMessageCls;
|
||||
static JClass longCls;
|
||||
static JClass pubSubOptionsCls;
|
||||
static JClass topicInfoCls;
|
||||
static JClass valueCls;
|
||||
static JClass valueEventDataCls;
|
||||
@@ -54,6 +55,7 @@ static const JClassInit classes[] = {
|
||||
{"java/lang/Float", &floatCls},
|
||||
{"edu/wpi/first/networktables/LogMessage", &logMessageCls},
|
||||
{"java/lang/Long", &longCls},
|
||||
{"edu/wpi/first/networktables/PubSubOptions", &pubSubOptionsCls},
|
||||
{"edu/wpi/first/networktables/TopicInfo", &topicInfoCls},
|
||||
{"edu/wpi/first/networktables/NetworkTableValue", &valueCls},
|
||||
{"edu/wpi/first/networktables/ValueEventData", &valueEventDataCls}};
|
||||
@@ -117,20 +119,45 @@ JNIEXPORT void JNICALL JNI_OnUnload(JavaVM* vm, void* reserved) {
|
||||
// Conversions from Java objects to C++
|
||||
//
|
||||
|
||||
static std::span<const nt::PubSubOption> FromJavaPubSubOptions(
|
||||
JNIEnv* env, jintArray optionTypes, jintArray optionValues,
|
||||
wpi::SmallVectorImpl<nt::PubSubOption>& arr) {
|
||||
JIntArrayRef types{env, optionTypes};
|
||||
JIntArrayRef values{env, optionValues};
|
||||
if (types.size() != values.size()) {
|
||||
static nt::PubSubOptions FromJavaPubSubOptions(JNIEnv* env, jobject joptions) {
|
||||
if (!joptions) {
|
||||
return {};
|
||||
}
|
||||
arr.clear();
|
||||
arr.reserve(types.size());
|
||||
for (size_t i = 0, iend = types.size(); i != iend; ++i) {
|
||||
arr.emplace_back(static_cast<NT_PubSubOptionType>(types[i]), values[i]);
|
||||
#define FIELD(name, sig) \
|
||||
static jfieldID name##Field = nullptr; \
|
||||
if (!name##Field) { \
|
||||
name##Field = env->GetFieldID(pubSubOptionsCls, #name, sig); \
|
||||
}
|
||||
return arr;
|
||||
|
||||
FIELD(pollStorage, "I");
|
||||
FIELD(periodic, "D");
|
||||
FIELD(excludePublisher, "I");
|
||||
FIELD(sendAll, "Z");
|
||||
FIELD(topicsOnly, "Z");
|
||||
FIELD(keepDuplicates, "Z");
|
||||
FIELD(prefixMatch, "Z");
|
||||
FIELD(disableRemote, "Z");
|
||||
FIELD(disableLocal, "Z");
|
||||
FIELD(excludeSelf, "Z");
|
||||
|
||||
#undef FIELD
|
||||
|
||||
#define FIELD(ctype, jtype, name) \
|
||||
.name = static_cast<ctype>(env->Get##jtype##Field(joptions, name##Field))
|
||||
|
||||
return {FIELD(unsigned int, Int, pollStorage),
|
||||
FIELD(double, Double, periodic),
|
||||
FIELD(NT_Publisher, Int, excludePublisher),
|
||||
FIELD(bool, Boolean, sendAll),
|
||||
FIELD(bool, Boolean, topicsOnly),
|
||||
FIELD(bool, Boolean, keepDuplicates),
|
||||
FIELD(bool, Boolean, prefixMatch),
|
||||
FIELD(bool, Boolean, disableRemote),
|
||||
FIELD(bool, Boolean, disableLocal),
|
||||
FIELD(bool, Boolean, excludeSelf)};
|
||||
|
||||
#undef GET
|
||||
#undef FIELD
|
||||
}
|
||||
|
||||
//
|
||||
@@ -356,7 +383,7 @@ Java_edu_wpi_first_networktables_NetworkTablesJNI_getInstanceFromHandle
|
||||
* Signature: (ILjava/lang/String;)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_edu_wpi_first_networktables_NetworkTablesJNI_getEntry__ILjava_lang_String_2
|
||||
Java_edu_wpi_first_networktables_NetworkTablesJNI_getEntry
|
||||
(JNIEnv* env, jclass, jint inst, jstring key)
|
||||
{
|
||||
if (!key) {
|
||||
@@ -720,17 +747,15 @@ Java_edu_wpi_first_networktables_NetworkTablesJNI_setTopicProperties
|
||||
/*
|
||||
* Class: edu_wpi_first_networktables_NetworkTablesJNI
|
||||
* Method: subscribe
|
||||
* Signature: (IILjava/lang/String;[I[I)I
|
||||
* Signature: (IILjava/lang/String;Ljava/lang/Object;)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_edu_wpi_first_networktables_NetworkTablesJNI_subscribe
|
||||
(JNIEnv* env, jclass, jint topic, jint type, jstring typeStr,
|
||||
jintArray optionTypes, jintArray optionValues)
|
||||
(JNIEnv* env, jclass, jint topic, jint type, jstring typeStr, jobject options)
|
||||
{
|
||||
wpi::SmallVector<nt::PubSubOption, 4> options;
|
||||
return nt::Subscribe(
|
||||
topic, static_cast<NT_Type>(type), JStringRef{env, typeStr},
|
||||
FromJavaPubSubOptions(env, optionTypes, optionValues, options));
|
||||
return nt::Subscribe(topic, static_cast<NT_Type>(type),
|
||||
JStringRef{env, typeStr},
|
||||
FromJavaPubSubOptions(env, options));
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -748,28 +773,26 @@ Java_edu_wpi_first_networktables_NetworkTablesJNI_unsubscribe
|
||||
/*
|
||||
* Class: edu_wpi_first_networktables_NetworkTablesJNI
|
||||
* Method: publish
|
||||
* Signature: (IILjava/lang/String;[I[I)I
|
||||
* Signature: (IILjava/lang/String;Ljava/lang/Object;)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_edu_wpi_first_networktables_NetworkTablesJNI_publish
|
||||
(JNIEnv* env, jclass, jint topic, jint type, jstring typeStr,
|
||||
jintArray optionTypes, jintArray optionValues)
|
||||
(JNIEnv* env, jclass, jint topic, jint type, jstring typeStr, jobject options)
|
||||
{
|
||||
wpi::SmallVector<nt::PubSubOption, 4> options;
|
||||
return nt::Publish(
|
||||
topic, static_cast<NT_Type>(type), JStringRef{env, typeStr},
|
||||
FromJavaPubSubOptions(env, optionTypes, optionValues, options));
|
||||
return nt::Publish(topic, static_cast<NT_Type>(type),
|
||||
JStringRef{env, typeStr},
|
||||
FromJavaPubSubOptions(env, options));
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_networktables_NetworkTablesJNI
|
||||
* Method: publishEx
|
||||
* Signature: (IILjava/lang/String;Ljava/lang/String;[I[I)I
|
||||
* Signature: (IILjava/lang/String;Ljava/lang/String;Ljava/lang/Object;)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_edu_wpi_first_networktables_NetworkTablesJNI_publishEx
|
||||
(JNIEnv* env, jclass, jint topic, jint type, jstring typeStr,
|
||||
jstring properties, jintArray optionTypes, jintArray optionValues)
|
||||
jstring properties, jobject options)
|
||||
{
|
||||
wpi::json j;
|
||||
try {
|
||||
@@ -783,10 +806,9 @@ Java_edu_wpi_first_networktables_NetworkTablesJNI_publishEx
|
||||
illegalArgEx.Throw(env, "properties is not a JSON object");
|
||||
return 0;
|
||||
}
|
||||
wpi::SmallVector<nt::PubSubOption, 4> options;
|
||||
return nt::PublishEx(
|
||||
topic, static_cast<NT_Type>(type), JStringRef{env, typeStr}, j,
|
||||
FromJavaPubSubOptions(env, optionTypes, optionValues, options));
|
||||
return nt::PublishEx(topic, static_cast<NT_Type>(type),
|
||||
JStringRef{env, typeStr}, j,
|
||||
FromJavaPubSubOptions(env, options));
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -803,18 +825,16 @@ Java_edu_wpi_first_networktables_NetworkTablesJNI_unpublish
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_networktables_NetworkTablesJNI
|
||||
* Method: getEntry
|
||||
* Signature: (IILjava/lang/String;[I[I)I
|
||||
* Method: getEntryImpl
|
||||
* Signature: (IILjava/lang/String;Ljava/lang/Object;)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_edu_wpi_first_networktables_NetworkTablesJNI_getEntry__IILjava_lang_String_2_3I_3I
|
||||
(JNIEnv* env, jclass, jint topic, jint type, jstring typeStr,
|
||||
jintArray optionTypes, jintArray optionValues)
|
||||
Java_edu_wpi_first_networktables_NetworkTablesJNI_getEntryImpl
|
||||
(JNIEnv* env, jclass, jint topic, jint type, jstring typeStr, jobject options)
|
||||
{
|
||||
wpi::SmallVector<nt::PubSubOption, 4> options;
|
||||
return nt::GetEntry(
|
||||
topic, static_cast<NT_Type>(type), JStringRef{env, typeStr},
|
||||
FromJavaPubSubOptions(env, optionTypes, optionValues, options));
|
||||
return nt::GetEntry(topic, static_cast<NT_Type>(type),
|
||||
JStringRef{env, typeStr},
|
||||
FromJavaPubSubOptions(env, options));
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -856,12 +876,11 @@ Java_edu_wpi_first_networktables_NetworkTablesJNI_getTopicFromHandle
|
||||
/*
|
||||
* Class: edu_wpi_first_networktables_NetworkTablesJNI
|
||||
* Method: subscribeMultiple
|
||||
* Signature: (I[Ljava/lang/Object;[I[I)I
|
||||
* Signature: (I[Ljava/lang/Object;Ljava/lang/Object;)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_edu_wpi_first_networktables_NetworkTablesJNI_subscribeMultiple
|
||||
(JNIEnv* env, jclass, jint inst, jobjectArray prefixes, jintArray optionTypes,
|
||||
jintArray optionValues)
|
||||
(JNIEnv* env, jclass, jint inst, jobjectArray prefixes, jobject options)
|
||||
{
|
||||
if (!prefixes) {
|
||||
nullPointerEx.Throw(env, "prefixes cannot be null");
|
||||
@@ -884,10 +903,8 @@ Java_edu_wpi_first_networktables_NetworkTablesJNI_subscribeMultiple
|
||||
prefixStringViews.emplace_back(prefixStrings.back());
|
||||
}
|
||||
|
||||
wpi::SmallVector<nt::PubSubOption, 4> options;
|
||||
return nt::SubscribeMultiple(
|
||||
inst, prefixStringViews,
|
||||
FromJavaPubSubOptions(env, optionTypes, optionValues, options));
|
||||
return nt::SubscribeMultiple(inst, prefixStringViews,
|
||||
FromJavaPubSubOptions(env, options));
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -38,7 +38,7 @@ namespace {
|
||||
|
||||
struct PublisherData {
|
||||
NT_Publisher handle;
|
||||
PubSubOptions options;
|
||||
PubSubOptionsImpl options;
|
||||
// in options as double, but copy here as integer; rounded to the nearest
|
||||
// 10 ms
|
||||
uint32_t periodMs;
|
||||
@@ -67,7 +67,7 @@ class CImpl : public ServerMessageHandler {
|
||||
|
||||
void Publish(NT_Publisher pubHandle, NT_Topic topicHandle,
|
||||
std::string_view name, std::string_view typeStr,
|
||||
const wpi::json& properties, const PubSubOptions& options);
|
||||
const wpi::json& properties, const PubSubOptionsImpl& options);
|
||||
bool Unpublish(NT_Publisher pubHandle, NT_Topic topicHandle);
|
||||
void SetValue(NT_Publisher pubHandle, const Value& value);
|
||||
|
||||
@@ -282,7 +282,8 @@ bool CImpl::CheckNetworkReady() {
|
||||
|
||||
void CImpl::Publish(NT_Publisher pubHandle, NT_Topic topicHandle,
|
||||
std::string_view name, std::string_view typeStr,
|
||||
const wpi::json& properties, const PubSubOptions& options) {
|
||||
const wpi::json& properties,
|
||||
const PubSubOptionsImpl& options) {
|
||||
unsigned int index = Handle{pubHandle}.GetIndex();
|
||||
if (index >= m_publishers.size()) {
|
||||
m_publishers.resize(index + 1);
|
||||
@@ -445,7 +446,7 @@ ClientStartup::~ClientStartup() {
|
||||
void ClientStartup::Publish(NT_Publisher pubHandle, NT_Topic topicHandle,
|
||||
std::string_view name, std::string_view typeStr,
|
||||
const wpi::json& properties,
|
||||
const PubSubOptions& options) {
|
||||
const PubSubOptionsImpl& options) {
|
||||
WPI_DEBUG4(m_client.m_impl->m_logger, "StartupPublish({}, {}, {}, {})",
|
||||
pubHandle, topicHandle, name, typeStr);
|
||||
m_client.m_impl->Publish(pubHandle, topicHandle, name, typeStr, properties,
|
||||
@@ -456,7 +457,7 @@ void ClientStartup::Publish(NT_Publisher pubHandle, NT_Topic topicHandle,
|
||||
|
||||
void ClientStartup::Subscribe(NT_Subscriber subHandle,
|
||||
std::span<const std::string> prefixes,
|
||||
const PubSubOptions& options) {
|
||||
const PubSubOptionsImpl& options) {
|
||||
WPI_DEBUG4(m_client.m_impl->m_logger, "StartupSubscribe({})", subHandle);
|
||||
WireEncodeSubscribe(m_textWriter.Add(), Handle{subHandle}.GetIndex(),
|
||||
prefixes, options);
|
||||
|
||||
@@ -21,7 +21,7 @@ class Logger;
|
||||
} // namespace wpi
|
||||
|
||||
namespace nt {
|
||||
class PubSubOptions;
|
||||
class PubSubOptionsImpl;
|
||||
class Value;
|
||||
} // namespace nt
|
||||
|
||||
@@ -64,9 +64,10 @@ class ClientStartup final : public NetworkStartupInterface {
|
||||
// NetworkStartupInterface interface
|
||||
void Publish(NT_Publisher pubHandle, NT_Topic topicHandle,
|
||||
std::string_view name, std::string_view typeStr,
|
||||
const wpi::json& properties, const PubSubOptions& options) final;
|
||||
const wpi::json& properties,
|
||||
const PubSubOptionsImpl& options) final;
|
||||
void Subscribe(NT_Subscriber subHandle, std::span<const std::string> prefixes,
|
||||
const PubSubOptions& options) final;
|
||||
const PubSubOptionsImpl& options) final;
|
||||
void SetValue(NT_Publisher pubHandle, const Value& value) final;
|
||||
|
||||
private:
|
||||
|
||||
@@ -24,7 +24,7 @@ struct PublishMsg {
|
||||
std::string name;
|
||||
std::string typeStr;
|
||||
wpi::json properties;
|
||||
PubSubOptions options; // will be empty when coming from network
|
||||
PubSubOptionsImpl options; // will be empty when coming from network
|
||||
};
|
||||
|
||||
struct UnpublishMsg {
|
||||
@@ -44,7 +44,7 @@ struct SubscribeMsg {
|
||||
static constexpr std::string_view kMethodStr = "subscribe";
|
||||
NT_Subscriber subHandle{0};
|
||||
std::vector<std::string> topicNames;
|
||||
PubSubOptions options;
|
||||
PubSubOptionsImpl options;
|
||||
};
|
||||
|
||||
struct UnsubscribeMsg {
|
||||
|
||||
@@ -15,7 +15,7 @@ class json;
|
||||
} // namespace wpi
|
||||
|
||||
namespace nt {
|
||||
class PubSubOptions;
|
||||
class PubSubOptionsImpl;
|
||||
class Value;
|
||||
} // namespace nt
|
||||
|
||||
@@ -42,10 +42,10 @@ class NetworkStartupInterface {
|
||||
virtual void Publish(NT_Publisher pubHandle, NT_Topic topicHandle,
|
||||
std::string_view name, std::string_view typeStr,
|
||||
const wpi::json& properties,
|
||||
const PubSubOptions& options) = 0;
|
||||
const PubSubOptionsImpl& options) = 0;
|
||||
virtual void Subscribe(NT_Subscriber subHandle,
|
||||
std::span<const std::string> topicNames,
|
||||
const PubSubOptions& options) = 0;
|
||||
const PubSubOptionsImpl& options) = 0;
|
||||
virtual void SetValue(NT_Publisher pubHandle, const Value& value) = 0;
|
||||
};
|
||||
|
||||
|
||||
@@ -33,13 +33,14 @@ class NetworkLoopQueue : public NetworkInterface {
|
||||
// NetworkInterface - calls to these append to the queue
|
||||
void Publish(NT_Publisher pubHandle, NT_Topic topicHandle,
|
||||
std::string_view name, std::string_view typeStr,
|
||||
const wpi::json& properties, const PubSubOptions& options) final;
|
||||
const wpi::json& properties,
|
||||
const PubSubOptionsImpl& options) final;
|
||||
void Unpublish(NT_Publisher pubHandle, NT_Topic topicHandle) final;
|
||||
void SetProperties(NT_Topic topicHandle, std::string_view name,
|
||||
const wpi::json& update) final;
|
||||
void Subscribe(NT_Subscriber subHandle,
|
||||
std::span<const std::string> topicNames,
|
||||
const PubSubOptions& options) final;
|
||||
const PubSubOptionsImpl& options) final;
|
||||
void Unsubscribe(NT_Subscriber subHandle) final;
|
||||
void SetValue(NT_Publisher pubHandle, const Value& value) final;
|
||||
|
||||
|
||||
@@ -34,7 +34,7 @@ inline void NetworkLoopQueue::Publish(NT_Publisher pubHandle,
|
||||
std::string_view name,
|
||||
std::string_view typeStr,
|
||||
const wpi::json& properties,
|
||||
const PubSubOptions& options) {
|
||||
const PubSubOptionsImpl& options) {
|
||||
std::scoped_lock lock{m_mutex};
|
||||
m_queue.emplace_back(
|
||||
ClientMessage{PublishMsg{pubHandle, topicHandle, std::string{name},
|
||||
@@ -57,7 +57,7 @@ inline void NetworkLoopQueue::SetProperties(NT_Topic topicHandle,
|
||||
|
||||
inline void NetworkLoopQueue::Subscribe(NT_Subscriber subHandle,
|
||||
std::span<const std::string> topicNames,
|
||||
const PubSubOptions& options) {
|
||||
const PubSubOptionsImpl& options) {
|
||||
std::scoped_lock lock{m_mutex};
|
||||
m_queue.emplace_back(ClientMessage{SubscribeMsg{
|
||||
subHandle, {topicNames.begin(), topicNames.end()}, options}});
|
||||
|
||||
@@ -159,7 +159,7 @@ class ClientData4Base : public ClientData, protected ClientMessageHandler {
|
||||
void ClientSetProperties(std::string_view name,
|
||||
const wpi::json& update) final;
|
||||
void ClientSubscribe(int64_t subuid, std::span<const std::string> topicNames,
|
||||
const PubSubOptions& options) final;
|
||||
const PubSubOptionsImpl& options) final;
|
||||
void ClientUnsubscribe(int64_t subuid) final;
|
||||
|
||||
void ClientSetValue(int64_t pubuid, const Value& value);
|
||||
@@ -362,7 +362,7 @@ struct PublisherData {
|
||||
|
||||
struct SubscriberData {
|
||||
SubscriberData(ClientData* client, std::span<const std::string> topicNames,
|
||||
int64_t subuid, const PubSubOptions& options)
|
||||
int64_t subuid, const PubSubOptionsImpl& options)
|
||||
: client{client},
|
||||
topicNames{topicNames.begin(), topicNames.end()},
|
||||
subuid{subuid},
|
||||
@@ -374,7 +374,7 @@ struct SubscriberData {
|
||||
}
|
||||
|
||||
void Update(std::span<const std::string> topicNames_,
|
||||
const PubSubOptions& options_) {
|
||||
const PubSubOptionsImpl& options_) {
|
||||
topicNames = {topicNames_.begin(), topicNames_.end()};
|
||||
options = options_;
|
||||
periodMs = std::lround(options_.periodicMs / 10.0) * 10;
|
||||
@@ -388,7 +388,7 @@ struct SubscriberData {
|
||||
ClientData* client;
|
||||
std::vector<std::string> topicNames;
|
||||
int64_t subuid;
|
||||
PubSubOptions options;
|
||||
PubSubOptionsImpl options;
|
||||
// in options as double, but copy here as integer; rounded to the nearest
|
||||
// 10 ms
|
||||
uint32_t periodMs;
|
||||
@@ -463,10 +463,11 @@ struct Writer : public mpack_writer_t {
|
||||
};
|
||||
} // namespace
|
||||
|
||||
static void WriteOptions(mpack_writer_t& w, const PubSubOptions& options) {
|
||||
int size = (options.sendAll ? 1 : 0) + (options.topicsOnly ? 1 : 0) +
|
||||
(options.periodicMs != PubSubOptions::kDefaultPeriodicMs ? 1 : 0) +
|
||||
(options.prefixMatch ? 1 : 0);
|
||||
static void WriteOptions(mpack_writer_t& w, const PubSubOptionsImpl& options) {
|
||||
int size =
|
||||
(options.sendAll ? 1 : 0) + (options.topicsOnly ? 1 : 0) +
|
||||
(options.periodicMs != PubSubOptionsImpl::kDefaultPeriodicMs ? 1 : 0) +
|
||||
(options.prefixMatch ? 1 : 0);
|
||||
mpack_start_map(&w, size);
|
||||
if (options.sendAll) {
|
||||
mpack_write_str(&w, "all");
|
||||
@@ -476,7 +477,7 @@ static void WriteOptions(mpack_writer_t& w, const PubSubOptions& options) {
|
||||
mpack_write_str(&w, "topicsonly");
|
||||
mpack_write_bool(&w, true);
|
||||
}
|
||||
if (options.periodicMs != PubSubOptions::kDefaultPeriodicMs) {
|
||||
if (options.periodicMs != PubSubOptionsImpl::kDefaultPeriodicMs) {
|
||||
mpack_write_str(&w, "periodic");
|
||||
mpack_write_float(&w, options.periodicMs / 1000.0);
|
||||
}
|
||||
@@ -616,7 +617,7 @@ void ClientData4Base::ClientSetProperties(std::string_view name,
|
||||
|
||||
void ClientData4Base::ClientSubscribe(int64_t subuid,
|
||||
std::span<const std::string> topicNames,
|
||||
const PubSubOptions& options) {
|
||||
const PubSubOptionsImpl& options) {
|
||||
DEBUG4("ClientSubscribe({}, ({}), {})", m_id, fmt::join(topicNames, ","),
|
||||
subuid);
|
||||
auto& sub = m_subscribers[subuid];
|
||||
@@ -2365,14 +2366,14 @@ std::string ServerImpl::LoadPersistent(std::string_view in) {
|
||||
void ServerStartup::Publish(NT_Publisher pubHandle, NT_Topic topicHandle,
|
||||
std::string_view name, std::string_view typeStr,
|
||||
const wpi::json& properties,
|
||||
const PubSubOptions& options) {
|
||||
const PubSubOptionsImpl& options) {
|
||||
m_server.m_impl->m_localClient->ClientPublish(pubHandle, name, typeStr,
|
||||
properties);
|
||||
}
|
||||
|
||||
void ServerStartup::Subscribe(NT_Subscriber subHandle,
|
||||
std::span<const std::string> topicNames,
|
||||
const PubSubOptions& options) {
|
||||
const PubSubOptionsImpl& options) {
|
||||
m_server.m_impl->m_localClient->ClientSubscribe(subHandle, topicNames,
|
||||
options);
|
||||
}
|
||||
|
||||
@@ -83,10 +83,11 @@ class ServerStartup final : public NetworkStartupInterface {
|
||||
// NetworkStartupInterface interface
|
||||
void Publish(NT_Publisher pubHandle, NT_Topic topicHandle,
|
||||
std::string_view name, std::string_view typeStr,
|
||||
const wpi::json& properties, const PubSubOptions& options) final;
|
||||
const wpi::json& properties,
|
||||
const PubSubOptionsImpl& options) final;
|
||||
void Subscribe(NT_Subscriber subHandle,
|
||||
std::span<const std::string> topicNames,
|
||||
const PubSubOptions& options) final;
|
||||
const PubSubOptionsImpl& options) final;
|
||||
void SetValue(NT_Publisher pubHandle, const Value& value) final;
|
||||
|
||||
private:
|
||||
|
||||
@@ -226,7 +226,7 @@ static void WireDecodeTextImpl(std::string_view in, T& out,
|
||||
}
|
||||
|
||||
// options
|
||||
PubSubOptions options;
|
||||
PubSubOptionsImpl options;
|
||||
auto optionsIt = params->find("options");
|
||||
if (optionsIt != params->end()) {
|
||||
auto joptions = optionsIt->second.get_ptr<wpi::json::object_t*>();
|
||||
@@ -243,6 +243,7 @@ static void WireDecodeTextImpl(std::string_view in, T& out,
|
||||
error = "periodic value must be a number";
|
||||
goto err;
|
||||
}
|
||||
options.periodic = val;
|
||||
options.periodicMs = val * 1000;
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ class json;
|
||||
} // namespace wpi
|
||||
|
||||
namespace nt {
|
||||
class PubSubOptions;
|
||||
class PubSubOptionsImpl;
|
||||
class Value;
|
||||
} // namespace nt
|
||||
|
||||
@@ -35,7 +35,7 @@ class ClientMessageHandler {
|
||||
const wpi::json& update) = 0;
|
||||
virtual void ClientSubscribe(int64_t subuid,
|
||||
std::span<const std::string> topicNames,
|
||||
const PubSubOptions& options) = 0;
|
||||
const PubSubOptionsImpl& options) = 0;
|
||||
virtual void ClientUnsubscribe(int64_t subuid) = 0;
|
||||
};
|
||||
|
||||
|
||||
@@ -76,7 +76,7 @@ static void EncodePrefixes(wpi::raw_ostream& os, std::span<const T> topicNames,
|
||||
template <typename T>
|
||||
static void WireEncodeSubscribeImpl(wpi::raw_ostream& os, int64_t subuid,
|
||||
std::span<const T> topicNames,
|
||||
const PubSubOptions& options) {
|
||||
const PubSubOptionsImpl& options) {
|
||||
wpi::json::serializer s{os, ' ', 0};
|
||||
os << "{\"method\":\"" << SubscribeMsg::kMethodStr << "\",\"params\":{";
|
||||
os << "\"options\":{";
|
||||
@@ -99,7 +99,7 @@ static void WireEncodeSubscribeImpl(wpi::raw_ostream& os, int64_t subuid,
|
||||
os << "\"prefix\":true";
|
||||
first = false;
|
||||
}
|
||||
if (options.periodicMs != PubSubOptions::kDefaultPeriodicMs) {
|
||||
if (options.periodicMs != PubSubOptionsImpl::kDefaultPeriodicMs) {
|
||||
if (!first) {
|
||||
os << ',';
|
||||
}
|
||||
@@ -115,13 +115,13 @@ static void WireEncodeSubscribeImpl(wpi::raw_ostream& os, int64_t subuid,
|
||||
|
||||
void nt::net::WireEncodeSubscribe(wpi::raw_ostream& os, int64_t subuid,
|
||||
std::span<const std::string_view> topicNames,
|
||||
const PubSubOptions& options) {
|
||||
const PubSubOptionsImpl& options) {
|
||||
WireEncodeSubscribeImpl(os, subuid, topicNames, options);
|
||||
}
|
||||
|
||||
void nt::net::WireEncodeSubscribe(wpi::raw_ostream& os, int64_t subuid,
|
||||
std::span<const std::string> topicNames,
|
||||
const PubSubOptions& options) {
|
||||
const PubSubOptionsImpl& options) {
|
||||
WireEncodeSubscribeImpl(os, subuid, topicNames, options);
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ class raw_ostream;
|
||||
} // namespace wpi
|
||||
|
||||
namespace nt {
|
||||
class PubSubOptions;
|
||||
class PubSubOptionsImpl;
|
||||
class Value;
|
||||
} // namespace nt
|
||||
|
||||
@@ -33,10 +33,10 @@ void WireEncodeSetProperties(wpi::raw_ostream& os, std::string_view name,
|
||||
const wpi::json& update);
|
||||
void WireEncodeSubscribe(wpi::raw_ostream& os, int64_t subuid,
|
||||
std::span<const std::string_view> topicNames,
|
||||
const PubSubOptions& options);
|
||||
const PubSubOptionsImpl& options);
|
||||
void WireEncodeSubscribe(wpi::raw_ostream& os, int64_t subuid,
|
||||
std::span<const std::string> topicNames,
|
||||
const PubSubOptions& options);
|
||||
const PubSubOptionsImpl& options);
|
||||
void WireEncodeUnsubscribe(wpi::raw_ostream& os, int64_t subuid);
|
||||
|
||||
// encoders for server text messages (avoids need to construct a Message struct)
|
||||
|
||||
@@ -44,7 +44,7 @@ struct PublisherData {
|
||||
|
||||
Entry* entry;
|
||||
NT_Publisher handle;
|
||||
PubSubOptions options;
|
||||
PubSubOptionsImpl options;
|
||||
// in options as double, but copy here as integer; rounded to the nearest
|
||||
// 10 ms
|
||||
uint32_t periodMs;
|
||||
@@ -98,7 +98,7 @@ class CImpl : public MessageHandler3 {
|
||||
// Outgoing handlers
|
||||
void Publish(NT_Publisher pubHandle, NT_Topic topicHandle,
|
||||
std::string_view name, std::string_view typeStr,
|
||||
const wpi::json& properties, const PubSubOptions& options);
|
||||
const wpi::json& properties, const PubSubOptionsImpl& options);
|
||||
void Unpublish(NT_Publisher pubHandle, NT_Topic topicHandle);
|
||||
void SetProperties(NT_Topic topicHandle, std::string_view name,
|
||||
const wpi::json& update);
|
||||
@@ -315,7 +315,8 @@ bool CImpl::CheckNetworkReady() {
|
||||
|
||||
void CImpl::Publish(NT_Publisher pubHandle, NT_Topic topicHandle,
|
||||
std::string_view name, std::string_view typeStr,
|
||||
const wpi::json& properties, const PubSubOptions& options) {
|
||||
const wpi::json& properties,
|
||||
const PubSubOptionsImpl& options) {
|
||||
DEBUG4("Publish('{}', '{}')", name, typeStr);
|
||||
unsigned int index = Handle{pubHandle}.GetIndex();
|
||||
if (index >= m_publishers.size()) {
|
||||
@@ -647,7 +648,7 @@ ClientStartup3::~ClientStartup3() = default;
|
||||
void ClientStartup3::Publish(NT_Publisher pubHandle, NT_Topic topicHandle,
|
||||
std::string_view name, std::string_view typeStr,
|
||||
const wpi::json& properties,
|
||||
const PubSubOptions& options) {
|
||||
const PubSubOptionsImpl& options) {
|
||||
WPI_DEBUG4(m_client.m_impl->m_logger, "StartupPublish({}, {}, {}, {})",
|
||||
pubHandle, topicHandle, name, typeStr);
|
||||
m_client.m_impl->Publish(pubHandle, topicHandle, name, typeStr, properties,
|
||||
@@ -656,7 +657,7 @@ void ClientStartup3::Publish(NT_Publisher pubHandle, NT_Topic topicHandle,
|
||||
|
||||
void ClientStartup3::Subscribe(NT_Subscriber subHandle,
|
||||
std::span<const std::string> prefixes,
|
||||
const PubSubOptions& options) {
|
||||
const PubSubOptionsImpl& options) {
|
||||
// NT3 ignores subscribes, so no action required
|
||||
}
|
||||
|
||||
|
||||
@@ -60,9 +60,10 @@ class ClientStartup3 final : public net::NetworkStartupInterface {
|
||||
// NetworkStartupInterface interface
|
||||
void Publish(NT_Publisher pubHandle, NT_Topic topicHandle,
|
||||
std::string_view name, std::string_view typeStr,
|
||||
const wpi::json& properties, const PubSubOptions& options) final;
|
||||
const wpi::json& properties,
|
||||
const PubSubOptionsImpl& options) final;
|
||||
void Subscribe(NT_Subscriber subHandle, std::span<const std::string> prefixes,
|
||||
const PubSubOptions& options) final;
|
||||
const PubSubOptionsImpl& options) final;
|
||||
void SetValue(NT_Publisher pubHandle, const Value& value) final;
|
||||
|
||||
private:
|
||||
|
||||
@@ -22,37 +22,36 @@ wpi::json Topic::GetProperties() const {
|
||||
return ::nt::GetTopicProperties(m_handle);
|
||||
}
|
||||
|
||||
GenericSubscriber Topic::GenericSubscribe(
|
||||
std::span<const PubSubOption> options) {
|
||||
GenericSubscriber Topic::GenericSubscribe(const PubSubOptions& options) {
|
||||
return GenericSubscribe("", options);
|
||||
}
|
||||
|
||||
GenericSubscriber Topic::GenericSubscribe(
|
||||
std::string_view typeString, std::span<const PubSubOption> options) {
|
||||
GenericSubscriber Topic::GenericSubscribe(std::string_view typeString,
|
||||
const PubSubOptions& options) {
|
||||
return GenericSubscriber{::nt::Subscribe(
|
||||
m_handle, ::nt::GetTypeFromString(typeString), typeString, options)};
|
||||
}
|
||||
|
||||
GenericPublisher Topic::GenericPublish(std::string_view typeString,
|
||||
std::span<const PubSubOption> options) {
|
||||
const PubSubOptions& options) {
|
||||
return GenericPublisher{::nt::Publish(
|
||||
m_handle, ::nt::GetTypeFromString(typeString), typeString, options)};
|
||||
}
|
||||
|
||||
GenericPublisher Topic::GenericPublishEx(
|
||||
std::string_view typeString, const wpi::json& properties,
|
||||
std::span<const PubSubOption> options) {
|
||||
GenericPublisher Topic::GenericPublishEx(std::string_view typeString,
|
||||
const wpi::json& properties,
|
||||
const PubSubOptions& options) {
|
||||
return GenericPublisher{::nt::PublishEx(m_handle,
|
||||
::nt::GetTypeFromString(typeString),
|
||||
typeString, properties, options)};
|
||||
}
|
||||
|
||||
GenericEntry Topic::GetGenericEntry(std::span<const PubSubOption> options) {
|
||||
GenericEntry Topic::GetGenericEntry(const PubSubOptions& options) {
|
||||
return GetGenericEntry("", options);
|
||||
}
|
||||
|
||||
GenericEntry Topic::GetGenericEntry(std::string_view typeString,
|
||||
std::span<const PubSubOption> options) {
|
||||
const PubSubOptions& options) {
|
||||
return GenericEntry{::nt::GetEntry(
|
||||
m_handle, ::nt::GetTypeFromString(typeString), typeString, options)};
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
#include "ntcore_c.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <cassert>
|
||||
@@ -101,6 +103,21 @@ static void DisposeEvent(NT_Event* event) {
|
||||
}
|
||||
}
|
||||
|
||||
static PubSubOptions ConvertToCpp(const NT_PubSubOptions* in) {
|
||||
PubSubOptions out;
|
||||
out.pollStorage = in->pollStorage;
|
||||
out.periodic = in->periodic;
|
||||
out.excludePublisher = in->excludePublisher;
|
||||
out.sendAll = in->sendAll;
|
||||
out.topicsOnly = in->topicsOnly;
|
||||
out.prefixMatch = in->prefixMatch;
|
||||
out.keepDuplicates = in->keepDuplicates;
|
||||
out.disableRemote = in->disableRemote;
|
||||
out.disableLocal = in->disableLocal;
|
||||
out.excludeSelf = in->excludeSelf;
|
||||
return out;
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
|
||||
/*
|
||||
@@ -314,14 +331,8 @@ NT_Bool NT_SetTopicProperties(NT_Topic topic, const char* properties) {
|
||||
}
|
||||
|
||||
NT_Subscriber NT_Subscribe(NT_Topic topic, NT_Type type, const char* typeStr,
|
||||
const struct NT_PubSubOption* options,
|
||||
size_t options_len) {
|
||||
wpi::SmallVector<nt::PubSubOption> o;
|
||||
o.reserve(options_len);
|
||||
for (size_t i = 0; i < options_len; ++i) {
|
||||
o.emplace_back(options[i].type, options[i].value);
|
||||
}
|
||||
return nt::Subscribe(topic, type, typeStr, o);
|
||||
const struct NT_PubSubOptions* options) {
|
||||
return nt::Subscribe(topic, type, typeStr, ConvertToCpp(options));
|
||||
}
|
||||
|
||||
void NT_Unsubscribe(NT_Subscriber sub) {
|
||||
@@ -329,20 +340,13 @@ void NT_Unsubscribe(NT_Subscriber sub) {
|
||||
}
|
||||
|
||||
NT_Publisher NT_Publish(NT_Topic topic, NT_Type type, const char* typeStr,
|
||||
const struct NT_PubSubOption* options,
|
||||
size_t options_len) {
|
||||
wpi::SmallVector<nt::PubSubOption> o;
|
||||
o.reserve(options_len);
|
||||
for (size_t i = 0; i < options_len; ++i) {
|
||||
o.emplace_back(options[i].type, options[i].value);
|
||||
}
|
||||
return nt::Publish(topic, type, typeStr, o);
|
||||
const struct NT_PubSubOptions* options) {
|
||||
return nt::Publish(topic, type, typeStr, ConvertToCpp(options));
|
||||
}
|
||||
|
||||
NT_Publisher NT_PublishEx(NT_Topic topic, NT_Type type, const char* typeStr,
|
||||
const char* properties,
|
||||
const struct NT_PubSubOption* options,
|
||||
size_t options_len) {
|
||||
const struct NT_PubSubOptions* options) {
|
||||
wpi::json j;
|
||||
if (properties[0] == '\0') {
|
||||
// gracefully handle empty string
|
||||
@@ -355,13 +359,7 @@ NT_Publisher NT_PublishEx(NT_Topic topic, NT_Type type, const char* typeStr,
|
||||
}
|
||||
}
|
||||
|
||||
wpi::SmallVector<nt::PubSubOption> o;
|
||||
o.reserve(options_len);
|
||||
for (size_t i = 0; i < options_len; ++i) {
|
||||
o.emplace_back(options[i].type, options[i].value);
|
||||
}
|
||||
|
||||
return nt::PublishEx(topic, type, typeStr, j, o);
|
||||
return nt::PublishEx(topic, type, typeStr, j, ConvertToCpp(options));
|
||||
}
|
||||
|
||||
void NT_Unpublish(NT_Handle pubentry) {
|
||||
@@ -369,14 +367,8 @@ void NT_Unpublish(NT_Handle pubentry) {
|
||||
}
|
||||
|
||||
NT_Entry NT_GetEntryEx(NT_Topic topic, NT_Type type, const char* typeStr,
|
||||
const struct NT_PubSubOption* options,
|
||||
size_t options_len) {
|
||||
wpi::SmallVector<nt::PubSubOption> o;
|
||||
o.reserve(options_len);
|
||||
for (size_t i = 0; i < options_len; ++i) {
|
||||
o.emplace_back(options[i].type, options[i].value);
|
||||
}
|
||||
return nt::GetEntry(topic, type, typeStr, o);
|
||||
const struct NT_PubSubOptions* options) {
|
||||
return nt::GetEntry(topic, type, typeStr, ConvertToCpp(options));
|
||||
}
|
||||
|
||||
void NT_ReleaseEntry(NT_Entry entry) {
|
||||
|
||||
@@ -310,7 +310,7 @@ bool SetTopicProperties(NT_Topic topic, const wpi::json& properties) {
|
||||
}
|
||||
|
||||
NT_Subscriber Subscribe(NT_Topic topic, NT_Type type, std::string_view typeStr,
|
||||
std::span<const PubSubOption> options) {
|
||||
const PubSubOptions& options) {
|
||||
if (auto ii = InstanceImpl::GetTyped(topic, Handle::kTopic)) {
|
||||
return ii->localStorage.Subscribe(topic, type, typeStr, options);
|
||||
} else {
|
||||
@@ -325,13 +325,13 @@ void Unsubscribe(NT_Subscriber sub) {
|
||||
}
|
||||
|
||||
NT_Publisher Publish(NT_Topic topic, NT_Type type, std::string_view typeStr,
|
||||
std::span<const PubSubOption> options) {
|
||||
const PubSubOptions& options) {
|
||||
return PublishEx(topic, type, typeStr, wpi::json::object(), options);
|
||||
}
|
||||
|
||||
NT_Publisher PublishEx(NT_Topic topic, NT_Type type, std::string_view typeStr,
|
||||
const wpi::json& properties,
|
||||
std::span<const PubSubOption> options) {
|
||||
const PubSubOptions& options) {
|
||||
if (auto ii = InstanceImpl::GetTyped(topic, Handle::kTopic)) {
|
||||
return ii->localStorage.Publish(topic, type, typeStr, properties, options);
|
||||
} else {
|
||||
@@ -346,7 +346,7 @@ void Unpublish(NT_Handle pubentry) {
|
||||
}
|
||||
|
||||
NT_Entry GetEntry(NT_Topic topic, NT_Type type, std::string_view typeStr,
|
||||
std::span<const PubSubOption> options) {
|
||||
const PubSubOptions& options) {
|
||||
if (auto ii = InstanceImpl::GetTyped(topic, Handle::kTopic)) {
|
||||
return ii->localStorage.GetEntry(topic, type, typeStr, options);
|
||||
} else {
|
||||
@@ -376,7 +376,7 @@ NT_Topic GetTopicFromHandle(NT_Handle pubsubentry) {
|
||||
|
||||
NT_MultiSubscriber SubscribeMultiple(NT_Inst inst,
|
||||
std::span<const std::string_view> prefixes,
|
||||
std::span<const PubSubOption> options) {
|
||||
const PubSubOptions& options) {
|
||||
if (auto ii = InstanceImpl::GetTyped(inst, Handle::kInstance)) {
|
||||
return ii->localStorage.SubscribeMultiple(prefixes, options);
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user