From 2cf03d293328c1e5e61eddcd25d640779a6f3a8d Mon Sep 17 00:00:00 2001 From: Kilian Hofmann Date: Sun, 11 Sep 2022 21:14:46 +0200 Subject: [PATCH] Refactor SimDatabase --- .vscode/settings.json | 3 +- build.sh | 6 +- file/CMakeLists.txt | 4 - file/include/logbook/logbook.h | 3 +- file/include/simdata/simDatabase.h | 77 +++++++++ file/include/simdata/simdataXP.h | 10 +- file/include/simdata/simulatorDatabase.hpp | 174 --------------------- file/logbook.cpp | 14 +- file/simDatabase.cpp | 121 ++++++++++++++ file/simdataXP.cpp | 22 +-- ixwebsocket/CMakeLists.txt | 9 +- utilities/include/util.hpp | 4 +- xplugin/CMakeLists.txt | 10 +- xplugin/include/main.h | 2 +- xplugin/main.cpp | 51 +++--- 15 files changed, 255 insertions(+), 255 deletions(-) create mode 100644 file/include/simdata/simDatabase.h delete mode 100644 file/include/simdata/simulatorDatabase.hpp create mode 100644 file/simDatabase.cpp diff --git a/.vscode/settings.json b/.vscode/settings.json index 74b74e8..31ce5a9 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -63,6 +63,7 @@ "thread": "cpp", "cinttypes": "cpp", "typeinfo": "cpp", - "valarray": "cpp" + "valarray": "cpp", + "__threading_support": "cpp" }, } \ No newline at end of file diff --git a/build.sh b/build.sh index e0e2975..908865e 100755 --- a/build.sh +++ b/build.sh @@ -17,11 +17,15 @@ case $1 in ;; "win32") cmake -DDEBUG=$DEBUG -DBIT=32 -DCMAKE_TOOLCHAIN_FILE=../toolchain-win-32.cmake .. + \cp -rf ../openSSL/win32/*.dll ESP/GAConnector/ + \cp -rf /opt/llvm-mingw/i686-w64-mingw32/bin/libc++.dll ESP/GAConnector/ + \cp -rf /opt/llvm-mingw/i686-w64-mingw32/bin/libunwind.dll ESP/GAConnector/ ;; "win64") cmake -DDEBUG=$DEBUG -DBIT=64 -DCMAKE_TOOLCHAIN_FILE=../toolchain-win-64.cmake .. \cp -rf ../openSSL/win64/*.dll X-Plane/GAConnector/64/ - \cp -rf ../openSSL/win64/*.dll ESP/GAConnector/ + \cp -rf /opt/llvm-mingw/x86_64-w64-mingw32/bin/libc++.dll X-Plane/GAConnector/64 + \cp -rf /opt/llvm-mingw/x86_64-w64-mingw32/bin/libunwind.dll X-Plane/GAConnector/64 ;; esac diff --git a/file/CMakeLists.txt b/file/CMakeLists.txt index cca5cc0..fba2a1c 100644 --- a/file/CMakeLists.txt +++ b/file/CMakeLists.txt @@ -81,8 +81,4 @@ elseif(WIN32) -Wl,-pdb= ) endif() - target_link_options(file PRIVATE - -static-libgcc - -static-libstdc++ - ) endif() diff --git a/file/include/logbook/logbook.h b/file/include/logbook/logbook.h index 7178c20..c8b6177 100644 --- a/file/include/logbook/logbook.h +++ b/file/include/logbook/logbook.h @@ -2,7 +2,6 @@ #define GERMANAIRLINESVA_FILE_LOGBOOK_H #include -#include #include #include #include @@ -33,7 +32,7 @@ namespace file private: std::vector entries; - void fromFile(const std::string &file); + void fromFile(); void readVersion1(std::ifstream &in); public: diff --git a/file/include/simdata/simDatabase.h b/file/include/simdata/simDatabase.h new file mode 100644 index 0000000..78e2e3d --- /dev/null +++ b/file/include/simdata/simDatabase.h @@ -0,0 +1,77 @@ +#ifndef GERMANAIRLINESVA_FILE_SIMDATABASE_H +#define GERMANAIRLINESVA_FILE_SIMDATABASE_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "config/config.h" +#include "constants.h" +#include "simdata/gate.h" +#include "simdata/runway.h" +#include "simdata/simdataXP.h" + +/* + * Header + * + * CHAR[5] | UINT8 + * --------+-------- + * VGAS | VERSION + */ +/* + * Airport + * + * UINT8 | CHAR[] | UINT16 | GATE[] | UINT8 | RUNWAY[] + * --------+--------+----------+--------+---------+--------- + * STRLEN | ICAO | NUMGATES | GATES | NUMRWYS | RUNWAYS + */ + +namespace germanairlinesva +{ +namespace file +{ + namespace simdata + { + class SimDatabase + { + private: + std::map< + std::string, + std::pair, + std::vector>> + airports; + + void fromFile(); + void readVersion1(std::ifstream &in); + + public: + SimDatabase(int xPlaneVersion, + const char *hash, + std::unique_ptr &configuration, + std::function toLog); + + void toFile() const; + + inline std::size_t getCount() const { return this->airports.size(); } + inline const std::pair, const std::vector> + operator[](std::string key) + { + try { + return this->airports.at(key); + } catch (std::out_of_range &ex) { + return std::pair, const std::vector>(); + } + } + }; + } // namespace simdata +} // namespace file +} // namespace germanairlinesva + +#endif diff --git a/file/include/simdata/simdataXP.h b/file/include/simdata/simdataXP.h index fc3fe0f..5789b37 100644 --- a/file/include/simdata/simdataXP.h +++ b/file/include/simdata/simdataXP.h @@ -19,20 +19,20 @@ namespace file const std::string defaultFile, const std::string sceneryPack, const std::string logFile, - std::map, std::vector>> + std::map, std::vector>> &airports); void makeAirport( const std::string &kind, std::ifstream &infile, - std::map, std::vector>> + std::map, std::vector>> &airports, std::ofstream &logfile); - void makeGate15(std::vector &gates, + void makeGate15(std::vector &gates, const std::vector &fields); - void makeRunway(std::vector &runways, + void makeRunway(std::vector &runways, const std::vector &fields); - void makeGate1300(std::vector &gates, + void makeGate1300(std::vector &gates, const std::vector &fields); } // namespace simdata } // namespace file diff --git a/file/include/simdata/simulatorDatabase.hpp b/file/include/simdata/simulatorDatabase.hpp deleted file mode 100644 index 86dcc37..0000000 --- a/file/include/simdata/simulatorDatabase.hpp +++ /dev/null @@ -1,174 +0,0 @@ -#ifndef GERMANAIRLINESVA_FILE_SIMULATORDATABASE_H -#define GERMANAIRLINESVA_FILE_SIMULATORDATABASE_H - -#include -#include -#include -#include -#include -#include -#include - -#include "simdata/gate.h" -#include "simdata/runway.h" - -/* - * Header - * - * CHAR[5] | UINT8 - * --------+-------- - * VGAS | VERSION - */ -/* - * Airport - * - * UINT8 | CHAR[] | UINT16 | GATE[] | UINT8 | RUNWAY[] - * --------+--------+----------+--------+---------+--------- - * STRLEN | ICAO | NUMGATES | GATES | NUMRWYS | RUNWAYS - */ - -namespace germanairlinesva -{ -namespace file -{ - namespace simdata - { - static inline void toFile( - std::map< - std::string, - std::pair, - std::vector>> - &airports, - const std::string &file) - { - std::uint8_t null = 0; - std::ofstream out(file, std::fstream::binary); - - // File Header, Last member is version - std::uint8_t header[] = {'V', 'G', 'A', 'S', '\0', 1}; - out.write(reinterpret_cast(header), 6); - // Num Airports - std::uint16_t numAirports = airports.size(); - out.write(reinterpret_cast(&numAirports), - sizeof(numAirports)); - // Airport - for (const std::pair< - const std::string, - std::pair, - std::vector>> - &airport : airports) { - std::string icao = airport.first; - std::vector gates = - airport.second.first; - std::vector runways = - airport.second.second; - // ICAO - std::uint8_t icaoLength = icao.length(); - out.write(reinterpret_cast(&icaoLength), - sizeof(icaoLength)); - out.write(icao.c_str(), icaoLength); - out.write(reinterpret_cast(&null), sizeof(null)); - // Gates - std::uint16_t numGates = gates.size(); - out.write(reinterpret_cast(&numGates), sizeof(numGates)); - for (germanairlinesva::file::simdata::Gate &gate : gates) { - gate.toFile(out); - } - // Runways - std::uint8_t numRunways = runways.size(); - out.write(reinterpret_cast(&numRunways), - sizeof(numRunways)); - for (germanairlinesva::file::simdata::Runway &runway : runways) { - runway.toFile(out); - } - } - out.close(); - } - - static inline std::map< - std::string, - std::pair, - std::vector>> - readVersion1(std::ifstream &in) - { - std::map, - std::vector>> - airports; - - std::uint16_t numAirports; - in.read(reinterpret_cast(&numAirports), sizeof(numAirports)); - - for (int i = 0; i < numAirports; i++) { - // ICAO - std::uint8_t icaoLength; - in.read(reinterpret_cast(&icaoLength), sizeof(icaoLength)); - char *icao = static_cast(calloc(icaoLength + 1, sizeof(char))); - in.read(icao, icaoLength + 1); - // Gates - std::uint16_t numGates; - in.read(reinterpret_cast(&numGates), sizeof(numGates)); - for (int j = 0; j < numGates; j++) { - std::string designator = readString(in); - struct germanairlinesva::geodata::point center = - read(in); - std::uint8_t radius = read(in); - - airports[icao].first.emplace_back(designator, center, radius); - } - // Runways - std::uint8_t numRunways; - in.read(reinterpret_cast(&numRunways), sizeof(numRunways)); - for (int j = 0; j < numRunways; j++) { - std::string designator = readString(in); - // Bounds - struct germanairlinesva::geodata::box bounds = - read(in); - std::uint8_t width = read(in); - std::uint16_t length = read(in); - std::uint16_t trueHeading = read(in); - - airports[icao].second.emplace_back(designator, - bounds, - width, - length, - trueHeading); - } - } - - in.close(); - - return airports; - } - - static inline std::map< - std::string, - std::pair, - std::vector>> - fromFile(const std::string &file) - { - std::map, - std::vector>> - airports; - std::ifstream in(file, std::ifstream::binary); - - // File Header - char ident[5]; - in.read(ident, 5); - if (strcmp(ident, "VGAS") != 0) { - throw std::invalid_argument("Wrong file"); - } - std::uint8_t version; - in.read(reinterpret_cast(&version), 1); - - if (version == 1) { - return readVersion1(in); - } - return airports; - } - } // namespace simdata -} // namespace file -} // namespace germanairlinesva - -#endif diff --git a/file/logbook.cpp b/file/logbook.cpp index 9a571b7..f9600dd 100644 --- a/file/logbook.cpp +++ b/file/logbook.cpp @@ -9,22 +9,20 @@ namespace file Logbook::Logbook() { if (germanairlinesva::util::fileExists(XPLANE_PLUGIN_DIRECTORY LOGBOOK)) { - this->fromFile(XPLANE_PLUGIN_DIRECTORY LOGBOOK); + this->fromFile(); } } - void Logbook::fromFile(const std::string &file) + void Logbook::fromFile() { - std::ifstream in(file, std::ifstream::binary); + std::ifstream in(XPLANE_PLUGIN_DIRECTORY LOGBOOK, std::ifstream::binary); // File Header - char ident[5]; - in.read(ident, 5); - if (strcmp(ident, "VGAL") != 0) { + std::string ident = readString(in, 5); + if (ident.compare("VGAL") != 0) { throw std::invalid_argument("Wrong file"); } - std::uint8_t version; - in.read(reinterpret_cast(&version), 1); + std::uint8_t version = read(in); if (version == 1) { while (in.peek() != EOF) { diff --git a/file/simDatabase.cpp b/file/simDatabase.cpp new file mode 100644 index 0000000..a3bf95a --- /dev/null +++ b/file/simDatabase.cpp @@ -0,0 +1,121 @@ +#include "simdata/simDatabase.h" + +namespace germanairlinesva +{ +namespace file +{ + namespace simdata + { + SimDatabase::SimDatabase(int xPlaneVersion, + const char *hash, + std::unique_ptr &configuration, + std::function toLog) + { + if (strcmp(configuration->getScenery().c_str(), hash) != 0 || + !util::fileExists(XPLANE_PLUGIN_DIRECTORY SIMDATABASE)) { + scan(xPlaneVersion < 12000 ? XPLANE11_BASE_SCENERY + : XPLANE12_BASE_SCENERY, + XPLANE_CUSTOM_SCENERY, + XPLANE_PLUGIN_DIRECTORY "log.txt", + airports); + + configuration->updateScenery(hash); + this->toFile(); + + toLog("Sim Database updated"); + } else { + this->fromFile(); + } + } + + void SimDatabase::fromFile() + { + std::ifstream in(XPLANE_PLUGIN_DIRECTORY SIMDATABASE, + std::ifstream::binary); + + // File Header + std::string ident = readString(in, 5); + if (ident.compare("VGAS") != 0) { + throw std::invalid_argument("Wrong file"); + } + std::uint8_t version = read(in); + + if (version == 1) { + this->readVersion1(in); + } + } + + void SimDatabase::readVersion1(std::ifstream &in) + { + std::uint16_t numAirports = read(in); + + for (int i = 0; i < numAirports; i++) { + // ICAO + std::string icao = readString(in); + // Gates + std::uint16_t numGates = read(in); + for (int j = 0; j < numGates; j++) { + std::string designator = readString(in); + struct germanairlinesva::geodata::point center = + read(in); + std::uint8_t radius = read(in); + + this->airports[icao].first.emplace_back(designator, center, radius); + } + // Runways + std::uint8_t numRunways = read(in); + for (int j = 0; j < numRunways; j++) { + std::string designator = readString(in); + // Bounds + struct germanairlinesva::geodata::box bounds = + read(in); + std::uint8_t width = read(in); + std::uint16_t length = read(in); + std::uint16_t trueHeading = read(in); + + this->airports[icao].second.emplace_back(designator, + bounds, + width, + length, + trueHeading); + } + } + + in.close(); + } + + void SimDatabase::toFile() const + { + std::ofstream out(XPLANE_PLUGIN_DIRECTORY SIMDATABASE, + std::fstream::binary); + + // File Header, Last member is version + std::uint8_t header[] = {'V', 'G', 'A', 'S', '\0', 1}; + out.write(reinterpret_cast(header), 6); + // Num Airports + write(out, airports.size()); + // Airport + for (const 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(); + } + } // namespace simdata +} // namespace file +} // namespace germanairlinesva \ No newline at end of file diff --git a/file/simdataXP.cpp b/file/simdataXP.cpp index dfdd8d4..dc2d0e3 100644 --- a/file/simdataXP.cpp +++ b/file/simdataXP.cpp @@ -10,7 +10,7 @@ namespace file const std::string defaultFile, const std::string sceneryPack, const std::string logFile, - std::map, std::vector>> + std::map, std::vector>> &airports) { std::ifstream base(defaultFile); @@ -71,14 +71,14 @@ namespace file void makeAirport( const std::string &kind, std::ifstream &infile, - std::map, std::vector>> + std::map, std::vector>> &airports, std::ofstream &logfile) { std::string line; std::string *currentIcao = nullptr; - std::vector tmpGates; - std::vector tmpRunways; + std::vector tmpGates; + std::vector tmpRunways; int apCount = 0; int validCount = 0; @@ -104,8 +104,8 @@ namespace file logfile << "\t " << *currentIcao << " had no gates or runways" << std::endl; } - tmpGates = std::vector(); - tmpRunways = std::vector(); + tmpGates = std::vector(); + tmpRunways = std::vector(); currentIcao = new std::string(fields[4]); apCount += 1; logfile << "\t<" << kind << "> " << line << std::endl; @@ -124,8 +124,8 @@ namespace file logfile << "\t " << *currentIcao << " had no gates or runways" << std::endl; } - tmpGates = std::vector(); - tmpRunways = std::vector(); + tmpGates = std::vector(); + tmpRunways = std::vector(); currentIcao = nullptr; logfile << "\t<" << kind << " SKIPPED> " << line << std::endl; } else if (currentIcao != nullptr && fields[0] == "100") { @@ -146,7 +146,7 @@ namespace file << validCount << " are valid" << std::endl; } - void makeGate15(std::vector &gates, + void makeGate15(std::vector &gates, const std::vector &fields) { std::string gateName; @@ -161,7 +161,7 @@ namespace file gates.emplace_back(gateName, pt, 40); } - void makeRunway(std::vector &runways, + void makeRunway(std::vector &runways, const std::vector &fields) { runways.emplace_back(fields[8], @@ -178,7 +178,7 @@ namespace file std::stod(fields[1])); } - void makeGate1300(std::vector &gates, + void makeGate1300(std::vector &gates, const std::vector &fields) { std::string gateName; diff --git a/ixwebsocket/CMakeLists.txt b/ixwebsocket/CMakeLists.txt index 0c322fe..f22ac80 100644 --- a/ixwebsocket/CMakeLists.txt +++ b/ixwebsocket/CMakeLists.txt @@ -78,7 +78,6 @@ elseif(UNIX) ) target_link_libraries(ixwebsocket PRIVATE ${OPENSSL_LIBRARIES} - pthread ) elseif(WIN32) if (BIT STREQUAL "32") @@ -107,10 +106,6 @@ elseif(WIN32) -Wl,-pdb= ) endif() - target_link_options(ixwebsocket PRIVATE - -static-libgcc - -static-libstdc++ - ) target_include_directories(ixwebsocket PRIVATE ${CMAKE_SOURCE_DIR}/openSSL/win${BIT}/include ) @@ -124,4 +119,8 @@ elseif(WIN32) ) endif() +target_link_libraries(ixwebsocket PRIVATE + pthread +) + add_library(ixwebsocket::ixwebsocket ALIAS ixwebsocket) \ No newline at end of file diff --git a/utilities/include/util.hpp b/utilities/include/util.hpp index 4ca61e4..3778ca0 100644 --- a/utilities/include/util.hpp +++ b/utilities/include/util.hpp @@ -170,7 +170,7 @@ namespace util static inline int generateMD5(const char *filepath, char *lastHash, - const std::function &toLog) + const std::function toLog) { int file_descript; unsigned long file_size; @@ -205,7 +205,7 @@ namespace util static inline int generateMD5(const char *filepath, char *buffer, - const std::function &toLog) + const std::function toLog) { int file_descriptor; unsigned long file_size; diff --git a/xplugin/CMakeLists.txt b/xplugin/CMakeLists.txt index e226651..e7b7e10 100644 --- a/xplugin/CMakeLists.txt +++ b/xplugin/CMakeLists.txt @@ -80,9 +80,6 @@ elseif(UNIX) target_compile_options(germanairlinesva_xplugin PRIVATE -nodefaultlibs ) - target_link_libraries(germanairlinesva_xplugin PRIVATE - pthread - ) elseif(WIN32) message("Building for Windows ${BIT} into ${PROJECT_BINARY_DIR}/X-Plane/${PLUGIN_NAME}/${BIT}") @@ -102,12 +99,6 @@ elseif(WIN32) -Wl,-pdb= ) endif() - target_link_options(germanairlinesva_xplugin PRIVATE - -static-libgcc - -static-libstdc++ - ) - target_link_libraries(germanairlinesva_xplugin PRIVATE - ) target_link_libraries(germanairlinesva_xplugin PRIVATE ${CMAKE_SOURCE_DIR}/XPSDK/Libraries/Win/XPLM_64.lib ) @@ -118,4 +109,5 @@ endif() target_link_libraries(germanairlinesva_xplugin PRIVATE ixwebsocket + pthread ) diff --git a/xplugin/include/main.h b/xplugin/include/main.h index 8cececa..01aa904 100644 --- a/xplugin/include/main.h +++ b/xplugin/include/main.h @@ -5,8 +5,8 @@ #include "constants.h" #include "logbook/logbook.h" #include "recording/recording.h" +#include "simdata/simDatabase.h" #include "simdata/simdataXP.h" -#include "simdata/simulatorDatabase.hpp" #include "util.hpp" #include "websocket.h" diff --git a/xplugin/main.cpp b/xplugin/main.cpp index d73862d..535e464 100644 --- a/xplugin/main.cpp +++ b/xplugin/main.cpp @@ -10,12 +10,9 @@ std::thread serverThread; std::thread recordingThread; std::atomic wantsExit; -germanairlinesva::file::config::Config configuration; -std::map, - std::vector>> - airports; -germanairlinesva_websocket::Websocket *connector; +std::unique_ptr configuration; +std::unique_ptr database; +std::unique_ptr connector; int xplaneVersion; /* Datarefs */ @@ -114,52 +111,39 @@ PLUGIN_API int XPluginStart(char *outName, char *outSig, char *outDesc) roll = XPLMFindDataRef("sim/flightmodel/position/phi"); // FLOAT quaternion = XPLMFindDataRef("sim/flightmodel/position/q"); // FLOAT[4] - configuration = germanairlinesva::file::config::Config(); + configuration = std::make_unique(); toLog("Config loaded"); try { - connector = new germanairlinesva_websocket::Websocket( + connector = std::make_unique( "wss://ws.hofmannnet.myhome-server.de:8000", - configuration.getUser(), + configuration->getUser(), toLog); } catch (const std::invalid_argument &e) { toLog(e.what()); return 0; } toLog("WebSocket started"); - char hash[2 * MD5LEN + 1] = ""; if (germanairlinesva::util::generateMD5(XPLANE_CUSTOM_SCENERY, hash, toLog) == 0) { - - if (strcmp(configuration.getScenery().c_str(), hash) != 0 || - !germanairlinesva::util::fileExists( - XPLANE_PLUGIN_DIRECTORY SIMDATABASE)) { - scan(xplaneVersion < 12000 ? XPLANE11_BASE_SCENERY - : XPLANE12_BASE_SCENERY, - XPLANE_CUSTOM_SCENERY, - XPLANE_PLUGIN_DIRECTORY "log.txt", - airports); - germanairlinesva::file::simdata::toFile( - airports, - XPLANE_PLUGIN_DIRECTORY SIMDATABASE); - - configuration.updateScenery(hash); - toLog("Sim Database updated"); - } else { - airports = germanairlinesva::file::simdata::fromFile( - XPLANE_PLUGIN_DIRECTORY SIMDATABASE); - toLog("Sim Database loaded"); - } + database = std::make_unique( + xplaneVersion, + hash, + configuration, + toLog); toLog("Readback test of sim database using EDDF"); - auto ap = airports["EDDF"]; + auto ap = (*database)["EDDF"]; for (const auto &it : ap.first) { toLog(" " + it.to_string()); } for (const auto &it : ap.second) { toLog(" " + it.to_string()); } + toLog("Readback test of sim database using XXXX"); + auto ap2 = (*database)["XXXX"]; + ap2.first.size() == 0 ? toLog(" SUCCESS") : toLog(" ERROR"); } // Thread for sending data to websocket @@ -209,10 +193,13 @@ PLUGIN_API void XPluginStop(void) XPLMUnregisterFlightLoopCallback(flightLoop, nullptr); /* End threads */ wantsExit = true; - delete connector; serverThread.join(); recordingThread.join(); + delete connector.release(); + delete database.release(); + delete configuration.release(); + p.toFile("flight.rec"); }