From 5c0974bc716cd4bdde7a36d97845ea261c79a163 Mon Sep 17 00:00:00 2001
From: Rainer Kartmann <rainer.kartmann@kit.edu>
Date: Mon, 30 Aug 2021 09:19:07 +0200
Subject: [PATCH] MNS: Move interesting stuff to plugin (not user), split
 listening from non-listening user

---
 .../MemoryNameSystemComponentPlugin.cpp       | 101 +++++++-----------
 .../client/MemoryNameSystemComponentPlugin.h  |  76 ++++++++-----
 .../libraries/armem/server/ComponentPlugin.h  |   2 +-
 3 files changed, 87 insertions(+), 92 deletions(-)

diff --git a/source/RobotAPI/libraries/armem/client/MemoryNameSystemComponentPlugin.cpp b/source/RobotAPI/libraries/armem/client/MemoryNameSystemComponentPlugin.cpp
index 60fae2cf9..07ef49f0f 100644
--- a/source/RobotAPI/libraries/armem/client/MemoryNameSystemComponentPlugin.cpp
+++ b/source/RobotAPI/libraries/armem/client/MemoryNameSystemComponentPlugin.cpp
@@ -21,14 +21,14 @@ namespace armarx::armem::client::plugins
         {
             properties->defineOptionalProperty<std::string>(
                 makePropertyName(PROPERTY_MNS_NAME_NAME),
-                parent<MemoryNameSystemComponentPluginUser>().memoryNameSystemName,
+                memoryNameSystemName,
                 "Name of the Memory Name System (MNS) component.");
         }
         if (!properties->hasDefinition(makePropertyName(PROPERTY_MNS_ENABLED_NAME)))
         {
             properties->defineOptionalProperty<bool>(
                 makePropertyName(PROPERTY_MNS_ENABLED_NAME),
-                parent<MemoryNameSystemComponentPluginUser>().memoryNameSystemEnabled,
+                memoryNameSystemEnabled,
                 "Whether to use (and depend on) the Memory Name System (MNS)."
                 "\nSet to false to use this memory as a stand-alone.");
         }
@@ -37,6 +37,7 @@ namespace armarx::armem::client::plugins
         properties->topic<MemoryListenerInterface>("MemoryUpdates");
     }
 
+
     void MemoryNameSystemComponentPlugin::preOnInitComponent()
     {
         if (isMemoryNameSystemEnabled())
@@ -45,37 +46,53 @@ namespace armarx::armem::client::plugins
         }
     }
 
+
     void MemoryNameSystemComponentPlugin::preOnConnectComponent()
     {
         if (isMemoryNameSystemEnabled())
         {
             ARMARX_DEBUG << "Creating MemoryNameSystem client with owning component '" << parent().getName() << "'.";
-            parent<MemoryNameSystemComponentPluginUser>().memoryNameSystem =
-                MemoryNameSystem(getMemoryNameSystem(), &parent());
+            memoryNameSystem = MemoryNameSystem(getMemoryNameSystemProxy(), &parent());
         }
     }
 
-    bool MemoryNameSystemComponentPlugin::isMemoryNameSystemEnabled()
+
+    bool
+    MemoryNameSystemComponentPlugin::isMemoryNameSystemEnabled()
     {
-        return parentDerives<Component>() ?
-               parent<Component>().getProperty<bool>(makePropertyName(PROPERTY_MNS_ENABLED_NAME)) :
-               parent<MemoryNameSystemComponentPluginUser>().memoryNameSystemEnabled;
+        return memoryNameSystemEnabled;
     }
 
-    std::string MemoryNameSystemComponentPlugin::getMemoryNameSystemName()
+
+    std::string
+    MemoryNameSystemComponentPlugin::getMemoryNameSystemName()
     {
-        return parentDerives<Component>() ?
-               parent<Component>().getProperty<std::string>(makePropertyName(PROPERTY_MNS_NAME_NAME)) :
-               std::string{parent<MemoryNameSystemComponentPluginUser>().memoryNameSystemName};
+        return memoryNameSystemName;
     }
 
-    mns::MemoryNameSystemInterfacePrx MemoryNameSystemComponentPlugin::getMemoryNameSystem()
+
+    mns::MemoryNameSystemInterfacePrx
+    MemoryNameSystemComponentPlugin::getMemoryNameSystemProxy()
     {
-        return isMemoryNameSystemEnabled() && parentDerives<Component>()
-               ? parent<Component>().getProxy<mns::MemoryNameSystemInterfacePrx>(getMemoryNameSystemName())
+        return isMemoryNameSystemEnabled() and parentDerives<ManagedIceObject>()
+               ? parent<ManagedIceObject>().getProxy<mns::MemoryNameSystemInterfacePrx>(getMemoryNameSystemName())
                : nullptr;
     }
 
