10 #include <SimoxUtility/algorithm/string/string_tools.h>
40 mns::dto::GetAllRegisteredServersResult result;
43 result = mns->getAllRegisteredServers();
45 catch (
const Ice::NotRegisteredException& e)
52 for (
const auto& [name, server] : result.servers)
54 auto [it, inserted] = servers.try_emplace(name, server);
62 auto foo = [](
auto & oldProxy,
const auto & newProxy)
64 if (oldProxy->ice_getIdentity() != newProxy->ice_getIdentity())
70 foo(it->second.reading, server.reading);
71 foo(it->second.writing, server.writing);
72 foo(it->second.prediction, server.prediction);
73 foo(it->second.actions, server.actions);
77 for (
auto it = servers.begin(); it != servers.end();)
79 if (result.servers.count(it->first) == 0)
81 it = servers.erase(it);
96 mns::dto::MemoryServerInterfaces
118 mns::dto::MemoryServerInterfaces MemoryNameSystem::waitForServer(
const MemoryID&
memoryID)
126 mns::dto::WaitForServerInput
input;
131 mns::dto::WaitForServerResult result = mns->waitForServer(
input);
135 return result.server;
139 throw error::CouldNotResolveMemoryServer(
memoryID, result.errorMessage);
144 mns::dto::MemoryServerInterfaces MemoryNameSystem::useServer(
const MemoryID&
memoryID)
147 <<
"Owning component not set when using a memory server. \n"
148 <<
"When calling `armem::mns::MemoryNameSystem::useServer()`, the owning component which should "
149 <<
"receive the dependency to the memory server must be set beforehand. \n\n"
150 <<
"Use `armem::mns::MemoryNameSystem::setComponent()` or pass the component on construction "
151 <<
"before calling useServer().";
152 return useServer(
memoryID, *component);
156 mns::dto::MemoryServerInterfaces MemoryNameSystem::useServer(
const MemoryID&
memoryID, ManagedIceObject& component)
158 mns::dto::MemoryServerInterfaces server = waitForServer(
memoryID);
160 component.usingProxy(server.reading->ice_getIdentity().name);
165 mns::dto::MemoryServerInterfaces MemoryNameSystem::useServer(
const std::string&
memoryName)
171 mns::dto::MemoryServerInterfaces MemoryNameSystem::useServer(
const std::string&
memoryName, ManagedIceObject& component)
180 return Reader(server.reading, server.prediction);
187 return Reader(server.reading, server.prediction);
194 return Reader(server.reading, server.prediction);
210 template <
class ClientT>
211 std::map<std::string, ClientT>
212 MemoryNameSystem::_getAllClients(ClientFactory<ClientT>&& factory)
const
214 std::map<std::string, ClientT> result;
215 for (
const auto& [name, server] : servers)
217 if (std::optional<ClientT> client = factory(server))
219 result[name] = client.value();
226 std::optional<Reader>
readerFactory(
const mns::dto::MemoryServerInterfaces& server)
228 if (
auto read = server.reading)
230 if (
auto predict = server.prediction)
243 std::optional<Writer>
writerFactory(
const mns::dto::MemoryServerInterfaces& server)
245 if (
auto write = server.writing)
319 if (result.size() > 0)
321 return result.begin()->second;
332 std::stringstream errors;
333 int errorCounter = 0;
335 std::map<std::string, std::vector<MemoryID>> idsPerMemory;
336 for (
const auto&
id : ids)
338 idsPerMemory[
id.memoryName].push_back(
id);
341 std::map<MemoryID, wm::EntityInstance> result;
342 for (
const auto& [
memoryName, ids] : idsPerMemory)
352 if (
id.hasInstanceIndex())
356 else if (
id.hasTimestamp())
360 else if (
id.hasEntityName())
362 result[id] = queryResult.
memory.
getEntity(
id).getLatestSnapshot().getInstance(0);
366 std::stringstream ss;
367 ss <<
"MemoryNameSystem::" << __FUNCTION__ <<
"requires IDs to be entity, snapshot or instance IDs,"
368 <<
"but ID has no entity name.";
374 errors <<
"\n#" << ++errorCounter <<
"\n"
375 <<
"Failed to retrieve " <<
id <<
" from query result: \n" << e.what();
381 errors <<
"\n# " << ++errorCounter <<
"\n"
386 if (errors.str().size() > 0)
388 ARMARX_INFO <<
"MemoryNameSystem::" << __FUNCTION__ <<
": The following errors may affect your result: "
389 <<
"\n\n" << errors.str() <<
"\n\n"
390 <<
"When querying entity instances: \n- "
391 << simox::alg::join(simox::alg::multi_to_string(ids),
"\n- ");
400 mns::dto::RegisterServerInput
input;
402 input.server = server;
406 mns::dto::RegisterServerResult result = mns->registerServer(
input);
416 mns::dto::RemoveServerInput
input;
420 mns::dto::RemoveServerResult result = mns->removeServer(
input);