MemoryNameSystem.h
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @package RobotAPI::libraries::armem
17 * @author Rainer Kartmann ( rainer dot kartmann at kit dot edu )
18 * @date 2020
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22
23#pragma once
24
25#include <map>
26#include <optional>
27
28#include <RobotAPI/interface/armem/mns/MemoryNameSystemInterface.h>
29#include <RobotAPI/interface/armem/server/MemoryInterface.h>
33
36
37namespace armarx
38{
39 class ManagedIceObject;
40}
41
43{
44 class Reader;
45 class Writer;
46 class Configurator;
47 class Loader;
48} // namespace armarx::armem::client
49
51{
52 class EntityInstance;
53}
54
56{
57
58 /**
59 * @brief The memory name system (MNS) client.
60 *
61 * This client class serves provides the MNS interface and is a local cache
62 * of the MNS registry. It can be used to resolve memory servers by their
63 * memory ID and to construct `client::Readers` and `client::Writers`.
64 *
65 * During server resolution, it first consults the locally cached registry.
66 * If the memory server is not known locally, the local registry is
67 * updated to that of the remote MNS before trying again.
68 *
69 * In addition, the MNS client can be used to send queries over multiple
70 * memory servers, as well as retrieving the data for arbitrary entity or
71 * entity snapshot IDs.
72 */
74 {
75 public:
77
78 /**
79 * @brief Construct an MNS client.
80 *
81 * @param mns The MNS proxy.
82 * @param component The owning component. When `using` a memory server,
83 * dependencies will be added to this component.
84 */
85 MemoryNameSystem(mns::MemoryNameSystemInterfacePrx mns,
86 ManagedIceObject* component = nullptr);
87
88 void initialize(mns::MemoryNameSystemInterfacePrx mns,
89 ManagedIceObject* component = nullptr);
90
91
92 mns::MemoryNameSystemInterfacePrx getMemoryNameSystem() const;
93 void getMemoryNameSystem(mns::MemoryNameSystemInterfacePrx mns);
94
95 void setComponent(ManagedIceObject* component);
96
97
98 // Name Resolution
99
100 /**
101 * @brief Update the internal registry to the data in the MNS.
102 *
103 * @throw `error::MemoryNameSystemQueryFailed` If the call to the MNS failed.
104 */
105 void update();
106
107 // Configurator construction
108
109 /**
110 * @brief Get a configurator to the given memory name.
111 *
112 * @param memoryID The memory ID.
113 * @return The reader.
114 *
115 * @throw `error::CouldNotResolveMemoryServer` If the memory name could not be resolved.
116 */
117 Configurator getConfigurator(const MemoryID& memoryID);
118
119 /// Use a memory server and get a configurator for it.
120 Configurator useConfigurator(const MemoryID& memoryID);
121 Configurator useConfigurator(const MemoryID& memoryID, ManagedIceObject& component);
122 Configurator useConfigurator(const std::string& memoryName);
123 Configurator useConfigurator(const std::string& memoryName, ManagedIceObject& component);
124
125 /**
126 * @brief Get Configurators for all registered servers.
127 *
128 * @param update If true, perform an update first.
129 * @return The configurators.
130 */
131 std::map<std::string, Configurator> getAllConfigurators(bool update = true);
132 /**
133 * @brief Get Configurators for all registered servers (without updating).
134 *
135 * @return The configurators.
136 */
137 std::map<std::string, Configurator> getAllConfigurators() const;
138
139 // Loader construction
140 Loader getLoader(const MemoryID& memoryID);
141
142 /// Use a memory server and get a configurator for it.
143 Loader useLoader(const MemoryID& memoryID);
144 Loader useLoader(const MemoryID& memoryID, ManagedIceObject& component);
145 Loader useLoader(const std::string& memoryName);
146 Loader useLoader(const std::string& memoryName, ManagedIceObject& component);
147
148 /**
149 * @brief Get Loaders for all registered servers.
150 *
151 * @param update If true, perform an update first.
152 * @return The loaders.
153 */
154 std::map<std::string, Loader> getAllLoaders(bool update = true);
155 /**
156 * @brief Get Loaders for all registered servers (without updating).
157 *
158 * @return The loaders.
159 */
160 std::map<std::string, Loader> getAllLoaders() const;
161
162 // Reader/Writer construction
163
164 /**
165 * @brief Get a reader to the given memory name.
166 *
167 * @param memoryID The memory ID.
168 * @return The reader.
169 *
170 * @throw `error::CouldNotResolveMemoryServer` If the memory name could not be resolved.
171 */
172 Reader getReader(const MemoryID& memoryID);
173
174 /// Use a memory server and get a reader for it.
175 Reader useReader(const MemoryID& memoryID);
176 Reader useReader(const MemoryID& memoryID, ManagedIceObject& component);
177 Reader useReader(const std::string& memoryName);
178 Reader useReader(const std::string& memoryName, ManagedIceObject& component);
179
180 /**
181 * @brief Get Readers for all registered servers.
182 *
183 * @param update If true, perform an update first.
184 * @return The readers.
185 */
186 std::map<std::string, Reader> getAllReaders(bool update = true);
187 /**
188 * @brief Get Readers for all registered servers (without updating).
189 *
190 * @return The readers.
191 */
192 std::map<std::string, Reader> getAllReaders() const;
193
194
195 /**
196 * @brief Get a writer to the given memory name.
197 *
198 * @param memoryID The memory ID.
199 * @return The writer.
200 *
201 * @throw `error::CouldNotResolveMemoryServer` If the memory name could not be resolved.
202 */
203 Writer getWriter(const MemoryID& memoryID);
204
205 /// Use a memory server and get a writer for it.
206 Writer useWriter(const MemoryID& memoryID);
207 Writer useWriter(const MemoryID& memoryID, ManagedIceObject& component);
208 Writer useWriter(const std::string& memoryName);
209 Writer useWriter(const std::string& memoryName, ManagedIceObject& component);
210
211 /**
212 * @brief Get Writers for all registered servers.
213 *
214 * @param update If true, perform an update first.
215 * @return The writers.
216 */
217 std::map<std::string, Writer> getAllWriters(bool update = true);
218 /**
219 * @brief Get Writers for all registered servers (without updating).
220 *
221 * @return The writers.
222 */
223 std::map<std::string, Writer> getAllWriters() const;
224
225
226 // ToDo: commit() and query()
227
228 /**
229 * @brief Resolve a memory ID to an EntityInstance.
230 *
231 * The ID can refer to an entity, an entity snapshot, or an entity
232 * instance. When not referring to an entity instance, the latest
233 * snapshot and first instance will be queried.
234 *
235 * @param id The entity, snapshot or instance ID.
236 * @return
237 */
238 std::optional<wm::EntityInstance> resolveEntityInstance(const MemoryID& id);
239
240 std::map<MemoryID, wm::EntityInstance>
241 resolveEntityInstances(const std::vector<MemoryID>& ids);
242
243 /**
244 * @brief Resolve the given memory server for the given memory ID.
245 *
246 * @param memoryID The memory ID.
247 * @return The memory server proxy.
248 *
249 * @throw `error::CouldNotResolveMemoryServer` If the memory name could not be resolved.
250 */
251 mns::dto::MemoryServerInterfaces resolveServer(const MemoryID& memoryID);
252
253 // Registration - only for memory servers
254
255 /**
256 * @brief Register a memory server in the MNS.
257 *
258 * @param memoryID The memoryID.
259 * @param server The memory server proxy.
260 *
261 * @throw `error::ServerRegistrationOrRemovalFailed` If the registration failed.
262 */
263 void registerServer(const MemoryID& memoryID, mns::dto::MemoryServerInterfaces server);
264
265 /**
266 * @brief Remove a memory server from the MNS.
267 *
268 * @param memoryID The memoryID.
269 *
270 * @throw `error::ServerRegistrationOrRemovalFailed` If the removal failed.
271 */
272 void removeServer(const MemoryID& memoryID);
273
274 // Operators
275
276 /// Indicate whether the proxy is set.
277 inline
278 operator bool() const
279 {
280 return bool(mns);
281 }
282
283
284 private:
285 /**
286 * @brief Wait for the given memory server.
287 *
288 * @param memoryID The memory ID.
289 * @return The memory server proxy.
290 *
291 * @throw `error::CouldNotResolveMemoryServer` If the memory name could not be resolved.
292 */
293 mns::dto::MemoryServerInterfaces waitForServer(const MemoryID& memoryID);
294
295 /**
296 * @brief Wait for the given memory server and add a dependency to the memory server.
297 *
298 * @param memoryID The memory ID.
299 * @param component The component that should depend on the memory server.
300 * @return The memory server proxy.
301 *
302 * @throw `error::CouldNotResolveMemoryServer` If the memory name could not be resolved.
303 */
304 mns::dto::MemoryServerInterfaces useServer(const MemoryID& memoryID);
305 mns::dto::MemoryServerInterfaces useServer(const MemoryID& memoryID,
307 mns::dto::MemoryServerInterfaces useServer(const std::string& memoryName);
308 mns::dto::MemoryServerInterfaces useServer(const std::string& memoryName,
310
311
312 private:
313 template <class ClientT>
314 using ClientFactory =
315 std::function<std::optional<ClientT>(const mns::dto::MemoryServerInterfaces& server)>;
316
317 template <class ClientT>
318 std::map<std::string, ClientT> _getAllClients(ClientFactory<ClientT>&& factory) const;
319
320
321 /// The MNS proxy.
322 mns::MemoryNameSystemInterfacePrx mns = nullptr;
323
324 /// The component to which dependencies will be added.
325 ManagedIceObject* component = nullptr;
326
327 /// The registered memory servers.
328 std::map<std::string, mns::dto::MemoryServerInterfaces> servers;
329 };
330
331
332} // namespace armarx::armem::client
The ManagedIceObject is the base class for all ArmarX objects.
Configure a memory server.
Load LTMs into WM.
Definition Loader.h:12
void initialize(mns::MemoryNameSystemInterfacePrx mns, ManagedIceObject *component=nullptr)
std::optional< wm::EntityInstance > resolveEntityInstance(const MemoryID &id)
Resolve a memory ID to an EntityInstance.
Loader getLoader(const MemoryID &memoryID)
mns::dto::MemoryServerInterfaces resolveServer(const MemoryID &memoryID)
Resolve the given memory server for the given memory ID.
Configurator useConfigurator(const MemoryID &memoryID)
Use a memory server and get a configurator for it.
std::map< std::string, Reader > getAllReaders() const
Get Readers for all registered servers (without updating).
Reader getReader(const MemoryID &memoryID)
Get a reader to the given memory name.
std::map< std::string, Writer > getAllWriters() const
Get Writers for all registered servers (without updating).
void removeServer(const MemoryID &memoryID)
Remove a memory server from the MNS.
std::map< std::string, Loader > getAllLoaders() const
Get Loaders for all registered servers (without updating).
Loader useLoader(const MemoryID &memoryID)
Use a memory server and get a configurator for it.
Writer useWriter(const MemoryID &memoryID)
Use a memory server and get a writer for it.
std::map< std::string, Configurator > getAllConfigurators() const
Get Configurators for all registered servers (without updating).
Reader useReader(const MemoryID &memoryID)
Use a memory server and get a reader for it.
Configurator getConfigurator(const MemoryID &memoryID)
Get a configurator to the given memory name.
void registerServer(const MemoryID &memoryID, mns::dto::MemoryServerInterfaces server)
Register a memory server in the MNS.
mns::MemoryNameSystemInterfacePrx getMemoryNameSystem() const
void update()
Update the internal registry to the data in the MNS.
void setComponent(ManagedIceObject *component)
std::map< MemoryID, wm::EntityInstance > resolveEntityInstances(const std::vector< MemoryID > &ids)
Writer getWriter(const MemoryID &memoryID)
Get a writer to the given memory name.
Reads data from a memory server.
Definition Reader.h:25
Helps a memory client sending data to a memory.
Definition Writer.h:23
Handles update signals from the memory system and distributes it to its subsribers.
Client-side working entity instance.
This file is part of ArmarX.
This file offers overloads of toIce() and fromIce() functions for STL container types.