+
+    MemoryNameSystem&
+    MemoryNameSystemComponentPlugin::getMemoryNameSystemClient()
+    {
+        return memoryNameSystem;
+    }
+
+
+    const MemoryNameSystem&
+    MemoryNameSystemComponentPlugin::getMemoryNameSystemClient() const
+    {
+        return memoryNameSystem;
+    }
+
 }
 
 namespace armarx::armem::client
@@ -90,64 +107,20 @@ namespace armarx::armem::client
     {
     }
 
-    void MemoryNameSystemComponentPluginUser::memoryUpdated(
-        const std::vector<data::MemoryID>& updatedSnapshotIDs, const Ice::Current&)
-    {
-        memoryNameSystem.updated(updatedSnapshotIDs);
-    }
 
-    armem::data::WaitForMemoryResult MemoryNameSystemComponentPluginUser::useMemoryServer(const MemoryID& id)
+    ListeningMemoryNameSystemComponentPluginUser::ListeningMemoryNameSystemComponentPluginUser()
     {
-        armem::data::WaitForMemoryResult result;
-        try
-        {
-            // Add dependency.
-            result.proxy = memoryNameSystem.useServer(id);
-            result.success = true;
-        }
-        catch (const armem::error::CouldNotResolveMemoryServer& e)
-        {
-            result.success = false;
-            result.errorMessage = e.what();
-        }
-        return result;
     }
 
-    armem::data::WaitForMemoryResult MemoryNameSystemComponentPluginUser::useMemoryServer(const std::string& memoryName)
+    ListeningMemoryNameSystemComponentPluginUser::~ListeningMemoryNameSystemComponentPluginUser()
     {
-        return useMemoryServer(MemoryID().withMemoryName(memoryName));
     }
 
-    armem::data::WaitForMemoryResult MemoryNameSystemComponentPluginUser::waitForMemoryServer(const std::string& memoryName)
-    {
-        armem::data::WaitForMemoryResult result;
-        try
-        {
-            result.proxy = memoryNameSystem.waitForServer(MemoryID().withMemoryName(memoryName));
-            result.success = true;
-        }
-        catch (const armem::error::CouldNotResolveMemoryServer& e)
-        {
-            result.success = false;
-            result.errorMessage = e.what();
-        }
-        return result;
-    }
 
-    armem::data::ResolveMemoryNameResult MemoryNameSystemComponentPluginUser::resolveMemoryServer(const std::string& memoryName)
+    void ListeningMemoryNameSystemComponentPluginUser::memoryUpdated(
+        const std::vector<data::MemoryID>& updatedSnapshotIDs, const Ice::Current&)
     {
-        armem::data::ResolveMemoryNameResult result;
-        try
-        {
-            result.proxy = memoryNameSystem.resolveServer(MemoryID().withMemoryName(memoryName));
-            result.success = true;
-        }
-        catch (const armem::error::CouldNotResolveMemoryServer& e)
-        {
-            result.success = false;
-            result.errorMessage = e.what();
-        }
-        return result;
+        memoryNameSystem().updated(updatedSnapshotIDs);
     }
 
 }
diff --git a/source/RobotAPI/libraries/armem/client/MemoryNameSystemComponentPlugin.h b/source/RobotAPI/libraries/armem/client/MemoryNameSystemComponentPlugin.h
index a6a2434a6..b7276e844 100644
--- a/source/RobotAPI/libraries/armem/client/MemoryNameSystemComponentPlugin.h
+++ b/source/RobotAPI/libraries/armem/client/MemoryNameSystemComponentPlugin.h
@@ -8,19 +8,14 @@
 #include <RobotAPI/libraries/armem/client/MemoryNameSystem.h>
 
 
