From b1c7ff63fe54cc945500e6de9ecaf03ee7234a03 Mon Sep 17 00:00:00 2001
From: Fabian Reister <fabian.reister@kit.edu>
Date: Tue, 8 Jun 2021 19:55:02 +0200
Subject: [PATCH] simple writer base class

---
 .../RobotAPI/libraries/armem/CMakeLists.txt   |  5 ++
 .../armem/client/util/SimpleWriterBase.cpp    | 60 ++++++++++++++
 .../armem/client/util/SimpleWriterBase.h      | 81 +++++++++++++++++++
 3 files changed, 146 insertions(+)
 create mode 100644 source/RobotAPI/libraries/armem/client/util/SimpleWriterBase.cpp
 create mode 100644 source/RobotAPI/libraries/armem/client/util/SimpleWriterBase.h

diff --git a/source/RobotAPI/libraries/armem/CMakeLists.txt b/source/RobotAPI/libraries/armem/CMakeLists.txt
index eb83670b3..a9d54ac0e 100644
--- a/source/RobotAPI/libraries/armem/CMakeLists.txt
+++ b/source/RobotAPI/libraries/armem/CMakeLists.txt
@@ -66,6 +66,8 @@ set(LIB_FILES
     client/Writer.cpp
     client/WriterComponentPlugin.cpp
 
+    client/util/SimpleWriterBase.cpp
+
     client/Query.cpp
     client/query/Builder.cpp
     client/query/selectors.cpp
@@ -163,6 +165,9 @@ set(LIB_HEADERS
     client/Writer.h
     client/WriterComponentPlugin.h
 
+    client/util/SimpleWriterBase.h
+
+
     client/Query.h
     client/query/Builder.h
     client/query/query_fns.h
diff --git a/source/RobotAPI/libraries/armem/client/util/SimpleWriterBase.cpp b/source/RobotAPI/libraries/armem/client/util/SimpleWriterBase.cpp
new file mode 100644
index 000000000..2a3b6d693
--- /dev/null
+++ b/source/RobotAPI/libraries/armem/client/util/SimpleWriterBase.cpp
@@ -0,0 +1,60 @@
+#include "SimpleWriterBase.h"
+
+#include "RobotAPI/libraries/armem/client/ComponentPlugin.h"
+
+namespace armarx::armem::client::util
+{
+    SimpleWriterBase::SimpleWriterBase(ComponentPluginUser& component) :
+        component(component)
+    {
+    }
+
+    void
+    SimpleWriterBase::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");
+
+        def->required(props.providerName,
+                      prefix + "Provider",
+                      "Name of this provider");
+    }
+
+    void SimpleWriterBase::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 << "SimpleWriterBase: Connected to memory '"
+                         << props.memoryName;
+
+        memoryWriterClient.setWritingMemory(result.proxy);
+        // memoryReader.setReadingMemory(result.proxy);
+    }
+    
+    std::mutex& SimpleWriterBase::memoryWriterMutex() 
+    {
+        return memoryMutex;
+    }
+    
+    armem::client::Writer& SimpleWriterBase::memoryWriter() 
+    {
+        return memoryWriterClient;
+    }
+    const SimpleWriterBase::Properties& SimpleWriterBase::properties() const
+    {
+        return props;
+    }
+} // namespace armarx::armem::client::util
\ No newline at end of file
diff --git a/source/RobotAPI/libraries/armem/client/util/SimpleWriterBase.h b/source/RobotAPI/libraries/armem/client/util/SimpleWriterBase.h
new file mode 100644
index 000000000..0d8436e42
--- /dev/null
+++ b/source/RobotAPI/libraries/armem/client/util/SimpleWriterBase.h
@@ -0,0 +1,81 @@
+/*
+ * 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"
+#include "RobotAPI/libraries/armem/client/Writer.h"
+
+namespace armarx::armem
+{
+    class ClientComponentPluginUser;
+}
+
+namespace armarx::armem::client
+{
+    class ComponentPluginUser;
+}
+
+namespace armarx::armem::client::util
+{
+
+    class SimpleWriterBase
+    {
+      public:
+        SimpleWriterBase(ComponentPluginUser& component);
+        virtual ~SimpleWriterBase() = default;
+
+        void registerPropertyDefinitions(armarx::PropertyDefinitionsPtr& def);
+        void connect();
+
+      protected:
+        std::mutex& memoryWriterMutex();
+        armem::client::Writer& memoryWriter();
+
+        struct Properties
+        {
+            std::string memoryName      = "";
+            std::string coreSegmentName = "";
+            std::string providerName    = ""; // required property
+        };
+
+        const Properties& properties() const;
+
+        virtual std::string propertyPrefix() const   = 0;
+        virtual Properties defaultProperties() const = 0;
+
+      private:
+        Properties props;
+
+        armem::client::Writer memoryWriterClient;
+        std::mutex memoryMutex;
+
+        // armem::client::Reader memoryReader;
+        // std::mutex memoryReaderMutex;
+
+        ComponentPluginUser& component;
+    };
+
+} // namespace armarx::armem::client::util
\ No newline at end of file
-- 
GitLab