From 3258b4cdbf74a7b9e9b6a3532dafe143baa4543e Mon Sep 17 00:00:00 2001
From: Rainer Kartmann <rainer.kartmann@kit.edu>
Date: Mon, 30 Aug 2021 09:20:22 +0200
Subject: [PATCH] Memory server plugin: Move interesting stuff to plugin,
 provide methods to access them.

Use working memory name to set property
---
 .../armem/server/ComponentPlugin.cpp          | 76 ++++++++++++++-----
 .../libraries/armem/server/ComponentPlugin.h  | 67 +++++++++++-----
 2 files changed, 103 insertions(+), 40 deletions(-)

diff --git a/source/RobotAPI/libraries/armem/server/ComponentPlugin.cpp b/source/RobotAPI/libraries/armem/server/ComponentPlugin.cpp
index 18a24d77f..c97d55af4 100644
--- a/source/RobotAPI/libraries/armem/server/ComponentPlugin.cpp
+++ b/source/RobotAPI/libraries/armem/server/ComponentPlugin.cpp
@@ -17,10 +17,15 @@ namespace armarx::armem::server::plugins
     {
         MemoryNameSystemComponentPlugin::postCreatePropertyDefinitions(properties);
 
-        ComponentPluginUser& parent = this->parent<ComponentPluginUser>();
-        properties->topic(memoryListener, parent.memoryListenerDefaultName);
+        const std::string prefix = "mem.";
+        if (not workingMemory.name().empty()
+            and not properties->hasDefinition(prefix + "MemoryName"))
+        {
+            properties->optional(workingMemory.name(), prefix + "MemoryName", "Name of this memory server.");
+        }
+        properties->optional(longtermMemoryEnabled, prefix + "ltm.00_enabled");
 
-        properties->optional(parent.longtermMemoryEnabled, "mem.ltm.00_enabled");
+        properties->topic(memoryTopic, memoryTopicDefaultName);
     }
 
 
@@ -28,16 +33,16 @@ namespace armarx::armem::server::plugins
     {
         ComponentPluginUser& parent = this->parent<ComponentPluginUser>();
 
-        if (isMemoryNameSystemEnabled() && parent.memoryNameSystem)
+        if (isMemoryNameSystemEnabled() && parent.memoryNameSystem())
         {
             registerMemory(parent);
         }
-        parent.iceMemory.setMemoryListener(memoryListener);
+        parent.iceAdapter().setMemoryListener(memoryTopic);
 
         // establishing connection to ltm and mongodb
-        if (parent.longtermMemoryEnabled)
+        if (parent.isLongtermMemoryEnabled())
         {
-            parent.longtermMemoryManager.reload();
+            parent.longtermMemoryManager().reload();
         }
     }
 
@@ -45,23 +50,30 @@ namespace armarx::armem::server::plugins
     void ComponentPlugin::preOnDisconnectComponent()
     {
         ComponentPluginUser& parent = this->parent<ComponentPluginUser>();
-        if (isMemoryNameSystemEnabled() && parent.memoryNameSystem)
+        if (isMemoryNameSystemEnabled() && memoryNameSystem)
         {
             removeMemory(parent);
         }
     }
 
 
