Plugin.cpp
Go to the documentation of this file.
1 #include "Plugin.h"
2 
3 #include <chrono>
4 
8 
12 
14 {
15 
16  Plugin::~Plugin() = default;
17 
18  Plugin::Plugin(ManagedIceObject& parent, std::string prefix) :
19  armarx::ComponentPlugin(parent, prefix)
20  {
22  addPlugin(clientPlugin);
23  ARMARX_CHECK_NOT_NULL(clientPlugin);
24  addPluginDependency(clientPlugin);
25  }
26 
27  void
29  {
30  const std::string prefix = "mem.";
31 
32  // set Memory name if not already set
33  if (workingMemory.name().empty())
34  {
35  Component& parent = this->parent<Component>();
37  }
38 
39  // also add scenario param to overwrite the chosen name
40  if (not properties->hasDefinition(prefix + "MemoryName"))
41  {
42  properties->optional(
43  workingMemory.name(), prefix + "MemoryName", "Name of this memory server.");
44  }
45 
46  longtermMemory.createPropertyDefinitions(properties, prefix + "ltm.");
47  }
48 
49  void
51  {
56  }
57 
58  void
60  {
61  Component& parent = this->parent<Component>();
62 
63  // activate LTM
65  if (not workingMemory.id().memoryName.empty())
66  {
68  }
69  else
70  {
72  }
74 
75  initialized = true;
76  }
77 
78  void
80  {
81  Component& parent = this->parent<Component>();
82 
83  // register new server info to MNS
84  if (clientPlugin->isMemoryNameSystemEnabled() and clientPlugin->getMemoryNameSystemClient())
85  {
87  }
88  }
89 
90  void
92  {
93  Component& parent = this->parent<Component>();
96 
97  this->iceAdapter.reloadFromLTM();
98 
99  connected = true;
100  }
101 
102  void
104  {
105  //Storing rest of WM into LTM when component is shut down:
106  try
107  {
109  {
112  << "Stored working memory contents into long-term memory on component stop";
113  }
114  else
115  {
117  << "Not storing WM into LTM on stop, as longtermMemory.p.storeOnstop is "
119  }
120  }
121  catch (...)
122  {
124  << "Could not store working memory into the long-term memory on component stop.";
125  }
126  //Storing statistics aout LTM recording:
127  try
128  {
130  {
131  ARMARX_INFO << "Recording still in progress, stopping component anyways. "
132  "Saving statistics...";
134  }
135  }
136  catch (...)
137  {
138  ARMARX_WARNING << "Statistics could not be saved for recording that was interrupted by "
139  "disconnecting the component";
140  }
141  //removing server:
142  if (clientPlugin->isMemoryNameSystemEnabled() and clientPlugin->getMemoryNameSystemClient())
143  {
144  removeServer();
145  }
146  }
147 
148  void
149  Plugin::setMemoryName(const std::string& memoryName)
150  {
151  if (initialized)
152  {
153  ARMARX_WARNING << "Please set the memory name before initializing the component. "
154  "Otherwise the WM and LTM may have different names";
155  }
156 
158  }
159 
160  mns::dto::RegisterServerResult
162  {
163  ARMARX_INFO << "Registering server for " << workingMemory.name();
164  ARMARX_TRACE;
165 
167  mns::dto::MemoryServerInterfaces server;
168  server.reading = ReadingMemoryInterfacePrx::uncheckedCast(parent.getProxy());
169  server.writing = WritingMemoryInterfacePrx::uncheckedCast(parent.getProxy());
170  server.prediction = PredictingMemoryInterfacePrx::uncheckedCast(parent.getProxy());
171  server.actions = actions::ActionsInterfacePrx::uncheckedCast(parent.getProxy());
172 
173  mns::dto::RegisterServerResult result;
174  try
175  {
176  clientPlugin->getMemoryNameSystemClient().registerServer(id, server);
177  result.success = true;
178  ARMARX_DEBUG << "Registered memory server for " << id
179  << " in the Memory Name System (MNS).";
180  }
182  {
183  result.success = false;
184  result.errorMessage = e.what();
185  ARMARX_WARNING << e.what();
186  }
187 
188 
189  return result;
190  }
191 
192  mns::dto::RemoveServerResult
194  {
196 
197  mns::dto::RemoveServerResult result;
198  try
199  {
200  clientPlugin->getMemoryNameSystemClient().removeServer(id);
201  result.success = true;
202  ARMARX_DEBUG << "Removed memory server for " << id
203  << " from the Memory Name System (MNS).";
204  }
206  {
207  result.success = false;
208  result.errorMessage = e.what();
209  ARMARX_WARNING << e.what();
210  }
211  catch (const Ice::NotRegisteredException&)
212  {
213  // It's ok, the MNS is gone.
214  result.success = false;
215  result.errorMessage = "Memory Name System is gone.";
216  }
217  return result;
218  }
219 
220 } // namespace armarx::armem::server::plugins
Plugin.h
armarx::armem::server::ltm::detail::MemoryBase::isRecording
bool isRecording() const
Definition: MemoryBase.h:307
armarx::armem::client::MemoryNameSystem::removeServer
void removeServer(const MemoryID &memoryID)
Remove a memory server from the MNS.
Definition: MemoryNameSystem.cpp:436
armarx::armem::server::plugins::Plugin::postOnConnectComponent
virtual void postOnConnectComponent() override
Definition: Plugin.cpp:91
armarx::armem::server::plugins::Plugin::iceAdapter
MemoryToIceAdapter iceAdapter
Helps connecting memory to ice. Used to handle Ice callbacks.
Definition: Plugin.h:67
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
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:316
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:167
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:417
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
armarx::armem::client::util::MemoryListener::MakeMemoryTopicName
static std::string MakeMemoryTopicName(const MemoryID &memoryID)
Definition: MemoryListener.cpp:17
armarx::armem::server::MemoryToIceAdapter::reloadFromLTM
armem::CommitResult reloadFromLTM()
Definition: MemoryToIceAdapter.cpp:411
ARMARX_TRACE
#define ARMARX_TRACE
Definition: trace.h:77
armarx::armem::server::MemoryToIceAdapter::setMemoryListener
void setMemoryListener(client::MemoryListenerInterfacePrx memoryListenerTopic)
Definition: MemoryToIceAdapter.cpp:34
armarx::armem::server::ltm::detail::MemoryBase::Properties::storeOnStop
bool storeOnStop
Definition: MemoryBase.h:408
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:161
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:81
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:18
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:73
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:28
ExpressionException.h
armarx::armem::server::plugins::Plugin::preOnConnectComponent
virtual void preOnConnectComponent() override
Definition: Plugin.cpp:79
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:149
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::armem::server::plugins::Plugin::postOnInitComponent
virtual void postOnInitComponent() override
Definition: Plugin.cpp:59
armarx::armem::server::plugins::Plugin::preOnDisconnectComponent
virtual void preOnDisconnectComponent() override
Definition: Plugin.cpp:103
armarx::armem::server::plugins::Plugin::memoryTopic
client::MemoryListenerInterfacePrx memoryTopic
Available after onConnect().
Definition: Plugin.h:75
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:50
armarx::armem::server::plugins::Plugin::workingMemory
server::wm::Memory workingMemory
The actual memory.
Definition: Plugin.h:64
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:193
armarx::armem::server::plugins
Definition: Plugin.cpp:13
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:193
armarx::human::MemoryID
const armem::MemoryID MemoryID
Definition: memory_ids.cpp:28