Compare commits

...

2 Commits

Author SHA1 Message Date
Peter Johnson
e6552d272e [ntcore] Remove table multi-subscriber (#4789)
This wasn't well thought out, and leaks horribly in Java.

Reverts part of #4640.
2022-12-09 21:25:29 -08:00
Ryan Blue
bde383f763 [hal] Replace const char* with std::string_view in Driver Station sim functions (#4532) 2022-12-09 13:10:23 -08:00
14 changed files with 65 additions and 62 deletions

View File

@@ -38,7 +38,7 @@ class FMSModel : public Model {
virtual void SetEnabled(bool val) = 0;
virtual void SetTest(bool val) = 0;
virtual void SetAutonomous(bool val) = 0;
virtual void SetGameSpecificMessage(const char* val) = 0;
virtual void SetGameSpecificMessage(std::string_view val) = 0;
};
/**

View File

@@ -47,7 +47,7 @@ class NTFMSModel : public FMSModel {
void SetEnabled(bool val) override {}
void SetTest(bool val) override {}
void SetAutonomous(bool val) override {}
void SetGameSpecificMessage(const char* val) override {}
void SetGameSpecificMessage(std::string_view val) override {}
void Update() override;
bool Exists() override;

View File

@@ -103,13 +103,13 @@ void HALSIM_SetJoystickIsXbox(int32_t stick, HAL_Bool isXbox) {}
void HALSIM_SetJoystickType(int32_t stick, int32_t type) {}
void HALSIM_SetJoystickName(int32_t stick, const char* name) {}
void HALSIM_SetJoystickName(int32_t stick, const char* name, size_t size) {}
void HALSIM_SetJoystickAxisType(int32_t stick, int32_t axis, int32_t type) {}
void HALSIM_SetGameSpecificMessage(const char* message) {}
void HALSIM_SetGameSpecificMessage(const char* message, size_t size) {}
void HALSIM_SetEventName(const char* name) {}
void HALSIM_SetEventName(const char* name, size_t size) {}
void HALSIM_SetMatchType(HAL_MatchType type) {}

View File

@@ -732,7 +732,8 @@ JNIEXPORT void JNICALL
Java_edu_wpi_first_hal_simulation_DriverStationDataJNI_setJoystickName
(JNIEnv* env, jclass, jint stick, jstring name)
{
HALSIM_SetJoystickName(stick, JStringRef{env, name}.c_str());
JStringRef nameJString{env, name};
HALSIM_SetJoystickName(stick, nameJString.c_str(), nameJString.size());
}
/*
@@ -756,7 +757,8 @@ JNIEXPORT void JNICALL
Java_edu_wpi_first_hal_simulation_DriverStationDataJNI_setGameSpecificMessage
(JNIEnv* env, jclass, jstring message)
{
HALSIM_SetGameSpecificMessage(JStringRef{env, message}.c_str());
JStringRef messageJString{env, message};
HALSIM_SetGameSpecificMessage(messageJString.c_str(), messageJString.size());
}
/*
@@ -768,7 +770,8 @@ JNIEXPORT void JNICALL
Java_edu_wpi_first_hal_simulation_DriverStationDataJNI_setEventName
(JNIEnv* env, jclass, jstring name)
{
HALSIM_SetEventName(JStringRef{env, name}.c_str());
JStringRef nameJString{env, name};
HALSIM_SetEventName(nameJString.c_str(), nameJString.size());
}
/*

View File

@@ -4,6 +4,8 @@
#pragma once
#include <cstddef>
#include "hal/DriverStationTypes.h"
#include "hal/Types.h"
#include "hal/simulation/NotifyListener.h"
@@ -145,11 +147,11 @@ void HALSIM_GetJoystickCounts(int32_t stick, int32_t* axisCount,
void HALSIM_SetJoystickIsXbox(int32_t stick, HAL_Bool isXbox);
void HALSIM_SetJoystickType(int32_t stick, int32_t type);
void HALSIM_SetJoystickName(int32_t stick, const char* name);
void HALSIM_SetJoystickName(int32_t stick, const char* name, size_t size);
void HALSIM_SetJoystickAxisType(int32_t stick, int32_t axis, int32_t type);
void HALSIM_SetGameSpecificMessage(const char* message);
void HALSIM_SetEventName(const char* name);
void HALSIM_SetGameSpecificMessage(const char* message, size_t size);
void HALSIM_SetEventName(const char* name, size_t size);
void HALSIM_SetMatchType(HAL_MatchType type);
void HALSIM_SetMatchNumber(int32_t matchNumber);
void HALSIM_SetReplayNumber(int32_t replayNumber);

View File

@@ -339,14 +339,17 @@ void DriverStationData::SetJoystickType(int32_t stick, int32_t type) {
m_joystickDescriptorCallbacks(stick, &m_joystickData[stick].descriptor);
}
void DriverStationData::SetJoystickName(int32_t stick, const char* name) {
void DriverStationData::SetJoystickName(int32_t stick, const char* name,
size_t size) {
if (stick < 0 || stick >= kNumJoysticks) {
return;
}
std::scoped_lock lock(m_joystickDataMutex);
std::strncpy(m_joystickData[stick].descriptor.name, name,
sizeof(m_joystickData[stick].descriptor.name) - 1);
*(std::end(m_joystickData[stick].descriptor.name) - 1) = '\0';
if (size > sizeof(m_joystickData[stick].descriptor.name) - 1) {
size = sizeof(m_joystickData[stick].descriptor.name) - 1;
}
std::strncpy(m_joystickData[stick].descriptor.name, name, size);
m_joystickData[stick].descriptor.name[size] = '\0';
m_joystickDescriptorCallbacks(stick, &m_joystickData[stick].descriptor);
}
@@ -363,19 +366,27 @@ void DriverStationData::SetJoystickAxisType(int32_t stick, int32_t axis,
m_joystickDescriptorCallbacks(stick, &m_joystickData[stick].descriptor);
}
void DriverStationData::SetGameSpecificMessage(const char* message) {
void DriverStationData::SetGameSpecificMessage(const char* message,
size_t size) {
std::scoped_lock lock(m_matchInfoMutex);
if (size > sizeof(m_matchInfo.gameSpecificMessage) - 1) {
size = sizeof(m_matchInfo.gameSpecificMessage) - 1;
}
std::strncpy(reinterpret_cast<char*>(m_matchInfo.gameSpecificMessage),
message, sizeof(m_matchInfo.gameSpecificMessage) - 1);
*(std::end(m_matchInfo.gameSpecificMessage) - 1) = '\0';
m_matchInfo.gameSpecificMessageSize = std::strlen(message);
message, size);
m_matchInfo.gameSpecificMessage[size] = '\0';
m_matchInfo.gameSpecificMessageSize =
std::strlen(reinterpret_cast<char*>(m_matchInfo.gameSpecificMessage));
m_matchInfoCallbacks(&m_matchInfo);
}
void DriverStationData::SetEventName(const char* name) {
void DriverStationData::SetEventName(const char* name, size_t size) {
std::scoped_lock lock(m_matchInfoMutex);
std::strncpy(m_matchInfo.eventName, name, sizeof(m_matchInfo.eventName) - 1);
*(std::end(m_matchInfo.eventName) - 1) = '\0';
if (size > sizeof(m_matchInfo.eventName) - 1) {
size = sizeof(m_matchInfo.eventName) - 1;
}
std::strncpy(m_matchInfo.eventName, name, size);
m_matchInfo.eventName[size] = '\0';
m_matchInfoCallbacks(&m_matchInfo);
}
@@ -540,20 +551,20 @@ void HALSIM_SetJoystickType(int32_t stick, int32_t type) {
SimDriverStationData->SetJoystickType(stick, type);
}
void HALSIM_SetJoystickName(int32_t stick, const char* name) {
SimDriverStationData->SetJoystickName(stick, name);
void HALSIM_SetJoystickName(int32_t stick, const char* name, size_t size) {
SimDriverStationData->SetJoystickName(stick, name, size);
}
void HALSIM_SetJoystickAxisType(int32_t stick, int32_t axis, int32_t type) {
SimDriverStationData->SetJoystickAxisType(stick, axis, type);
}
void HALSIM_SetGameSpecificMessage(const char* message) {
SimDriverStationData->SetGameSpecificMessage(message);
void HALSIM_SetGameSpecificMessage(const char* message, size_t size) {
SimDriverStationData->SetGameSpecificMessage(message, size);
}
void HALSIM_SetEventName(const char* name) {
SimDriverStationData->SetEventName(name);
void HALSIM_SetEventName(const char* name, size_t size) {
SimDriverStationData->SetEventName(name, size);
}
void HALSIM_SetMatchType(HAL_MatchType type) {

View File

@@ -107,11 +107,11 @@ class DriverStationData {
void SetJoystickIsXbox(int32_t stick, HAL_Bool isXbox);
void SetJoystickType(int32_t stick, int32_t type);
void SetJoystickName(int32_t stick, const char* name);
void SetJoystickName(int32_t stick, const char* name, size_t size);
void SetJoystickAxisType(int32_t stick, int32_t axis, int32_t type);
void SetGameSpecificMessage(const char* message);
void SetEventName(const char* name);
void SetGameSpecificMessage(const char* message, size_t size);
void SetEventName(const char* name, size_t size);
void SetMatchType(HAL_MatchType type);
void SetMatchNumber(int32_t matchNumber);
void SetReplayNumber(int32_t replayNumber);

View File

@@ -22,7 +22,6 @@ public final class NetworkTable {
private final String m_path;
private final String m_pathWithSep;
private final NetworkTableInstance m_inst;
private final MultiSubscriber m_topicSub;
/**
* Gets the "base name" of a key. For example, "/foo/bar" becomes "bar". If the key has a trailing
@@ -115,8 +114,6 @@ public final class NetworkTable {
m_path = path;
m_pathWithSep = path + PATH_SEPARATOR;
m_inst = inst;
m_topicSub =
new MultiSubscriber(inst, new String[] {m_pathWithSep}, PubSubOption.topicsOnly(true));
}
/**
@@ -533,7 +530,7 @@ public final class NetworkTable {
final NetworkTable parent = this;
return m_inst.addListener(
m_topicSub,
new String[] {m_pathWithSep},
EnumSet.of(NetworkTableEvent.Kind.kPublish, NetworkTableEvent.Kind.kImmediate),
new Consumer<NetworkTableEvent>() {
final Set<String> m_notifiedTables = new HashSet<>();
@@ -583,8 +580,4 @@ public final class NetworkTable {
public int hashCode() {
return Objects.hash(m_inst, m_path);
}
void close() {
m_topicSub.close();
}
}

View File

@@ -88,14 +88,9 @@ std::vector<std::string> NetworkTable::GetHierarchy(std::string_view key) {
NetworkTable::NetworkTable(NT_Inst inst, std::string_view path,
const private_init&)
: m_inst(inst),
m_path(path),
m_topicSub{::nt::SubscribeMultiple(inst, {{fmt::format("{}/", path)}},
{{PubSubOption::TopicsOnly(true)}})} {}
: m_inst(inst), m_path(path) {}
NetworkTable::~NetworkTable() {
::nt::UnsubscribeMultiple(m_topicSub);
}
NetworkTable::~NetworkTable() = default;
NetworkTableInstance NetworkTable::GetInstance() const {
return NetworkTableInstance{m_inst};
@@ -405,8 +400,8 @@ NT_Listener NetworkTable::AddSubTableListener(SubTableListener listener) {
// a shared_ptr to it.
auto notified_tables = std::make_shared<wpi::StringMap<char>>();
return ::nt::AddListener(
m_topicSub, NT_EVENT_PUBLISH | NT_EVENT_IMMEDIATE,
return NetworkTableInstance{m_inst}.AddListener(
{{fmt::format("{}/", m_path)}}, NT_EVENT_PUBLISH | NT_EVENT_IMMEDIATE,
[this, cb = std::move(listener), notified_tables](const Event& event) {
auto topicInfo = event.GetTopicInfo();
if (!topicInfo) {

View File

@@ -48,7 +48,6 @@ class NetworkTable final {
private:
NT_Inst m_inst;
std::string m_path;
NT_MultiSubscriber m_topicSub;
mutable wpi::mutex m_mutex;
mutable wpi::StringMap<NT_Entry> m_entries;

View File

@@ -254,8 +254,8 @@ class FMSSimModel : public glass::FMSModel {
void SetAutonomous(bool val) override {
HALSIM_SetDriverStationAutonomous(val);
}
void SetGameSpecificMessage(const char* val) override {
HALSIM_SetGameSpecificMessage(val);
void SetGameSpecificMessage(std::string_view val) override {
HALSIM_SetGameSpecificMessage(val.data(), val.size());
}
void Update() override;

View File

@@ -166,8 +166,8 @@ void HALSimWSProviderDriverStation::OnNetValueChanged(const wpi::json& json) {
HALSIM_SetDriverStationMatchTime(it.value());
}
if ((it = json.find(">game_data")) != json.end()) {
HALSIM_SetGameSpecificMessage(
it.value().get_ref<const std::string&>().c_str());
std::string message = it.value().get_ref<const std::string&>();
HALSIM_SetGameSpecificMessage(message.c_str(), message.length());
}
// Only notify usercode if we get the new data message

View File

@@ -234,20 +234,20 @@ void DriverStationSim::SetJoystickType(int stick, int type) {
HALSIM_SetJoystickType(stick, type);
}
void DriverStationSim::SetJoystickName(int stick, const char* name) {
HALSIM_SetJoystickName(stick, name);
void DriverStationSim::SetJoystickName(int stick, std::string_view name) {
HALSIM_SetJoystickName(stick, name.data(), name.size());
}
void DriverStationSim::SetJoystickAxisType(int stick, int axis, int type) {
HALSIM_SetJoystickAxisType(stick, axis, type);
}
void DriverStationSim::SetGameSpecificMessage(const char* message) {
HALSIM_SetGameSpecificMessage(message);
void DriverStationSim::SetGameSpecificMessage(std::string_view message) {
HALSIM_SetGameSpecificMessage(message.data(), message.size());
}
void DriverStationSim::SetEventName(const char* name) {
HALSIM_SetEventName(name);
void DriverStationSim::SetEventName(std::string_view name) {
HALSIM_SetEventName(name.data(), name.size());
}
void DriverStationSim::SetMatchType(DriverStation::MatchType type) {

View File

@@ -337,7 +337,7 @@ class DriverStationSim {
* @param stick The joystick number
* @param name The value of name
*/
static void SetJoystickName(int stick, const char* name);
static void SetJoystickName(int stick, std::string_view name);
/**
* Sets the types of Axes for a joystick.
@@ -353,14 +353,14 @@ class DriverStationSim {
*
* @param message the game specific message
*/
static void SetGameSpecificMessage(const char* message);
static void SetGameSpecificMessage(std::string_view message);
/**
* Sets the event name.
*
* @param name the event name
*/
static void SetEventName(const char* name);
static void SetEventName(std::string_view name);
/**
* Sets the match type.