mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-23 01:21:42 +00:00
[hal] WS Simulation: Add message filtering capability (#5395)
This commit is contained in:
@@ -76,6 +76,16 @@ void HALSimHttpConnection::ProcessWsUpgrade() {
|
||||
}
|
||||
|
||||
void HALSimHttpConnection::OnSimValueChanged(const wpi::json& msg) {
|
||||
// Skip sending if this message is not in the allowed filter list
|
||||
try {
|
||||
auto& type = msg.at("type").get_ref<const std::string&>();
|
||||
if (!m_server->CanSendMessage(type)) {
|
||||
return;
|
||||
}
|
||||
} catch (wpi::json::exception& e) {
|
||||
fmt::print(stderr, "Error with message: {}\n", e.what());
|
||||
}
|
||||
|
||||
// render json to buffers
|
||||
wpi::SmallVector<uv::Buffer, 4> sendBufs;
|
||||
wpi::raw_uv_ostream os{sendBufs, [this]() -> uv::Buffer {
|
||||
|
||||
@@ -77,6 +77,22 @@ bool HALSimWeb::Initialize() {
|
||||
m_port = 3300;
|
||||
}
|
||||
|
||||
const char* msgFilters = std::getenv("HALSIMWS_FILTERS");
|
||||
if (msgFilters != nullptr) {
|
||||
m_useMsgFiltering = true;
|
||||
|
||||
std::string_view filters(msgFilters);
|
||||
filters = wpi::trim(filters);
|
||||
wpi::SmallVector<std::string_view, 16> filtersSplit;
|
||||
|
||||
wpi::split(filters, filtersSplit, ',', -1, false);
|
||||
for (auto val : filtersSplit) {
|
||||
m_msgFilters[wpi::trim(val)] = true;
|
||||
}
|
||||
} else {
|
||||
m_useMsgFiltering = false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -100,6 +116,16 @@ void HALSimWeb::Start() {
|
||||
m_server->Listen();
|
||||
fmt::print("Listening at http://localhost:{}\n", m_port);
|
||||
fmt::print("WebSocket URI: {}\n", m_uri);
|
||||
|
||||
// Print any filters we are using
|
||||
if (m_useMsgFiltering) {
|
||||
fmt::print("WS Message Filters:");
|
||||
for (auto filter : m_msgFilters.keys()) {
|
||||
fmt::print("* \"{}\"\n", filter);
|
||||
}
|
||||
} else {
|
||||
fmt::print("No WS Message Filters specified");
|
||||
}
|
||||
}
|
||||
|
||||
bool HALSimWeb::RegisterWebsocket(
|
||||
@@ -157,3 +183,10 @@ void HALSimWeb::OnNetValueChanged(const wpi::json& msg) {
|
||||
fmt::print(stderr, "Error with incoming message: {}\n", e.what());
|
||||
}
|
||||
}
|
||||
|
||||
bool HALSimWeb::CanSendMessage(std::string_view type) {
|
||||
if (!m_useMsgFiltering) {
|
||||
return true;
|
||||
}
|
||||
return m_msgFilters.count(type) > 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user