-namespace armarx::armem::mns
-{
-    class MemoryNameSystemComponentPluginUser;
-}
-
-
 namespace armarx::armem::client::plugins
 {
     /**
      * @brief A base plugin offering optional access and dependency
      * to the Memory Name System (MNS).
      */
-    class MemoryNameSystemComponentPlugin : public ComponentPlugin
+    class MemoryNameSystemComponentPlugin :
+        public ComponentPlugin
     {
     public:
 
@@ -32,6 +27,9 @@ namespace armarx::armem::client::plugins
         void preOnInitComponent() override;
         void preOnConnectComponent() override;
 
+
+    public:
+
         /**
          * @brief Indicate whether the Memory Name System (MNS) is enabled.
          */
@@ -45,11 +43,29 @@ namespace armarx::armem::client::plugins
          * @brief Get the MNS proxy.
          * @return The MNS proxy when MNS is enabled, nullptr when MNS is disabled.
          */
-        mns::MemoryNameSystemInterfacePrx getMemoryNameSystem();
+        mns::MemoryNameSystemInterfacePrx getMemoryNameSystemProxy();
+
+        /**
+         * @brief Get the MNS client.
+         * Only valid when enabled.
+         */
+        MemoryNameSystem& getMemoryNameSystemClient();
+        const MemoryNameSystem& getMemoryNameSystemClient() const;
+
+
+    protected:
+
+        MemoryNameSystem memoryNameSystem;
+
+        bool memoryNameSystemEnabled = true;
+        std::string memoryNameSystemName = "MemoryNameSystem";
+
+
+    protected:
 
-    public:
         static constexpr const char* PROPERTY_MNS_ENABLED_NAME = "mns.MemoryNameSystemEnabled";
         static constexpr const char* PROPERTY_MNS_NAME_NAME = "mns.MemoryNameSystemName";
+
     };
 
 }
@@ -61,8 +77,7 @@ namespace armarx::armem::client
 {
 
     class MemoryNameSystemComponentPluginUser :
-        virtual public ManagedIceObject,
-        virtual public MemoryListenerInterface
+        virtual public ManagedIceObject
     {
     protected:
 
@@ -70,32 +85,39 @@ namespace armarx::armem::client
         virtual ~MemoryNameSystemComponentPluginUser() override;
 
 
-        virtual void memoryUpdated(const std::vector<data::MemoryID>& updatedSnapshotIDs, const Ice::Current& current) override;
+    public:
 
+        MemoryNameSystem& memoryNameSystem()
+        {
+            return plugin->getMemoryNameSystemClient();
+        }
+        const MemoryNameSystem& memoryNameSystem() const
+        {
+            return plugin->getMemoryNameSystemClient();
+        }
 
-        [[deprecated("Use memoryNameSystem member instead of function inherited by MemoryNameSystemComponentPluginUser.")]]
-        armem::data::WaitForMemoryResult waitForMemoryServer(const std::string& memoryName);
-        [[deprecated("Use memoryNameSystem member instead of function inherited by MemoryNameSystemComponentPluginUser.")]]
-        armem::data::ResolveMemoryNameResult resolveMemoryServer(const std::string& memoryName);
 
-        [[deprecated("Use memoryNameSystem member instead of function inherited by MemoryNameSystemComponentPluginUser.")]]
-        armem::data::WaitForMemoryResult useMemoryServer(const MemoryID& id);
-        [[deprecated("Use memoryNameSystem member instead of function inherited by MemoryNameSystemComponentPluginUser.")]]
-        virtual armem::data::WaitForMemoryResult useMemoryServer(const std::string& memoryName);
+    private:
 
+        plugins::MemoryNameSystemComponentPlugin* plugin = nullptr;
 
-    public:
+    };
 
-        /// Only valid when enabled.
-        MemoryNameSystem memoryNameSystem;
 
-        bool memoryNameSystemEnabled = true;
-        std::string memoryNameSystemName = "MemoryNameSystem";
 
+    class ListeningMemoryNameSystemComponentPluginUser :
+        virtual public MemoryNameSystemComponentPluginUser,
+        virtual public MemoryListenerInterface
+    {
+    protected:
 
-    private:
+        ListeningMemoryNameSystemComponentPluginUser();
+        virtual ~ListeningMemoryNameSystemComponentPluginUser() override;
 
-        plugins::MemoryNameSystemComponentPlugin* plugin = nullptr;
+
+        // MemoryListenerInterface
+        virtual void memoryUpdated(const std::vector<data::MemoryID>& updatedSnapshotIDs,
+                                   const Ice::Current&) override;
 
     };
 }
diff --git a/source/RobotAPI/libraries/armem/server/ComponentPlugin.h b/source/RobotAPI/libraries/armem/server/ComponentPlugin.h
index f55bfc6c0..8a0c3fbc6 100644
--- a/source/RobotAPI/libraries/armem/server/ComponentPlugin.h
+++ b/source/RobotAPI/libraries/armem/server/ComponentPlugin.h
@@ -68,7 +68,7 @@ namespace armarx::armem::server
     class ComponentPluginUser :
         virtual public ManagedIceObject
         , virtual public MemoryInterface
-        , virtual public client::MemoryNameSystemComponentPluginUser
+        , virtual public client::ListeningMemoryNameSystemComponentPluginUser
     {
     public:
 
-- 
GitLab