137 lines
4.5 KiB
C++
137 lines
4.5 KiB
C++
#include "include/websocket.h"
|
|
|
|
namespace germanairlinesva
|
|
{
|
|
namespace gaconnector
|
|
{
|
|
namespace websocket
|
|
{
|
|
Websocket::Websocket(std::string host,
|
|
std::string user,
|
|
std::string token,
|
|
std::function<void(const std::string)> toLog)
|
|
: host(host), user(user), token(token), toLog(std::move(toLog))
|
|
{
|
|
#ifdef IBM
|
|
// Required on Windows
|
|
ix::initNetSystem();
|
|
#endif
|
|
|
|
this->webSocket = new ix::WebSocket();
|
|
this->webSocket->setExtraHeaders({
|
|
{"origin", "GAClient"},
|
|
{"authentication", "Bearer " + this->token},
|
|
{"user", this->user},
|
|
});
|
|
this->webSocket->enableAutomaticReconnection();
|
|
this->webSocket->setUrl(host);
|
|
this->webSocket->setOnMessageCallback(
|
|
[this](const ix::WebSocketMessagePtr &msg) {
|
|
this->onClientMessageCallback(msg);
|
|
});
|
|
this->webSocket->start();
|
|
}
|
|
|
|
Websocket::~Websocket()
|
|
{
|
|
this->webSocket->stop();
|
|
this->toLog("WebSocket stopped");
|
|
#ifdef IBM
|
|
// Required on Windows
|
|
ix::uninitNetSystem();
|
|
#endif
|
|
}
|
|
|
|
void Websocket::onClientMessageCallback(const ix::WebSocketMessagePtr &msg)
|
|
{
|
|
if (msg->type == ix::WebSocketMessageType::Open) {
|
|
std::stringstream debug_msg;
|
|
|
|
debug_msg << "New connection" << std::endl;
|
|
debug_msg << "\tUri: " << msg->openInfo.uri << std::endl;
|
|
debug_msg << "\tHeaders:" << std::endl;
|
|
for (const auto &it : msg->openInfo.headers) {
|
|
debug_msg << "\t\t" << it.first << ": " << it.second << std::endl;
|
|
}
|
|
this->toLog(debug_msg.str());
|
|
|
|
this->toLog("Connected as " + user);
|
|
this->isLoggedIn = true;
|
|
this->failureMode = failures::NONE;
|
|
} else if (msg->type == ix::WebSocketMessageType::Close) {
|
|
std::stringstream debug_msg;
|
|
|
|
debug_msg << "Connection closed" << std::endl;
|
|
debug_msg << "\tCode: " << msg->closeInfo.code << std::endl;
|
|
debug_msg << "\tReason: " << msg->closeInfo.reason << std::endl;
|
|
debug_msg << "\tRemote: " << msg->closeInfo.remote << std::endl;
|
|
|
|
this->toLog(debug_msg.str());
|
|
this->isLoggedIn = false;
|
|
} else if (msg->type == ix::WebSocketMessageType::Error) {
|
|
std::stringstream debug_msg;
|
|
|
|
debug_msg << "Connection error" << std::endl;
|
|
debug_msg << "\tDecompression: " << msg->errorInfo.decompressionError
|
|
<< std::endl;
|
|
debug_msg << "\tHTTP status: " << msg->errorInfo.http_status
|
|
<< std::endl;
|
|
debug_msg << "\tReason: " << msg->errorInfo.reason << std::endl;
|
|
debug_msg << "\tRetries: " << msg->errorInfo.retries << std::endl;
|
|
debug_msg << "\tWait time: " << msg->errorInfo.wait_time << std::endl;
|
|
|
|
if (msg->errorInfo.http_status == 401) {
|
|
this->webSocket->disableAutomaticReconnection();
|
|
|
|
this->toLog("Disabling automatic reconnection due to auth failure");
|
|
this->failureMode = failures::AUTH;
|
|
} else if (msg->errorInfo.http_status == 500 &&
|
|
msg->errorInfo.reason.find("Duplicate") !=
|
|
std::string::npos) {
|
|
this->webSocket->disableAutomaticReconnection();
|
|
|
|
this->toLog("Disabling automatic reconnection due to duplicate");
|
|
this->failureMode = failures::DUPLICATESIM;
|
|
} else {
|
|
this->failureMode = failures::OTHER;
|
|
}
|
|
|
|
this->toLog(debug_msg.str());
|
|
this->isLoggedIn = false;
|
|
} else if (msg->type == ix::WebSocketMessageType::Message) {
|
|
if (!msg->str.empty()) {
|
|
this->toLog(msg->str);
|
|
}
|
|
}
|
|
}
|
|
|
|
void Websocket::sendData(data &d)
|
|
{
|
|
if (strcmp(d.path, this->lastPath) != 0) {
|
|
strcpy(this->lastPath, d.path);
|
|
if (utilities::generateMD5(d.path, this->lastHash, this->toLog)) {
|
|
strcpy(this->lastHash, "NO HASH");
|
|
}
|
|
}
|
|
|
|
nlohmann::json json = {
|
|
{"altitude", d.alt},
|
|
{"vs", d.vs},
|
|
{"ias", d.ias},
|
|
{"magHdg", d.magHeading},
|
|
{"truHdg", d.truHdg},
|
|
{"totFuel", d.totFuelKg},
|
|
{"fuelFlow", d.ff},
|
|
{"path", d.path},
|
|
{"hash", this->lastHash},
|
|
};
|
|
|
|
if (this->webSocket != nullptr) {
|
|
std::ostringstream msg;
|
|
msg << "SEND:" << user << ":" << json.dump();
|
|
this->webSocket->send(msg.str(), false);
|
|
}
|
|
}
|
|
} // namespace websocket
|
|
} // namespace gaconnector
|
|
} // namespace germanairlinesva
|