From 176c43d5a688f48debde9933332a15e799441e3d Mon Sep 17 00:00:00 2001 From: Kilian Hofmann Date: Sat, 22 Oct 2022 01:42:23 +0200 Subject: [PATCH] Fix Database Add WS Header --- file/include/simdata/simDatabase.hpp | 72 +++++++++++++--------------- recorder/recorder.cpp | 1 + websocket/include/websocket.h | 2 + websocket/websocket.cpp | 6 ++- 4 files changed, 42 insertions(+), 39 deletions(-) diff --git a/file/include/simdata/simDatabase.hpp b/file/include/simdata/simDatabase.hpp index a4bc119..a0c91f0 100644 --- a/file/include/simdata/simDatabase.hpp +++ b/file/include/simdata/simDatabase.hpp @@ -91,10 +91,8 @@ namespace file in.close(); } - inline void fromFile(const char *file) + inline void fromFile(const char *path) { - std::string path(BASE_DIRECTORY); - path.append(file); std::ifstream in(path, std::ifstream::binary); std::string ident = readString(in, 4); @@ -108,6 +106,39 @@ namespace file } } + inline void toFile(const char *path) const + { + std::ofstream out(path, std::fstream::binary); + + // File Header + out.write(SIMDATABASE_HEADER, 4); + out.write("\1", 1); + // Num Airports + write(out, airports.size()); + // Airport + for (const std::pair< + const std::string, + std::pair, std::vector>> &airport : + airports) { + std::string icao = airport.first; + const std::vector gates = airport.second.first; + const std::vector runways = airport.second.second; + // ICAO + writeString(out, icao); + // Gates + write(out, gates.size()); + for (const Gate &gate : gates) { + gate.toFile(out); + } + // Runways + write(out, runways.size()); + for (const Runway &runway : runways) { + runway.toFile(out); + } + } + out.close(); + } + inline const char *resolveFilename(int simVersion) const { switch (simVersion) { @@ -165,41 +196,6 @@ namespace file } } - inline void toFile(const char *file) const - { - std::string path(BASE_DIRECTORY); - path.append(file); - std::ofstream out(path, std::fstream::binary); - - // File Header - out.write(SIMDATABASE_HEADER, 4); - out.write("\1", 1); - // Num Airports - write(out, airports.size()); - // Airport - for (const std::pair< - const std::string, - std::pair, std::vector>> &airport : - airports) { - std::string icao = airport.first; - const std::vector gates = airport.second.first; - const std::vector runways = airport.second.second; - // ICAO - writeString(out, icao); - // Gates - write(out, gates.size()); - for (const Gate &gate : gates) { - gate.toFile(out); - } - // Runways - write(out, runways.size()); - for (const Runway &runway : runways) { - runway.toFile(out); - } - } - out.close(); - } - inline std::size_t getCount() const { return this->airports.size(); } inline const std::pair, diff --git a/recorder/recorder.cpp b/recorder/recorder.cpp index 6a3f7a2..c257b1f 100644 --- a/recorder/recorder.cpp +++ b/recorder/recorder.cpp @@ -25,6 +25,7 @@ namespace gaconnector this->connector = std::make_unique(WEBSOCKET_ADDRESS, this->configuration->getUser(), + this->configuration->getToken(), this->toLog); this->toLog("WebSocket started"); diff --git a/websocket/include/websocket.h b/websocket/include/websocket.h index baa4f52..31fa201 100644 --- a/websocket/include/websocket.h +++ b/websocket/include/websocket.h @@ -33,11 +33,13 @@ namespace gaconnector ix::WebSocket *webSocket = nullptr; std::string host; std::string user; + std::string token; std::function toLog; public: Websocket(std::string host, std::string user, + std::string token, std::function toLog); ~Websocket(); void onClientMessageCallback(const ix::WebSocketMessagePtr &msg); diff --git a/websocket/websocket.cpp b/websocket/websocket.cpp index ef5f966..cbc46c6 100644 --- a/websocket/websocket.cpp +++ b/websocket/websocket.cpp @@ -8,8 +8,9 @@ namespace gaconnector { Websocket::Websocket(std::string host, std::string user, + std::string token, std::function toLog) - : host(host), user(user), toLog(std::move(toLog)) + : host(host), user(user), token(token), toLog(std::move(toLog)) { #ifdef IBM // Required on Windows @@ -17,6 +18,9 @@ namespace gaconnector #endif this->webSocket = new ix::WebSocket(); + this->webSocket->setExtraHeaders({ + { "Authentication", "Bearer " + this->token } + }); this->webSocket->enableAutomaticReconnection(); this->webSocket->setUrl(host); this->webSocket->setOnMessageCallback(