[sim] Add WebSocket extension (client/server) (#2589)

This allows access to HAL-level simulation data via a WebSocket connection.

The server additionally serves local files.

The following environment variables can be used for configuration:
HALSIMWS_USERROOT (server) - local directory to use for file serving for /user/ URIs, defaults to ./sim/user
HALSIMWS_SYSROOT (server) - local directory to use for file serving for all other URIs, defaults to ./sim
HALSIMWS_URI (client or server) - WebSocket URI, defaults to /wpilibws
HALSIMWS_PORT (client or server) - port number, defaults to 8080
HALSIMWS_HOST (client) - host to connect to, defaults to localhost

Co-authored-by: Zhiquan Yeo <zyeo8@bloomberg.net>
Co-authored-by: Peter Johnson <johnson.peter@gmail.com>
Co-authored-by: jpokornyiii <jpokornyiii@gmail.com>
This commit is contained in:
Zhiquan Yeo
2020-08-20 01:14:03 -04:00
committed by GitHub
parent e127bac7fd
commit 932bfcf374
51 changed files with 3696 additions and 1 deletions

View File

@@ -0,0 +1,36 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2020 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
#include <iostream>
#include <thread>
#include <hal/DriverStation.h>
#include <hal/HALBase.h>
#include <hal/Main.h>
#include <wpi/Format.h>
#include <wpi/raw_ostream.h>
extern "C" int HALSIM_InitExtension(void);
int main() {
HAL_Initialize(500, 0);
HALSIM_InitExtension();
// HAL_ObserveUserProgramStarting();
HAL_RunMain();
int cycleCount = 0;
while (cycleCount < 1000) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
cycleCount++;
std::cout << "Count: " << cycleCount << std::endl;
}
std::cout << "DONE" << std::endl;
HAL_ExitMain();
}

View File

