Registry.cpp
Go to the documentation of this file.
1 #include "Registry.h"
2 
4 #include <RobotAPI/interface/armem/server/ActionsInterface.h>
5 
6 
7 namespace armarx::armem::mns
8 {
9 
10  Registry::Registry(const std::string& logTag)
11  {
13  }
14 
15 
16  bool Registry::hasServer(const std::string& memoryName) const
17  {
18  return servers.count(memoryName) > 0;
19  }
20 
21 
22  dto::RegisterServerResult Registry::registerServer(const dto::RegisterServerInput& input)
23  {
24  dto::RegisterServerResult result;
25 
26  if (not(input.server.reading or input.server.writing))
27  {
28  result.success = false;
29  std::stringstream ss;
30  ss << "Could not register the memory server '" << input.name << "'."
31  << "\nGiven proxies are null."
32  << "\nIf you want to remove a memory server, use `removeServer()`.";
33  result.errorMessage = ss.str();
34  return result;
35  }
36 
37  auto it = servers.find(input.name);
38  if (it == servers.end())
39  {
40  it = servers.emplace(input.name, ServerInfo{}).first;
41  }
42  else if (not input.existOk)
43  {
44  result.success = false;
45  std::stringstream ss;
46  ss << "Could not register the memory '" << input.name << "'."
47  << "\nMemory '" << input.name << "' is already registered. "
48  << "\nIf this is ok, set 'existOk' to true when registering the memory.";
49  result.errorMessage = ss.str();
50  return result;
51  }
52 
53  ServerInfo& info = it->second;
54  info.name = input.name;
55  info.server = input.server;
57  ARMARX_DEBUG << "Registered memory '" << info.name << "'.";
58 
59  result.success = true;
60  return result;
61  }
62 
63 
64  dto::RemoveServerResult Registry::removeServer(const dto::RemoveServerInput& input)
65  {
66  dto::RemoveServerResult result;
67 
68  if (auto it = servers.find(input.name); it != servers.end())
69  {
70  result.success = true;
71  servers.erase(it);
72 
73  ARMARX_DEBUG << "Removed memory '" << input.name << "'.";
74  }
75  else if (!input.notExistOk)
76  {
77  result.success = false;
78  std::stringstream ss;
79  ss << "Could not remove the memory '" << input.name << "."
80  << "\nMemory '" << input.name << "' is not registered. "
81  << "\nIf this is ok, set 'notExistOk' to true when removing the memory.";
82  result.errorMessage = ss.str();
83  }
84 
85  return result;
86  }
87 
88 
89  template <class Prx>
90  bool isAvailable(const Prx& proxy)
91  {
92  if (proxy)
93  {
94  try
95  {
96  proxy->ice_ping();
97  return true;
98  }
99  catch (const Ice::Exception&)
100  {
101  }
102  }
103  return false;
104  }
105 
106 
107  dto::GetAllRegisteredServersResult
109  {
110  dto::GetAllRegisteredServersResult result;
111  result.success = true;
112  result.errorMessage = "";
113 
114  for (const auto& [name, info] : servers)
115  {
116  if (isAvailable(info.server.reading) or isAvailable(info.server.writing))
117  {
118  result.servers[name] = info.server;
119  }
120  }
121 
122  return result;
123  }
124 
125 
126  dto::ResolveServerResult Registry::resolveServer(const dto::ResolveServerInput& input)
127  {
128  dto::ResolveServerResult result;
129  try
130  {
131  ServerInfo& info = servers.at(input.name);
132 
133  result.success = true;
134  result.server = info.server;
135 
136  ARMARX_DEBUG << "Resolved memory name '" << input.name << "'.";
137  }
138  catch (const std::out_of_range&)
139  {
140  result.success = false;
141  std::stringstream ss;
142  ss << "Could not resolve the memory name '" << input.name << "'."
143  << "\nServer '" << input.name << "' is not registered.";
144  result.errorMessage = ss.str();
145  }
146 
147  return result;
148  }
149 
150 }
armarx::armem::mns::Registry::resolveServer
dto::ResolveServerResult resolveServer(const dto::ResolveServerInput &input)
Gets a server entry, if it is available.
Definition: Registry.cpp:126
armarx::armem::mns::isAvailable
bool isAvailable(const Prx &proxy)
Definition: Registry.cpp:90
armarx::armem::mns::Registry::ServerInfo
Information about a memory entry.
Definition: Registry.h:58
armarx::core::time::DateTime::Now
static DateTime Now()
Definition: DateTime.cpp:55
armarx::armem::mns::Registry::ServerInfo::server
mns::dto::MemoryServerInterfaces server
Definition: Registry.h:61
armarx::armem::mns::Registry::hasServer
bool hasServer(const std::string &memoryName) const
Indicates whether a server entry for that name exists.
Definition: Registry.cpp:16
armarx::armem::mns::Registry::removeServer
dto::RemoveServerResult removeServer(const dto::RemoveServerInput &input)
Remove a server entry.
Definition: Registry.cpp:64
armarx::armem::mns::Registry::getAllRegisteredServers
dto::GetAllRegisteredServersResult getAllRegisteredServers()
Definition: Registry.cpp:108
armarx::armem::mns::Registry::ServerInfo::timeRegistered
Time timeRegistered
Definition: Registry.h:62
armarx::armem::mns::Registry::Registry
Registry(const std::string &logTag="MemoryNameSystem Registry")
Definition: Registry.cpp:10
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:177
armarx::aron::input
ReaderT::InputType & input
Definition: rw.h:19
armarx::armem::mns
Definition: MemoryNameSystem.cpp:4
Registry.h
armarx::armem::mns::Registry::registerServer
virtual dto::RegisterServerResult registerServer(const dto::RegisterServerInput &input)
Register a new memory server or update an existing entry.
Definition: Registry.cpp:22
armarx::armem::laser_scans::constants::memoryName
const std::string memoryName
Definition: constants.h:28
armarx::armem::mns::Registry::ServerInfo::name
std::string name
Definition: Registry.h:60
armarx::Logging::setTag
void setTag(const LogTag &tag)
Definition: Logging.cpp:55
Logging.h
armarx::armem::mns::Registry::servers
std::map< std::string, ServerInfo > servers
The registered memories.
Definition: Registry.h:66