Namespaces

Better File handling
This commit is contained in:
2022-09-06 00:29:19 +02:00
parent 9dcca746ed
commit b22a60c85e
24 changed files with 1106 additions and 781 deletions
+46 -43
View File
@@ -3,53 +3,56 @@
#include <cstdint>
/* Structures and enums */
typedef struct data {
std::int32_t pause = 0;
float pBrake = 0;
std::int32_t onGrnd = 0;
float totFuelKg = 0;
float truHdg = 0;
double alt = 0;
float gs = 0;
float ias = 0;
float vs = 0;
double lat = 0;
double lon = 0;
float ff[8] = {0, 0, 0, 0, 0, 0, 0, 0};
float maxSpd = 0;
char path[513] = "";
float uptime = 0;
float magHeading = 0;
float payloadKg = 0;
float totalWeightKg = 0;
} data;
typedef enum commands {
PROCESS,
SAVE,
LOAD,
TEXT,
TIME,
UNPAUSE,
PAUSE,
PORT,
END
} commands;
typedef struct command_base {
commands type;
} command_base;
#pragma pack(push) /* push current alignment to stack */
#pragma pack(1) /* set alignment to 1 byte boundary */
namespace germanairlinesva_websocket
{
/* Structures and enums */
typedef struct data {
std::int32_t pause = 0;
float pBrake = 0;
std::int32_t onGrnd = 0;
float totFuelKg = 0;
float truHdg = 0;
double alt = 0;
float gs = 0;
float ias = 0;
float vs = 0;
double lat = 0;
double lon = 0;
float ff[8] = {0, 0, 0, 0, 0, 0, 0, 0};
float maxSpd = 0;
char path[513] = "";
float uptime = 0;
float magHeading = 0;
float payloadKg = 0;
float totalWeightKg = 0;
} data;
typedef struct command_port {
double latitude;
double longitude;
float trueHeading;
} command_port;
typedef enum commands {
PROCESS,
SAVE,
LOAD,
TEXT,
TIME,
UNPAUSE,
PAUSE,
PORT,
END
} commands;
typedef struct command_base {
commands type;
} command_base;
typedef struct command_port {
double latitude;
double longitude;
float trueHeading;
} command_port;
#pragma pack(pop) /* restore original alignment from stack */
} // namespace germanairlinesva_websocket
#endif
+19 -16
View File
@@ -19,23 +19,26 @@
#include <string>
#include <utility>
class Websocket
namespace germanairlinesva_websocket
{
private:
char lastPath[513] = "";
char lastHash[2 * MD5LEN + 1] = "";
ix::WebSocket *webSocket = nullptr;
std::string host;
std::string user;
std::function<void(std::string)> toLog;
class Websocket
{
private:
char lastPath[513] = "";
char lastHash[2 * MD5LEN + 1] = "";
ix::WebSocket *webSocket = nullptr;
std::string host;
std::string user;
std::function<void(std::string)> toLog;
public:
explicit Websocket(std::string host,
std::string user,
std::function<void(const std::string)> toLog);
~Websocket();
void onClientMessageCallback(const ix::WebSocketMessagePtr &msg);
void sendData(data &d);
};
public:
explicit Websocket(std::string host,
std::string user,
std::function<void(const std::string)> toLog);
~Websocket();
void onClientMessageCallback(const ix::WebSocketMessagePtr &msg);
void sendData(data &d);
};
} // namespace germanairlinesva_websocket
#endif
+88 -85
View File
@@ -1,107 +1,110 @@
#include "include/websocket.h"
Websocket::Websocket(std::string host,
std::string user,
std::function<void(const std::string)> toLog)
: host(host), user(user), toLog(std::move(toLog))
namespace germanairlinesva_websocket
{
Websocket::Websocket(std::string host,
std::string user,
std::function<void(const std::string)> toLog)
: host(host), user(user), toLog(std::move(toLog))
{
#ifdef IBM
// Required on Windows
ix::initNetSystem();
// Required on Windows
ix::initNetSystem();
#endif
webSocket = new ix::WebSocket();
webSocket->enableAutomaticReconnection();
webSocket->setUrl(host);
webSocket->setOnMessageCallback([this](const ix::WebSocketMessagePtr &msg) {
this->onClientMessageCallback(msg);
});
webSocket->start();
}
webSocket = new ix::WebSocket();
webSocket->enableAutomaticReconnection();
webSocket->setUrl(host);
webSocket->setOnMessageCallback([this](const ix::WebSocketMessagePtr &msg) {
this->onClientMessageCallback(msg);
});
webSocket->start();
}
Websocket::~Websocket()
{
webSocket->stop();
toLog("WebSocket stopped");
Websocket::~Websocket()
{
webSocket->stop();
toLog("WebSocket stopped");
#ifdef IBM
// Required on Windows
ix::uninitNetSystem();
// 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 << "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());
webSocket->send("MASTER:" + user);
toLog("Connecting as " + user);
} else if (msg->type == ix::WebSocketMessageType::Close) {
if (msg->closeInfo.reason.compare("DUPLICATE")) {
webSocket->disableAutomaticReconnection();
toLog("Disconnected due to beeing a duplicate simualtor");
} else {
void Websocket::onClientMessageCallback(const ix::WebSocketMessagePtr &msg)
{
if (msg->type == ix::WebSocketMessageType::Open) {
std::stringstream debug_msg;
debug_msg << "Connection closed" << 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;
debug_msg << "New connection" << 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());
}
} else if (msg->type == ix::WebSocketMessageType::Error) {
std::stringstream debug_msg;
debug_msg << "Connection error" << 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;
webSocket->send("MASTER:" + user);
toLog("Connecting as " + user);
} else if (msg->type == ix::WebSocketMessageType::Close) {
if (msg->closeInfo.reason.compare("DUPLICATE")) {
webSocket->disableAutomaticReconnection();
toLog(debug_msg.str());
} else if (msg->type == ix::WebSocketMessageType::Message) {
if (!msg->str.empty()) {
toLog(msg->str);
}
}
}
toLog("Disconnected due to beeing a duplicate simualtor");
} else {
std::stringstream debug_msg;
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");
debug_msg << "Connection closed" << 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());
}
} else if (msg->type == ix::WebSocketMessageType::Error) {
std::stringstream debug_msg;
debug_msg << "Connection error" << 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());
} else if (msg->type == ix::WebSocketMessageType::Message) {
if (!msg->str.empty()) {
toLog(msg->str);
}
}
}
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},
};
void Websocket::sendData(data &d)
{
if (strcmp(d.path, lastPath) != 0) {
strcpy(lastPath, d.path);
if (germanairlinesva_util::generateMD5(d.path, lastHash, toLog)) {
strcpy(lastHash, "NOT SET");
}
}
if (webSocket != nullptr) {
std::ostringstream msg;
msg << "SEND:" << user << ":" << json.dump();
webSocket->send(msg.str(), false);
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},
};
if (webSocket != nullptr) {
std::ostringstream msg;
msg << "SEND:" << user << ":" << json.dump();
webSocket->send(msg.str(), false);
}
}
}
} // namespace germanairlinesva_websocket