@@ -0,0 +1,299 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-2020 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
#include "HALSimHttpConnection.h"
#include <uv.h>
#include <wpi/FileSystem.h>
#include <wpi/MimeTypes.h>
#include <wpi/Path.h>
#include <wpi/SmallVector.h>
#include <wpi/UrlParser.h>
#include <wpi/raw_istream.h>
#include <wpi/raw_ostream.h>
#include <wpi/raw_uv_ostream.h>
#include <wpi/uv/Request.h>
#include "HALSimWSServer.h"
namespace uv = wpi::uv;
namespace wpilibws {
bool HALSimHttpConnection::IsValidWsUpgrade(wpi::StringRef protocol) {
auto hws = HALSimWeb::GetInstance();
if (m_request.GetUrl() != hws->GetServerUri()) {
MySendError(404, "invalid websocket address");
return false;
}
return true;
}
void HALSimHttpConnection::ProcessWsUpgrade() {
m_websocket->open.connect_extended([this](auto conn, wpi::StringRef) {
conn.disconnect(); // one-shot
m_buffers = std::make_unique<BufferPool>();
m_exec =
UvExecFunc::Create(m_stream.GetLoop(), [](auto out, LoopFunc func) {
func();
out.set_value();
});
auto hws = HALSimWeb::GetInstance();
if (!hws) {
Log(503);
m_websocket->Fail(503, "HALSimWeb unavailable");
return;
}
if (!hws->RegisterWebsocket(shared_from_this())) {
Log(409);
m_websocket->Fail(409, "Only a single simulation websocket is allowed");
return;
}
Log(200);
m_isWsConnected = true;
wpi::errs() << "HALWebSim: websocket connected\n";
});
// parse incoming JSON, dispatch to parent
m_websocket->text.connect([this](wpi::StringRef msg, bool) {
auto hws = HALSimWeb::GetInstance();
if (!m_isWsConnected || !hws) {
return;
}
wpi::json j;
try {
j = wpi::json::parse(msg);
} catch (const wpi::json::parse_error& e) {
std::string err("JSON parse failed: ");
err += e.what();
m_websocket->Fail(400, err);
return;
}
hws->OnNetValueChanged(j);
});
m_websocket->closed.connect([this](uint16_t, wpi::StringRef) {
// unset the global, allow another websocket to connect
if (m_isWsConnected) {
wpi::errs() << "HALWebSim: websocket disconnected\n";
m_isWsConnected = false;
auto hws = HALSimWeb::GetInstance();
if (hws) {
hws->CloseWebsocket(shared_from_this());
}
}
});
}
void HALSimHttpConnection::OnSimValueChanged(const wpi::json& msg) {
// render json to buffers
wpi::SmallVector<uv::Buffer, 4> sendBufs;
wpi::raw_uv_ostream os{sendBufs, [this]() -> uv::Buffer {
std::lock_guard lock(m_buffers_mutex);
return m_buffers->Allocate();
}};
os << msg;
// call the websocket send function on the uv loop
m_exec->Call([this, sendBufs]() mutable {
m_websocket->SendText(sendBufs, [this](auto bufs, wpi::uv::Error err) {
{
std::lock_guard lock(m_buffers_mutex);
m_buffers->Release(bufs);
}
if (err) {
wpi::errs() << err.str() << "\n";
wpi::errs().flush();
}
});
});
}
class SendfileReq : public uv::RequestImpl<SendfileReq, uv_fs_t> {
public:
SendfileReq(uv_file out, uv_file in, int64_t inOffset, size_t len)
: m_out(out), m_in(in), m_inOffset(inOffset), m_len(len) {
error = [this](uv::Error err) { GetLoop().error(err); };
}
uv::Loop& GetLoop() const {
return *static_cast<uv::Loop*>(GetRaw()->loop->data);
}
int Send(uv::Loop& loop) {
int err = uv_fs_sendfile(loop.GetRaw(), GetRaw(), m_out, m_in, m_inOffset,
m_len, [](uv_fs_t* req) {
auto& h = *static_cast<SendfileReq*>(req->data);
if (req->result < 0) {
h.ReportError(req->result);
h.complete();
h.Release();
return;
}
h.m_inOffset += req->result;
h.m_len -= req->result;
if (h.m_len == 0) {
// done
h.complete();
h.Release(); // this is always a one-shot
return;
}
// need to send more
h.Send(h.GetLoop());
});
if (err < 0) {
ReportError(err);
complete();
}
return err;
}
wpi::sig::Signal<> complete;
private:
uv_file m_out;
uv_file m_in;
int64_t m_inOffset;
size_t m_len;
};
void Sendfile(uv::Loop& loop, uv_file out, uv_file in, int64_t inOffset,
size_t len, std::function<void()> complete) {
auto req = std::make_shared<SendfileReq>(out, in, inOffset, len);
if (complete) req->complete.connect(complete);
int err = req->Send(loop);
if (err >= 0) req->Keep();
}
void HALSimHttpConnection::SendFileResponse(int code,
const wpi::Twine& codeText,
const wpi::Twine& contentType,
const wpi::Twine& filename,
const wpi::Twine& extraHeader) {
// open file
int infd;
if (wpi::sys::fs::openFileForRead(filename, infd)) {
MySendError(404, "error opening file");
return;
}
// get status (to get file size)
wpi::sys::fs::file_status status;
if (wpi::sys::fs::status(infd, status)) {
MySendError(404, "error getting file size");
wpi::sys::fs::file_t file = uv_get_osfhandle(infd);
wpi::sys::fs::closeFile(file);
return;
}
uv_os_fd_t outfd;
int err = uv_fileno(m_stream.GetRawHandle(), &outfd);
if (err < 0) {
m_stream.GetLoopRef().ReportError(err);
MySendError(404, "error getting fd");
wpi::sys::fs::file_t file = uv_get_osfhandle(infd);
wpi::sys::fs::closeFile(file);
return;
}
wpi::SmallVector<uv::Buffer, 4> toSend;
wpi::raw_uv_ostream os{toSend, 4096};
BuildHeader(os, code, codeText, contentType, status.getSize(), 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::raw_fd_istream is{infd, true};
std::string fileBuf;
size_t oldSize = 0;
while (fileBuf.size() < status.getSize()) {
oldSize = fileBuf.size();
fileBuf.resize(oldSize + 1);
is.read(&(*fileBuf.begin()) + oldSize, 1);
}
bodyOs << fileBuf;
SendData(bodyOs.bufs(), false);
if (!m_keepAlive) {
m_stream.Close();
}
}
void HALSimHttpConnection::ProcessRequest() {
wpi::UrlParser url{m_request.GetUrl(),
m_request.GetMethod() == wpi::HTTP_CONNECT};
if (!url.IsValid()) {
// failed to parse URL
MySendError(400, "Invalid URL");
return;
}
wpi::StringRef path;
if (url.HasPath()) path = url.GetPath();
if (m_request.GetMethod() == wpi::HTTP_GET && path.startswith("/") &&
!path.contains("..")) {
// convert to fs native representation
wpi::SmallVector<char, 32> nativePath;
wpi::sys::path::native(path, nativePath);
if (path.startswith("/user/")) {
std::string prefix = (wpi::sys::path::get_separator() + "user" +
wpi::sys::path::get_separator())
.str();
wpi::sys::path::replace_path_prefix(nativePath, prefix, m_webroot_user);
} else {
wpi::sys::path::replace_path_prefix(
nativePath, wpi::sys::path::get_separator(), m_webroot_sys);
}
if (wpi::sys::fs::is_directory(nativePath)) {
wpi::sys::path::append(nativePath, "index.html");
}
if (!wpi::sys::fs::exists(nativePath) ||
wpi::sys::fs::is_directory(nativePath)) {
MySendError(404, "Resource '" + path + "' not found");
} else {
auto contentType = wpi::MimeTypeFromPath(wpi::Twine(nativePath).str());
SendFileResponse(200, "OK", contentType, nativePath);
}
} else {
MySendError(404, "Resource not found");
}
}
void HALSimHttpConnection::MySendError(int code, const wpi::Twine& message) {
Log(code);
SendError(code, message);
}
void HALSimHttpConnection::Log(int code) {
auto method = wpi::http_method_str(m_request.GetMethod());
wpi::errs() << method << " " << m_request.GetUrl() << " HTTP/"
<< m_request.GetMajor() << "." << m_request.GetMinor() << " "
<< code << "\n";
}
} // namespace wpilibws

View File

@@ -0,0 +1,181 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-2020 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
#include "HALSimWSServer.h"
#include <wpi/FileSystem.h>
#include <wpi/Path.h>
#include <wpi/SmallString.h>
#include <wpi/Twine.h>
#include <wpi/UrlParser.h>
#include <wpi/WebSocketServer.h>
#include <wpi/raw_uv_ostream.h>
#include <wpi/uv/Loop.h>
#include <wpi/uv/Tcp.h>
#include "HALSimHttpConnection.h"
namespace uv = wpi::uv;
namespace wpilibws {
std::shared_ptr<HALSimWeb> HALSimWeb::g_instance;
bool HALSimWeb::Initialize() {
// determine where to get static content from
// wpi::SmallVector<char, 64> tmp;
wpi::SmallString<64> tmp;
const char* webroot_sys = std::getenv("HALSIMWS_SYSROOT");
if (webroot_sys != NULL) {
wpi::StringRef tstr(webroot_sys);
tmp.append(tstr);
} else {
wpi::sys::fs::current_path(tmp);
wpi::sys::path::append(tmp, "sim");
}
wpi::sys::fs::make_absolute(tmp);
m_webroot_sys = wpi::Twine(tmp).str();
tmp.clear();
const char* webroot_user = std::getenv("HALSIMWS_USERROOT");
if (webroot_user != NULL) {
wpi::StringRef tstr(webroot_user);
tmp.append(tstr);
} else {
wpi::sys::fs::current_path(tmp);
wpi::sys::path::append(tmp, "sim", "user");
}
wpi::sys::fs::make_absolute(tmp);
m_webroot_user = wpi::Twine(tmp).str();
const char* uri = std::getenv("HALSIMWS_URI");
if (uri != NULL) {
m_uri = uri;
} else {
m_uri = "/wpilibws";
}
const char* port = std::getenv("HALSIMWS_PORT");
if (port != NULL) {
try {
m_port = std::stoi(port);
} catch (const std::invalid_argument& err) {
wpi::errs() << "Error decoding HALSIMWS_PORT (" << err.what() << ")\n";
return false;
}
} else {
m_port = 8080;
}
// create libuv things
m_loop = uv::Loop::Create();
if (!m_loop) {
return false;
}
m_loop->error.connect([](uv::Error err) {
wpi::errs() << "HALSim WS Server libuv ERROR: " << err.str() << '\n';
});
m_server = uv::Tcp::Create(m_loop);
if (!m_server) {
return false;
}
m_server->Bind("", m_port);
return true;
}
void HALSimWeb::Main(void* param) {
GetInstance()->MainLoop();
SetInstance(nullptr);
}
void HALSimWeb::MainLoop() {
// when we get a connection, accept it and start reading
m_server->connection.connect([this, srv = m_server.get()] {
auto tcp = srv->Accept();
if (!tcp) return;
tcp->SetNoDelay(true);
auto conn = std::make_shared<HALSimHttpConnection>(tcp, m_webroot_sys,
m_webroot_user);
tcp->SetData(conn);
});
// start listening for incoming connections
m_server->Listen();
wpi::outs() << "Listening at http://localhost:" << m_port << "\n";
wpi::outs() << "WebSocket URI: " << m_uri << "\n";
m_loop->Run();
}
void HALSimWeb::Exit(void* param) {
auto inst = GetInstance();
if (!inst) return;
auto loop = inst->m_loop;
loop->Walk([](uv::Handle& h) {
h.SetLoopClosing(true);
h.Close();
});
}
bool HALSimWeb::RegisterWebsocket(
std::shared_ptr<HALSimBaseWebSocketConnection> hws) {
if (m_hws.lock()) {
return false;
}
m_hws = hws;
m_simDevicesProvider.OnNetworkConnected(hws);
// notify all providers that they should use this new websocket instead
m_providers.ForEach([hws](std::shared_ptr<HALSimWSBaseProvider> provider) {
provider->OnNetworkConnected(hws);
});
return true;
}
void HALSimWeb::CloseWebsocket(
std::shared_ptr<HALSimBaseWebSocketConnection> hws) {
// Inform the providers that they need to cancel callbacks
m_simDevicesProvider.OnNetworkDisconnected();
m_providers.ForEach([](std::shared_ptr<HALSimWSBaseProvider> provider) {
provider->OnNetworkDisconnected();
});
if (hws == m_hws.lock()) {
m_hws.reset();
}
}
void HALSimWeb::OnNetValueChanged(const wpi::json& msg) {
// Look for "type" and "device" fields so that we can
// generate the key
try {
std::string type = msg.at("type").get<std::string>();
std::string device = msg.at("device").get<std::string>();
auto data = msg.at("data");
auto key = type + "/" + device;
auto provider = m_providers.Get(key);
if (provider) {
provider->OnNetValueChanged(data);
}
} catch (wpi::json::exception& e) {
wpi::errs() << "Error with incoming message: " << e.what() << "\n";
}
}
} // namespace wpilibws

View File

@@ -0,0 +1,67 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-2020 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
#include <WSProviderContainer.h>
#include <WSProvider_Analog.h>
#include <WSProvider_DIO.h>
#include <WSProvider_DriverStation.h>
#include <WSProvider_Encoder.h>
#include <WSProvider_Joystick.h>
#include <WSProvider_PWM.h>
#include <WSProvider_Relay.h>
#include <WSProvider_RoboRIO.h>
#include <WSProvider_SimDevice.h>
#include <WSProvider_dPWM.h>
#include <hal/Main.h>
#include <wpi/raw_ostream.h>
#include "HALSimWSServer.h"
using namespace std::placeholders;
using namespace wpilibws;
// Currently, robots never terminate, so we keep static objects that are
// never properly released or cleaned up.
static ProviderContainer providers;
static HALSimWSProviderSimDevices simDevices(providers);
extern "C" {
#if defined(WIN32) || defined(_WIN32)
__declspec(dllexport)
#endif
int HALSIM_InitExtension(void) {
wpi::outs() << "Websocket Simulator Initializing.\n";
auto hsw = std::make_shared<HALSimWeb>(providers, simDevices);
HALSimWeb::SetInstance(hsw);
if (!hsw->Initialize()) {
return -1;
}
WSRegisterFunc registerFunc = [&](auto key, auto provider) {
providers.Add(key, provider);
};
HALSimWSProviderAnalogIn::Initialize(registerFunc);
HALSimWSProviderAnalogOut::Initialize(registerFunc);
HALSimWSProviderDIO::Initialize(registerFunc);
HALSimWSProviderDigitalPWM::Initialize(registerFunc);
HALSimWSProviderDriverStation::Initialize(registerFunc);
HALSimWSProviderEncoder::Initialize(registerFunc);
HALSimWSProviderJoystick::Initialize(registerFunc);
HALSimWSProviderPWM::Initialize(registerFunc);
HALSimWSProviderRelay::Initialize(registerFunc);
HALSimWSProviderRoboRIO::Initialize(registerFunc);
simDevices.Initialize(hsw->GetLoop());
HAL_SetMain(nullptr, HALSimWeb::Main, HALSimWeb::Exit);
wpi::outs() << "Websocket Simulator Initialized!\n";
return 0;
}
} // extern "C"

View File

@@ -0,0 +1,74 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-2020 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
#pragma once
#include <cinttypes>
#include <memory>
#include <string>
#include <vector>
#include <HALSimBaseWebSocketConnection.h>
#include <wpi/HttpWebSocketServerConnection.h>
#include <wpi/json.h>
#include <wpi/mutex.h>
#include <wpi/uv/AsyncFunction.h>
#include <wpi/uv/Buffer.h>
namespace wpilibws {
class HALSimWeb;
class HALSimHttpConnection
: public wpi::HttpWebSocketServerConnection<HALSimHttpConnection>,
public HALSimBaseWebSocketConnection {
public:
using BufferPool = wpi::uv::SimpleBufferPool<4>;
using LoopFunc = std::function<void(void)>;
using UvExecFunc = wpi::uv::AsyncFunction<void(LoopFunc)>;
explicit HALSimHttpConnection(std::shared_ptr<wpi::uv::Stream> stream,
wpi::StringRef webroot_sys,
wpi::StringRef webroot_user)
: wpi::HttpWebSocketServerConnection<HALSimHttpConnection>(stream, {}),
m_webroot_sys(webroot_sys),
m_webroot_user(webroot_user) {}
public:
// callable from any thread
void OnSimValueChanged(const wpi::json& msg) override;
protected:
void ProcessRequest() override;
bool IsValidWsUpgrade(wpi::StringRef protocol) override;
void ProcessWsUpgrade() override;
void SendFileResponse(int code, const wpi::Twine& codeText,
const wpi::Twine& contentType,
const wpi::Twine& filename,
const wpi::Twine& extraHeader = wpi::Twine{});
void MySendError(int code, const wpi::Twine& message);
void Log(int code);
private:
// Absolute paths of folders to retrieve data from
// -> /
std::string m_webroot_sys;
// -> /user
std::string m_webroot_user;
// is the websocket connected?
bool m_isWsConnected = false;
// these are only valid if the websocket is connected
std::shared_ptr<UvExecFunc> m_exec;
std::unique_ptr<BufferPool> m_buffers;
std::mutex m_buffers_mutex;
};
} // namespace wpilibws

View File

@@ -0,0 +1,81 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-2020 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
#pragma once
#include <memory>
#include <string>
#include <WSBaseProvider.h>
#include <WSProviderContainer.h>
#include <WSProvider_SimDevice.h>
#include <wpi/StringMap.h>
#include <wpi/json.h>
#include <wpi/mutex.h>
#include <wpi/uv/AsyncFunction.h>
#include <wpi/uv/Buffer.h>
#include <wpi/uv/Loop.h>
#include <wpi/uv/Tcp.h>
namespace wpilibws {
class HALSimHttpConnection;
class HALSimWeb {
public:
static std::shared_ptr<HALSimWeb> GetInstance() { return g_instance; }
static void SetInstance(std::shared_ptr<HALSimWeb> inst) {
g_instance = inst;
}
explicit HALSimWeb(ProviderContainer& providers,
HALSimWSProviderSimDevices& simDevicesProvider)
: m_providers(providers), m_simDevicesProvider(simDevicesProvider) {}
HALSimWeb(const HALSimWeb&) = delete;
HALSimWeb& operator=(const HALSimWeb&) = delete;
bool Initialize();
static void Main(void*);
static void Exit(void*);
bool RegisterWebsocket(std::shared_ptr<HALSimBaseWebSocketConnection> hws);
void CloseWebsocket(std::shared_ptr<HALSimBaseWebSocketConnection> hws);
// network -> sim
void OnNetValueChanged(const wpi::json& msg);
std::string GetServerUri() const { return m_uri; }
int GetServerPort() { return m_port; }
std::shared_ptr<wpi::uv::Loop> GetLoop() { return m_loop; }
private:
static std::shared_ptr<HALSimWeb> g_instance;
void MainLoop();
// connected http connection that contains active websocket
std::weak_ptr<HALSimBaseWebSocketConnection> m_hws;
// list of providers
ProviderContainer& m_providers;
HALSimWSProviderSimDevices& m_simDevicesProvider;
std::shared_ptr<wpi::uv::Loop> m_loop;
std::shared_ptr<wpi::uv::Tcp> m_server;
// Absolute paths of folders to retrieve data from
// -> /
std::string m_webroot_sys;
// -> /user
std::string m_webroot_user;
std::string m_uri;
int m_port;
};
} // namespace wpilibws

View File

@@ -0,0 +1,177 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2020 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
#include "WebServerClientTest.h"
#include <sstream>
#include <wpi/SmallString.h>
#include <wpi/raw_ostream.h>
#include <wpi/raw_uv_ostream.h>
#include <wpi/uv/util.h>
static constexpr int kTcpConnectAttemptTimeout = 1000;
namespace uv = wpi::uv;
namespace wpilibws {
std::shared_ptr<WebServerClientTest> WebServerClientTest::g_instance;
// Create Web Socket and specify event callbacks
void WebServerClientTest::InitializeWebSocket(const std::string& host, int port,
const std::string& uri) {
std::stringstream ss;
ss << host << ":" << port;
wpi::outs() << "Will attempt to connect to: " << ss.str() << uri << "\n";
m_websocket =
wpi::WebSocket::CreateClient(*m_tcp_client.get(), uri, ss.str());
// Hook up events
m_websocket->open.connect_extended([this](auto conn, wpi::StringRef) {
conn.disconnect();
m_buffers = std::make_unique<BufferPool>();
m_exec = UvExecFunc::Create(m_tcp_client->GetLoop(),
[](auto out, LoopFunc func) {
func();
out.set_value();
});
m_ws_connected = true;
wpi::errs() << "WebServerClientTest: WebSocket Connected\n";
});
m_websocket->text.connect([this](wpi::StringRef msg, bool) {
wpi::json j;
try {
j = wpi::json::parse(msg);
} catch (const wpi::json::parse_error& e) {
std::string err("JSON parse failed: ");
err += e.what();
wpi::errs() << err << "\n";
m_websocket->Fail(1003, err);
return;
}
// Save last message received
m_json = j;
// If terminate flag set, end loop after message recieved
if (m_terminateFlag) {
m_loop->Stop();
}
});
m_websocket->closed.connect([this](uint16_t, wpi::StringRef) {
if (m_ws_connected) {
wpi::errs() << "WebServerClientTest: Websocket Disconnected\n";
m_ws_connected = false;
}
});
}
// Create tcp client, specify callbacks, and create timers for loop
bool WebServerClientTest::Initialize(std::shared_ptr<uv::Loop>& loop) {
m_loop = loop;
m_loop->error.connect(
[](uv::Error err) { wpi::errs() << "uv Error: " << err.str() << "\n"; });
m_tcp_client = uv::Tcp::Create(m_loop);
if (!m_tcp_client) {
wpi::errs() << "ERROR: Could not create TCP Client\n";
return false;
}
// Hook up TCP client events
m_tcp_client->error.connect(
[this, socket = m_tcp_client.get()](wpi::uv::Error err) {
if (m_tcp_connected) {
m_tcp_connected = false;
m_connect_attempts = 0;
m_loop->Stop();
return;
}
// If we weren't previously connected, attempt a reconnection
m_connect_timer->Start(uv::Timer::Time(kTcpConnectAttemptTimeout));
});
m_tcp_client->closed.connect(
[]() { wpi::errs() << "TCP connection closed\n"; });
// Set up the connection timer
m_connect_timer = uv::Timer::Create(m_loop);
if (!m_connect_timer) {
return false;
}
// Set up the timer to attempt connection
m_connect_timer->timeout.connect([this] { AttemptConnect(); });
m_connect_timer->Start(uv::Timer::Time(0));
wpi::outs() << "WebServerClientTest Initialized\n";
return true;
}
void WebServerClientTest::AttemptConnect() {
m_connect_attempts++;
wpi::outs() << "Test Client Connection Attempt " << m_connect_attempts
<< "\n";
if (m_connect_attempts >= 5) {
wpi::errs() << "Test Client Timeout. Unable to connect\n";
Exit();
return;
}
struct sockaddr_in dest;
uv::NameToAddr("localhost", 8080, &dest);
m_tcp_client->Connect(dest, [this]() {
m_tcp_connected = true;
InitializeWebSocket("localhost", 8080, "/wpilibws");
});
}
void WebServerClientTest::Exit() {
m_loop->Walk([](uv::Handle& h) {
h.SetLoopClosing(true);
h.Close();
});
}
void WebServerClientTest::SendMessage(const wpi::json& msg) {
if (msg.empty()) {
wpi::errs() << "Message to send is empty\n";
return;
}
wpi::SmallVector<uv::Buffer, 4> sendBufs;
wpi::raw_uv_ostream os{sendBufs, [this]() -> uv::Buffer {
std::lock_guard lock(m_buffers_mutex);
return m_buffers->Allocate();
}};
os << msg;
// Call the websocket send function on the uv loop
m_exec->Call([this, sendBufs]() mutable {
m_websocket->SendText(sendBufs, [this](auto bufs, wpi::uv::Error err) {
{
std::lock_guard lock(m_buffers_mutex);
m_buffers->Release(bufs);
}
if (err) {
wpi::errs() << err.str() << "\n";
wpi::errs().flush();
}
});
});
}
const wpi::json& WebServerClientTest::GetLastMessage() { return m_json; }
} // namespace wpilibws

View File

@@ -0,0 +1,155 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2020 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
#include <hal/HALBase.h>
#include <hal/Main.h>
#include <hal/simulation/DIOData.h>
#include <wpi/raw_ostream.h>
#include <wpi/uv/Loop.h>
#include "HALSimWSServer.h"
#include "WebServerClientTest.h"
#include "gtest/gtest.h"
using namespace wpilibws;
extern "C" int HALSIM_InitExtension(
void); // from simulation/halsim_ws_server/src/main/native/cpp/main.cpp
const int POLLING_SPEED = 10; // 10 ms polling
class WebServerIntegrationTest : public ::testing::Test {
public:
WebServerIntegrationTest() {
const int MAX_TEST_TIME = 1000; // 1 second
// Initialize HAL layer including webserver
HALSIM_InitExtension();
// Create and initialize client
m_webserver_client =
std::shared_ptr<WebServerClientTest>((new WebServerClientTest()));
WebServerClientTest::SetInstance(m_webserver_client);
auto webserver = HALSimWeb::GetInstance();
auto loop = webserver->GetLoop();
m_webserver_client->Initialize(loop);
// Create failover timer to prevent running forever
m_failoverTimer = wpi::uv::Timer::Create(loop);
m_failoverTimer->timeout.connect([loop] { loop->Stop(); });
m_failoverTimer->Start(uv::Timer::Time(MAX_TEST_TIME));
}
~WebServerIntegrationTest() {
// Exit HAL layer which exits webserver
HAL_ExitMain();
HALSimWeb::Exit(nullptr);
// Exit client
m_webserver_client->Exit();
WebServerClientTest::SetInstance(nullptr);
// Unreference timers from loop
m_failoverTimer->Unreference();
}
bool IsConnectedClientWS() { return m_webserver_client->IsConnectedWS(); }
void TerminateOnNextMessage(bool flag) {
m_webserver_client->SetTerminateFlag(flag);
}
private:
std::shared_ptr<WebServerClientTest> m_webserver_client;
std::shared_ptr<wpi::uv::Timer> m_failoverTimer;
};
TEST_F(WebServerIntegrationTest, DigitalOutput) {
// Create expected results
const bool EXPECTED_VALUE = false;
const int PIN = 0;
// Attach timer to loop for test function
auto ws = HALSimWeb::GetInstance();
auto loop = ws->GetLoop();
auto timer = wpi::uv::Timer::Create(loop);
timer->timeout.connect([&] {
if (IsConnectedClientWS()) {
wpi::outs() << "***** Setting DIO value for pin " << PIN << " to "
<< (EXPECTED_VALUE ? "true" : "false") << "\n";
HALSIM_SetDIOValue(PIN, EXPECTED_VALUE);
TerminateOnNextMessage(true);
} else {
// Recheck in POLLING_SPEED ms
timer->Start(uv::Timer::Time(POLLING_SPEED));
}
});
timer->Start(uv::Timer::Time(POLLING_SPEED));
HAL_RunMain();
timer->Unreference();
// Get values from JSON message
std::string test_type;
std::string test_device;
bool test_value = true; // Default value from HAL initialization
try {
auto& msg = WebServerClientTest::GetInstance()->GetLastMessage();
test_type = msg.at("type").get_ref<const std::string&>();
test_device = msg.at("device").get_ref<const std::string&>();
auto& data = msg.at("data");
wpi::json::const_iterator it = data.find("<>value");
if (it != data.end()) {
test_value = it.value();
}
} catch (wpi::json::exception& e) {
wpi::errs() << "Error with incoming message: " << e.what() << "\n";
}
// Compare results
EXPECT_EQ("DIO", test_type);
EXPECT_EQ(std::to_string(PIN), test_device);
EXPECT_EQ(EXPECTED_VALUE, test_value);
}
TEST_F(WebServerIntegrationTest, DigitalInput) {
// Create expected results
const bool EXPECTED_VALUE = false;
const int PIN = 0;
// Attach timer to loop for test function
auto ws = HALSimWeb::GetInstance();
auto loop = ws->GetLoop();
auto timer = wpi::uv::Timer::Create(loop);
timer->timeout.connect([&] {
if (IsConnectedClientWS()) {
wpi::json msg = {{"type", "DIO"},
{"device", std::to_string(PIN)},
{"data", {{"<>value", EXPECTED_VALUE}}}};
wpi::outs() << "***** Input JSON: " << msg.dump() << "\n";
WebServerClientTest::GetInstance()->SendMessage(msg);
loop->Stop();
} else {
// Recheck in POLLING_SPEED ms
timer->Start(uv::Timer::Time(POLLING_SPEED));
}
});
timer->Start(uv::Timer::Time(POLLING_SPEED));
HAL_RunMain();
timer->Unreference();
// Compare results
bool test_value = HALSIM_GetDIOValue(PIN);
EXPECT_EQ(EXPECTED_VALUE, test_value);
}
int main(int argc, char* argv[]) {
::testing::InitGoogleTest(&argc, argv);
HAL_Initialize(500, 0);
int ret = RUN_ALL_TESTS();
return ret;
}

View File

@@ -0,0 +1,72 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2020 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
#pragma once
#include <memory>
#include <string>
#include <wpi/WebSocket.h>
#include <wpi/json.h>
#include <wpi/uv/AsyncFunction.h>
#include <wpi/uv/Buffer.h>
#include <wpi/uv/Loop.h>
#include <wpi/uv/Stream.h>
#include <wpi/uv/Tcp.h>
#include <wpi/uv/Timer.h>
namespace uv = wpi::uv;
namespace wpilibws {
class WebServerClientTest {
public:
using BufferPool = wpi::uv::SimpleBufferPool<4>;
using LoopFunc = std::function<void(void)>;
using UvExecFunc = wpi::uv::AsyncFunction<void(LoopFunc)>;
static std::shared_ptr<WebServerClientTest> GetInstance() {
return g_instance;
}
static void SetInstance(std::shared_ptr<WebServerClientTest> inst) {
g_instance = inst;
}
WebServerClientTest() {}
WebServerClientTest(const WebServerClientTest&) = delete;
WebServerClientTest& operator=(const WebServerClientTest&) = delete;
bool Initialize(std::shared_ptr<uv::Loop>& loop);
void AttemptConnect();
void Exit();
void SendMessage(const wpi::json& msg);
const wpi::json& GetLastMessage();
bool IsConnectedWS() { return m_ws_connected; }
void SetTerminateFlag(bool flag) { m_terminateFlag = flag; }
private:
void InitializeWebSocket(const std::string& host, int port,
const std::string& uri);
bool m_terminateFlag = false;
static std::shared_ptr<WebServerClientTest> g_instance;
bool m_tcp_connected = false;
std::shared_ptr<wpi::uv::Timer> m_connect_timer;
int m_connect_attempts = 0;
std::shared_ptr<wpi::uv::Loop> m_loop;
std::shared_ptr<wpi::uv::Tcp> m_tcp_client;
wpi::json m_json;
bool m_ws_connected = false;
std::shared_ptr<wpi::WebSocket> m_websocket;
std::shared_ptr<UvExecFunc> m_exec;
std::unique_ptr<BufferPool> m_buffers;
std::mutex m_buffers_mutex;
};
} // namespace wpilibws