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
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 {
40 workingMemory.name() = parent.getName();
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
61
62 void
64 {
66
67 // activate LTM
69
70
71 if (not workingMemory.id().memoryName.empty())
72 {
73 longtermMemory.setMemoryID(workingMemory.id());
74 }
75 else
76 {
77 longtermMemory.setMemoryID(MemoryID(parent.getDefaultName(), ""));
78 }
79
80 longtermMemory.configure();
81
82 initialized = true;
83 }
84
85 void
87 {
89
90 // register new server info to MNS
91 if (clientPlugin->isMemoryNameSystemEnabled() and clientPlugin->getMemoryNameSystemClient())
92 {
94 }
95 }
96
97 void
99 {
102 iceAdapter.setMemoryListener(memoryTopic);
103
104 this->iceAdapter.reloadFromLTMOnStartup();
105
106 connected = true;
107 }
108
109 void
111 {
112 //Storing rest of WM into LTM when component is shut down:
113 try
114 {
115 if (longtermMemory.p.storeOnStop)
116 {
117 longtermMemory.directlyStore(workingMemory);
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 "
125 << longtermMemory.p.storeOnStop;
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 {
136 if (longtermMemory.isRecording())
137 {
138 ARMARX_INFO << "Recording still in progress, stopping component anyways. "
139 "Saving statistics...";
140 longtermMemory.getAndSaveStatistics();
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
164 workingMemory.name() = memoryName;
165 }
166
167 mns::dto::RegisterServerResult
169 {
170 ARMARX_INFO << "Registering server for " << workingMemory.name();
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
Baseclass for all ArmarX ManagedIceObjects requiring properties.
Definition Component.h:94
void addPluginDependency(ManagedIceObjectPlugin *dependedOn)
PluginT * addPlugin(const std::string prefix="", ParamsT &&... params)
const std::string & prefix() const
void offeringTopic(const std::string &name)
Registers a topic for retrival after initialization.
TopicProxyType getTopic(const std::string &name)
Returns a proxy of the specified topic.
MemoryID withMemoryName(const std::string &name) const
Definition MemoryID.cpp:401
static std::string MakeMemoryTopicName(const MemoryID &memoryID)
Indicates that a query to the Memory Name System failed.
Definition mns.h:36
void setMemoryName(const std::string &memoryName)
Set the name of the wm and the ltm.
Definition Plugin.cpp:156
virtual void postOnInitComponent() override
Definition Plugin.cpp:63
std::string memoryTopicName
Available at onInit().
Definition Plugin.h:76
server::ltm::Memory longtermMemory
A manager class for the ltm. It internally holds a normal wm instance as a cache.
Definition Plugin.h:84
virtual void preOnInitComponent() override
Definition Plugin.cpp:54
virtual void preOnConnectComponent() override
Definition Plugin.cpp:86
mns::dto::RegisterServerResult registerServer(armarx::Component &parent)
Register the parent component in the MNS.
Definition Plugin.cpp:168
mns::dto::RemoveServerResult removeServer()
Remove the parent component from the MNS.
Definition Plugin.cpp:203
client::MemoryListenerInterfacePrx memoryTopic
Available after onConnect().
Definition Plugin.h:78
virtual void postOnConnectComponent() override
Definition Plugin.cpp:98
virtual void postCreatePropertyDefinitions(PropertyDefinitionsPtr &properties) override
Definition Plugin.cpp:32
MemoryToIceAdapter iceAdapter
Helps connecting memory to ice. Used to handle Ice callbacks.
Definition Plugin.h:70
virtual void preOnDisconnectComponent() override
Definition Plugin.cpp:110
server::wm::Memory workingMemory
The actual memory.
Definition Plugin.h:67
Plugin(ManagedIceObject &parent, std::string prefix)
Definition Plugin.cpp:22
#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...
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_DEBUG
The logging level for output that is only interesting while debugging.
Definition Logging.h:184
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
#define ARMARX_TRACE
Definition trace.h:77