From 0561fde18054472754056f5a6ed4dde57dba45c7 Mon Sep 17 00:00:00 2001 From: Adam Date: Wed, 5 Feb 2025 20:13:27 -0800 Subject: [PATCH 01/15] first pass at adding waypoint caching --- .../local_transceiver/src/local_transceiver.cpp | 17 +++++++++++++++++ .../src/local_transceiver_ros_intf.cpp | 11 +++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/network_systems/projects/local_transceiver/src/local_transceiver.cpp b/src/network_systems/projects/local_transceiver/src/local_transceiver.cpp index 1a55264f8..fa944a965 100644 --- a/src/network_systems/projects/local_transceiver/src/local_transceiver.cpp +++ b/src/network_systems/projects/local_transceiver/src/local_transceiver.cpp @@ -22,6 +22,8 @@ #include "at_cmds.h" #include "cmn_hdrs/ros_info.h" #include "cmn_hdrs/shared_constants.h" +#include "filesystem" +#include "fstream" #include "global_path.pb.h" #include "sensors.pb.h" #include "waypoint.pb.h" @@ -318,6 +320,21 @@ custom_interfaces::msg::Path LocalTransceiver::receive() break; } + //need to check if this even works + // save serialized data to local cache (extract to function?) + std::filesystem::path cache_path{"global_waypoint_cache"}; + if (std::filesystem::exists(cache_path)) { + std::filesystem::path cache_temp{"global_waypoint_cache_temp"}; + std::ofstream writeFile("global_waypoint_cache_temp"); + writeFile << receivedDataBuffer; + writeFile.close(); + std::filesystem::rename(cache_temp, cache_path); + } else { + std::ofstream writeFile("global_waypoint_cache"); + writeFile << receivedDataBuffer; + writeFile.close(); + } + custom_interfaces::msg::Path to_publish = parseInMsg(receivedDataBuffer); return to_publish; } diff --git a/src/network_systems/projects/local_transceiver/src/local_transceiver_ros_intf.cpp b/src/network_systems/projects/local_transceiver/src/local_transceiver_ros_intf.cpp index 1c2ce7542..87ba9d241 100644 --- a/src/network_systems/projects/local_transceiver/src/local_transceiver_ros_intf.cpp +++ b/src/network_systems/projects/local_transceiver/src/local_transceiver_ros_intf.cpp @@ -79,6 +79,17 @@ class LocalTransceiverIntf : public NetNode sub_local_path_data = this->create_subscription( ros_topics::LOCAL_PATH, ROS_Q_SIZE, std::bind(&LocalTransceiverIntf::sub_local_path_data_cb, this, std::placeholders::_1)); + + //check for cached waypoints and publish immediately + std::filesystem::path cache_path{"global_waypoint_cache"}; + if (std::filesystem::exists(cache_path)) { + ifstream f("global_waypoint_cache", ios::binary); + if (!f) { + //error: failed to read cache + } + custom_interfaces::msg::Path to_publish = parseInMsg(receivedDataBuffer); + pub_->publish(to_publish); + } } } From 46c43f8f60b22d3dbf45688379442821ffe4c938 Mon Sep 17 00:00:00 2001 From: adambrett40 Date: Wed, 5 Feb 2025 22:40:05 -0800 Subject: [PATCH 02/15] better version, needs testing --- .../local_transceiver/inc/local_transceiver.h | 7 +++++ .../src/local_transceiver.cpp | 29 +++++++++++++++---- .../src/local_transceiver_ros_intf.cpp | 18 +++++++----- 3 files changed, 40 insertions(+), 14 deletions(-) diff --git a/src/network_systems/projects/local_transceiver/inc/local_transceiver.h b/src/network_systems/projects/local_transceiver/inc/local_transceiver.h index 2fbeb94ee..f3853cd38 100644 --- a/src/network_systems/projects/local_transceiver/inc/local_transceiver.h +++ b/src/network_systems/projects/local_transceiver/inc/local_transceiver.h @@ -129,6 +129,13 @@ class LocalTransceiver // TEST bool checkMailbox(); + /** + * @brief Read and parse the data from the global waypoints file, if it exists + * + * @return The global waypoints from the cache + */ + static std::optional getCache(); + private: // Serial port read/write timeout constexpr static const struct timeval TIMEOUT diff --git a/src/network_systems/projects/local_transceiver/src/local_transceiver.cpp b/src/network_systems/projects/local_transceiver/src/local_transceiver.cpp index fa944a965..f132171d8 100644 --- a/src/network_systems/projects/local_transceiver/src/local_transceiver.cpp +++ b/src/network_systems/projects/local_transceiver/src/local_transceiver.cpp @@ -25,6 +25,7 @@ #include "filesystem" #include "fstream" #include "global_path.pb.h" +#include "iostream" #include "sensors.pb.h" #include "waypoint.pb.h" @@ -324,15 +325,19 @@ custom_interfaces::msg::Path LocalTransceiver::receive() // save serialized data to local cache (extract to function?) std::filesystem::path cache_path{"global_waypoint_cache"}; if (std::filesystem::exists(cache_path)) { + std::ofstream writeFile("global_waypoint_cache_temp.txt", std::ios::binary); + if (!writeFile) { + //err + } + writeFile.write(receivedDataBuffer.data(), static_cast(receivedDataBuffer.size())); std::filesystem::path cache_temp{"global_waypoint_cache_temp"}; - std::ofstream writeFile("global_waypoint_cache_temp"); - writeFile << receivedDataBuffer; - writeFile.close(); std::filesystem::rename(cache_temp, cache_path); } else { - std::ofstream writeFile("global_waypoint_cache"); - writeFile << receivedDataBuffer; - writeFile.close(); + std::ofstream writeFile("global_waypoint_cache.txt", std::ios::binary); + if (!writeFile) { + //err + } + writeFile.write(receivedDataBuffer.data(), static_cast(receivedDataBuffer.size())); } custom_interfaces::msg::Path to_publish = parseInMsg(receivedDataBuffer); @@ -370,6 +375,18 @@ custom_interfaces::msg::Path LocalTransceiver::parseInMsg(const std::string & ms return soln; } +std::optional LocalTransceiver::getCache() +{ + std::filesystem::path cache_path{"global_waypoint_cache"}; + if (std::filesystem::exists(cache_path)) { + std::ifstream input("global_waypoint_cache", std::ios::binary); + std::string cachedDataBuffer(std::istreambuf_iterator(input), {}); + custom_interfaces::msg::Path to_publish = parseInMsg(cachedDataBuffer); + return to_publish; + } + return std::nullopt; +} + bool LocalTransceiver::rcvRsp(const AT::Line & expected_rsp) { bio::streambuf buf; diff --git a/src/network_systems/projects/local_transceiver/src/local_transceiver_ros_intf.cpp b/src/network_systems/projects/local_transceiver/src/local_transceiver_ros_intf.cpp index 87ba9d241..122b42811 100644 --- a/src/network_systems/projects/local_transceiver/src/local_transceiver_ros_intf.cpp +++ b/src/network_systems/projects/local_transceiver/src/local_transceiver_ros_intf.cpp @@ -1,3 +1,5 @@ +#include + #include #include #include @@ -9,6 +11,8 @@ #include "cmn_hdrs/ros_info.h" #include "cmn_hdrs/shared_constants.h" +#include "filesystem" +#include "fstream" #include "local_transceiver.h" #include "net_node.h" @@ -81,14 +85,12 @@ class LocalTransceiverIntf : public NetNode std::bind(&LocalTransceiverIntf::sub_local_path_data_cb, this, std::placeholders::_1)); //check for cached waypoints and publish immediately - std::filesystem::path cache_path{"global_waypoint_cache"}; - if (std::filesystem::exists(cache_path)) { - ifstream f("global_waypoint_cache", ios::binary); - if (!f) { - //error: failed to read cache - } - custom_interfaces::msg::Path to_publish = parseInMsg(receivedDataBuffer); - pub_->publish(to_publish); + //note: should check for cache_temp? + // if exists, should be more up-to-date than cache + //also, check if I can just use pub_cb or not since its not done initializing? + auto msg = lcl_trns_->getCache(); + if (msg) { + pub_->publish(*msg); } } } From 5ed355dbb8b1450af2baa24954c08294ee0a07e8 Mon Sep 17 00:00:00 2001 From: adambrett40 <159198197+adambrett40@users.noreply.github.com> Date: Sat, 8 Feb 2025 15:58:41 -0800 Subject: [PATCH 03/15] extract caching to function --- .../src/local_transceiver.cpp | 37 +++++++++++-------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/src/network_systems/projects/local_transceiver/src/local_transceiver.cpp b/src/network_systems/projects/local_transceiver/src/local_transceiver.cpp index f132171d8..b0c8be62e 100644 --- a/src/network_systems/projects/local_transceiver/src/local_transceiver.cpp +++ b/src/network_systems/projects/local_transceiver/src/local_transceiver.cpp @@ -225,6 +225,26 @@ std::optional LocalTransceiver::debugSend(const std::string & cmd) return readRsp(); } +void cacheGlobalWaypoints(std::string receivedDataBuffer) +{ + std::filesystem::path cache_path{"global_waypoint_cache"}; + if (std::filesystem::exists(cache_path)) { + std::ofstream writeFile("global_waypoint_cache_temp.txt", std::ios::binary); + if (!writeFile) { + //err + } + writeFile.write(receivedDataBuffer.data(), static_cast(receivedDataBuffer.size())); + std::filesystem::path cache_temp{"global_waypoint_cache_temp"}; + std::filesystem::rename(cache_temp, cache_path); + } else { + std::ofstream writeFile("global_waypoint_cache.txt", std::ios::binary); + if (!writeFile) { + //err + } + writeFile.write(receivedDataBuffer.data(), static_cast(receivedDataBuffer.size())); + } +} + custom_interfaces::msg::Path LocalTransceiver::receive() { static constexpr int MAX_NUM_RETRIES = 20; @@ -323,22 +343,7 @@ custom_interfaces::msg::Path LocalTransceiver::receive() //need to check if this even works // save serialized data to local cache (extract to function?) - std::filesystem::path cache_path{"global_waypoint_cache"}; - if (std::filesystem::exists(cache_path)) { - std::ofstream writeFile("global_waypoint_cache_temp.txt", std::ios::binary); - if (!writeFile) { - //err - } - writeFile.write(receivedDataBuffer.data(), static_cast(receivedDataBuffer.size())); - std::filesystem::path cache_temp{"global_waypoint_cache_temp"}; - std::filesystem::rename(cache_temp, cache_path); - } else { - std::ofstream writeFile("global_waypoint_cache.txt", std::ios::binary); - if (!writeFile) { - //err - } - writeFile.write(receivedDataBuffer.data(), static_cast(receivedDataBuffer.size())); - } + cacheGlobalWaypoints(receivedDataBuffer); custom_interfaces::msg::Path to_publish = parseInMsg(receivedDataBuffer); return to_publish; From 420f74cc429587332cec729deee74ab8613f1dc9 Mon Sep 17 00:00:00 2001 From: adambrett40 <159198197+adambrett40@users.noreply.github.com> Date: Sat, 8 Feb 2025 16:27:42 -0800 Subject: [PATCH 04/15] basic test for caching --- .../local_transceiver/inc/local_transceiver.h | 2 + .../test/test_local_transceiver.cpp | 72 +++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/src/network_systems/projects/local_transceiver/inc/local_transceiver.h b/src/network_systems/projects/local_transceiver/inc/local_transceiver.h index 3459bcda8..01584b72c 100644 --- a/src/network_systems/projects/local_transceiver/inc/local_transceiver.h +++ b/src/network_systems/projects/local_transceiver/inc/local_transceiver.h @@ -111,6 +111,8 @@ class LocalTransceiver */ std::optional debugSend(const std::string & cmd); + static void cacheGlobalWaypoints(std::string receivedDataBuffer); + /** * @brief Retrieve the latest message from the remote server via the serial port * diff --git a/src/network_systems/projects/local_transceiver/test/test_local_transceiver.cpp b/src/network_systems/projects/local_transceiver/test/test_local_transceiver.cpp index 94914455c..c18708634 100644 --- a/src/network_systems/projects/local_transceiver/test/test_local_transceiver.cpp +++ b/src/network_systems/projects/local_transceiver/test/test_local_transceiver.cpp @@ -16,6 +16,7 @@ #include "at_cmds.h" #include "cmn_hdrs/shared_constants.h" +#include "filesystem" #include "global_path.pb.h" #include "local_transceiver.h" #include "sensors.pb.h" @@ -227,6 +228,77 @@ TEST_F(TestLocalTransceiver, parseInMsgValid) EXPECT_EQ(parsed_test.waypoints[1].longitude, holder); } +TEST_F(TestLocalTransceiver, checkCache) +{ + constexpr float holder = 14.3; + constexpr float updated = 17.9; + std::vector waypoints; + + // protobuf + Polaris::GlobalPath path; + + Polaris::Waypoint * waypoint_a = path.add_waypoints(); + waypoint_a->set_latitude(holder); + waypoint_a->set_longitude(holder); + + Polaris::Waypoint * waypoint_b = path.add_waypoints(); + waypoint_b->set_latitude(holder); + waypoint_b->set_longitude(holder); + + // convert protobuf to string + std::string serialized_test = path.SerializeAsString(); + + std::filesystem::path cache_path{"global_waypoint_cache"}; + //check that cache doesn't exist yet + ASSERT_FALSE(std::filesystem::exists(cache_path)); + + LocalTransceiver::cacheGlobalWaypoints(serialized_test); + + //check that after caching the cache exists + ASSERT_TRUE(std::filesystem::exists(cache_path)); + + auto cache_obj = LocalTransceiver::getCache(); + + //getCache returns optional + ASSERT_TRUE(cache_obj); + custom_interfaces::msg::Path parsed_cache = *cache_obj; + + custom_interfaces::msg::Path parsed_test = LocalTransceiver::parseInMsg(serialized_test); + EXPECT_EQ(parsed_test.waypoints[0].latitude, parsed_cache.waypoints[0].latitude); + EXPECT_EQ(parsed_test.waypoints[0].longitude, parsed_cache.waypoints[0].latitude); + EXPECT_EQ(parsed_test.waypoints[1].latitude, parsed_cache.waypoints[0].latitude); + EXPECT_EQ(parsed_test.waypoints[1].longitude, parsed_cache.waypoints[0].latitude); + + //update waypoints so we have to use cache_temp and rename + waypoint_a->set_latitude(updated); + waypoint_a->set_longitude(updated); + + waypoint_b->set_latitude(updated); + waypoint_b->set_longitude(updated); + + serialized_test = path.SerializeAsString(); + LocalTransceiver::cacheGlobalWaypoints(serialized_test); + + std::filesystem::path cache_temp_path{"global_waypoint_cache_temp"}; + ASSERT_TRUE(std::filesystem::exists(cache_path)); + //make sure temp path doesn't exist after rename + ASSERT_FALSE(std::filesystem::exists(cache_temp_path)); + + cache_obj = LocalTransceiver::getCache(); + + //getCache returns optional + ASSERT_TRUE(cache_obj); + parsed_cache = *cache_obj; + + parsed_test = LocalTransceiver::parseInMsg(serialized_test); + EXPECT_EQ(parsed_test.waypoints[0].latitude, parsed_cache.waypoints[0].latitude); + EXPECT_EQ(parsed_test.waypoints[0].longitude, parsed_cache.waypoints[0].latitude); + EXPECT_EQ(parsed_test.waypoints[1].latitude, parsed_cache.waypoints[0].latitude); + EXPECT_EQ(parsed_test.waypoints[1].longitude, parsed_cache.waypoints[0].latitude); + + //TODO: check that cached waypoints get sent over ROS after starting +} + // std::mutex port_mutex; // TEST_F(TestLocalTransceiver, testMailboxBlackbox) From dd20a13c93150bc548bfcba8bac1f0d848ad954a Mon Sep 17 00:00:00 2001 From: adambrett40 <159198197+adambrett40@users.noreply.github.com> Date: Sat, 8 Feb 2025 16:43:08 -0800 Subject: [PATCH 05/15] cache testing update --- .../local_transceiver/inc/local_transceiver.h | 17 +++++++++-------- .../local_transceiver/src/local_transceiver.cpp | 2 +- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/network_systems/projects/local_transceiver/inc/local_transceiver.h b/src/network_systems/projects/local_transceiver/inc/local_transceiver.h index 01584b72c..3aa30b331 100644 --- a/src/network_systems/projects/local_transceiver/inc/local_transceiver.h +++ b/src/network_systems/projects/local_transceiver/inc/local_transceiver.h @@ -130,6 +130,15 @@ class LocalTransceiver */ static std::optional getCache(); + //temp public for testing + /** + * @brief Parse the message received from the remote server + * + * @param msg message received from the remote server + * @return the data byte string payload from the message + */ + static custom_interfaces::msg::Path parseInMsg(const std::string & msg); + private: // Serial port read/write timeout constexpr static const struct timeval TIMEOUT @@ -163,14 +172,6 @@ class LocalTransceiver std::optional readRsp(); - /** - * @brief Parse the message received from the remote server - * - * @param msg message received from the remote server - * @return the data byte string payload from the message - */ - static custom_interfaces::msg::Path parseInMsg(const std::string & msg); - /** * @brief Convert a boost::asio::streambuf into a string * @warning Flushes the streambuf object diff --git a/src/network_systems/projects/local_transceiver/src/local_transceiver.cpp b/src/network_systems/projects/local_transceiver/src/local_transceiver.cpp index 889591406..a545e4792 100644 --- a/src/network_systems/projects/local_transceiver/src/local_transceiver.cpp +++ b/src/network_systems/projects/local_transceiver/src/local_transceiver.cpp @@ -208,7 +208,7 @@ std::optional LocalTransceiver::debugSend(const std::string & cmd) return readRsp(); } -void cacheGlobalWaypoints(std::string receivedDataBuffer) +void LocalTransceiver::cacheGlobalWaypoints(std::string receivedDataBuffer) { std::filesystem::path cache_path{"global_waypoint_cache"}; if (std::filesystem::exists(cache_path)) { From 1d655fb36b43cac0487cc50934674f049ac53689 Mon Sep 17 00:00:00 2001 From: adambrett40 <159198197+adambrett40@users.noreply.github.com> Date: Sat, 8 Feb 2025 21:48:12 -0800 Subject: [PATCH 06/15] tests pass... kinda (on first try. need to cleanup added files on teardown) --- sailbot.code-workspace | 15 +++------ .../src/local_transceiver.cpp | 23 ++++++++----- .../test/test_local_transceiver.cpp | 33 ++++++++++--------- 3 files changed, 38 insertions(+), 33 deletions(-) diff --git a/sailbot.code-workspace b/sailbot.code-workspace index 6e5e2821b..3891e900e 100644 --- a/sailbot.code-workspace +++ b/sailbot.code-workspace @@ -370,7 +370,6 @@ // for the documentation about the tasks.json format "version": "2.0.0", "tasks": [ - // Build tasks { "label": "Build All", "detail": "Build all packages in the workspace", @@ -401,7 +400,6 @@ "group": "build", "problemMatcher": "$gcc" }, - // Test tasks { "label": "test", "detail": "Run all unit tests and show results.", @@ -410,9 +408,9 @@ "group": { "kind": "test", "isDefault": true - } + }, + "problemMatcher": [] }, - // Clean { "label": "clean", "detail": "Run the clean target", @@ -427,7 +425,6 @@ "command": "sudo rm -rf build install log; py3clean .", "problemMatcher": [] }, - // Linting and static code analysis tasks { "label": "lint_cmake", "detail": "Run lint on cmake files.", @@ -471,7 +468,7 @@ "type": "shell", "command": "LINTER=mypy LOCAL_RUN=true scripts/ament-lint.sh", "problemMatcher": [ - "$ament_mypy", + "$ament_mypy" ], "presentation": { "panel": "dedicated", @@ -484,7 +481,7 @@ "type": "shell", "command": "LINTER=xmllint LOCAL_RUN=true scripts/ament-lint.sh", "problemMatcher": [ - "$ament_xmllint", + "$ament_xmllint" ], "presentation": { "panel": "dedicated", @@ -498,11 +495,10 @@ "lint_cmake", "flake8", "mypy", - "xmllint", + "xmllint" ], "problemMatcher": [] }, - // Workspace editing tasks { "label": "setup", "detail": "Set up the workspace", @@ -510,7 +506,6 @@ "command": "./scripts/setup.sh", "problemMatcher": [] } - ], "inputs": [ { diff --git a/src/network_systems/projects/local_transceiver/src/local_transceiver.cpp b/src/network_systems/projects/local_transceiver/src/local_transceiver.cpp index a545e4792..ee3b3e7d8 100644 --- a/src/network_systems/projects/local_transceiver/src/local_transceiver.cpp +++ b/src/network_systems/projects/local_transceiver/src/local_transceiver.cpp @@ -210,20 +210,27 @@ std::optional LocalTransceiver::debugSend(const std::string & cmd) void LocalTransceiver::cacheGlobalWaypoints(std::string receivedDataBuffer) { - std::filesystem::path cache_path{"global_waypoint_cache"}; - if (std::filesystem::exists(cache_path)) { - std::ofstream writeFile("global_waypoint_cache_temp.txt", std::ios::binary); + std::string CACHE_PATH = "global_waypoint_cache"; + std::string CACHE_TEMP_PATH = "global_waypoint_cache_temp"; + std::filesystem::path cache{CACHE_PATH}; + std::cout << "STARTING cacheGlobalWaypoints" << std::endl; + if (std::filesystem::exists(cache)) { + std::filesystem::path cache_temp{CACHE_TEMP_PATH}; + std::cout << "CACHE EXISTS " << std::endl; + std::ofstream writeFile(CACHE_TEMP_PATH, std::ios::binary); if (!writeFile) { - //err + std::cerr << "Failed to create temp cache file" << std::endl; } writeFile.write(receivedDataBuffer.data(), static_cast(receivedDataBuffer.size())); - std::filesystem::path cache_temp{"global_waypoint_cache_temp"}; - std::filesystem::rename(cache_temp, cache_path); + std::filesystem::rename(CACHE_TEMP_PATH, CACHE_PATH); } else { - std::ofstream writeFile("global_waypoint_cache.txt", std::ios::binary); + std::cout << "CACHE DOESN'T EXIST" << std::endl; + std::ofstream writeFile(CACHE_PATH, std::ios::binary); if (!writeFile) { - //err + std::cerr << "Failed to create cache file" << std::endl; } + std::cout << "Writing to: " << std::filesystem::absolute(CACHE_PATH) << std::endl; + std::cout << "Current Working Directory: " << std::filesystem::current_path() << std::endl; writeFile.write(receivedDataBuffer.data(), static_cast(receivedDataBuffer.size())); } } diff --git a/src/network_systems/projects/local_transceiver/test/test_local_transceiver.cpp b/src/network_systems/projects/local_transceiver/test/test_local_transceiver.cpp index c18708634..c4660188c 100644 --- a/src/network_systems/projects/local_transceiver/test/test_local_transceiver.cpp +++ b/src/network_systems/projects/local_transceiver/test/test_local_transceiver.cpp @@ -50,15 +50,15 @@ class TestLocalTransceiver : public ::testing::Test TestLocalTransceiver() { - try { - lcl_trns_ = new LocalTransceiver(LOCAL_TRANSCEIVER_TEST_PORT, SATELLITE_BAUD_RATE); - } catch (boost::system::system_error & e) { - std::stringstream ss; - ss << "Failed to create Local Transceiver for tests, is only one instance of: \"" - << RUN_VIRTUAL_IRIDIUM_SCRIPT_PATH << "\" running?" << std::endl; - ss << e.what() << std::endl; - throw std::runtime_error(ss.str()); - } + // try { + // lcl_trns_ = new LocalTransceiver(LOCAL_TRANSCEIVER_TEST_PORT, SATELLITE_BAUD_RATE); + // } catch (boost::system::system_error & e) { + // std::stringstream ss; + // ss << "Failed to create Local Transceiver for tests, is only one instance of: \"" + // << RUN_VIRTUAL_IRIDIUM_SCRIPT_PATH << "\" running?" << std::endl; + // ss << e.what() << std::endl; + // throw std::runtime_error(ss.str()); + // } } ~TestLocalTransceiver() override { @@ -248,14 +248,17 @@ TEST_F(TestLocalTransceiver, checkCache) // convert protobuf to string std::string serialized_test = path.SerializeAsString(); - std::filesystem::path cache_path{"global_waypoint_cache"}; + std::string CACHE_PATH = "global_waypoint_cache"; + std::string CACHE_TEMP_PATH = "global_waypoint_cache_temp"; + std::filesystem::path cache{CACHE_PATH}; //check that cache doesn't exist yet - ASSERT_FALSE(std::filesystem::exists(cache_path)); + ASSERT_FALSE(std::filesystem::exists(cache)); LocalTransceiver::cacheGlobalWaypoints(serialized_test); + std::cout << "REACHED HERE " << std::endl; //check that after caching the cache exists - ASSERT_TRUE(std::filesystem::exists(cache_path)); + ASSERT_TRUE(std::filesystem::exists(cache)); auto cache_obj = LocalTransceiver::getCache(); @@ -279,10 +282,10 @@ TEST_F(TestLocalTransceiver, checkCache) serialized_test = path.SerializeAsString(); LocalTransceiver::cacheGlobalWaypoints(serialized_test); - std::filesystem::path cache_temp_path{"global_waypoint_cache_temp"}; - ASSERT_TRUE(std::filesystem::exists(cache_path)); + std::filesystem::path cache_temp{CACHE_TEMP_PATH}; + ASSERT_TRUE(std::filesystem::exists(cache)); //make sure temp path doesn't exist after rename - ASSERT_FALSE(std::filesystem::exists(cache_temp_path)); + ASSERT_FALSE(std::filesystem::exists(CACHE_TEMP_PATH)); cache_obj = LocalTransceiver::getCache(); From 7da2df626b6147defbe9b371b781aa95c1aac97e Mon Sep 17 00:00:00 2001 From: adambrett40 <159198197+adambrett40@users.noreply.github.com> Date: Sun, 9 Feb 2025 15:25:23 -0800 Subject: [PATCH 07/15] updated teardown so consecutive tests pass --- .../local_transceiver/src/local_transceiver.cpp | 8 +------- .../src/local_transceiver_ros_intf.cpp | 4 ++-- .../test/test_local_transceiver.cpp | 15 ++++++++++----- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/network_systems/projects/local_transceiver/src/local_transceiver.cpp b/src/network_systems/projects/local_transceiver/src/local_transceiver.cpp index ee3b3e7d8..8b07ddd62 100644 --- a/src/network_systems/projects/local_transceiver/src/local_transceiver.cpp +++ b/src/network_systems/projects/local_transceiver/src/local_transceiver.cpp @@ -210,13 +210,12 @@ std::optional LocalTransceiver::debugSend(const std::string & cmd) void LocalTransceiver::cacheGlobalWaypoints(std::string receivedDataBuffer) { + //writes to /build/network_systems/projects/local_transceiver/PATH std::string CACHE_PATH = "global_waypoint_cache"; std::string CACHE_TEMP_PATH = "global_waypoint_cache_temp"; std::filesystem::path cache{CACHE_PATH}; - std::cout << "STARTING cacheGlobalWaypoints" << std::endl; if (std::filesystem::exists(cache)) { std::filesystem::path cache_temp{CACHE_TEMP_PATH}; - std::cout << "CACHE EXISTS " << std::endl; std::ofstream writeFile(CACHE_TEMP_PATH, std::ios::binary); if (!writeFile) { std::cerr << "Failed to create temp cache file" << std::endl; @@ -224,13 +223,10 @@ void LocalTransceiver::cacheGlobalWaypoints(std::string receivedDataBuffer) writeFile.write(receivedDataBuffer.data(), static_cast(receivedDataBuffer.size())); std::filesystem::rename(CACHE_TEMP_PATH, CACHE_PATH); } else { - std::cout << "CACHE DOESN'T EXIST" << std::endl; std::ofstream writeFile(CACHE_PATH, std::ios::binary); if (!writeFile) { std::cerr << "Failed to create cache file" << std::endl; } - std::cout << "Writing to: " << std::filesystem::absolute(CACHE_PATH) << std::endl; - std::cout << "Current Working Directory: " << std::filesystem::current_path() << std::endl; writeFile.write(receivedDataBuffer.data(), static_cast(receivedDataBuffer.size())); } } @@ -331,8 +327,6 @@ custom_interfaces::msg::Path LocalTransceiver::receive() break; } - //need to check if this even works - // save serialized data to local cache (extract to function?) cacheGlobalWaypoints(receivedDataBuffer); custom_interfaces::msg::Path to_publish = parseInMsg(receivedDataBuffer); diff --git a/src/network_systems/projects/local_transceiver/src/local_transceiver_ros_intf.cpp b/src/network_systems/projects/local_transceiver/src/local_transceiver_ros_intf.cpp index 38722c323..ca723b87c 100644 --- a/src/network_systems/projects/local_transceiver/src/local_transceiver_ros_intf.cpp +++ b/src/network_systems/projects/local_transceiver/src/local_transceiver_ros_intf.cpp @@ -98,8 +98,8 @@ class LocalTransceiverIntf : public NetNode //check for cached waypoints and publish immediately //note: should check for cache_temp? - // if exists, should be more up-to-date than cache - //also, check if I can just use pub_cb or not since its not done initializing? + // if exists, could be more up-to-date than cache, if power was lost AFTER finishing writing and BEFORE + //renaming the file auto msg = lcl_trns_->getCache(); if (msg) { pub_->publish(*msg); diff --git a/src/network_systems/projects/local_transceiver/test/test_local_transceiver.cpp b/src/network_systems/projects/local_transceiver/test/test_local_transceiver.cpp index c4660188c..8ccb5ff11 100644 --- a/src/network_systems/projects/local_transceiver/test/test_local_transceiver.cpp +++ b/src/network_systems/projects/local_transceiver/test/test_local_transceiver.cpp @@ -23,6 +23,8 @@ namespace bp = boost::process; +std::string CACHE_PATH = "global_waypoint_cache"; +std::string CACHE_TEMP_PATH = "global_waypoint_cache_temp"; /* >>>>>README<<<<<< Local Transceiver unit tests rely on two other programs: Virtual Iridium and HTTP Echo Server 1. Spawning a separate process for RUN_VIRTUAL_IRIDIUM_SCRIPT_PATH doesn't work very well because the script @@ -46,7 +48,12 @@ class TestLocalTransceiver : public ::testing::Test } } - static void TearDownTestSuite() { http_echo_server_proc_.terminate(); } + static void TearDownTestSuite() + { + http_echo_server_proc_.terminate(); + std::filesystem::remove(CACHE_PATH); + std::filesystem::remove(CACHE_TEMP_PATH); + } TestLocalTransceiver() { @@ -62,8 +69,8 @@ class TestLocalTransceiver : public ::testing::Test } ~TestLocalTransceiver() override { - lcl_trns_->stop(); - delete lcl_trns_; + // lcl_trns_->stop(); + // delete lcl_trns_; } LocalTransceiver * lcl_trns_; @@ -248,8 +255,6 @@ TEST_F(TestLocalTransceiver, checkCache) // convert protobuf to string std::string serialized_test = path.SerializeAsString(); - std::string CACHE_PATH = "global_waypoint_cache"; - std::string CACHE_TEMP_PATH = "global_waypoint_cache_temp"; std::filesystem::path cache{CACHE_PATH}; //check that cache doesn't exist yet ASSERT_FALSE(std::filesystem::exists(cache)); From 08f715fb827aaa985bb9852713ba68d2c23e63b0 Mon Sep 17 00:00:00 2001 From: adambrett40 <159198197+adambrett40@users.noreply.github.com> Date: Sun, 23 Feb 2025 23:56:05 -0800 Subject: [PATCH 08/15] cleaned up and made paths constant --- .../lib/cmn_hdrs/shared_constants.h | 3 +++ .../src/local_transceiver.cpp | 10 +++---- .../test/test_local_transceiver.cpp | 27 ++++++++----------- 3 files changed, 18 insertions(+), 22 deletions(-) diff --git a/src/network_systems/lib/cmn_hdrs/shared_constants.h b/src/network_systems/lib/cmn_hdrs/shared_constants.h index e66f9deef..4b5a9a774 100644 --- a/src/network_systems/lib/cmn_hdrs/shared_constants.h +++ b/src/network_systems/lib/cmn_hdrs/shared_constants.h @@ -17,6 +17,9 @@ static const std::string DEV = "development"; constexpr unsigned int MAX_LOCAL_TO_REMOTE_PAYLOAD_SIZE_BYTES = 340; constexpr unsigned int MAX_REMOTE_TO_LOCAL_PAYLOAD_SIZE_BYTES = 270; +static const std::string CACHE_PATH = "global_waypoint_cache"; +static const std::string CACHE_TEMP_PATH = "global_waypoint_cache_temp"; + constexpr int NUM_BATTERIES = []() constexpr { using batteries_arr = custom_interfaces::msg::Batteries::_batteries_type; diff --git a/src/network_systems/projects/local_transceiver/src/local_transceiver.cpp b/src/network_systems/projects/local_transceiver/src/local_transceiver.cpp index 8b07ddd62..f36cf10b3 100644 --- a/src/network_systems/projects/local_transceiver/src/local_transceiver.cpp +++ b/src/network_systems/projects/local_transceiver/src/local_transceiver.cpp @@ -211,12 +211,10 @@ std::optional LocalTransceiver::debugSend(const std::string & cmd) void LocalTransceiver::cacheGlobalWaypoints(std::string receivedDataBuffer) { //writes to /build/network_systems/projects/local_transceiver/PATH - std::string CACHE_PATH = "global_waypoint_cache"; - std::string CACHE_TEMP_PATH = "global_waypoint_cache_temp"; std::filesystem::path cache{CACHE_PATH}; if (std::filesystem::exists(cache)) { std::filesystem::path cache_temp{CACHE_TEMP_PATH}; - std::ofstream writeFile(CACHE_TEMP_PATH, std::ios::binary); + std::ofstream writeFile(CACHE_TEMP_PATH, std::ios::binary); if (!writeFile) { std::cerr << "Failed to create temp cache file" << std::endl; } @@ -366,9 +364,9 @@ custom_interfaces::msg::Path LocalTransceiver::parseInMsg(const std::string & ms std::optional LocalTransceiver::getCache() { - std::filesystem::path cache_path{"global_waypoint_cache"}; - if (std::filesystem::exists(cache_path)) { - std::ifstream input("global_waypoint_cache", std::ios::binary); + std::filesystem::path cache{CACHE_PATH}; + if (std::filesystem::exists(cache)) { + std::ifstream input(CACHE_PATH, std::ios::binary); std::string cachedDataBuffer(std::istreambuf_iterator(input), {}); custom_interfaces::msg::Path to_publish = parseInMsg(cachedDataBuffer); return to_publish; diff --git a/src/network_systems/projects/local_transceiver/test/test_local_transceiver.cpp b/src/network_systems/projects/local_transceiver/test/test_local_transceiver.cpp index 8ccb5ff11..048020921 100644 --- a/src/network_systems/projects/local_transceiver/test/test_local_transceiver.cpp +++ b/src/network_systems/projects/local_transceiver/test/test_local_transceiver.cpp @@ -23,8 +23,6 @@ namespace bp = boost::process; -std::string CACHE_PATH = "global_waypoint_cache"; -std::string CACHE_TEMP_PATH = "global_waypoint_cache_temp"; /* >>>>>README<<<<<< Local Transceiver unit tests rely on two other programs: Virtual Iridium and HTTP Echo Server 1. Spawning a separate process for RUN_VIRTUAL_IRIDIUM_SCRIPT_PATH doesn't work very well because the script @@ -57,20 +55,20 @@ class TestLocalTransceiver : public ::testing::Test TestLocalTransceiver() { - // try { - // lcl_trns_ = new LocalTransceiver(LOCAL_TRANSCEIVER_TEST_PORT, SATELLITE_BAUD_RATE); - // } catch (boost::system::system_error & e) { - // std::stringstream ss; - // ss << "Failed to create Local Transceiver for tests, is only one instance of: \"" - // << RUN_VIRTUAL_IRIDIUM_SCRIPT_PATH << "\" running?" << std::endl; - // ss << e.what() << std::endl; - // throw std::runtime_error(ss.str()); - // } + try { + lcl_trns_ = new LocalTransceiver(LOCAL_TRANSCEIVER_TEST_PORT, SATELLITE_BAUD_RATE); + } catch (boost::system::system_error & e) { + std::stringstream ss; + ss << "Failed to create Local Transceiver for tests, is only one instance of: \"" + << RUN_VIRTUAL_IRIDIUM_SCRIPT_PATH << "\" running?" << std::endl; + ss << e.what() << std::endl; + throw std::runtime_error(ss.str()); + } } ~TestLocalTransceiver() override { - // lcl_trns_->stop(); - // delete lcl_trns_; + lcl_trns_->stop(); + delete lcl_trns_; } LocalTransceiver * lcl_trns_; @@ -260,7 +258,6 @@ TEST_F(TestLocalTransceiver, checkCache) ASSERT_FALSE(std::filesystem::exists(cache)); LocalTransceiver::cacheGlobalWaypoints(serialized_test); - std::cout << "REACHED HERE " << std::endl; //check that after caching the cache exists ASSERT_TRUE(std::filesystem::exists(cache)); @@ -303,8 +300,6 @@ TEST_F(TestLocalTransceiver, checkCache) EXPECT_EQ(parsed_test.waypoints[0].longitude, parsed_cache.waypoints[0].latitude); EXPECT_EQ(parsed_test.waypoints[1].latitude, parsed_cache.waypoints[0].latitude); EXPECT_EQ(parsed_test.waypoints[1].longitude, parsed_cache.waypoints[0].latitude); - - //TODO: check that cached waypoints get sent over ROS after starting } // std::mutex port_mutex; From 84f2a2e46e799f4fefe79516bbcffd67b6da6211 Mon Sep 17 00:00:00 2001 From: adambrett40 <159198197+adambrett40@users.noreply.github.com> Date: Mon, 24 Feb 2025 00:36:38 -0800 Subject: [PATCH 09/15] removed comments --- .../projects/local_transceiver/src/local_transceiver.cpp | 2 +- .../local_transceiver/src/local_transceiver_ros_intf.cpp | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/src/network_systems/projects/local_transceiver/src/local_transceiver.cpp b/src/network_systems/projects/local_transceiver/src/local_transceiver.cpp index f36cf10b3..9bcf55133 100644 --- a/src/network_systems/projects/local_transceiver/src/local_transceiver.cpp +++ b/src/network_systems/projects/local_transceiver/src/local_transceiver.cpp @@ -210,7 +210,7 @@ std::optional LocalTransceiver::debugSend(const std::string & cmd) void LocalTransceiver::cacheGlobalWaypoints(std::string receivedDataBuffer) { - //writes to /build/network_systems/projects/local_transceiver/PATH + //writes to /build/network_systems/projects/local_transceiver/PATH when running tests std::filesystem::path cache{CACHE_PATH}; if (std::filesystem::exists(cache)) { std::filesystem::path cache_temp{CACHE_TEMP_PATH}; diff --git a/src/network_systems/projects/local_transceiver/src/local_transceiver_ros_intf.cpp b/src/network_systems/projects/local_transceiver/src/local_transceiver_ros_intf.cpp index ca723b87c..de80257f2 100644 --- a/src/network_systems/projects/local_transceiver/src/local_transceiver_ros_intf.cpp +++ b/src/network_systems/projects/local_transceiver/src/local_transceiver_ros_intf.cpp @@ -96,10 +96,6 @@ class LocalTransceiverIntf : public NetNode ros_topics::LOCAL_PATH, ROS_Q_SIZE, std::bind(&LocalTransceiverIntf::sub_local_path_data_cb, this, std::placeholders::_1)); - //check for cached waypoints and publish immediately - //note: should check for cache_temp? - // if exists, could be more up-to-date than cache, if power was lost AFTER finishing writing and BEFORE - //renaming the file auto msg = lcl_trns_->getCache(); if (msg) { pub_->publish(*msg); From bdc81d9055002e9a9acb71f7fc4994db6a37b91d Mon Sep 17 00:00:00 2001 From: adambrett40 <159198197+adambrett40@users.noreply.github.com> Date: Wed, 26 Feb 2025 20:27:07 -0800 Subject: [PATCH 10/15] minor cleanup --- .../local_transceiver/src/local_transceiver.cpp | 1 - .../src/local_transceiver_ros_intf.cpp | 2 -- .../test/test_local_transceiver.cpp | 15 ++++----------- 3 files changed, 4 insertions(+), 14 deletions(-) diff --git a/src/network_systems/projects/local_transceiver/src/local_transceiver.cpp b/src/network_systems/projects/local_transceiver/src/local_transceiver.cpp index 9bcf55133..0e347836e 100644 --- a/src/network_systems/projects/local_transceiver/src/local_transceiver.cpp +++ b/src/network_systems/projects/local_transceiver/src/local_transceiver.cpp @@ -24,7 +24,6 @@ #include "filesystem" #include "fstream" #include "global_path.pb.h" -#include "iostream" #include "sensors.pb.h" #include "waypoint.pb.h" diff --git a/src/network_systems/projects/local_transceiver/src/local_transceiver_ros_intf.cpp b/src/network_systems/projects/local_transceiver/src/local_transceiver_ros_intf.cpp index de80257f2..6a8519e42 100644 --- a/src/network_systems/projects/local_transceiver/src/local_transceiver_ros_intf.cpp +++ b/src/network_systems/projects/local_transceiver/src/local_transceiver_ros_intf.cpp @@ -11,8 +11,6 @@ #include "cmn_hdrs/ros_info.h" #include "cmn_hdrs/shared_constants.h" -#include "filesystem" -#include "fstream" #include "local_transceiver.h" #include "net_node.h" diff --git a/src/network_systems/projects/local_transceiver/test/test_local_transceiver.cpp b/src/network_systems/projects/local_transceiver/test/test_local_transceiver.cpp index 048020921..571e41f76 100644 --- a/src/network_systems/projects/local_transceiver/test/test_local_transceiver.cpp +++ b/src/network_systems/projects/local_transceiver/test/test_local_transceiver.cpp @@ -239,7 +239,6 @@ TEST_F(TestLocalTransceiver, checkCache) constexpr float updated = 17.9; std::vector waypoints; - // protobuf Polaris::GlobalPath path; Polaris::Waypoint * waypoint_a = path.add_waypoints(); @@ -250,31 +249,27 @@ TEST_F(TestLocalTransceiver, checkCache) waypoint_b->set_latitude(holder); waypoint_b->set_longitude(holder); - // convert protobuf to string std::string serialized_test = path.SerializeAsString(); - std::filesystem::path cache{CACHE_PATH}; - //check that cache doesn't exist yet + ASSERT_FALSE(std::filesystem::exists(cache)); LocalTransceiver::cacheGlobalWaypoints(serialized_test); - //check that after caching the cache exists ASSERT_TRUE(std::filesystem::exists(cache)); auto cache_obj = LocalTransceiver::getCache(); - - //getCache returns optional + ASSERT_TRUE(cache_obj); - custom_interfaces::msg::Path parsed_cache = *cache_obj; + custom_interfaces::msg::Path parsed_cache = *cache_obj; custom_interfaces::msg::Path parsed_test = LocalTransceiver::parseInMsg(serialized_test); + EXPECT_EQ(parsed_test.waypoints[0].latitude, parsed_cache.waypoints[0].latitude); EXPECT_EQ(parsed_test.waypoints[0].longitude, parsed_cache.waypoints[0].latitude); EXPECT_EQ(parsed_test.waypoints[1].latitude, parsed_cache.waypoints[0].latitude); EXPECT_EQ(parsed_test.waypoints[1].longitude, parsed_cache.waypoints[0].latitude); - //update waypoints so we have to use cache_temp and rename waypoint_a->set_latitude(updated); waypoint_a->set_longitude(updated); @@ -286,12 +281,10 @@ TEST_F(TestLocalTransceiver, checkCache) std::filesystem::path cache_temp{CACHE_TEMP_PATH}; ASSERT_TRUE(std::filesystem::exists(cache)); - //make sure temp path doesn't exist after rename ASSERT_FALSE(std::filesystem::exists(CACHE_TEMP_PATH)); cache_obj = LocalTransceiver::getCache(); - //getCache returns optional ASSERT_TRUE(cache_obj); parsed_cache = *cache_obj; From 995ef9ac608d1c83afad8ba45fc7ee31cc9909cb Mon Sep 17 00:00:00 2001 From: adambrett40 <159198197+adambrett40@users.noreply.github.com> Date: Thu, 27 Feb 2025 01:45:39 -0800 Subject: [PATCH 11/15] fixed copy/paste error --- .../test/test_local_transceiver.cpp | 37 ++++++++++--------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/src/network_systems/projects/local_transceiver/test/test_local_transceiver.cpp b/src/network_systems/projects/local_transceiver/test/test_local_transceiver.cpp index 571e41f76..c70bb3ff4 100644 --- a/src/network_systems/projects/local_transceiver/test/test_local_transceiver.cpp +++ b/src/network_systems/projects/local_transceiver/test/test_local_transceiver.cpp @@ -235,21 +235,23 @@ TEST_F(TestLocalTransceiver, parseInMsgValid) TEST_F(TestLocalTransceiver, checkCache) { - constexpr float holder = 14.3; - constexpr float updated = 17.9; + constexpr float holder_lat = 14.3; + constexpr float holder_long = -177.6; + constexpr float updated_lat = 17.9; + constexpr float updated_long = 0.1; std::vector waypoints; Polaris::GlobalPath path; Polaris::Waypoint * waypoint_a = path.add_waypoints(); - waypoint_a->set_latitude(holder); - waypoint_a->set_longitude(holder); + waypoint_a->set_latitude(holder_lat); + waypoint_a->set_longitude(holder_long); Polaris::Waypoint * waypoint_b = path.add_waypoints(); - waypoint_b->set_latitude(holder); - waypoint_b->set_longitude(holder); + waypoint_b->set_latitude(holder_lat); + waypoint_b->set_longitude(holder_long); - std::string serialized_test = path.SerializeAsString(); + std::string serialized_test = path.SerializeAsString(); std::filesystem::path cache{CACHE_PATH}; ASSERT_FALSE(std::filesystem::exists(cache)); @@ -259,22 +261,22 @@ TEST_F(TestLocalTransceiver, checkCache) ASSERT_TRUE(std::filesystem::exists(cache)); auto cache_obj = LocalTransceiver::getCache(); - + ASSERT_TRUE(cache_obj); custom_interfaces::msg::Path parsed_cache = *cache_obj; - custom_interfaces::msg::Path parsed_test = LocalTransceiver::parseInMsg(serialized_test); + custom_interfaces::msg::Path parsed_test = LocalTransceiver::parseInMsg(serialized_test); EXPECT_EQ(parsed_test.waypoints[0].latitude, parsed_cache.waypoints[0].latitude); - EXPECT_EQ(parsed_test.waypoints[0].longitude, parsed_cache.waypoints[0].latitude); + EXPECT_EQ(parsed_test.waypoints[0].longitude, parsed_cache.waypoints[0].longitude); EXPECT_EQ(parsed_test.waypoints[1].latitude, parsed_cache.waypoints[0].latitude); - EXPECT_EQ(parsed_test.waypoints[1].longitude, parsed_cache.waypoints[0].latitude); + EXPECT_EQ(parsed_test.waypoints[1].longitude, parsed_cache.waypoints[0].longitude); - waypoint_a->set_latitude(updated); - waypoint_a->set_longitude(updated); + waypoint_a->set_latitude(updated_lat); + waypoint_a->set_longitude(updated_long); - waypoint_b->set_latitude(updated); - waypoint_b->set_longitude(updated); + waypoint_b->set_latitude(updated_lat); + waypoint_b->set_longitude(updated_long); serialized_test = path.SerializeAsString(); LocalTransceiver::cacheGlobalWaypoints(serialized_test); @@ -285,14 +287,15 @@ TEST_F(TestLocalTransceiver, checkCache) cache_obj = LocalTransceiver::getCache(); + ASSERT_TRUE(std::filesystem::exists(cache)); ASSERT_TRUE(cache_obj); parsed_cache = *cache_obj; parsed_test = LocalTransceiver::parseInMsg(serialized_test); EXPECT_EQ(parsed_test.waypoints[0].latitude, parsed_cache.waypoints[0].latitude); - EXPECT_EQ(parsed_test.waypoints[0].longitude, parsed_cache.waypoints[0].latitude); + EXPECT_EQ(parsed_test.waypoints[0].longitude, parsed_cache.waypoints[0].longitude); EXPECT_EQ(parsed_test.waypoints[1].latitude, parsed_cache.waypoints[0].latitude); - EXPECT_EQ(parsed_test.waypoints[1].longitude, parsed_cache.waypoints[0].latitude); + EXPECT_EQ(parsed_test.waypoints[1].longitude, parsed_cache.waypoints[0].longitude); } // std::mutex port_mutex; From 23bbf8800cc62c191444634829f2839286be80f3 Mon Sep 17 00:00:00 2001 From: adambrett40 <159198197+adambrett40@users.noreply.github.com> Date: Mon, 3 Mar 2025 01:12:12 -0800 Subject: [PATCH 12/15] make cacheGlobalWaypoints async --- .../local_transceiver/inc/local_transceiver.h | 2 +- .../src/local_transceiver.cpp | 68 ++++++++++++++----- 2 files changed, 53 insertions(+), 17 deletions(-) diff --git a/src/network_systems/projects/local_transceiver/inc/local_transceiver.h b/src/network_systems/projects/local_transceiver/inc/local_transceiver.h index 3aa30b331..6486fb15c 100644 --- a/src/network_systems/projects/local_transceiver/inc/local_transceiver.h +++ b/src/network_systems/projects/local_transceiver/inc/local_transceiver.h @@ -111,7 +111,7 @@ class LocalTransceiver */ std::optional debugSend(const std::string & cmd); - static void cacheGlobalWaypoints(std::string receivedDataBuffer); + static std::future cacheGlobalWaypoints(std::string receivedDataBuffer); /** * @brief Retrieve the latest message from the remote server via the serial port diff --git a/src/network_systems/projects/local_transceiver/src/local_transceiver.cpp b/src/network_systems/projects/local_transceiver/src/local_transceiver.cpp index 0e347836e..9359c24b5 100644 --- a/src/network_systems/projects/local_transceiver/src/local_transceiver.cpp +++ b/src/network_systems/projects/local_transceiver/src/local_transceiver.cpp @@ -207,25 +207,58 @@ std::optional LocalTransceiver::debugSend(const std::string & cmd) return readRsp(); } -void LocalTransceiver::cacheGlobalWaypoints(std::string receivedDataBuffer) +std::future LocalTransceiver::cacheGlobalWaypoints(std::string receivedDataBuffer) { //writes to /build/network_systems/projects/local_transceiver/PATH when running tests - std::filesystem::path cache{CACHE_PATH}; - if (std::filesystem::exists(cache)) { - std::filesystem::path cache_temp{CACHE_TEMP_PATH}; - std::ofstream writeFile(CACHE_TEMP_PATH, std::ios::binary); - if (!writeFile) { - std::cerr << "Failed to create temp cache file" << std::endl; - } - writeFile.write(receivedDataBuffer.data(), static_cast(receivedDataBuffer.size())); - std::filesystem::rename(CACHE_TEMP_PATH, CACHE_PATH); - } else { - std::ofstream writeFile(CACHE_PATH, std::ios::binary); - if (!writeFile) { - std::cerr << "Failed to create cache file" << std::endl; + return std::async(std::launch::async, [receivedDataBuffer] { + try { + std::filesystem::path cache{CACHE_PATH}; + if (std::filesystem::exists(cache)) { + std::filesystem::path cache_temp{CACHE_TEMP_PATH}; + std::ofstream writeFile(CACHE_TEMP_PATH, std::ios::binary); + if (!writeFile) { + std::cerr << "Failed to create temp cache file" << std::endl; + } + writeFile.write(receivedDataBuffer.data(), static_cast(receivedDataBuffer.size())); + writeFile.close(); + std::filesystem::rename(CACHE_TEMP_PATH, CACHE_PATH); + } else { + std::ofstream writeFile(CACHE_PATH, std::ios::binary); + if (!writeFile) { + std::cerr << "Failed to create cache file" << std::endl; + } + writeFile.write(receivedDataBuffer.data(), static_cast(receivedDataBuffer.size())); + writeFile.close(); + } + } catch (const std::exception & e) { + std::cerr << "Error caching global waypoints: " << e.what() << std::endl; } - writeFile.write(receivedDataBuffer.data(), static_cast(receivedDataBuffer.size())); - } + }); + // std::filesystem::path cache{CACHE_PATH}; + // if (std::filesystem::exists(cache)) { + // auto future = std::async(std::launch::async, [=] { + // std::ofstream file(CACHE_TEMP_PATH, std::ios::binary); + // if (file.is_open()) { + // file.write(receivedData.data(), static_cast(receivedData.size())); + // std::cout << "File written asynchronously.\n"; //!!! REMOVE + // } else { + // std::cerr << "Failed to open file.\n"; + // } + // }); + // std::filesystem::rename(CACHE_TEMP_PATH, CACHE_PATH); + // } else { + // std::ofstream writeFile(CACHE_PATH, std::ios::binary); + // // if (!writeFile) { + // // std::cerr << "Failed to create cache file" << std::endl; + // // } + // boost::system::error_code ec; + // bio::write(writeFile, bio::buffer(receivedData), ec); + // if (ec) { + // std::cerr << "Temp cache write failed with error: " << ec.message() << std::endl; + // return; + // } + // writeFile.write(receivedData.data(), static_cast(receivedDataBuffer.size())); + // } } custom_interfaces::msg::Path LocalTransceiver::receive() @@ -361,6 +394,9 @@ custom_interfaces::msg::Path LocalTransceiver::parseInMsg(const std::string & ms return soln; } +//TODO(adambrett40): make async w/ callback to publish over ros +// need to ensure the callback publisher can't publish cached waypoints after new waypoints come in +// put mutex lock on publishing, give to getCache thread? std::optional LocalTransceiver::getCache() { std::filesystem::path cache{CACHE_PATH}; From 0721dfad368a063fb85deaad3f7a229d9f921e17 Mon Sep 17 00:00:00 2001 From: adambrett40 <159198197+adambrett40@users.noreply.github.com> Date: Tue, 4 Mar 2025 18:11:46 -0800 Subject: [PATCH 13/15] made caching async, ros node run virtual iridium in background --- .../lib/cmn_hdrs/shared_constants.h | 18 +++++++- .../local_transceiver/inc/local_transceiver.h | 8 +++- .../src/local_transceiver.cpp | 33 +------------- .../src/local_transceiver_ros_intf.cpp | 45 +++++++++++++------ .../test/test_local_transceiver.cpp | 6 ++- 5 files changed, 61 insertions(+), 49 deletions(-) diff --git a/src/network_systems/lib/cmn_hdrs/shared_constants.h b/src/network_systems/lib/cmn_hdrs/shared_constants.h index 4b5a9a774..c0b39af9c 100644 --- a/src/network_systems/lib/cmn_hdrs/shared_constants.h +++ b/src/network_systems/lib/cmn_hdrs/shared_constants.h @@ -17,8 +17,22 @@ static const std::string DEV = "development"; constexpr unsigned int MAX_LOCAL_TO_REMOTE_PAYLOAD_SIZE_BYTES = 340; constexpr unsigned int MAX_REMOTE_TO_LOCAL_PAYLOAD_SIZE_BYTES = 270; -static const std::string CACHE_PATH = "global_waypoint_cache"; -static const std::string CACHE_TEMP_PATH = "global_waypoint_cache_temp"; +inline std::string getCachePath() +{ + const char * ros_ws = std::getenv("ROS_WORKSPACE"); //NOLINT (concurrency-mt-unsafe) + return (ros_ws != nullptr ? std::string(ros_ws) : "/workspaces/sailbot_workspace") + + "/build/network_systems/projects/local_transceiver/global_waypoint_cache"; +} + +inline std::string getCacheTempPath() +{ + const char * ros_ws = std::getenv("ROS_WORKSPACE"); //NOLINT (concurrency-mt-unsafe) + return (ros_ws != nullptr ? std::string(ros_ws) : "/workspaces/sailbot_workspace") + + "/build/network_systems/projects/local_transceiver/global_waypoint_cache_temp"; +} + +static const std::string CACHE_PATH = getCachePath(); +static const std::string CACHE_TEMP_PATH = getCacheTempPath(); constexpr int NUM_BATTERIES = []() constexpr { diff --git a/src/network_systems/projects/local_transceiver/inc/local_transceiver.h b/src/network_systems/projects/local_transceiver/inc/local_transceiver.h index 6486fb15c..d7f6a9c7e 100644 --- a/src/network_systems/projects/local_transceiver/inc/local_transceiver.h +++ b/src/network_systems/projects/local_transceiver/inc/local_transceiver.h @@ -111,7 +111,13 @@ class LocalTransceiver */ std::optional debugSend(const std::string & cmd); - static std::future cacheGlobalWaypoints(std::string receivedDataBuffer); + /** + * @brief Asynchronously save the received serialized data to a local file + * + * @param receivedDataBuffer string to cache + * @return future object representing completion of the function + */ + static std::future cacheGlobalWaypointsAsync(std::string receivedDataBuffer); /** * @brief Retrieve the latest message from the remote server via the serial port diff --git a/src/network_systems/projects/local_transceiver/src/local_transceiver.cpp b/src/network_systems/projects/local_transceiver/src/local_transceiver.cpp index 9359c24b5..4fd992cf7 100644 --- a/src/network_systems/projects/local_transceiver/src/local_transceiver.cpp +++ b/src/network_systems/projects/local_transceiver/src/local_transceiver.cpp @@ -207,9 +207,8 @@ std::optional LocalTransceiver::debugSend(const std::string & cmd) return readRsp(); } -std::future LocalTransceiver::cacheGlobalWaypoints(std::string receivedDataBuffer) +std::future LocalTransceiver::cacheGlobalWaypointsAsync(std::string receivedDataBuffer) { - //writes to /build/network_systems/projects/local_transceiver/PATH when running tests return std::async(std::launch::async, [receivedDataBuffer] { try { std::filesystem::path cache{CACHE_PATH}; @@ -234,31 +233,6 @@ std::future LocalTransceiver::cacheGlobalWaypoints(std::string receivedDat std::cerr << "Error caching global waypoints: " << e.what() << std::endl; } }); - // std::filesystem::path cache{CACHE_PATH}; - // if (std::filesystem::exists(cache)) { - // auto future = std::async(std::launch::async, [=] { - // std::ofstream file(CACHE_TEMP_PATH, std::ios::binary); - // if (file.is_open()) { - // file.write(receivedData.data(), static_cast(receivedData.size())); - // std::cout << "File written asynchronously.\n"; //!!! REMOVE - // } else { - // std::cerr << "Failed to open file.\n"; - // } - // }); - // std::filesystem::rename(CACHE_TEMP_PATH, CACHE_PATH); - // } else { - // std::ofstream writeFile(CACHE_PATH, std::ios::binary); - // // if (!writeFile) { - // // std::cerr << "Failed to create cache file" << std::endl; - // // } - // boost::system::error_code ec; - // bio::write(writeFile, bio::buffer(receivedData), ec); - // if (ec) { - // std::cerr << "Temp cache write failed with error: " << ec.message() << std::endl; - // return; - // } - // writeFile.write(receivedData.data(), static_cast(receivedDataBuffer.size())); - // } } custom_interfaces::msg::Path LocalTransceiver::receive() @@ -357,7 +331,7 @@ custom_interfaces::msg::Path LocalTransceiver::receive() break; } - cacheGlobalWaypoints(receivedDataBuffer); + cacheGlobalWaypointsAsync(receivedDataBuffer); custom_interfaces::msg::Path to_publish = parseInMsg(receivedDataBuffer); return to_publish; @@ -394,9 +368,6 @@ custom_interfaces::msg::Path LocalTransceiver::parseInMsg(const std::string & ms return soln; } -//TODO(adambrett40): make async w/ callback to publish over ros -// need to ensure the callback publisher can't publish cached waypoints after new waypoints come in -// put mutex lock on publishing, give to getCache thread? std::optional LocalTransceiver::getCache() { std::filesystem::path cache{CACHE_PATH}; diff --git a/src/network_systems/projects/local_transceiver/src/local_transceiver_ros_intf.cpp b/src/network_systems/projects/local_transceiver/src/local_transceiver_ros_intf.cpp index 6a8519e42..038b17e4a 100644 --- a/src/network_systems/projects/local_transceiver/src/local_transceiver_ros_intf.cpp +++ b/src/network_systems/projects/local_transceiver/src/local_transceiver_ros_intf.cpp @@ -46,16 +46,27 @@ class LocalTransceiverIntf : public NetNode } else if (mode == SYSTEM_MODE::DEV) { default_port = LOCAL_TRANSCEIVER_TEST_PORT; std::string run_iridium_cmd = "$ROS_WORKSPACE/scripts/run_virtual_iridium.sh"; - int result = std::system(run_iridium_cmd.c_str()); //NOLINT(concurrency-mt-unsafe) - if (result != 0) { - std::string msg = "Error: could not start virtual iridium"; - std::cerr << msg << std::endl; - throw std::exception(); - } + std::thread vi_thread(std::system, run_iridium_cmd.c_str()); + //vi needs to run in background + vi_thread.detach(); + + const int MAX_ATTEMPTS = 100; // 100ms timeout, should only take <5 + int attempts = 0; + const int SLEEP_MS = 1; std::string set_baud_cmd = "stty 19200 < $LOCAL_TRANSCEIVER_TEST_PORT"; - result = std::system(set_baud_cmd.c_str()); //NOLINT(concurrency-mt-unsafe) - if (result != 0) { - std::string msg = "Error: could not set baud rate for virtual iridium"; + while (attempts < MAX_ATTEMPTS) { + if (std::filesystem::exists(LOCAL_TRANSCEIVER_TEST_PORT)) { + int result = std::system(set_baud_cmd.c_str()); //NOLINT(concurrency-mt-unsafe) + if (result == 0) { + break; + } + } + std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_MS)); + attempts++; + } + + if (attempts == MAX_ATTEMPTS) { + std::string msg = "Error: could not start virtual iridium"; std::cerr << msg << std::endl; throw std::exception(); } @@ -94,10 +105,8 @@ class LocalTransceiverIntf : public NetNode ros_topics::LOCAL_PATH, ROS_Q_SIZE, std::bind(&LocalTransceiverIntf::sub_local_path_data_cb, this, std::placeholders::_1)); - auto msg = lcl_trns_->getCache(); - if (msg) { - pub_->publish(*msg); - } + std::thread cache_thread(&LocalTransceiverIntf::getAndPublishCache, this); + cache_thread.detach(); } } @@ -112,6 +121,16 @@ class LocalTransceiverIntf : public NetNode rclcpp::Subscription::SharedPtr sub_gps; rclcpp::Subscription::SharedPtr sub_local_path_data; + // may want mutex to ensure cached waypoints can't be published after newly received waypoints, + // but don't think practically necessary since pub_cb won't fire until 5 minutes after cache call. + void getAndPublishCache() + { + auto msg = lcl_trns_->getCache(); + if (msg) { + pub_->publish(*msg); + } + } + /** * @brief Callback function to publish to onboard ROS network * diff --git a/src/network_systems/projects/local_transceiver/test/test_local_transceiver.cpp b/src/network_systems/projects/local_transceiver/test/test_local_transceiver.cpp index c70bb3ff4..876eeadc8 100644 --- a/src/network_systems/projects/local_transceiver/test/test_local_transceiver.cpp +++ b/src/network_systems/projects/local_transceiver/test/test_local_transceiver.cpp @@ -256,7 +256,8 @@ TEST_F(TestLocalTransceiver, checkCache) ASSERT_FALSE(std::filesystem::exists(cache)); - LocalTransceiver::cacheGlobalWaypoints(serialized_test); + std::future future = LocalTransceiver::cacheGlobalWaypointsAsync(serialized_test); + future.get(); ASSERT_TRUE(std::filesystem::exists(cache)); @@ -279,7 +280,8 @@ TEST_F(TestLocalTransceiver, checkCache) waypoint_b->set_longitude(updated_long); serialized_test = path.SerializeAsString(); - LocalTransceiver::cacheGlobalWaypoints(serialized_test); + future = LocalTransceiver::cacheGlobalWaypointsAsync(serialized_test); + future.get(); std::filesystem::path cache_temp{CACHE_TEMP_PATH}; ASSERT_TRUE(std::filesystem::exists(cache)); From c9b328b054e878a87c5f2a045743fa5c697ca75e Mon Sep 17 00:00:00 2001 From: adambrett40 <159198197+adambrett40@users.noreply.github.com> Date: Sat, 22 Mar 2025 10:34:04 -0700 Subject: [PATCH 14/15] made requested changes --- sailbot.code-workspace | 14 +++-- .../local_transceiver/inc/local_transceiver.h | 2 +- .../src/local_transceiver.cpp | 51 ++++++++----------- .../src/local_transceiver_ros_intf.cpp | 32 ++++++------ .../test/test_local_transceiver.cpp | 10 ++-- 5 files changed, 51 insertions(+), 58 deletions(-) diff --git a/sailbot.code-workspace b/sailbot.code-workspace index 875327e76..9a4a2f223 100644 --- a/sailbot.code-workspace +++ b/sailbot.code-workspace @@ -370,6 +370,7 @@ // for the documentation about the tasks.json format "version": "2.0.0", "tasks": [ + // Build tasks { "label": "Build All", "detail": "Build all packages in the workspace", @@ -400,6 +401,7 @@ "group": "build", "problemMatcher": "$gcc" }, + // Test tasks { "label": "Test All", "detail": "Run all unit tests and show results.", @@ -408,8 +410,7 @@ "group": { "kind": "test", "isDefault": true - }, - "problemMatcher": [] + } }, { "label": "Test Package", @@ -436,6 +437,7 @@ "command": "sudo rm -rf build install log; py3clean .", "problemMatcher": [] }, + // Linting and static code analysis tasks { "label": "lint_cmake", "detail": "Run lint on cmake files.", @@ -479,7 +481,7 @@ "type": "shell", "command": "LINTER=mypy LOCAL_RUN=true scripts/ament-lint.sh", "problemMatcher": [ - "$ament_mypy" + "$ament_mypy", ], "presentation": { "panel": "dedicated", @@ -492,7 +494,7 @@ "type": "shell", "command": "LINTER=xmllint LOCAL_RUN=true scripts/ament-lint.sh", "problemMatcher": [ - "$ament_xmllint" + "$ament_xmllint", ], "presentation": { "panel": "dedicated", @@ -506,10 +508,11 @@ "lint_cmake", "flake8", "mypy", - "xmllint" + "xmllint", ], "problemMatcher": [] }, + // Workspace editing tasks { "label": "setup", "detail": "Set up the workspace", @@ -517,6 +520,7 @@ "command": "./scripts/setup.sh", "problemMatcher": [] } + ], "inputs": [ { diff --git a/src/network_systems/projects/local_transceiver/inc/local_transceiver.h b/src/network_systems/projects/local_transceiver/inc/local_transceiver.h index d7f6a9c7e..3955c83e4 100644 --- a/src/network_systems/projects/local_transceiver/inc/local_transceiver.h +++ b/src/network_systems/projects/local_transceiver/inc/local_transceiver.h @@ -117,7 +117,7 @@ class LocalTransceiver * @param receivedDataBuffer string to cache * @return future object representing completion of the function */ - static std::future cacheGlobalWaypointsAsync(std::string receivedDataBuffer); + static void cacheGlobalWaypoints(std::string receivedDataBuffer); /** * @brief Retrieve the latest message from the remote server via the serial port diff --git a/src/network_systems/projects/local_transceiver/src/local_transceiver.cpp b/src/network_systems/projects/local_transceiver/src/local_transceiver.cpp index 37aafde1d..2e87fbbd9 100644 --- a/src/network_systems/projects/local_transceiver/src/local_transceiver.cpp +++ b/src/network_systems/projects/local_transceiver/src/local_transceiver.cpp @@ -87,11 +87,14 @@ LocalTransceiver::LocalTransceiver(const std::string & port_name, const uint32_t serial_.set_option(bio::serial_port_base::baud_rate(baud_rate)); // Set a timeout for read/write operations on the serial port setsockopt(serial_.native_handle(), SOL_SOCKET, SO_RCVTIMEO, &TIMEOUT, sizeof(TIMEOUT)); + std::ofstream cacheFile(CACHE_PATH); + cacheFile.close(); }; LocalTransceiver::~LocalTransceiver() { - // Intentionally left blank + std::filesystem::remove(CACHE_PATH); + std::filesystem::remove(CACHE_TEMP_PATH); } void LocalTransceiver::stop() @@ -207,32 +210,17 @@ std::optional LocalTransceiver::debugSend(const std::string & cmd) return readRsp(); } -std::future LocalTransceiver::cacheGlobalWaypointsAsync(std::string receivedDataBuffer) +void LocalTransceiver::cacheGlobalWaypoints(std::string receivedDataBuffer) { - return std::async(std::launch::async, [receivedDataBuffer] { - try { - std::filesystem::path cache{CACHE_PATH}; - if (std::filesystem::exists(cache)) { - std::filesystem::path cache_temp{CACHE_TEMP_PATH}; - std::ofstream writeFile(CACHE_TEMP_PATH, std::ios::binary); - if (!writeFile) { - std::cerr << "Failed to create temp cache file" << std::endl; - } - writeFile.write(receivedDataBuffer.data(), static_cast(receivedDataBuffer.size())); - writeFile.close(); - std::filesystem::rename(CACHE_TEMP_PATH, CACHE_PATH); - } else { - std::ofstream writeFile(CACHE_PATH, std::ios::binary); - if (!writeFile) { - std::cerr << "Failed to create cache file" << std::endl; - } - writeFile.write(receivedDataBuffer.data(), static_cast(receivedDataBuffer.size())); - writeFile.close(); - } - } catch (const std::exception & e) { - std::cerr << "Error caching global waypoints: " << e.what() << std::endl; - } - }); + std::filesystem::path cache{CACHE_PATH}; + std::filesystem::path cache_temp{CACHE_TEMP_PATH}; + std::ofstream writeFile(CACHE_TEMP_PATH, std::ios::binary); + if (!writeFile) { + std::cerr << "Failed to create temp cache file" << std::endl; + } + writeFile.write(receivedDataBuffer.data(), static_cast(receivedDataBuffer.size())); + writeFile.close(); + std::filesystem::rename(CACHE_TEMP_PATH, CACHE_PATH); } custom_interfaces::msg::Path LocalTransceiver::receive() @@ -331,10 +319,12 @@ custom_interfaces::msg::Path LocalTransceiver::receive() receivedDataBuffer = message; break; } - - cacheGlobalWaypointsAsync(receivedDataBuffer); + + std::future fut = std::async(std::launch::async, cacheGlobalWaypoints, receivedDataBuffer); custom_interfaces::msg::Path to_publish = parseInMsg(receivedDataBuffer); + + fut.get(); return to_publish; } @@ -371,12 +361,11 @@ custom_interfaces::msg::Path LocalTransceiver::parseInMsg(const std::string & ms std::optional LocalTransceiver::getCache() { - std::filesystem::path cache{CACHE_PATH}; - if (std::filesystem::exists(cache)) { + if (std::filesystem::exists(CACHE_PATH) && std::filesystem::file_size(CACHE_PATH) > 0) { std::ifstream input(CACHE_PATH, std::ios::binary); std::string cachedDataBuffer(std::istreambuf_iterator(input), {}); custom_interfaces::msg::Path to_publish = parseInMsg(cachedDataBuffer); - return to_publish; + return std::make_optional(to_publish); } return std::nullopt; } diff --git a/src/network_systems/projects/local_transceiver/src/local_transceiver_ros_intf.cpp b/src/network_systems/projects/local_transceiver/src/local_transceiver_ros_intf.cpp index 038b17e4a..23d8a1a84 100644 --- a/src/network_systems/projects/local_transceiver/src/local_transceiver_ros_intf.cpp +++ b/src/network_systems/projects/local_transceiver/src/local_transceiver_ros_intf.cpp @@ -50,16 +50,21 @@ class LocalTransceiverIntf : public NetNode //vi needs to run in background vi_thread.detach(); - const int MAX_ATTEMPTS = 100; // 100ms timeout, should only take <5 - int attempts = 0; - const int SLEEP_MS = 1; - std::string set_baud_cmd = "stty 19200 < $LOCAL_TRANSCEIVER_TEST_PORT"; + const int MAX_ATTEMPTS = 100; // 100ms timeout, should only take <5 + int attempts = 0; + const int SLEEP_MS = 1; + const int IOCTL_ERR_CODE = 256; + std::string set_baud_cmd = "stty 19200 < $LOCAL_TRANSCEIVER_TEST_PORT 2>/dev/null"; + //silence stderr to not clutter console while polling while (attempts < MAX_ATTEMPTS) { if (std::filesystem::exists(LOCAL_TRANSCEIVER_TEST_PORT)) { int result = std::system(set_baud_cmd.c_str()); //NOLINT(concurrency-mt-unsafe) if (result == 0) { break; } + if (result != IOCTL_ERR_CODE) { + std::cerr << "Failed to set baud rate with code: " << result << std::endl; + } } std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_MS)); attempts++; @@ -84,6 +89,9 @@ class LocalTransceiverIntf : public NetNode this->get_logger(), "Running Local Transceiver in mode: %s, with port: %s.", mode.c_str(), port.c_str()); lcl_trns_ = std::make_unique(port, SATELLITE_BAUD_RATE); + std::future> fut = + std::async(std::launch::async, lcl_trns_->getCache); + static constexpr int ROS_Q_SIZE = 5; static constexpr auto TIMER_INTERVAL = std::chrono::milliseconds(300000); timer_ = this->create_wall_timer(TIMER_INTERVAL, std::bind(&LocalTransceiverIntf::pub_cb, this)); @@ -105,8 +113,10 @@ class LocalTransceiverIntf : public NetNode ros_topics::LOCAL_PATH, ROS_Q_SIZE, std::bind(&LocalTransceiverIntf::sub_local_path_data_cb, this, std::placeholders::_1)); - std::thread cache_thread(&LocalTransceiverIntf::getAndPublishCache, this); - cache_thread.detach(); + std::optional msg = fut.get(); + if (msg) { + pub_->publish(*msg); + } } } @@ -121,16 +131,6 @@ class LocalTransceiverIntf : public NetNode rclcpp::Subscription::SharedPtr sub_gps; rclcpp::Subscription::SharedPtr sub_local_path_data; - // may want mutex to ensure cached waypoints can't be published after newly received waypoints, - // but don't think practically necessary since pub_cb won't fire until 5 minutes after cache call. - void getAndPublishCache() - { - auto msg = lcl_trns_->getCache(); - if (msg) { - pub_->publish(*msg); - } - } - /** * @brief Callback function to publish to onboard ROS network * diff --git a/src/network_systems/projects/local_transceiver/test/test_local_transceiver.cpp b/src/network_systems/projects/local_transceiver/test/test_local_transceiver.cpp index 00bc19911..c72bea985 100644 --- a/src/network_systems/projects/local_transceiver/test/test_local_transceiver.cpp +++ b/src/network_systems/projects/local_transceiver/test/test_local_transceiver.cpp @@ -254,12 +254,13 @@ TEST_F(TestLocalTransceiver, checkCache) std::string serialized_test = path.SerializeAsString(); std::filesystem::path cache{CACHE_PATH}; - ASSERT_FALSE(std::filesystem::exists(cache)); + ASSERT_TRUE(std::filesystem::exists(cache)); + ASSERT_TRUE(std::filesystem::file_size(CACHE_PATH) == 0); - std::future future = LocalTransceiver::cacheGlobalWaypointsAsync(serialized_test); - future.get(); + LocalTransceiver::cacheGlobalWaypoints(serialized_test); ASSERT_TRUE(std::filesystem::exists(cache)); + ASSERT_TRUE(std::filesystem::file_size(CACHE_PATH) > 0); auto cache_obj = LocalTransceiver::getCache(); @@ -280,8 +281,7 @@ TEST_F(TestLocalTransceiver, checkCache) waypoint_b->set_longitude(updated_long); serialized_test = path.SerializeAsString(); - future = LocalTransceiver::cacheGlobalWaypointsAsync(serialized_test); - future.get(); + LocalTransceiver::cacheGlobalWaypoints(serialized_test); std::filesystem::path cache_temp{CACHE_TEMP_PATH}; ASSERT_TRUE(std::filesystem::exists(cache)); From d0246a5e281d9f50a0ab2268bb29c4b96633dc02 Mon Sep 17 00:00:00 2001 From: adambrett40 <159198197+adambrett40@users.noreply.github.com> Date: Sat, 29 Mar 2025 11:27:04 -0700 Subject: [PATCH 15/15] fixed extra merge conflict --- .../local_transceiver/test/test_local_transceiver.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/network_systems/projects/local_transceiver/test/test_local_transceiver.cpp b/src/network_systems/projects/local_transceiver/test/test_local_transceiver.cpp index 4ffc772bd..69de58756 100644 --- a/src/network_systems/projects/local_transceiver/test/test_local_transceiver.cpp +++ b/src/network_systems/projects/local_transceiver/test/test_local_transceiver.cpp @@ -300,11 +300,11 @@ TEST_F(TestLocalTransceiver, checkCache) EXPECT_EQ(parsed_test.waypoints[1].longitude, parsed_cache.waypoints[0].longitude); } -std::mutex port_mutex; +// std::mutex port_mutex; -TEST_F(TestLocalTransceiver, testMailboxBlackbox) -{ - std::lock_guard lock(port_mutex); // because same port is being used +// TEST_F(TestLocalTransceiver, testMailboxBlackbox) +// { +// std::lock_guard lock(port_mutex); // because same port is being used // std::string holder = "curl -X POST -F \"test=1234\" http://localhost:8080"; // std::string holder2 = "printf \"at+sbdix\r\" > $LOCAL_TRANSCEIVER_TEST_PORT";