Skip to content
Snippets Groups Projects
Commit ca228b77 authored by Rainer Kartmann's avatar Rainer Kartmann
Browse files

Hookup to MNS in MemoryComponentPlugin

parent d07acba5
No related branches found
No related tags found
1 merge request!91Memory Name System (MNS)
......@@ -7,6 +7,134 @@
#include "IceMemory.h"
namespace armarx::armem::plugins
{
void MemoryComponentPlugin::postCreatePropertyDefinitions(armarx::PropertyDefinitionsPtr& properties)
{
if (!properties->hasDefinition(makePropertyName(PROPERTY_MNS_NAME_NAME)))
{
properties->defineOptionalProperty<std::string>(
makePropertyName(PROPERTY_MNS_NAME_NAME),
PROPERTY_MNS_NAME_DEFAULT,
"Name of the Memory Name System (MNS) component.");
}
if (!properties->hasDefinition(makePropertyName(PROPERTY_MNS_ENABLED_NAME)))
{
properties->defineOptionalProperty<bool>(
makePropertyName(PROPERTY_MNS_ENABLED_NAME),
PROPERTY_MNS_ENABLED_DEFAULT,
"Whether to use (and depend on) the Memory Name System (MNS)."
"\nSet to false to use this memory as a stand-alone.");
}
}
bool MemoryComponentPlugin::isMemoryNameSystemEnabled()
{
return parentDerives<Component>() ?
parent<Component>().getProperty<bool>(makePropertyName(PROPERTY_MNS_ENABLED_NAME)) :
PROPERTY_MNS_ENABLED_DEFAULT;
}
std::string MemoryComponentPlugin::getMemoryNameSystemName()
{
return parentDerives<Component>() ?
parent<Component>().getProperty<std::string>(makePropertyName(PROPERTY_MNS_NAME_NAME)) :
std::string{PROPERTY_MNS_NAME_DEFAULT};
}
MemoryNameSystemInterfacePrx MemoryComponentPlugin::getMemoryNameSystem()
{
return isMemoryNameSystemEnabled() && parentDerives<Component>()
? parent<Component>().getProxy<MemoryNameSystemInterfacePrx>(getMemoryNameSystemName())
: nullptr;
}
void MemoryComponentPlugin::preOnInitComponent()
{
if (isMemoryNameSystemEnabled())
{
parent().usingProxy(getMemoryNameSystemName());
}
}
void MemoryComponentPlugin::preOnConnectComponent()
{
if (isMemoryNameSystemEnabled())
{
parent<MemoryComponentPluginUser>().memoryNameSystem = getMemoryNameSystem();
}
}
void MemoryComponentPlugin::postOnConnectComponent()
{
MemoryComponentPluginUser& parent = this->parent<MemoryComponentPluginUser>();
if (isMemoryNameSystemEnabled() && parent.memoryNameSystem)
{
registerMemory(parent);
}
}
void MemoryComponentPlugin::preOnDisconnectComponent()
{
MemoryComponentPluginUser& parent = this->parent<MemoryComponentPluginUser>();
if (isMemoryNameSystemEnabled() && parent.memoryNameSystem)
{
removeMemory(parent);
}
}
data::RegisterMemoryResult MemoryComponentPlugin::registerMemory(MemoryComponentPluginUser& parent)
{
data::RegisterMemoryInput input;
input.name = parent.memory.name;
input.proxy = MemoryInterfacePrx::checkedCast(parent.getProxy());
ARMARX_CHECK_NOT_NULL(input.proxy);
data::RegisterMemoryResult result = parent.memoryNameSystem->registerMemory(input);
if (result.success)
{
ARMARX_DEBUG << "Registered memory '" << input.name << "' in the Memory Name System (MNS).";
}
else
{
ARMARX_WARNING << "Failed to register this memory in the Memory Name System (MNS):"
<< "\n" << result.errorMessage;
}
return result;
}
data::RemoveMemoryResult MemoryComponentPlugin::removeMemory(MemoryComponentPluginUser& parent)
{
data::RemoveMemoryResult result;
try
{
data::RemoveMemoryInput input;
input.name = parent.memory.name;
result = parent.memoryNameSystem->removeMemory(input);
if (result.success)
{
ARMARX_DEBUG << "Removed memory '" << input.name << "' from the Memory Name System (MNS).";
}
else
{
ARMARX_WARNING << "Failed to remove this memory in the Memory Name System (MNS):"
<< "\n" << result.errorMessage;
}
}
catch (const Ice::NotRegisteredException&)
{
// It's ok, the MNS is gone.
result.success = false;
result.errorMessage = "Memory Name System is gone.";
}
return result;
}
}
namespace armarx::armem
{
......
......@@ -5,11 +5,18 @@
#include <ArmarXCore/core/Component.h>
#include <RobotAPI/interface/armem/MemoryInterface.h>
#include <RobotAPI/interface/armem/MemoryNameSystemInterface.h>
#include "../memory/Memory.h"
#include "IceMemory.h"
namespace armarx::armem
{
class MemoryComponentPluginUser;
}
namespace armarx::armem::plugins
{
......@@ -18,6 +25,52 @@ namespace armarx::armem::plugins
public:
using ComponentPlugin::ComponentPlugin;
void postCreatePropertyDefinitions(PropertyDefinitionsPtr& properties) override;
void preOnInitComponent() override;
void preOnConnectComponent() override;
void postOnConnectComponent() override;
void preOnDisconnectComponent() override;
/**
* @brief Indicate whether the Memory Name System (MNS) is enabled.
*/
bool isMemoryNameSystemEnabled();
/**
* @brief Get the name of the MNS component.
*/
std::string getMemoryNameSystemName();
/**
* @brief Get the MNS proxy.
* @return The MNS proxy when MNS is enabled, nullptr when MNS is disabled.
*/
MemoryNameSystemInterfacePrx getMemoryNameSystem();
/**
* @brief Register the parent component in the MNS.
* Called before onConnect() if MNS is enabled.
*/
data::RegisterMemoryResult registerMemory(MemoryComponentPluginUser& parent);
/**
* @brief Remove the parent component from the MNS.
* Called before onDisconnect() if MNS is enabled.
*/
data::RemoveMemoryResult removeMemory(MemoryComponentPluginUser& parent);
private:
static constexpr const char* PROPERTY_MNS_ENABLED_NAME = "mns.MemoryNameSystemEnabled";
static constexpr const bool PROPERTY_MNS_ENABLED_DEFAULT = true;
static constexpr const char* PROPERTY_MNS_NAME_NAME = "mns.MemoryNameSystemName";
static constexpr const char* PROPERTY_MNS_NAME_DEFAULT = "ArMemMemoryNameSystem";
};
}
......@@ -60,6 +113,10 @@ namespace armarx::armem
IceMemory iceMemory { &memory };
/// Only set when enabled.
MemoryNameSystemInterfacePrx memoryNameSystem = nullptr;
private:
armem::plugins::MemoryComponentPlugin* plugin = nullptr;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment