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 to MNS
84  if (clientPlugin->isMemoryNameSystemEnabled() and clientPlugin->getMemoryNameSystemClient())
85  {
87  }
90 
91  this->iceAdapter.reloadFromLTM();
92 
93  connected = true;
94  }
95 
96  void
98  {
99  /*
100  ARMARX_INFO << "Preparing to save statistics for " << this->workingMemory.name();
101  try
102  {
103  auto first_stats = longtermMemory.getFilterStatistics();
104  std::map<std::string,
105  std::map<std::string, ltm::processor::SnapshotFilter::FilterStatistics>>
106  information;
107  std::map<std::string, armarx::core::time::DateTime> times;
108 
109  try
110  {
111  times["Started LTM1"] = longtermMemory.getStatistics().firstStarted;
112  times["Stopped LTM1"] = longtermMemory.getStatistics().firstStopped;
113  information["LTM"] = first_stats;
114  }
115  catch (...)
116  {
117  ARMARX_INFO << "Something went wrong after getting the statistics";
118  }
119  auto exportPath = longtermMemory.getMemoryBasePath();
120  auto exportName = longtermMemory.getExportName();
121  auto recording_started = longtermMemory.getStatistics().firstStarted;
122  auto recording_stopped = longtermMemory.getStatistics().firstStopped;
123  test::save_statistics(information,
124  times,
125  recording_started,
126  recording_stopped,
127  exportPath,
128  exportName,
129  longtermMemory.name());
130  }
131  catch (...)
132  {
133  ARMARX_INFO << "Something went wrong with the statistics saving process";
134  }
135 
136  statistics_saved = true;
137  */
138  try
139  {
141  {
142  ARMARX_INFO << "Recording still in progress, stopping component anyways. "
143  "Saving statistics...";
145  }
146  }
147  catch (...)
148  {
149  ARMARX_WARNING << "Statistics could not be saved for recording that was interrupted by "
150  "disconnecting the component";
151  }
152 
153  if (clientPlugin->isMemoryNameSystemEnabled() and clientPlugin->getMemoryNameSystemClient())
154  {
155  removeServer();
156  }
157  }
158 
159  void
160  Plugin::setMemoryName(const std::string& memoryName)
161  {
162  if (initialized)
163  {
164  ARMARX_WARNING << "Please set the memory name before initializing the component. "
165  "Otherwise the WM and LTM may have different names";
166  }
167 
169  }
170 
171  mns::dto::RegisterServerResult
173  {
174  ARMARX_INFO << "Registering server for " << workingMemory.name();
175  ARMARX_TRACE;
176 
178  mns::dto::MemoryServerInterfaces server;
179  server.reading = ReadingMemoryInterfacePrx::uncheckedCast(parent.getProxy());
180  server.writing = WritingMemoryInterfacePrx::uncheckedCast(parent.getProxy());
181  server.prediction = PredictingMemoryInterfacePrx::uncheckedCast(parent.getProxy());
182  server.actions = actions::ActionsInterfacePrx::uncheckedCast(parent.getProxy());
183 
184  mns::dto::RegisterServerResult result;
185  try
186  {
187  clientPlugin->getMemoryNameSystemClient().registerServer(id, server);
188  result.success = true;
189  ARMARX_DEBUG << "Registered memory server for " << id
190  << " in the Memory Name System (MNS).";
191  }
193  {
194  result.success = false;
195  result.errorMessage = e.what();
196  ARMARX_WARNING << e.what();
197  }
198 
199 
200  return result;
201  }
202 
203  mns::dto::RemoveServerResult
205  {
207 
208  mns::dto::RemoveServerResult result;
209  try
210  {
211  clientPlugin->getMemoryNameSystemClient().removeServer(id);
212  result.success = true;
213  ARMARX_DEBUG << "Removed memory server for " << id
214  << " from the Memory Name System (MNS).";
215  }
217  {
218  result.success = false;
219  result.errorMessage = e.what();
220  ARMARX_WARNING << e.what();
221  }
222  catch (const Ice::NotRegisteredException&)
223  {
224  // It's ok, the MNS is gone.
225  result.success = false;
226  result.errorMessage = "Memory Name System is gone.";
227  }
228  return result;
229  }
230 
231 } // namespace armarx::armem::server::plugins
Plugin.h
armarx::armem::server::ltm::detail::MemoryBase::isRecording
bool isRecording() const
Definition: MemoryBase.h:261
armarx::armem::client::MemoryNameSystem::removeServer
void removeServer(const MemoryID &memoryID)
Remove a memory server from the MNS.
Definition: MemoryNameSystem.cpp:414
armarx::armem::server::plugins::Plugin::postOnConnectComponent
virtual void postOnConnectComponent() override
Definition: Plugin.cpp:79
armarx::armem::server::plugins::Plugin::iceAdapter
MemoryToIceAdapter iceAdapter
Helps connecting memory to ice. Used to handle Ice callbacks.
Definition: Plugin.h:66
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:69
armarx::ManagedIceObjectPlugin::prefix
const std::string & prefix() const
Definition: ManagedIceObjectPlugin.cpp:63
armarx::armem::MemoryID::withMemoryName
MemoryID withMemoryName(const std::string &name) const
Definition: MemoryID.cpp:396
armarx::armem::client::plugins::Plugin::getMemoryNameSystemClient
MemoryNameSystem & getMemoryNameSystemClient()
Get the MNS client.
Definition: Plugin.cpp:92
armarx::ManagedIceObjectPlugin::parent
ManagedIceObject & parent()
Definition: ManagedIceObjectPlugin.cpp:69
armarx::armem::server::ltm::Memory::getAndSaveStatistics
void getAndSaveStatistics()
getAndSaveStatistics generates and saves statistics for a LTM recording
Definition: Memory.cpp:311
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::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:170
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:398
armarx::armem::base::MemoryBase::name
std::string & name()
Definition: MemoryBase.h:92
armarx::ManagedIceObjectPlugin::addPlugin
PluginT * addPlugin(const std::string prefix="", ParamsT &&...params)
Definition: ManagedIceObjectPlugin.h:104
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:388
ARMARX_TRACE
#define ARMARX_TRACE
Definition: trace.h:69
armarx::armem::server::MemoryToIceAdapter::setMemoryListener
void setMemoryListener(client::MemoryListenerInterfacePrx memoryListenerTopic)
Definition: MemoryToIceAdapter.cpp:34
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:172
armarx::ComponentPlugin
Definition: ComponentPlugin.h:38
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:80
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:177
error.h
armarx::armem::server::plugins::Plugin::Plugin
Plugin(ManagedIceObject &parent, std::string prefix)
Definition: Plugin.cpp:18
armarx::armem::server::plugins::Plugin::memoryTopicName
std::string memoryTopicName
Available at onInit().
Definition: Plugin.h:72
armarx::armem::base::detail::MemoryItem::id
MemoryID & id()
Definition: MemoryItem.h:27
Component.h
armarx::Component
Baseclass for all ArmarX ManagedIceObjects requiring properties.
Definition: Component.h:95
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::ManagedIceObject
The ManagedIceObject is the base class for all ArmarX objects.
Definition: ManagedIceObject.h:163
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::ManagedIceObject::getTopic
TopicProxyType getTopic(const std::string &name)
Returns a proxy of the specified topic.
Definition: ManagedIceObject.h:451
armarx::ManagedIceObject::offeringTopic
void offeringTopic(const std::string &name)
Registers a topic for retrival after initialization.
Definition: ManagedIceObject.cpp:290
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:40
armarx::armem::server::plugins::Plugin::setMemoryName
void setMemoryName(const std::string &memoryName)
Set the name of the wm and the ltm.
Definition: Plugin.cpp:160
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:97
armarx::armem::server::plugins::Plugin::memoryTopic
client::MemoryListenerInterfacePrx memoryTopic
Available after onConnect().
Definition: Plugin.h:74
armarx::ManagedIceObject::getName
std::string getName() const
Retrieve name of object.
Definition: ManagedIceObject.cpp:107
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:63
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
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:393
MemoryListener.h
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::armem::server::ltm::detail::MemoryBase::configure
void configure()
initialize config
Definition: MemoryBase.h:53
armarx::armem::server::plugins::Plugin::removeServer
mns::dto::RemoveServerResult removeServer()
Remove the parent component from the MNS.
Definition: Plugin.cpp:204
armarx::human::MemoryID
const armem::MemoryID MemoryID
Definition: memory_ids.cpp:29