From 7de6bbd78fe18416fd8316d9a313108c9ca753fa Mon Sep 17 00:00:00 2001 From: jwillmartin Date: Thu, 27 Jun 2024 14:45:36 -0400 Subject: [PATCH] modified: src/v2i-hub/MapPlugin/src/MapPlugin.cpp modified: src/v2i-hub/MapPlugin/src/MapPlugin.h --- src/v2i-hub/MapPlugin/src/MapPlugin.cpp | 70 +++++++++++++------------ src/v2i-hub/MapPlugin/src/MapPlugin.h | 25 +++++---- 2 files changed, 48 insertions(+), 47 deletions(-) diff --git a/src/v2i-hub/MapPlugin/src/MapPlugin.cpp b/src/v2i-hub/MapPlugin/src/MapPlugin.cpp index 12d783a12..7ee8d4ae7 100644 --- a/src/v2i-hub/MapPlugin/src/MapPlugin.cpp +++ b/src/v2i-hub/MapPlugin/src/MapPlugin.cpp @@ -236,23 +236,33 @@ namespace MapPlugin { } } - std::string MapPlugin::checkMapContent(std::ifstream &in) + std::string MapPlugin::checkMapContent(const std::string &fn) { + PLOG(logDEBUG4) << "In MapPlugin :: checkMapContent"; try { - std::string content((std::istreambuf_iterator(in)), std::istreambuf_iterator()); - in.close(); - // Remove any newline characters - content.erase(remove(content.begin(), content.end(), '\n'), content.end()); - PLOG(logDEBUG4) << "Map without newline " << content; - std::string payload = removeMessageFrame(content); - - return payload; + std::ifstream in(fn.c_str(), std::ios::binary); + if (!in) + { + PLOG(logERROR) << "Failed to open file: " << fn.c_str(); + throw std::ios_base::failure("Failed to open file: " + fn); + } + else + { + std::string content((std::istreambuf_iterator(in)), std::istreambuf_iterator()); + in.close(); + // Remove any newline characters + content.erase(remove(content.begin(), content.end(), '\n'), content.end()); + PLOG(logDEBUG4) << "Map without newline " << content; + std::string payload = removeMessageFrame(content); + + return payload; + } } catch (const std::ios_base::failure& e) { PLOG(logERROR) << "Exception encountered while reading file: " << e.what(); - return ""; + throw; } } @@ -279,10 +289,10 @@ namespace MapPlugin { inType = "ISD"; else if (fn.substr(fn.size() - 4) == ".txt") inType = "TXT"; - else if (fn.substr(fn.size()- 5) == ".uper") - inType ="UPER"; - else + else if (fn.substr(fn.size() - 4) == ".xml") inType = "XML"; + else + PLOG(logWARNING) << "Incorrect MapFile extension entered!"; if (inType == "ISD") { @@ -293,30 +303,22 @@ namespace MapPlugin { } else if (inType == "TXT") { - std::ifstream in(fn, std::ios::binary); - if (!in) - { - PLOG(logERROR) << "Failed to open file: " << fn; - } - else + std::string payload = checkMapContent(fn); + byte_stream bytes; + std::istringstream streamableContent(payload); + streamableContent >> bytes; + PLOG(logINFO) << "MAP encoded bytes are " << bytes; + tmx::messages::MapDataMessage *mapMsg = tmx::messages::MapDataEncodedMessage::decode_j2735_message>(bytes); + + if (mapMsg) { - std::string payload = checkMapContent(in); - byte_stream bytes; - std::istringstream streamableContent(payload); - streamableContent >> bytes; - PLOG(logINFO) << "MAP encoded bytes are " << bytes; - tmx::messages::MapDataMessage *mapMsg = tmx::messages::MapDataEncodedMessage::decode_j2735_message>(bytes); - - if (mapMsg) - { - PLOG(logDEBUG) << "Map is " << *mapMsg; + PLOG(logDEBUG) << "Map is " << *mapMsg; - tmx::messages::MapDataEncodedMessage mapEnc; - mapEnc.encode_j2735_message(*mapMsg); - mapFile.set_Bytes(mapEnc.get_payload_str()); + tmx::messages::MapDataEncodedMessage mapEnc; + mapEnc.encode_j2735_message(*mapMsg); + mapFile.set_Bytes(mapEnc.get_payload_str()); - PLOG(logINFO) << "J2735 message bytes encoded as " << mapFile.get_Bytes(); - } + PLOG(logINFO) << "J2735 message bytes encoded as " << mapFile.get_Bytes(); } } else if (inType == "XML") diff --git a/src/v2i-hub/MapPlugin/src/MapPlugin.h b/src/v2i-hub/MapPlugin/src/MapPlugin.h index 5ede90e3e..f0da14270 100644 --- a/src/v2i-hub/MapPlugin/src/MapPlugin.h +++ b/src/v2i-hub/MapPlugin/src/MapPlugin.h @@ -65,7 +65,7 @@ class MapFile: public tmx::message { std_attribute(this->msg, std::string, InputType, "", ); std_attribute(this->msg, std::string, Bytes, "", ); - static tmx::message_tree_type to_tree(const MapFile& m) { + static tmx::message_tree_type to_tree(const MapFile &m) { return tmx::message::to_tree(static_cast(m)); } @@ -89,27 +89,26 @@ class MapPlugin: public tmx::utils::PluginClientClockAware { void OnMessageReceived(IvpMessage *msg) override; void OnStateChange(IvpPluginState state) override; + bool LoadMapFiles(); + void DebugPrintMapFiles(); + + std::string enum_to_hex_string(); + std::string removeMessageFrame(const std::string &fileContent); + std::string checkMapContent(const std::string &fn); + private: + tmx::messages::J2735MessageFactory factory; + tmx::utils::FrequencyThrottle errThrottle; + std::atomic _mapAction {-1}; std::atomic _isMapFileNew {false}; std::atomic _cohdaR63 {false}; - std::map _mapFiles; std::mutex data_lock; - - tmx::messages::J2735MessageFactory factory; - + std::map _mapFiles; int sendFrequency = 1000; - tmx::utils::FrequencyThrottle errThrottle; std::array mapID_buffer; - - bool LoadMapFiles(); - void DebugPrintMapFiles(); - std::string enum_to_hex_string(); - std::string removeMessageFrame(const std::string &fileContent); - std::string checkMapContent(std::ifstream &in); - }; } // namespace MapPlugin