Registry.cpp
Go to the documentation of this file.
1#include "Registry.h"
2
4
5#include <RobotAPI/interface/armem/server/ActionsInterface.h>
6
7namespace armarx::armem::mns
8{
9
10 Registry::Registry(const std::string& logTag)
11 {
13 }
14
15 bool
16 Registry::hasServer(const std::string& memoryName) const
17 {
18 return servers.count(memoryName) > 0;
19 }
20
21 dto::RegisterServerResult
22 Registry::registerServer(const dto::RegisterServerInput& input)
23 {
24 dto::RegisterServerResult result;
25
26 if (not(input.server.reading or input.server.writing or input.server.configuration or input.server.loading))
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_INFO << "Registered memory '" << info.name << "'.";
58
59 result.success = true;
60 return result;
61 }
62
63 dto::RemoveServerResult
64 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 template <class Prx>
89 bool
90 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 dto::GetAllRegisteredServersResult
108 {
109 dto::GetAllRegisteredServersResult result;
110 result.success = true;
111 result.errorMessage = "";
112
113 for (const auto& [name, info] : servers)
114 {
115 if (isAvailable(info.server.reading) or isAvailable(info.server.writing) or isAvailable(info.server.configuration) or isAvailable(info.server.loading))
116 {
117 result.servers[name] = info.server;
118 }
119 }
120
121 return result;
122 }
123
124 dto::ResolveServerResult
125 Registry::resolveServer(const dto::ResolveServerInput& input)
126 {
127 dto::ResolveServerResult result;
128 try
129 {
130 ServerInfo& info = servers.at(input.name);
131
132 result.success = true;
133 result.server = info.server;
134
135 ARMARX_DEBUG << "Resolved memory name '" << input.name << "'.";
136 }
137 catch (const std::out_of_range&)
138 {
139 result.success = false;
140 std::stringstream ss;
141 ss << "Could not resolve the memory name '" << input.name << "'."
142 << "\nServer '" << input.name << "' is not registered.";
143 result.errorMessage = ss.str();
144 }
145
146 return result;
147 }
148
149} // namespace armarx::armem::mns
void setTag(const LogTag &tag)
Definition Logging.cpp:54
dto::GetAllRegisteredServersResult getAllRegisteredServers()
Definition Registry.cpp:107
std::map< std::string, ServerInfo > servers
The registered memories.
Definition Registry.h:62
bool hasServer(const std::string &memoryName) const
Indicates whether a server entry for that name exists.
Definition Registry.cpp:16
virtual dto::RegisterServerResult registerServer(const dto::RegisterServerInput &input)
Register a new memory server or update an existing entry.
Definition Registry.cpp:22
dto::RemoveServerResult removeServer(const dto::RemoveServerInput &input)
Remove a server entry.
Definition Registry.cpp:64
dto::ResolveServerResult resolveServer(const dto::ResolveServerInput &input)
Gets a server entry, if it is available.
Definition Registry.cpp:125
Registry(const std::string &logTag="MemoryNameSystem Registry")
Definition Registry.cpp:10
static DateTime Now()
Definition DateTime.cpp:51
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_DEBUG
The logging level for output that is only interesting while debugging.
Definition Logging.h:184
bool isAvailable(const Prx &proxy)
Definition Registry.cpp:90
Information about a memory entry.
Definition Registry.h:55
mns::dto::MemoryServerInterfaces server
Definition Registry.h:57