diff --git a/source/RobotAPI/libraries/armem/mns/MemoryNameSystem.cpp b/source/RobotAPI/libraries/armem/mns/MemoryNameSystem.cpp index 3247c4cc45133132a1346fa42b46166c183a60ad..f143e1436df1b25e521aa3a3b98ae03caf2afb57 100644 --- a/source/RobotAPI/libraries/armem/mns/MemoryNameSystem.cpp +++ b/source/RobotAPI/libraries/armem/mns/MemoryNameSystem.cpp @@ -1,21 +1,29 @@ #include "MemoryNameSystem.h" - namespace armarx::armem::mns { // dto::WaitForServerResult MemoryNameSystem::waitForServer(const dto::WaitForServerInput& input) - void MemoryNameSystem::waitForServer_async( + void + MemoryNameSystem::waitForServer_async( const AMD_MemoryNameSystemInterface_waitForServerPtr& future, const dto::WaitForServerInput& input) { - waitForServerFutures[input.name].push_back(future); + ARMARX_INFO << "Waiting for server `" << input.name << "`."; + + { + std::lock_guard g{futuresMtx}; + waitForServerFutures[input.name].push_back(future); + } + waitForServer_processOnce(); } - - void MemoryNameSystem::waitForServer_processOnce() + void + MemoryNameSystem::waitForServer_processOnce() { + std::lock_guard g{futuresMtx}; + for (auto it = waitForServerFutures.begin(); it != waitForServerFutures.end();) { auto& [name, futures] = *it; @@ -32,16 +40,20 @@ namespace armarx::armem::mns { future->ice_response(result); } + + // it: iterator following the last removed element (https://en.cppreference.com/w/cpp/container/map/erase) it = waitForServerFutures.erase(it); } else { - ++it; // Skip. + ++it; // Skip. + ARMARX_INFO << "Server `" << name << "` not available yet."; } } } - - dto::RegisterServerResult MemoryNameSystem::registerServer(const dto::RegisterServerInput& input) + + dto::RegisterServerResult + MemoryNameSystem::registerServer(const dto::RegisterServerInput& input) { const auto result = Registry::registerServer(input); waitForServer_processOnce(); @@ -49,8 +61,8 @@ namespace armarx::armem::mns return result; } - - armarx::RemoteGui::Client::GridLayout MemoryNameSystem::RemoteGui_buildInfoGrid() + armarx::RemoteGui::Client::GridLayout + MemoryNameSystem::RemoteGui_buildInfoGrid() { using namespace armarx::RemoteGui::Client; @@ -58,10 +70,9 @@ namespace armarx::armem::mns int row = 0; grid.add(Label("Memory Name"), {row, 0}) - .add(Label("Component Name"), {row, 1}) - .add(Label("R/W/P/A"), {row, 2}) - .add(Label("Registration Time"), {row, 3}) - ; + .add(Label("Component Name"), {row, 1}) + .add(Label("R/W/P/A"), {row, 2}) + .add(Label("Registration Time"), {row, 3}); row++; for (const auto& [name, info] : servers) @@ -109,4 +120,4 @@ namespace armarx::armem::mns return grid; } -} +} // namespace armarx::armem::mns diff --git a/source/RobotAPI/libraries/armem/mns/MemoryNameSystem.h b/source/RobotAPI/libraries/armem/mns/MemoryNameSystem.h index e0cf5b88753cb4a96caad2c004a853dcd4d11a34..9ca9cc260ead437543d8cc2903f42fceb9009e0b 100644 --- a/source/RobotAPI/libraries/armem/mns/MemoryNameSystem.h +++ b/source/RobotAPI/libraries/armem/mns/MemoryNameSystem.h @@ -1,14 +1,14 @@ #pragma once -#include "Registry.h" - -#include <RobotAPI/interface/armem/mns/MemoryNameSystemInterface.h> +#include <map> +#include <mutex> +#include <string> #include <ArmarXGui/libraries/ArmarXGuiComponentPlugins/LightweightRemoteGuiComponentPlugin.h> -#include <map> -#include <string> +#include <RobotAPI/interface/armem/mns/MemoryNameSystemInterface.h> +#include "Registry.h" namespace armarx::armem::mns { @@ -16,18 +16,15 @@ namespace armarx::armem::mns class MemoryNameSystem : public Registry { public: - using WaitForServerFuturePtr = AMD_MemoryNameSystemInterface_waitForServerPtr; public: - /** * @brief Store the call in a container for later response. */ - void waitForServer_async( - const AMD_MemoryNameSystemInterface_waitForServerPtr& future, - const dto::WaitForServerInput& input); + void waitForServer_async(const AMD_MemoryNameSystemInterface_waitForServerPtr& future, + const dto::WaitForServerInput& input); void waitForServer_processOnce(); @@ -38,11 +35,11 @@ namespace armarx::armem::mns armarx::RemoteGui::Client::GridLayout RemoteGui_buildInfoGrid(); - public: - + private: /// Queued calls to `waitForServer`. std::map<std::string, std::vector<WaitForServerFuturePtr>> waitForServerFutures; + std::mutex futuresMtx; }; -} +} // namespace armarx::armem::mns