diff --git a/source/RobotAPI/libraries/armem/CMakeLists.txt b/source/RobotAPI/libraries/armem/CMakeLists.txt index a9d54ac0e339f3e901ddda23cf7c9712d70546ec..c4ad28ab1f6880f59adb4ca7eda5e4e8a8a65007 100644 --- a/source/RobotAPI/libraries/armem/CMakeLists.txt +++ b/source/RobotAPI/libraries/armem/CMakeLists.txt @@ -66,6 +66,7 @@ set(LIB_FILES client/Writer.cpp client/WriterComponentPlugin.cpp + client/util/SimpleReaderBase.cpp client/util/SimpleWriterBase.cpp client/Query.cpp @@ -165,6 +166,7 @@ set(LIB_HEADERS client/Writer.h client/WriterComponentPlugin.h + client/util/SimpleReaderBase.h client/util/SimpleWriterBase.h diff --git a/source/RobotAPI/libraries/armem/client/util/SimpleReaderBase.cpp b/source/RobotAPI/libraries/armem/client/util/SimpleReaderBase.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4a6ba069eb877499c4abe3df051c1bf9c22227bc --- /dev/null +++ b/source/RobotAPI/libraries/armem/client/util/SimpleReaderBase.cpp @@ -0,0 +1,54 @@ +#include "SimpleReaderBase.h" + +#include "RobotAPI/libraries/armem/client/ComponentPlugin.h" + +namespace armarx::armem::client::util +{ + SimpleReaderBase::SimpleReaderBase(ComponentPluginUser& component) : + component(component) + { + } + + void + SimpleReaderBase::registerPropertyDefinitions(armarx::PropertyDefinitionsPtr& def) + { + ARMARX_DEBUG << "Writer: registerPropertyDefinitions"; + + const std::string prefix = propertyPrefix(); + + def->optional(props.memoryName, prefix + "Memory"); + def->optional(props.coreSegmentName, prefix + "CoreSegment"); + } + + void SimpleReaderBase::connect() + { + // Wait for the memory to become available and add it as dependency. + ARMARX_IMPORTANT << "Writer: Waiting for memory '" + << props.memoryName << "' ..."; + auto result = component.useMemory(props.memoryName); + if (not result.success) + { + ARMARX_ERROR << result.errorMessage; + return; + } + + ARMARX_IMPORTANT << "SimpleReaderBase: Connected to memory '" + << props.memoryName; + + memoryReaderClient.setReadingMemory(result.proxy); + } + + std::mutex& SimpleReaderBase::memoryReaderMutex() + { + return memoryMutex; + } + + armem::client::Reader& SimpleReaderBase::memoryReader() + { + return memoryReaderClient; + } + const SimpleReaderBase::Properties& SimpleReaderBase::properties() const + { + return props; + } +} // namespace armarx::armem::client::util \ No newline at end of file diff --git a/source/RobotAPI/libraries/armem/client/util/SimpleReaderBase.h b/source/RobotAPI/libraries/armem/client/util/SimpleReaderBase.h new file mode 100644 index 0000000000000000000000000000000000000000..8503cda610a0062e71bd88242a94fa87345152ff --- /dev/null +++ b/source/RobotAPI/libraries/armem/client/util/SimpleReaderBase.h @@ -0,0 +1,77 @@ +/* + * This file is part of ArmarX. + * + * ArmarX is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * ArmarX is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * @author Fabian Reister ( fabian dot reister at kit dot edu ) + * @date 2021 + * @copyright http://www.gnu.org/licenses/gpl-2.0.txt + * GNU General Public License + */ + +#pragma once + +#include <mutex> + +#include "ArmarXCore/core/application/properties/PropertyDefinitionContainer.h" + +#include "RobotAPI/libraries/armem/client/Reader.h" + +namespace armarx::armem +{ + class ClientComponentPluginUser; +} + +namespace armarx::armem::client +{ + class ComponentPluginUser; +} + +namespace armarx::armem::client::util +{ + + class SimpleReaderBase + { + public: + SimpleReaderBase(ComponentPluginUser& memoryClient); + virtual ~SimpleReaderBase() = default; + + void registerPropertyDefinitions(armarx::PropertyDefinitionsPtr& def); + void connect(); + + protected: + // Properties + struct Properties + { + std::string memoryName = ""; + std::string coreSegmentName = ""; + }; + + const Properties& properties() const; + + virtual std::string propertyPrefix() const = 0; + virtual Properties defaultProperties() const = 0; + + std::mutex& memoryReaderMutex(); + armem::client::Reader& memoryReader(); + + private: + Properties props; + + armem::client::Reader memoryReaderClient; + std::mutex memoryMutex; + + ComponentPluginUser& component; + }; + +} // namespace armarx::armem::client::util