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 
34 namespace armarx
35 {
36  class ManagedIceObject;
37 }
38 
39 namespace armarx::armem::client
40 {
41  class Reader;
42  class Writer;
43 } // namespace armarx::armem::client
44 
46 {
47  class EntityInstance;
48 }
49 
50 namespace armarx::armem::client
51 {
52 
53  /**
54  * @brief The memory name system (MNS) client.
55  *
56  * This client class serves provides the MNS interface and is a local cache
57  * of the MNS registry. It can be used to resolve memory servers by their
58  * memory ID and to construct `client::Readers` and `client::Writers`.
59  *
60  * During server resolution, it first consults the locally cached registry.
61  * If the memory server is not known locally, the local registry is
62  * updated to that of the remote MNS before trying again.
63  *
64  * In addition, the MNS client can be used to send queries over multiple
65  * memory servers, as well as retrieving the data for arbitrary entity or
66  * entity snapshot IDs.
67  */
69  {
70  public:
72 
73  /**
74  * @brief Construct an MNS client.
75  *
76  * @param mns The MNS proxy.
77  * @param component The owning component. When `using` a memory server,
78  * dependencies will be added to this component.
79  */
80  MemoryNameSystem(mns::MemoryNameSystemInterfacePrx mns,
81  ManagedIceObject* component = nullptr);
82 
83  void initialize(mns::MemoryNameSystemInterfacePrx mns,
84  ManagedIceObject* component = nullptr);
85 
86 
87  mns::MemoryNameSystemInterfacePrx getMemoryNameSystem() const;
88  void getMemoryNameSystem(mns::MemoryNameSystemInterfacePrx mns);
89 
90  void setComponent(ManagedIceObject* component);
91 
92 
93  // Name Resolution
94 
95  /**
96  * @brief Update the internal registry to the data in the MNS.
97  *
98  * @throw `error::MemoryNameSystemQueryFailed` If the call to the MNS failed.
99  */
100  void update();
101 
102 
103  // Reader/Writer construction
104 
105  /**
106  * @brief Get a reader to the given memory name.
107  *
108  * @param memoryID The memory ID.
109  * @return The reader.
110  *
111  * @throw `error::CouldNotResolveMemoryServer` If the memory name could not be resolved.
112  */
114 
115  /// Use a memory server and get a reader for it.
118  Reader useReader(const std::string& memoryName);
119  Reader useReader(const std::string& memoryName, ManagedIceObject& component);
120 
121  /**
122  * @brief Get Readers for all registered servers.
123  *
124  * @param update If true, perform an update first.
125  * @return The readers.
126  */
127  std::map<std::string, Reader> getAllReaders(bool update = true);
128  /**
129  * @brief Get Readers for all registered servers (without updating).
130  *
131  * @return The readers.
132  */
133  std::map<std::string, Reader> getAllReaders() const;
134 
135 
136  /**
137  * @brief Get a writer to the given memory name.
138  *
139  * @param memoryID The memory ID.
140  * @return The writer.
141  *
142  * @throw `error::CouldNotResolveMemoryServer` If the memory name could not be resolved.
143  */
145 
146  /// Use a memory server and get a writer for it.
149  Writer useWriter(const std::string& memoryName);
150  Writer useWriter(const std::string& memoryName, ManagedIceObject& component);
151 
152  /**
153  * @brief Get Writers for all registered servers.
154  *
155  * @param update If true, perform an update first.
156  * @return The readers.
157  */
158  std::map<std::string, Writer> getAllWriters(bool update = true);
159  /**
160  * @brief Get Writers for all registered servers (without updating).
161  *
162  * @return The readers.
163  */
164  std::map<std::string, Writer> getAllWriters() const;
165 
166 
167  // ToDo: commit() and query()
168 
169  /**
170  * @brief Resolve a memory ID to an EntityInstance.
171  *
172  * The ID can refer to an entity, an entity snapshot, or an entity
173  * instance. When not referring to an entity instance, the latest
174  * snapshot and first instance will be queried.
175  *
176  * @param id The entity, snapshot or instance ID.
177  * @return
178  */
179  std::optional<wm::EntityInstance> resolveEntityInstance(const MemoryID& id);
180 
181  std::map<MemoryID, wm::EntityInstance>
182  resolveEntityInstances(const std::vector<MemoryID>& ids);
183 
184  /**
185  * @brief Resolve the given memory server for the given memory ID.
186  *
187  * @param memoryID The memory ID.
188  * @return The memory server proxy.
189  *
190  * @throw `error::CouldNotResolveMemoryServer` If the memory name could not be resolved.
191  */
192  mns::dto::MemoryServerInterfaces resolveServer(const MemoryID& memoryID);
193 
194  // Registration - only for memory servers
195 
196  /**
197  * @brief Register a memory server in the MNS.
198  *
199  * @param memoryID The memoryID.
200  * @param server The memory server proxy.
201  *
202  * @throw `error::ServerRegistrationOrRemovalFailed` If the registration failed.
203  */
204  void registerServer(const MemoryID& memoryID, mns::dto::MemoryServerInterfaces server);
205 
206  /**
207  * @brief Remove a memory server from the MNS.
208  *
209  * @param memoryID The memoryID.
210  *
211  * @throw `error::ServerRegistrationOrRemovalFailed` If the removal failed.
212  */
213  void removeServer(const MemoryID& memoryID);
214 
215  // Operators
216 
217  /// Indicate whether the proxy is set.
218  inline
219  operator bool() const
220  {
221  return bool(mns);
222  }
223 
224 
225  private:
226  /**
227  * @brief Wait for the given memory server.
228  *
229  * @param memoryID The memory ID.
230  * @return The memory server proxy.
231  *
232  * @throw `error::CouldNotResolveMemoryServer` If the memory name could not be resolved.
233  */
234  mns::dto::MemoryServerInterfaces waitForServer(const MemoryID& memoryID);
235 
236  /**
237  * @brief Wait for the given memory server and add a dependency to the memory server.
238  *
239  * @param memoryID The memory ID.
240  * @param component The component that should depend on the memory server.
241  * @return The memory server proxy.
242  *
243  * @throw `error::CouldNotResolveMemoryServer` If the memory name could not be resolved.
244  */
245  mns::dto::MemoryServerInterfaces useServer(const MemoryID& memoryID);
246  mns::dto::MemoryServerInterfaces useServer(const MemoryID& memoryID,
247  ManagedIceObject& component);
248  mns::dto::MemoryServerInterfaces useServer(const std::string& memoryName);
249  mns::dto::MemoryServerInterfaces useServer(const std::string& memoryName,
250  ManagedIceObject& component);
251 
252 
253  private:
254  template <class ClientT>
255  using ClientFactory =
256  std::function<std::optional<ClientT>(const mns::dto::MemoryServerInterfaces& server)>;
257 
258  template <class ClientT>
259  std::map<std::string, ClientT> _getAllClients(ClientFactory<ClientT>&& factory) const;
260 
261 
262  /// The MNS proxy.
263  mns::MemoryNameSystemInterfacePrx mns = nullptr;
264 
265  /// The component to which dependencies will be added.
266  ManagedIceObject* component = nullptr;
267 
268  /// The registered memory servers.
269  std::map<std::string, mns::dto::MemoryServerInterfaces> servers;
270  };
271 
272 
273 } // 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:113
armarx::armem::client::MemoryNameSystem::removeServer
void removeServer(const MemoryID &memoryID)
Remove a memory server from the MNS.
Definition: MemoryNameSystem.cpp:436
armarx::armem::client::Reader
Reads data from a memory server.
Definition: Reader.h:23
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:277
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:289
armarx::armem::client::MemoryNameSystem::getAllWriters
std::map< std::string, Writer > getAllWriters() const
Get Writers for all registered servers (without updating).
Definition: MemoryNameSystem.cpp:323
armarx::armem::client
This file is part of ArmarX.
Definition: forward_declarations.h:6
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::client::MemoryNameSystem::resolveEntityInstance
std::optional< wm::EntityInstance > resolveEntityInstance(const MemoryID &id)
Resolve a memory ID to an EntityInstance.
Definition: MemoryNameSystem.cpp:329
armarx::armem::client::MemoryNameSystem::getWriter
Writer getWriter(const MemoryID &memoryID)
Get a writer to the given memory name.
Definition: MemoryNameSystem.cpp:283
armarx::armem::wm
Definition: MemoryNameSystem.h:45
armarx::armem::MemoryID
A memory ID.
Definition: MemoryID.h:47
armarx::armem::client::MemoryNameSystem::useReader
Reader useReader(const MemoryID &memoryID)
Use a memory server and get a reader for it.
Definition: MemoryNameSystem.cpp:198
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:450
armarx::armem::client::MemoryNameSystem::resolveEntityInstances
std::map< MemoryID, wm::EntityInstance > resolveEntityInstances(const std::vector< MemoryID > &ids)
Definition: MemoryNameSystem.cpp:343
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:462
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
The memory name system (MNS) client.
Definition: MemoryNameSystem.h:68
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:191
MemoryListener.h
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