125 lines
3.8 KiB
C++

#include "include/websocket.h"
Websocket::Websocket(std::function<void(const std::string)> toLog)
: toLog(std::move(toLog))
{
#ifdef IBM
// Required on Windows
ix::initNetSystem();
#endif
server = new ix::WebSocketServer(8080, "0.0.0.0");
server->setOnClientMessageCallback(
[this](std::shared_ptr<ix::ConnectionState> connectionState,
ix::WebSocket &ws,
const ix::WebSocketMessagePtr &msg) {
this->onClientMessageCallback(connectionState, ws, msg);
});
std::pair<bool, std::string> res = server->listen();
if (!res.first) {
// Error handling
throw std::invalid_argument("Encountered: " + res.second + "\n");
}
// Run the server in the background. Server can be stopped by calling
server->start();
}
Websocket::~Websocket()
{
server->stop();
#ifdef IBM
// Required on Windows
ix::uninitNetSystem();
#endif
}
void Websocket::onClientMessageCallback(
std::shared_ptr<ix::ConnectionState> &connectionState,
ix::WebSocket &ws,
const ix::WebSocketMessagePtr &msg)
{
if (msg->type == ix::WebSocketMessageType::Open) {
std::stringstream debug_msg;
debug_msg << "New connection" << std::endl;
debug_msg << "Remote ip: " << connectionState->getRemoteIp()
<< std::endl;
debug_msg << "id: " << connectionState->getId() << std::endl;
debug_msg << "Uri: " << msg->openInfo.uri << std::endl;
debug_msg << "Headers:" << std::endl;
for (const auto &it : msg->openInfo.headers) {
debug_msg << it.first << ": " << it.second << std::endl;
}
toLog(debug_msg.str());
const std::lock_guard<std::mutex> guard(wsLock);
webSocket = &ws;
} else if (msg->type == ix::WebSocketMessageType::Close) {
std::stringstream debug_msg;
debug_msg << "Connection closed" << std::endl;
debug_msg << "id: " << connectionState->getId() << std::endl;
debug_msg << "Code: " << msg->closeInfo.code << std::endl;
debug_msg << "Reason: " << msg->closeInfo.reason << std::endl;
debug_msg << "Remote: " << msg->closeInfo.remote << std::endl;
toLog(debug_msg.str());
const std::lock_guard<std::mutex> guard(wsLock);
webSocket = nullptr;
} else if (msg->type == ix::WebSocketMessageType::Error) {
std::stringstream debug_msg;
debug_msg << "Connection error" << std::endl;
debug_msg << "id: " << connectionState->getId() << std::endl;
debug_msg << "Decompression: " << msg->errorInfo.decompressionError
<< std::endl;
debug_msg << "HTTP status: " << msg->errorInfo.http_status << std::endl;
debug_msg << "Reason: " << msg->errorInfo.reason << std::endl;
debug_msg << "Retries: " << msg->errorInfo.retries << std::endl;
debug_msg << "Wait time: " << msg->errorInfo.wait_time << std::endl;
toLog(debug_msg.str());
const std::lock_guard<std::mutex> guard(wsLock);
webSocket = nullptr;
} else if (msg->type == ix::WebSocketMessageType::Message) {
if (!msg->str.empty()) {
ws.send("Echo: " + msg->str, false);
}
}
}
void Websocket::sendData(data d)
{
if (strcmp(d.path, lastPath) != 0) {
strcpy(lastPath, d.path);
if (util::generateMD5(d.path, lastHash, toLog)) {
strcpy(lastHash, "NOT SET");
}
}
nlohmann::json json = {
{"altitude", d.alt},
{"vs", d.vs},
{"ias", d.ias},
{"magHdg", d.magHeading},
{"truHdg", d.truHdg},
{"totFuel", d.totFuelKg},
{"fuelFlow", d.ff},
{"hash", lastHash},
};
{
std::lock_guard<std::mutex> lock(wsLock);
if (webSocket != nullptr) {
webSocket->send(json.dump(), false);
}
}
}