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 
37 namespace armarx
38 {
39  class ManagedIceObject;
40 }
41 
42 namespace armarx::armem::client
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 
55 namespace armarx::armem::client
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  */
118 
119  /// Use a memory server and get a configurator for it.
122  Configurator useConfigurator(const std::string& memoryName);
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
141 
142  /// Use a memory server and get a configurator for it.
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  */
173 
174  /// Use a memory server and get a reader for it.
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  */
204 
205  /// Use a memory server and get a writer for it.
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,
306  ManagedIceObject& component);
307  mns::dto::MemoryServerInterfaces useServer(const std::string& memoryName);
308  mns::dto::MemoryServerInterfaces useServer(const std::string& memoryName,
309  ManagedIceObject& component);
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
armarx::armem::client::MemoryNameSystem::resolveServer
mns::dto::MemoryServerInterfaces resolveServer(const MemoryID &memoryID)
Resolve the given memory server for the given memory ID.
Definition: MemoryNameSystem.cpp:116
armarx::armem::client::MemoryNameSystem::removeServer
void removeServer(const MemoryID &memoryID)
Remove a memory server from the MNS.
Definition: MemoryNameSystem.cpp:570
armarx::armem::client::Reader
Reads data from a memory server.
Definition: Reader.h:24
armarx::armem::wm::EntityInstance
Client-side working entity instance.
Definition: memory_definitions.h:32
armarx::ProxyType::component
@ component
armarx::armem::client::MemoryNameSystem::getAllReaders
std::map< std::string, Reader > getAllReaders() const
Get Readers for all registered servers (without updating).
Definition: MemoryNameSystem.cpp:407
armarx::armem::client::MemoryNameSystem::getAllLoaders
std::map< std::string, Loader > getAllLoaders() const
Get Loaders for all registered servers (without updating).
Definition: MemoryNameSystem.cpp:373
Loader.h
MemoryID.h
armarx::armem::client::MemoryNameSystem::useWriter
Writer useWriter(const MemoryID &memoryID)
Use a memory server and get a writer for it.
Definition: MemoryNameSystem.cpp:419
armarx::armem::client::MemoryNameSystem::getAllWriters
std::map< std::string, Writer > getAllWriters() const
Get Writers for all registered servers (without updating).
Definition: MemoryNameSystem.cpp:453
armarx::armem::client
This file is part of ArmarX.
Definition: Configurator.cpp:5
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::client::MemoryNameSystem::resolveEntityInstance
std::optional< wm::EntityInstance > resolveEntityInstance(const MemoryID &id)
Resolve a memory ID to an EntityInstance.
Definition: MemoryNameSystem.cpp:459
armarx::armem::client::MemoryNameSystem::getAllConfigurators
std::map< std::string, Configurator > getAllConfigurators() const
Get Configurators for all registered servers (without updating).
Definition: MemoryNameSystem.cpp:390
armarx::armem::client::Loader
Load LTMs into WM.
Definition: Loader.h:11
armarx::armem::client::MemoryNameSystem::getWriter
Writer getWriter(const MemoryID &memoryID)
Get a writer to the given memory name.
Definition: MemoryNameSystem.cpp:413
armarx::armem::wm
Definition: MemoryNameSystem.h:50
armarx::armem::MemoryID
A memory ID.
Definition: MemoryID.h:47
armarx::armem::client::MemoryNameSystem::useConfigurator
Configurator useConfigurator(const MemoryID &memoryID)
Use a memory server and get a configurator for it.
Definition: MemoryNameSystem.cpp:201
armarx::armem::client::MemoryNameSystem::useReader
Reader useReader(const MemoryID &memoryID)
Use a memory server and get a reader for it.
Definition: MemoryNameSystem.cpp:267
armarx::armem::client::Writer
Helps a memory client sending data to a memory.
Definition: Writer.h:22
armarx::armem::client::MemoryNameSystem::getMemoryNameSystem
mns::MemoryNameSystemInterfacePrx getMemoryNameSystem() const
Definition: MemoryNameSystem.cpp:584
armarx::armem::client::MemoryNameSystem::resolveEntityInstances
std::map< MemoryID, wm::EntityInstance > resolveEntityInstances(const std::vector< MemoryID > &ids)
Definition: MemoryNameSystem.cpp:473
armarx::ManagedIceObject
The ManagedIceObject is the base class for all ArmarX objects.
Definition: ManagedIceObject.h:162
Registry.h
armarx::armem::client::MemoryNameSystem::setComponent
void setComponent(ManagedIceObject *component)
Definition: MemoryNameSystem.cpp:596
armarx::armem::client::util::MemoryListener
Handles update signals from the memory system and distributes it to its subsribers.
Definition: MemoryListener.h:25
armarx::armem::laser_scans::constants::memoryName
const std::string memoryName
Definition: constants.h:28
armarx::armem::index::memoryID
const MemoryID memoryID
Definition: memory_ids.cpp:28
armarx::armem::client::MemoryNameSystem::initialize
void initialize(mns::MemoryNameSystemInterfacePrx mns, ManagedIceObject *component=nullptr)
Definition: MemoryNameSystem.cpp:26
armarx::armem::client::MemoryNameSystem::getLoader
Loader getLoader(const MemoryID &memoryID)
Definition: MemoryNameSystem.cpp:227
armarx::armem::client::MemoryNameSystem
The memory name system (MNS) client.
Definition: MemoryNameSystem.h:73
armarx::armem::client::Configurator
Configure a memory server.
Definition: Configurator.h:17
armarx::armem::client::MemoryNameSystem::update
void update()
Update the internal registry to the data in the MNS.
Definition: MemoryNameSystem.cpp:33
armarx::armem::client::MemoryNameSystem::getReader
Reader getReader(const MemoryID &memoryID)
Get a reader to the given memory name.
Definition: MemoryNameSystem.cpp:260
Configurator.h
MemoryListener.h
armarx::armem::client::MemoryNameSystem::useLoader
Loader useLoader(const MemoryID &memoryID)
Use a memory server and get a configurator for it.
Definition: MemoryNameSystem.cpp:234
armarx::armem::client::MemoryNameSystem::MemoryNameSystem
MemoryNameSystem()
Definition: MemoryNameSystem.cpp:15
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::armem::client::MemoryNameSystem::getConfigurator
Configurator getConfigurator(const MemoryID &memoryID)
Get a configurator to the given memory name.
Definition: MemoryNameSystem.cpp:194