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>
30 
34 
35 
36 namespace armarx
37 {
38  class ManagedIceObject;
39 }
40 namespace armarx::armem::client
41 {
42  class Reader;
43  class Writer;
44 }
45 
47 {
48  class EntityInstance;
49 }
50 
51 namespace armarx::armem::client
52 {
53 
54  /**
55  * @brief The memory name system (MNS) client.
56  *
57  * This client class serves provides the MNS interface and is a local cache
58  * of the MNS registry. It can be used to resolve memory servers by their
59  * memory ID and to construct `client::Readers` and `client::Writers`.
60  *
61  * During server resolution, it first consults the locally cached registry.
62  * If the memory server is not known locally, the local registry is
63  * updated to that of the remote MNS before trying again.
64  *
65  * In addition, the MNS client can be used to send queries over multiple
66  * memory servers, as well as retrieving the data for arbitrary entity or
67  * entity snapshot IDs.
68  */
70  {
71  public:
72 
74 
75  /**
76  * @brief Construct an MNS client.
77  *
78  * @param mns The MNS proxy.
79  * @param component The owning component. When `using` a memory server,
80  * dependencies will be added to this component.
81  */
82  MemoryNameSystem(mns::MemoryNameSystemInterfacePrx mns,
83  ManagedIceObject* component = nullptr);
84 
85  void initialize(mns::MemoryNameSystemInterfacePrx mns, ManagedIceObject* component = nullptr);
86 
87 
88  mns::MemoryNameSystemInterfacePrx getMemoryNameSystem() const;
89  void getMemoryNameSystem(mns::MemoryNameSystemInterfacePrx mns);
90 
91  void setComponent(ManagedIceObject* component);
92 
93 
94  // Name Resolution
95 
96  /**
97  * @brief Update the internal registry to the data in the MNS.
98  *
99  * @throw `error::MemoryNameSystemQueryFailed` If the call to the MNS failed.
100  */
101  void update();
102 
103 
104  // Reader/Writer construction
105 
106  /**
107  * @brief Get a reader to the given memory name.
108  *
109  * @param memoryID The memory ID.
110  * @return The reader.
111  *
112  * @throw `error::CouldNotResolveMemoryServer` If the memory name could not be resolved.
113  */
115 
116  /// Use a memory server and get a reader for it.
119  Reader useReader(const std::string& memoryName);
120  Reader useReader(const std::string& memoryName, ManagedIceObject& component);
121 
122  /**
123  * @brief Get Readers for all registered servers.
124  *
125  * @param update If true, perform an update first.
126  * @return The readers.
127  */
128  std::map<std::string, Reader> getAllReaders(bool update = true);
129  /**
130  * @brief Get Readers for all registered servers (without updating).
131  *
132  * @return The readers.
133  */
134  std::map<std::string, Reader> getAllReaders() const;
135 
136 
137  /**
138  * @brief Get a writer to the given memory name.
139  *
140  * @param memoryID The memory ID.
141  * @return The writer.
142  *
143  * @throw `error::CouldNotResolveMemoryServer` If the memory name could not be resolved.
144  */
146 
147  /// Use a memory server and get a writer for it.
150  Writer useWriter(const std::string& memoryName);
151  Writer useWriter(const std::string& memoryName, ManagedIceObject& component);
152 
153  /**
154  * @brief Get Writers for all registered servers.
155  *
156  * @param update If true, perform an update first.
157  * @return The readers.
158  */
159  std::map<std::string, Writer> getAllWriters(bool update = true);
160  /**
161  * @brief Get Writers for all registered servers (without updating).
162  *
163  * @return The readers.
164  */
165  std::map<std::string, Writer> getAllWriters() const;
166 
167 
168  // ToDo: commit() and query()
169 
170  /**
171  * @brief Resolve a memory ID to an EntityInstance.
172  *
173  * The ID can refer to an entity, an entity snapshot, or an entity
174  * instance. When not referring to an entity instance, the latest
175  * snapshot and first instance will be queried.
176  *
177  * @param id The entity, snapshot or instance ID.
178  * @return
179  */
180  std::optional<wm::EntityInstance> resolveEntityInstance(const MemoryID& id);
181 
182  std::map<MemoryID, wm::EntityInstance> 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 
216 
217  // Operators
218 
219  /// Indicate whether the proxy is set.
220  inline operator bool() const
221  {
222  return bool(mns);
223  }
224 
225 
226  private:
227 
228  /**
229  * @brief Wait for the given memory server.
230  *
231  * @param memoryID The memory ID.
232  * @return The memory server proxy.
233  *
234  * @throw `error::CouldNotResolveMemoryServer` If the memory name could not be resolved.
235  */
236  mns::dto::MemoryServerInterfaces waitForServer(const MemoryID& memoryID);
237 
238  /**
239  * @brief Wait for the given memory server and add a dependency to the memory server.
240  *
241  * @param memoryID The memory ID.
242  * @param component The component that should depend on the memory server.
243  * @return The memory server proxy.
244  *
245  * @throw `error::CouldNotResolveMemoryServer` If the memory name could not be resolved.
246  */
247  mns::dto::MemoryServerInterfaces useServer(const MemoryID& memoryID);
248  mns::dto::MemoryServerInterfaces useServer(const MemoryID& memoryID, ManagedIceObject& component);
249  mns::dto::MemoryServerInterfaces useServer(const std::string& memoryName);
250  mns::dto::MemoryServerInterfaces useServer(const std::string& memoryName, ManagedIceObject& component);
251 
252 
253  private:
254 
255  template <class ClientT>
256  using ClientFactory = 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 
274 }
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:97
armarx::armem::client::MemoryNameSystem::removeServer
void removeServer(const MemoryID &memoryID)
Remove a memory server from the MNS.
Definition: MemoryNameSystem.cpp:414
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:264
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:276
armarx::armem::client::MemoryNameSystem::getAllWriters
std::map< std::string, Writer > getAllWriters() const
Get Writers for all registered servers (without updating).
Definition: MemoryNameSystem.cpp:310
armarx::armem::client
This file is part of ArmarX.
Definition: forward_declarations.h:7
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::client::MemoryNameSystem::resolveEntityInstance
std::optional< wm::EntityInstance > resolveEntityInstance(const MemoryID &id)
Resolve a memory ID to an EntityInstance.
Definition: MemoryNameSystem.cpp:316
armarx::armem::client::MemoryNameSystem::getWriter
Writer getWriter(const MemoryID &memoryID)
Get a writer to the given memory name.
Definition: MemoryNameSystem.cpp:270
armarx::armem::wm
Definition: MemoryNameSystem.h:46
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:184
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:428
armarx::armem::client::MemoryNameSystem::resolveEntityInstances
std::map< MemoryID, wm::EntityInstance > resolveEntityInstances(const std::vector< MemoryID > &ids)
Definition: MemoryNameSystem.cpp:330
armarx::ManagedIceObject
The ManagedIceObject is the base class for all ArmarX objects.
Definition: ManagedIceObject.h:163
Registry.h
armarx::armem::client::MemoryNameSystem::setComponent
void setComponent(ManagedIceObject *component)
Definition: MemoryNameSystem.cpp:440
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:29
armarx::armem::client::MemoryNameSystem::initialize
void initialize(mns::MemoryNameSystemInterfacePrx mns, ManagedIceObject *component=nullptr)
Definition: MemoryNameSystem.cpp:29
armarx::armem::client::MemoryNameSystem
The memory name system (MNS) client.
Definition: MemoryNameSystem.h:69
armarx::armem::client::MemoryNameSystem::update
void update()
Update the internal registry to the data in the MNS.
Definition: MemoryNameSystem.cpp:36
armarx::armem::client::MemoryNameSystem::getReader
Reader getReader(const MemoryID &memoryID)
Get a reader to the given memory name.
Definition: MemoryNameSystem.cpp:177
MemoryListener.h
armarx::armem::client::MemoryNameSystem::MemoryNameSystem
MemoryNameSystem()
Definition: MemoryNameSystem.cpp:17
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28