[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,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;
}