Plugin.cpp
Go to the documentation of this file.
1 #include "Plugin.h"
2 
3 #include <chrono>
4 
9 
16 
18 {
19 
20  Plugin::~Plugin() = default;
21 
22  Plugin::Plugin(ManagedIceObject& parent, std::string prefix) :
23  armarx::ComponentPlugin(parent, prefix)
24  {
26  addPlugin(clientPlugin);
27  ARMARX_CHECK_NOT_NULL(clientPlugin);
28  addPluginDependency(clientPlugin);
29  }
30 
31  void
33  {
34  const std::string prefix = "mem.";
35 
36  // set Memory name if not already set
37  if (workingMemory.name().empty())
38  {
39  Component& parent = this->parent<Component>();
41  }
42 
43  // also add scenario param to overwrite the chosen name
44  if (not properties->hasDefinition(prefix + "MemoryName"))
45  {
46  properties->optional(
47  workingMemory.name(), prefix + "MemoryName", "Name of this memory server.");
48  }
49 
50  longtermMemory.createPropertyDefinitions(properties, prefix + "ltm.");
51  }
52 
53  void
55  {
60  }
61 
62  void
64  {
65  Component& parent = this->parent<Component>();
66 
67  // activate LTM
69 
70 
71  if (not workingMemory.id().memoryName.empty())
72  {
74  }
75  else
76  {
78  }
79 
81 
82  initialized = true;
83  }
84 
85  void
87  {
88  Component& parent = this->parent<Component>();
89 
90  // register new server info to MNS
91  if (clientPlugin->isMemoryNameSystemEnabled() and clientPlugin->getMemoryNameSystemClient())
92  {
94  }
95  }
96 
97  void
99  {
100  Component& parent = this->parent<Component>();
103 
105 
106  connected = true;
107  }
108 
109  void
111  {
112  //Storing rest of WM into LTM when component is shut down:
113  try
114  {
116  {
119  << "Stored working memory contents into long-term memory on component stop";
120  }
121  else
122  {
124  << "Not storing WM into LTM on stop, as longtermMemory.p.storeOnstop is "
126  }
127  }
128  catch (...)
129  {
131  << "Could not store working memory into the long-term memory on component stop.";
132  }
133  //Storing statistics aout LTM recording:
134  try
135  {
137  {
138  ARMARX_INFO << "Recording still in progress, stopping component anyways. "
139  "Saving statistics...";
141  }
142  }
143  catch (...)
144  {
145  ARMARX_WARNING << "Statistics could not be saved for recording that was interrupted by "
146  "disconnecting the component";
147  }
148  //removing server:
149  if (clientPlugin->isMemoryNameSystemEnabled() and clientPlugin->getMemoryNameSystemClient())
150  {
151  removeServer();
152  }
153  }
154 
155  void
156  Plugin::setMemoryName(const std::string& memoryName)
157  {
158  if (initialized)
159  {
160  ARMARX_WARNING << "Please set the memory name before initializing the component. "
161  "Otherwise the WM and LTM may have different names";
162  }
163 
165  }
166 
167  mns::dto::RegisterServerResult
169  {
170  ARMARX_INFO << "Registering server for " << workingMemory.name();
171  ARMARX_TRACE;
172 
174  mns::dto::MemoryServerInterfaces server;
175  server.reading = ReadingMemoryInterfacePrx::uncheckedCast(parent.getProxy());
176  server.writing = WritingMemoryInterfacePrx::uncheckedCast(parent.getProxy());
177  server.prediction = PredictingMemoryInterfacePrx::uncheckedCast(parent.getProxy());
178  server.actions = actions::ActionsInterfacePrx::uncheckedCast(parent.getProxy());
179  server.configuration = ConfiguringMemoryInterfacePrx::uncheckedCast(parent.getProxy());
180  server.loading = LoadingMemoryInterfacePrx::uncheckedCast(parent.getProxy());
181  server.readingLtm = ReadingLongTermMemoryInterfacePrx::uncheckedCast(parent.getProxy());
182 
183  mns::dto::RegisterServerResult result;
184  try
185  {
186  clientPlugin->getMemoryNameSystemClient().registerServer(id, server);
187  result.success = true;
188  ARMARX_DEBUG << "Registered memory server for " << id
189  << " in the Memory Name System (MNS).";
190  }
192  {
193  result.success = false;
194  result.errorMessage = e.what();
195  ARMARX_WARNING << e.what();
196  }
197 
198 
199  return result;
200  }
201 
202  mns::dto::RemoveServerResult
204  {
206 
207  mns::dto::RemoveServerResult result;
208  try
209  {
210  clientPlugin->getMemoryNameSystemClient().removeServer(id);
211  result.success = true;
212  ARMARX_DEBUG << "Removed memory server for " << id
213  << " from the Memory Name System (MNS).";
214  }
216  {
217  result.success = false;
218  result.errorMessage = e.what();
219  ARMARX_WARNING << e.what();
220  }
221  catch (const Ice::NotRegisteredException&)
222  {
223  // It's ok, the MNS is gone.
224  result.success = false;
225  result.errorMessage = "Memory Name System is gone.";
226  }
227  return result;
228  }
229 
230 } // namespace armarx::armem::server::plugins
Plugin.h
armarx::armem::server::ltm::detail::MemoryBase::isRecording
bool isRecording() const
Definition: MemoryBase.h:318
armarx::armem::client::MemoryNameSystem::removeServer
void removeServer(const MemoryID &memoryID)
Remove a memory server from the MNS.
Definition: MemoryNameSystem.cpp:570
armarx::armem::server::plugins::Plugin::postOnConnectComponent
virtual void postOnConnectComponent() override
Definition: Plugin.cpp:98
RestPersistence.h
armarx::armem::server::plugins::Plugin::iceAdapter
MemoryToIceAdapter iceAdapter
Helps connecting memory to ice. Used to handle Ice callbacks.
Definition: Plugin.h:70
armarx::ManagedIceObjectPlugin::addPluginDependency
void addPluginDependency(ManagedIceObjectPlugin *dependedOn)
Definition: ManagedIceObjectPlugin.cpp:42
armarx::armem::client::plugins::Plugin::isMemoryNameSystemEnabled
bool isMemoryNameSystemEnabled()
Indicate whether the Memory Name System (MNS) is enabled.
Definition: Plugin.cpp:68
armarx::ManagedIceObjectPlugin::prefix
const std::string & prefix() const
Definition: ManagedIceObjectPlugin.cpp:66
armarx::armem::MemoryID::withMemoryName
MemoryID withMemoryName(const std::string &name) const
Definition: MemoryID.cpp:401
armarx::armem::client::plugins::Plugin::getMemoryNameSystemClient
MemoryNameSystem & getMemoryNameSystemClient()
Get the MNS client.
Definition: Plugin.cpp:89
MemoryPersistenceStrategy.h
armarx::ManagedIceObjectPlugin::parent
ManagedIceObject & parent()
Definition: ManagedIceObjectPlugin.cpp:72
armarx::armem::server::ltm::Memory::getAndSaveStatistics
void getAndSaveStatistics()
getAndSaveStatistics generates and saves statistics for a LTM recording
Definition: Memory.cpp:438
armarx::armem::server::plugins::Plugin::~Plugin
virtual ~Plugin() override
ARMARX_CHECK_NOT_NULL
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
Definition: ExpressionException.h:206
armarx::ManagedIceObjectPlugin::addPlugin
PluginT * addPlugin(const std::string prefix="", ParamsT &&... params)
Definition: ManagedIceObjectPlugin.h:131
armarx::armem::server::ltm::Memory::createPropertyDefinitions
void createPropertyDefinitions(PropertyDefinitionsPtr &defs, const std::string &prefix) override
default parameters. Implementation should use the configuration to configure
Definition: Memory.cpp:244
trace.h
armarx::armem::client::MemoryNameSystem::registerServer
void registerServer(const MemoryID &memoryID, mns::dto::MemoryServerInterfaces server)
Register a memory server in the MNS.
Definition: MemoryNameSystem.cpp:547
armarx::armem::base::MemoryBase::name
std::string & name()
Definition: MemoryBase.h:92
armarx::armem::server::ltm::detail::MemoryBase::p
struct armarx::armem::server::ltm::detail::MemoryBase::Properties p
RedundantPersistenceStrategy.h
armarx::armem::client::util::MemoryListener::MakeMemoryTopicName
static std::string MakeMemoryTopicName(const MemoryID &memoryID)
Definition: MemoryListener.cpp:17
armarx::armem::server::MemoryToIceAdapter::reloadFromLTMOnStartup
armem::CommitResult reloadFromLTMOnStartup()
Triggers a reload (.
Definition: MemoryToIceAdapter.cpp:490
ARMARX_TRACE
#define ARMARX_TRACE
Definition: trace.h:77
armarx::armem::server::MemoryToIceAdapter::setMemoryListener
void setMemoryListener(client::MemoryListenerInterfacePrx memoryListenerTopic)
Definition: MemoryToIceAdapter.cpp:38
armarx::armem::server::ltm::detail::MemoryBase::Properties::storeOnStop
bool storeOnStop
Definition: MemoryBase.h:419
armarx::armem::MemoryID
A memory ID.
Definition: MemoryID.h:47
armarx::armem::server::ltm::detail::MemoryItem::setMemoryID
void setMemoryID(const MemoryID &)
Definition: MemoryItem.cpp:30
armarx::armem::server::plugins::Plugin::registerServer
mns::dto::RegisterServerResult registerServer(armarx::Component &parent)
Register the parent component in the MNS.
Definition: Plugin.cpp:168
armarx::ComponentPlugin
Definition: ComponentPlugin.h:37
armarx::armem::server::plugins::Plugin::longtermMemory
server::ltm::Memory longtermMemory
A manager class for the ltm. It internally holds a normal wm instance as a cache.
Definition: Plugin.h:84
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:184
error.h
armarx::armem::server::plugins::Plugin::Plugin
Plugin(ManagedIceObject &parent, std::string prefix)
Definition: Plugin.cpp:22
armarx::armem::server::ltm::detail::mixin::BufferedMemoryMixin::directlyStore
void directlyStore(const armem::wm::Memory &memory, bool simulatedVersion=false)
Definition: BufferedMemoryMixin.h:31
armarx::armem::server::plugins::Plugin::memoryTopicName
std::string memoryTopicName
Available at onInit().
Definition: Plugin.h:76
armarx::armem::base::detail::MemoryItem::id
MemoryID & id()
Definition: MemoryItem.h:25
Component.h
armarx::Component
Baseclass for all ArmarX ManagedIceObjects requiring properties.
Definition: Component.h:91
armarx::armem::MemoryID::memoryName
std::string memoryName
Definition: MemoryID.h:50
armarx::ManagedIceObject::getDefaultName
virtual std::string getDefaultName() const =0
Retrieve default name of component.
armarx::armem::server::plugins::Plugin::postCreatePropertyDefinitions
virtual void postCreatePropertyDefinitions(PropertyDefinitionsPtr &properties) override
Definition: Plugin.cpp:32
ExpressionException.h
armarx::armem::server::plugins::Plugin::preOnConnectComponent
virtual void preOnConnectComponent() override
Definition: Plugin.cpp:86
armarx::ManagedIceObject
The ManagedIceObject is the base class for all ArmarX objects.
Definition: ManagedIceObject.h:162
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
armarx::ManagedIceObject::getTopic
TopicProxyType getTopic(const std::string &name)
Returns a proxy of the specified topic.
Definition: ManagedIceObject.h:480
armarx::ManagedIceObject::offeringTopic
void offeringTopic(const std::string &name)
Registers a topic for retrival after initialization.
Definition: ManagedIceObject.cpp:300
armarx::armem::laser_scans::constants::memoryName
const std::string memoryName
Definition: constants.h:28
armarx::armem::error::ServerRegistrationOrRemovalFailed
Indicates that a query to the Memory Name System failed.
Definition: mns.h:35
armarx::armem::server::plugins::Plugin::setMemoryName
void setMemoryName(const std::string &memoryName)
Set the name of the wm and the ltm.
Definition: Plugin.cpp:156
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::armem::server::plugins::Plugin::postOnInitComponent
virtual void postOnInitComponent() override
Definition: Plugin.cpp:63
armarx::armem::server::plugins::Plugin::preOnDisconnectComponent
virtual void preOnDisconnectComponent() override
Definition: Plugin.cpp:110
armarx::armem::server::plugins::Plugin::memoryTopic
client::MemoryListenerInterfacePrx memoryTopic
Available after onConnect().
Definition: Plugin.h:78
armarx::ManagedIceObject::getName
std::string getName() const
Retrieve name of object.
Definition: ManagedIceObject.cpp:108
armarx::armem::server::plugins::Plugin::preOnInitComponent
virtual void preOnInitComponent() override
Definition: Plugin.cpp:54
Logging.h
armarx::armem::server::plugins::Plugin::workingMemory
server::wm::Memory workingMemory
The actual memory.
Definition: Plugin.h:67
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:193
armarx::armem::server::plugins
Definition: Plugin.cpp:17
armarx::ManagedIceObject::getProxy
Ice::ObjectPrx getProxy(long timeoutMs=0, bool waitForScheduler=true) const
Returns the proxy of this object (optionally it waits for the proxy)
Definition: ManagedIceObject.cpp:407
MemoryListener.h
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::armem::server::ltm::detail::MemoryBase::configure
void configure()
initialize config
Definition: MemoryBase.h:97
armarx::armem::server::plugins::Plugin::removeServer
mns::dto::RemoveServerResult removeServer()
Remove the parent component from the MNS.
Definition: Plugin.cpp:203
armarx::human::MemoryID
const armem::MemoryID MemoryID
Definition: memory_ids.cpp:28