+    void ComponentPlugin::setMemoryName(const std::string& memoryName)
+    {
+        workingMemory.name() = memoryName;
+        longtermMemoryManager.setName(memoryName);
+    }
+
+
     data::RegisterMemoryResult ComponentPlugin::registerMemory(ComponentPluginUser& parent)
     {
-        MemoryID id = MemoryID().withMemoryName(parent.workingMemory.name());
+        MemoryID id = MemoryID().withMemoryName(parent.workingMemory().name());
         MemoryInterfacePrx proxy = MemoryInterfacePrx::checkedCast(parent.getProxy());
         ARMARX_CHECK_NOT_NULL(proxy);
 
         data::RegisterMemoryResult result;
         try
         {
-            parent.memoryNameSystem.registerServer(id, proxy);
+            parent.memoryNameSystem().registerServer(id, proxy);
             result.success = true;
             ARMARX_DEBUG << "Registered memory server for " << id << " in the Memory Name System (MNS).";
         }
@@ -77,12 +89,12 @@ namespace armarx::armem::server::plugins
 
     data::RemoveMemoryResult ComponentPlugin::removeMemory(ComponentPluginUser& parent)
     {
-        MemoryID id = MemoryID().withMemoryName(parent.workingMemory.name());
+        MemoryID id = MemoryID().withMemoryName(parent.workingMemory().name());
 
         data::RemoveMemoryResult result;
         try
         {
-            parent.memoryNameSystem.removeServer(id);
+            parent.memoryNameSystem().removeServer(id);
             result.success = true;
             ARMARX_DEBUG << "Removed memory server for " << id << " from the Memory Name System (MNS).";
         }
@@ -116,8 +128,7 @@ namespace armarx::armem::server
     // Set the name of a memory
     void ComponentPluginUser::setMemoryName(const std::string& memoryName)
     {
-        workingMemory.name() = memoryName;
-        longtermMemoryManager.setName(memoryName);
+        plugin->setMemoryName(memoryName);
     }
 
 
@@ -132,7 +143,7 @@ namespace armarx::armem::server
     data::AddSegmentsResult ComponentPluginUser::addSegments(const data::AddSegmentsInput& input, bool addCoreSegments)
     {
         ARMARX_TRACE;
-        data::AddSegmentsResult result = iceMemory.addSegments(input, addCoreSegments);
+        data::AddSegmentsResult result = iceAdapter().addSegments(input, addCoreSegments);
         return result;
     }
 
@@ -140,7 +151,7 @@ namespace armarx::armem::server
     data::CommitResult ComponentPluginUser::commit(const data::Commit& commitIce, const Ice::Current&)
     {
         ARMARX_TRACE;
-        return iceMemory.commit(commitIce);
+        return iceAdapter().commit(commitIce);
     }
 
 
@@ -148,7 +159,7 @@ namespace armarx::armem::server
     armem::query::data::Result ComponentPluginUser::query(const armem::query::data::Input& input, const Ice::Current&)
     {
         ARMARX_TRACE;
-        return iceMemory.query(input);
+        return iceAdapter().query(input);
     }
 
 
@@ -156,10 +167,37 @@ namespace armarx::armem::server
     data::StoreResult ComponentPluginUser::store(const data::StoreInput& input, const Ice::Current&)
     {
         ARMARX_TRACE;
-        return iceMemory.store(input);
+        return iceAdapter().store(input);
+    }
+
+
+    ComponentPlugin& ComponentPluginUser::memoryServerPlugin()
+    {
+        return *plugin;
     }
 
 
-    // LTM LOADING
+    wm::Memory& ComponentPluginUser::workingMemory()
+    {
+        return plugin->workingMemory;
+    }
+
+
+    MemoryToIceAdapter& ComponentPluginUser::iceAdapter()
+    {
+        return plugin->iceAdapter;
+    }
+
+
+    bool ComponentPluginUser::isLongtermMemoryEnabled()
+    {
+        return plugin->longtermMemoryEnabled;
+    }
+
+
+    ltm::mongodb::MemoryManager& ComponentPluginUser::longtermMemoryManager()
+    {
+        return plugin->longtermMemoryManager;
+    }
 
 }
diff --git a/source/RobotAPI/libraries/armem/server/ComponentPlugin.h b/source/RobotAPI/libraries/armem/server/ComponentPlugin.h
index 8a0c3fbc6..ba4e890f5 100644
--- a/source/RobotAPI/libraries/armem/server/ComponentPlugin.h
+++ b/source/RobotAPI/libraries/armem/server/ComponentPlugin.h
@@ -27,16 +27,25 @@ namespace armarx::armem::server::plugins
     class ComponentPlugin : public client::plugins::MemoryNameSystemComponentPlugin
     {
         using Base = client::plugins::MemoryNameSystemComponentPlugin;
+
     public:
 
         using Base::MemoryNameSystemComponentPlugin;
         virtual ~ComponentPlugin() override;
 
-        void postCreatePropertyDefinitions(PropertyDefinitionsPtr& properties) override;
+        virtual void postCreatePropertyDefinitions(PropertyDefinitionsPtr& properties) override;
+
+        virtual void postOnConnectComponent() override;
+        virtual void preOnDisconnectComponent() override;
 
-        void postOnConnectComponent() override;
-        void preOnDisconnectComponent() override;
 
+    public:
+
+        /// Set the name of the wm and the ltm (if enabled)
+        void setMemoryName(const std::string& memoryName);
+
+
+    protected:
 
         /**
          * @brief Register the parent component in the MNS.
@@ -49,11 +58,37 @@ namespace armarx::armem::server::plugins
          */
         data::RemoveMemoryResult removeMemory(ComponentPluginUser& parent);
 
-        client::MemoryListenerInterfacePrx memoryListener;
+
+
+    public:
+
+        // Working Memory
+
+        /// The actual memory.
+        server::wm::Memory workingMemory;
+
+        /// Helps connecting `memory` to ice. Used to handle Ice callbacks.
+        MemoryToIceAdapter iceAdapter { &workingMemory, &longtermMemoryManager};
+
+
+        // Working Memory Updates
+
+        std::string memoryTopicDefaultName = "MemoryUpdates";
+        client::MemoryListenerInterfacePrx memoryTopic;
+
+
+        // Long-Term Memory
+
+        /// Indicates whether to use or not to use the ltm feature.
+        bool longtermMemoryEnabled = true;
+
+        /// A manager class for the ltm. It internally holds a normal wm instance as a cache.
+        server::ltm::mongodb::MemoryManager longtermMemoryManager;
 
         std::string longTermMemoryDatabaseHost;
         std::string longTermMemoryDatabaseUser;
         std::string longTermMemoryDatabasePassword;
+
     };
 
 }
@@ -64,6 +99,8 @@ namespace armarx::armem::server
 
     /**
      * @brief Base class of memory server components.
+     *
+     * Implements the server ice interfaces using the ice adapter of the plugin.
      */
     class ComponentPluginUser :
         virtual public ManagedIceObject
@@ -94,27 +131,15 @@ namespace armarx::armem::server
         virtual data::StoreResult store(const data::StoreInput&, const Ice::Current& = Ice::emptyCurrent) override;
 
 
-        // LoadingInterface interface
-
-
     public:
 
-        /// The actual memory.
-        server::wm::Memory workingMemory;
-        // [[deprecated ("The global working memory mutex is deprecated. Use the core segment mutexes instead.")]]
-        // std::mutex workingMemoryMutex;
+        ComponentPlugin& memoryServerPlugin();
 
-        /// Parameter to indicate whether to use or not to use the ltm feature
-        bool longtermMemoryEnabled = true;
+        server::wm::Memory& workingMemory();
+        MemoryToIceAdapter& iceAdapter();
 
-        /// A manager class for the ltm. It internally holds a normal wm instance
-        server::ltm::mongodb::MemoryManager longtermMemoryManager;
-
-        /// property defaults
-        std::string memoryListenerDefaultName = "MemoryUpdates";
-
-        /// Helps connecting `memory` to ice. Used to handle Ice callbacks.
-        MemoryToIceAdapter iceMemory { &workingMemory, &longtermMemoryManager};
+        bool isLongtermMemoryEnabled();
+        server::ltm::mongodb::MemoryManager& longtermMemoryManager();
 
 
     private:
-- 
GitLab