2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2020-08-20 01:14:03 -04:00
|
|
|
|
|
|
|
|
#include "WebServerClientTest.h"
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
#include <cstdio>
|
2020-08-20 01:14:03 -04:00
|
|
|
|
|
|
|
|
#include <wpi/SmallString.h>
|
2024-05-12 06:25:42 -07:00
|
|
|
#include <wpi/print.h>
|
2022-05-07 10:54:14 -07:00
|
|
|
#include <wpinet/raw_uv_ostream.h>
|
|
|
|
|
#include <wpinet/uv/util.h>
|
2020-08-20 01:14:03 -04:00
|
|
|
|
|
|
|
|
static constexpr int kTcpConnectAttemptTimeout = 1000;
|
|
|
|
|
|
|
|
|
|
namespace uv = wpi::uv;
|
|
|
|
|
|
|
|
|
|
namespace wpilibws {
|
|
|
|
|
|
|
|
|
|
// Create Web Socket and specify event callbacks
|
|
|
|
|
void WebServerClientTest::InitializeWebSocket(const std::string& host, int port,
|
|
|
|
|
const std::string& uri) {
|
2024-05-12 06:25:42 -07:00
|
|
|
wpi::print("Will attempt to connect to: {}:{}{}\n", host, port, uri);
|
2021-06-06 16:13:58 -07:00
|
|
|
m_websocket = wpi::WebSocket::CreateClient(*m_tcp_client.get(), uri,
|
|
|
|
|
fmt::format("{}:{}", host, port));
|
2020-08-20 01:14:03 -04:00
|
|
|
|
|
|
|
|
// Hook up events
|
2021-06-06 16:13:58 -07:00
|
|
|
m_websocket->open.connect_extended([this](auto conn, auto) {
|
2020-08-20 01:14:03 -04:00
|
|
|
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;
|
2021-06-06 16:13:58 -07:00
|
|
|
std::fputs("WebServerClientTest: WebSocket Connected\n", stderr);
|
2020-08-20 01:14:03 -04:00
|
|
|
});
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
m_websocket->text.connect([this](auto msg, bool) {
|
2020-08-20 01:14:03 -04:00
|
|
|
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();
|
2024-05-12 06:25:42 -07:00
|
|
|
wpi::print(stderr, "{}\n", err);
|
2020-08-20 01:14:03 -04:00
|
|
|
m_websocket->Fail(1003, err);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// Save last message received
|
|
|
|
|
m_json = j;
|
|
|
|
|
});
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
m_websocket->closed.connect([this](uint16_t, auto) {
|
2020-08-20 01:14:03 -04:00
|
|
|
if (m_ws_connected) {
|
2021-06-06 16:13:58 -07:00
|
|
|
std::fputs("WebServerClientTest: Websocket Disconnected\n", stderr);
|
2020-08-20 01:14:03 -04:00
|
|
|
m_ws_connected = false;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create tcp client, specify callbacks, and create timers for loop
|
2020-09-04 10:57:05 -07:00
|
|
|
bool WebServerClientTest::Initialize() {
|
|
|
|
|
m_loop.error.connect(
|
2024-05-12 06:25:42 -07:00
|
|
|
[](uv::Error err) { wpi::print(stderr, "uv Error: {}\n", err.str()); });
|
2020-08-20 01:14:03 -04:00
|
|
|
|
|
|
|
|
m_tcp_client = uv::Tcp::Create(m_loop);
|
|
|
|
|
if (!m_tcp_client) {
|
2021-06-06 16:13:58 -07:00
|
|
|
std::fputs("ERROR: Could not create TCP Client\n", stderr);
|
2020-08-20 01:14:03 -04:00
|
|
|
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;
|
2020-09-04 10:57:05 -07:00
|
|
|
m_loop.Stop();
|
2020-08-20 01:14:03 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If we weren't previously connected, attempt a reconnection
|
|
|
|
|
m_connect_timer->Start(uv::Timer::Time(kTcpConnectAttemptTimeout));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
m_tcp_client->closed.connect(
|
2021-06-06 16:13:58 -07:00
|
|
|
[]() { std::fputs("TCP connection closed\n", stderr); });
|
2020-08-20 01:14:03 -04:00
|
|
|
|
|
|
|
|
// 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));
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
std::puts("WebServerClientTest Initialized");
|
2020-08-20 01:14:03 -04:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebServerClientTest::AttemptConnect() {
|
|
|
|
|
m_connect_attempts++;
|
2024-05-12 06:25:42 -07:00
|
|
|
wpi::print("Test Client Connection Attempt {}\n", m_connect_attempts);
|
2020-08-20 01:14:03 -04:00
|
|
|
|
|
|
|
|
if (m_connect_attempts >= 5) {
|
2021-06-06 16:13:58 -07:00
|
|
|
std::fputs("Test Client Timeout. Unable to connect\n", stderr);
|
2020-09-04 10:57:05 -07:00
|
|
|
m_loop.Stop();
|
2020-08-20 01:14:03 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct sockaddr_in dest;
|
2020-12-10 20:39:14 -08:00
|
|
|
uv::NameToAddr("localhost", 3300, &dest);
|
2020-08-20 01:14:03 -04:00
|
|
|
m_tcp_client->Connect(dest, [this]() {
|
|
|
|
|
m_tcp_connected = true;
|
2020-12-10 20:39:14 -08:00
|
|
|
InitializeWebSocket("localhost", 3300, "/wpilibws");
|
2020-08-20 01:14:03 -04:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebServerClientTest::SendMessage(const wpi::json& msg) {
|
|
|
|
|
if (msg.empty()) {
|
2021-06-06 16:13:58 -07:00
|
|
|
std::fputs("Message to send is empty\n", stderr);
|
2020-08-20 01:14:03 -04:00
|
|
|
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) {
|
2024-05-12 06:25:42 -07:00
|
|
|
wpi::print(stderr, "{}\n", err.str());
|
2021-06-06 16:13:58 -07:00
|
|
|
std::fflush(stderr);
|
2020-08-20 01:14:03 -04:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
const wpi::json& WebServerClientTest::GetLastMessage() {
|
|
|
|
|
return m_json;
|
|
|
|
|
}
|
2020-08-20 01:14:03 -04:00
|
|
|
|
|
|
|
|
} // namespace wpilibws
|