mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-25 01:41:43 +00:00
SCRIPT namespace replacements
This commit is contained in:
committed by
Peter Johnson
parent
ae6c043632
commit
9aca8e0fd6
@@ -20,7 +20,7 @@
|
||||
#include "wpi/util/fs.hpp"
|
||||
#include "wpi/util/print.hpp"
|
||||
|
||||
namespace uv = wpi::uv;
|
||||
namespace uv = wpi::net::uv;
|
||||
|
||||
using namespace wpilibws;
|
||||
|
||||
@@ -54,10 +54,10 @@ void HALSimHttpConnection::ProcessWsUpgrade() {
|
||||
return;
|
||||
}
|
||||
|
||||
wpi::json j;
|
||||
wpi::util::json j;
|
||||
try {
|
||||
j = wpi::json::parse(msg);
|
||||
} catch (const wpi::json::parse_error& e) {
|
||||
j = wpi::util::json::parse(msg);
|
||||
} catch (const wpi::util::json::parse_error& e) {
|
||||
std::string err("JSON parse failed: ");
|
||||
err += e.what();
|
||||
m_websocket->Fail(400, err);
|
||||
@@ -77,20 +77,20 @@ void HALSimHttpConnection::ProcessWsUpgrade() {
|
||||
});
|
||||
}
|
||||
|
||||
void HALSimHttpConnection::OnSimValueChanged(const wpi::json& msg) {
|
||||
void HALSimHttpConnection::OnSimValueChanged(const wpi::util::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) {
|
||||
wpi::print(stderr, "Error with message: {}\n", e.what());
|
||||
} catch (wpi::util::json::exception& e) {
|
||||
wpi::util::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 {
|
||||
wpi::util::SmallVector<uv::Buffer, 4> sendBufs;
|
||||
wpi::net::raw_uv_ostream os{sendBufs, [this]() -> uv::Buffer {
|
||||
std::lock_guard lock(m_buffers_mutex);
|
||||
return m_buffers.Allocate();
|
||||
}};
|
||||
@@ -99,14 +99,14 @@ void HALSimHttpConnection::OnSimValueChanged(const wpi::json& msg) {
|
||||
// call the websocket send function on the uv loop
|
||||
m_server->GetExec().Send([self = shared_from_this(), sendBufs] {
|
||||
self->m_websocket->SendText(sendBufs,
|
||||
[self](auto bufs, wpi::uv::Error err) {
|
||||
[self](auto bufs, wpi::net::uv::Error err) {
|
||||
{
|
||||
std::lock_guard lock(self->m_buffers_mutex);
|
||||
self->m_buffers.Release(bufs);
|
||||
}
|
||||
|
||||
if (err) {
|
||||
wpi::print(stderr, "{}\n", err.str());
|
||||
wpi::util::print(stderr, "{}\n", err.str());
|
||||
std::fflush(stderr);
|
||||
}
|
||||
});
|
||||
@@ -127,22 +127,22 @@ void HALSimHttpConnection::SendFileResponse(int code, std::string_view codeText,
|
||||
}
|
||||
|
||||
// open file
|
||||
auto fileBuffer = wpi::MemoryBuffer::GetFile(filename);
|
||||
auto fileBuffer = wpi::util::MemoryBuffer::GetFile(filename);
|
||||
if (!fileBuffer) {
|
||||
MySendError(404, "error opening file");
|
||||
return;
|
||||
}
|
||||
|
||||
wpi::SmallVector<uv::Buffer, 4> toSend;
|
||||
wpi::raw_uv_ostream os{toSend, 4096};
|
||||
wpi::util::SmallVector<uv::Buffer, 4> toSend;
|
||||
wpi::net::raw_uv_ostream os{toSend, 4096};
|
||||
BuildHeader(os, code, codeText, contentType, size, extraHeader);
|
||||
SendData(os.bufs(), false);
|
||||
|
||||
Log(code);
|
||||
|
||||
// Read the file byte by byte
|
||||
wpi::SmallVector<uv::Buffer, 4> bodyData;
|
||||
wpi::raw_uv_ostream bodyOs{bodyData, 4096};
|
||||
wpi::util::SmallVector<uv::Buffer, 4> bodyData;
|
||||
wpi::net::raw_uv_ostream bodyOs{bodyData, 4096};
|
||||
|
||||
bodyOs << fileBuffer.value()->GetBuffer();
|
||||
|
||||
@@ -153,8 +153,8 @@ void HALSimHttpConnection::SendFileResponse(int code, std::string_view codeText,
|
||||
}
|
||||
|
||||
void HALSimHttpConnection::ProcessRequest() {
|
||||
wpi::UrlParser url{m_request.GetUrl(),
|
||||
m_request.GetMethod() == wpi::HTTP_CONNECT};
|
||||
wpi::net::UrlParser url{m_request.GetUrl(),
|
||||
m_request.GetMethod() == wpi::net::HTTP_CONNECT};
|
||||
if (!url.IsValid()) {
|
||||
// failed to parse URL
|
||||
MySendError(400, "Invalid URL");
|
||||
@@ -166,17 +166,17 @@ void HALSimHttpConnection::ProcessRequest() {
|
||||
path = url.GetPath();
|
||||
}
|
||||
|
||||
if (m_request.GetMethod() == wpi::HTTP_GET && wpi::starts_with(path, '/') &&
|
||||
!wpi::contains(path, "..") && !wpi::contains(path, "//")) {
|
||||
if (m_request.GetMethod() == wpi::net::HTTP_GET && wpi::util::starts_with(path, '/') &&
|
||||
!wpi::util::contains(path, "..") && !wpi::util::contains(path, "//")) {
|
||||
// convert to fs native representation
|
||||
fs::path nativePath;
|
||||
if (auto userPath = wpi::remove_prefix(path, "/user/")) {
|
||||
if (auto userPath = wpi::util::remove_prefix(path, "/user/")) {
|
||||
nativePath = fs::path{m_server->GetWebrootSys()} /
|
||||
fs::path{*userPath, fs::path::format::generic_format};
|
||||
} else {
|
||||
nativePath =
|
||||
fs::path{m_server->GetWebrootSys()} /
|
||||
fs::path{wpi::drop_front(path), fs::path::format::generic_format};
|
||||
fs::path{wpi::util::drop_front(path), fs::path::format::generic_format};
|
||||
}
|
||||
|
||||
if (fs::is_directory(nativePath)) {
|
||||
@@ -186,7 +186,7 @@ void HALSimHttpConnection::ProcessRequest() {
|
||||
if (!fs::exists(nativePath) || fs::is_directory(nativePath)) {
|
||||
MySendError(404, fmt::format("Resource '{}' not found", path));
|
||||
} else {
|
||||
auto contentType = wpi::MimeTypeFromPath(nativePath.string());
|
||||
auto contentType = wpi::net::MimeTypeFromPath(nativePath.string());
|
||||
SendFileResponse(200, "OK", contentType, nativePath.string());
|
||||
}
|
||||
} else {
|
||||
@@ -200,7 +200,7 @@ void HALSimHttpConnection::MySendError(int code, std::string_view message) {
|
||||
}
|
||||
|
||||
void HALSimHttpConnection::Log(int code) {
|
||||
auto method = wpi::http_method_str(m_request.GetMethod());
|
||||
wpi::print(stderr, "{} {} HTTP/{}.{} {}\n", method, m_request.GetUrl(),
|
||||
auto method = wpi::net::http_method_str(m_request.GetMethod());
|
||||
wpi::util::print(stderr, "{} {} HTTP/{}.{} {}\n", method, m_request.GetUrl(),
|
||||
m_request.GetMajor(), m_request.GetMinor(), code);
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ using namespace wpilibws;
|
||||
|
||||
bool HALSimWSServer::Initialize() {
|
||||
bool result = true;
|
||||
runner.ExecSync([&](wpi::uv::Loop& loop) {
|
||||
runner.ExecSync([&](wpi::net::uv::Loop& loop) {
|
||||
simWeb = std::make_shared<HALSimWeb>(loop, providers, simDevices);
|
||||
|
||||
if (!simWeb->Initialize()) {
|
||||
|
||||
@@ -17,17 +17,17 @@
|
||||
#include "wpi/util/fs.hpp"
|
||||
#include "wpi/util/print.hpp"
|
||||
|
||||
namespace uv = wpi::uv;
|
||||
namespace uv = wpi::net::uv;
|
||||
|
||||
using namespace wpilibws;
|
||||
|
||||
HALSimWeb::HALSimWeb(wpi::uv::Loop& loop, ProviderContainer& providers,
|
||||
HALSimWeb::HALSimWeb(wpi::net::uv::Loop& loop, ProviderContainer& providers,
|
||||
HALSimWSProviderSimDevices& simDevicesProvider)
|
||||
: m_loop(loop),
|
||||
m_providers(providers),
|
||||
m_simDevicesProvider(simDevicesProvider) {
|
||||
m_loop.error.connect([](uv::Error err) {
|
||||
wpi::print(stderr, "HALSim WS Server libuv ERROR: {}\n", err.str());
|
||||
wpi::util::print(stderr, "HALSim WS Server libuv ERROR: {}\n", err.str());
|
||||
});
|
||||
|
||||
m_server = uv::Tcp::Create(m_loop);
|
||||
@@ -72,7 +72,7 @@ bool HALSimWeb::Initialize() {
|
||||
try {
|
||||
m_port = std::stoi(port);
|
||||
} catch (const std::invalid_argument& err) {
|
||||
wpi::print(stderr, "Error decoding HALSIMWS_PORT ({})\n", err.what());
|
||||
wpi::util::print(stderr, "Error decoding HALSIMWS_PORT ({})\n", err.what());
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
@@ -82,8 +82,8 @@ bool HALSimWeb::Initialize() {
|
||||
const char* msgFilters = std::getenv("HALSIMWS_FILTERS");
|
||||
if (msgFilters != nullptr) {
|
||||
m_useMsgFiltering = true;
|
||||
wpi::split(wpi::trim(msgFilters), ',', -1, false,
|
||||
[&](auto val) { m_msgFilters[wpi::trim(val)] = true; });
|
||||
wpi::util::split(wpi::util::trim(msgFilters), ',', -1, false,
|
||||
[&](auto val) { m_msgFilters[wpi::util::trim(val)] = true; });
|
||||
} else {
|
||||
m_useMsgFiltering = false;
|
||||
}
|
||||
@@ -109,17 +109,17 @@ void HALSimWeb::Start() {
|
||||
|
||||
// start listening for incoming connections
|
||||
m_server->Listen();
|
||||
wpi::print("Listening at http://localhost:{}\n", m_port);
|
||||
wpi::print("WebSocket URI: {}\n", m_uri);
|
||||
wpi::util::print("Listening at http://localhost:{}\n", m_port);
|
||||
wpi::util::print("WebSocket URI: {}\n", m_uri);
|
||||
|
||||
// Print any filters we are using
|
||||
if (m_useMsgFiltering) {
|
||||
wpi::print("WS Message Filters:");
|
||||
wpi::util::print("WS Message Filters:");
|
||||
for (auto&& filter : m_msgFilters) {
|
||||
wpi::print("* \"{}\"\n", filter.first);
|
||||
wpi::util::print("* \"{}\"\n", filter.first);
|
||||
}
|
||||
} else {
|
||||
wpi::print("No WS Message Filters specified");
|
||||
wpi::util::print("No WS Message Filters specified");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -155,7 +155,7 @@ void HALSimWeb::CloseWebsocket(
|
||||
}
|
||||
}
|
||||
|
||||
void HALSimWeb::OnNetValueChanged(const wpi::json& msg) {
|
||||
void HALSimWeb::OnNetValueChanged(const wpi::util::json& msg) {
|
||||
// Look for "type" and "device" fields so that we can
|
||||
// generate the key
|
||||
|
||||
@@ -163,7 +163,7 @@ void HALSimWeb::OnNetValueChanged(const wpi::json& msg) {
|
||||
auto& type = msg.at("type").get_ref<const std::string&>();
|
||||
auto& device = msg.at("device").get_ref<const std::string&>();
|
||||
|
||||
wpi::SmallString<64> key;
|
||||
wpi::util::SmallString<64> key;
|
||||
key.append(type);
|
||||
if (!device.empty()) {
|
||||
key.append("/");
|
||||
@@ -174,8 +174,8 @@ void HALSimWeb::OnNetValueChanged(const wpi::json& msg) {
|
||||
if (provider) {
|
||||
provider->OnNetValueChanged(msg.at("data"));
|
||||
}
|
||||
} catch (wpi::json::exception& e) {
|
||||
wpi::print(stderr, "Error with incoming message: {}\n", e.what());
|
||||
} catch (wpi::util::json::exception& e) {
|
||||
wpi::util::print(stderr, "Error with incoming message: {}\n", e.what());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user