10 std::vector<std::string>
17 return std::vector<std::string>();
20 return getRestContainers(
id);
23 std::vector<std::string>
30 return std::vector<std::string>();
33 return getRestItems(
id);
46 return restContainsContainer(
id, key);
59 return restContainsItem(
id, key);
65 std::vector<unsigned char>&
data)
74 writeItemToRest(
id, key,
data);
77 std::vector<unsigned char>
84 return std::vector<unsigned char>();
89 return readItemFromRest(
id, key);
92 return std::vector<unsigned char>();
97 const std::string& prefix)
99 defs->optional(host_, prefix +
"host");
100 defs->optional(port_, prefix +
"port");
106 std::string path =
"/ltm";
108 auto res = client_->Get(path);
113 <<
", port=" << port_;
119 <<
", port=" << port_;
125 RestPersistence::resetClient()
132 client_ = std::make_shared<httplib::Client>(host_, port_);
138 std::string path = buildPath(
id);
139 std::string
query =
"item=" + key;
140 std::string url = path +
"?" +
query;
142 ARMARX_DEBUG <<
"GET request: Read data from MemoryX-REST server: item=" << key
145 auto res = client_->Get(url);
151 ARMARX_DEBUG <<
"GET request successful: REST-LTM contain item with key=" << key;
158 ARMARX_DEBUG <<
"GET request successful: REST-LTM does not contain item with key="
166 ARMARX_DEBUG <<
"GET request failed. Status: " << res->status;
171 ARMARX_DEBUG <<
"GET request failed. No connection could be established to the "
172 "MemoryX-REST server at host="
173 << host_ <<
", port=" << port_;
180 RestPersistence::restContainsContainer(
const armarx::armem::MemoryID&
id, std::string& key)
182 std::string path = buildPath(
id);
183 std::string query =
"container=" + key;
184 std::string url = path +
"?" + query;
186 ARMARX_DEBUG <<
"GET request: Read data from MemoryX-REST server: container=" << key
189 auto res = client_->Get(url);
195 ARMARX_DEBUG <<
"GET request successful: REST-LTM contain container with key=" << key;
202 ARMARX_DEBUG <<
"GET request successful: REST-LTM does not contain container with key="
210 ARMARX_DEBUG <<
"GET request failed. Status: " << res->status;
215 ARMARX_DEBUG <<
"GET request failed. No connection could be established to the "
216 "MemoryX-REST server at host="
217 << host_ <<
", port=" << port_;
223 std::vector<std::string>
224 RestPersistence::getRestItems(
const armarx::armem::MemoryID&
id)
226 std::string path = buildPath(
id);
227 std::string query =
"items";
228 std::string url = path +
"?" + query;
230 ARMARX_DEBUG <<
"GET request: Get items from MemoryX-REST server: url=" << url;
232 auto res = client_->Get(url);
234 std::vector<std::string> item_keys;
242 nlohmann::json json_response = nlohmann::json::parse(res->body);
244 if (json_response.contains(
"keys") && json_response[
"keys"].is_array())
246 item_keys = json_response[
"keys"].get<std::vector<std::string>>();
249 catch (
const std::exception& e)
251 ARMARX_DEBUG <<
"GET request: Error parsing JSON: " << e.what();
256 ARMARX_DEBUG <<
"GET request failed. status=" << res->status <<
", url=" << url;
260 ARMARX_DEBUG <<
"GET request failed. No connection could be established to the "
261 "MemoryX-REST server at host="
262 << host_ <<
", port=" << port_;
268 std::vector<std::string>
269 RestPersistence::getRestContainers(
const armarx::armem::MemoryID&
id)
271 std::string path = buildPath(
id);
272 std::string query =
"containers";
273 std::string url = path +
"?" + query;
275 ARMARX_DEBUG <<
"GET request: Get containers from MemoryX-REST server: url=" << url;
277 auto res = client_->Get(url);
279 std::vector<std::string> item_keys;
287 nlohmann::json json_response = nlohmann::json::parse(res->body);
289 if (json_response.contains(
"keys") && json_response[
"keys"].is_array())
291 item_keys = json_response[
"keys"].get<std::vector<std::string>>();
294 catch (
const std::exception& e)
296 ARMARX_ERROR <<
"GET request: Error parsing JSON: " << e.what();
301 ARMARX_DEBUG <<
"GET request failed. status=" << res->status <<
", url=" << url;
305 ARMARX_DEBUG <<
"GET request failed. No connection could be established to the "
306 "MemoryX-REST server at host="
307 << host_ <<
", port=" << port_;
313 std::vector<unsigned char>
314 RestPersistence::readItemFromRest(
const armarx::armem::MemoryID&
id, std::string& key)
316 std::string path = buildPath(
id);
317 std::string query =
"item=" + key;
318 std::string url = path +
"?" + query;
320 ARMARX_DEBUG <<
"GET request: Read data from MemoryX-REST server: key=" << key
323 auto res = client_->Get(url);
325 std::vector<unsigned char>
data;
331 data = std::vector<unsigned char>(res->body.begin(), res->body.end());
335 ARMARX_DEBUG <<
"GET request failed: REST-LTM does not contain any item with key="
340 ARMARX_DEBUG <<
"GET request failed. status=" << res->status <<
", key=" << key
345 ARMARX_DEBUG <<
"GET request failed. No connection could be established to the "
346 "MemoryX-REST server at host="
347 << host_ <<
", port=" << port_;
354 RestPersistence::writeItemToRest(
const armarx::armem::MemoryID&
id,
356 const std::vector<unsigned char>& data)
358 std::string path = buildPath(
id);
359 std::string query =
"item=" + key;
360 std::string url = path +
"?" + query;
362 ARMARX_DEBUG <<
"PUT request: Write data to MemoryX-REST server: key=" << key
365 std::string body(
data.begin(),
data.end());
367 auto res = client_->Put(url, body,
"application/json");
375 ARMARX_DEBUG <<
"PUT request failed. status=" << res->status <<
", key=" << key
380 ARMARX_DEBUG <<
"PUT request failed. No connection could be established to the "
381 "MemoryX-REST server at host="
382 << host_ <<
", port=" << port_;
387 RestPersistence::writeItemToRest(
const armarx::armem::MemoryID&
id,
389 nlohmann::json& jsonData)
391 std::string str_data = jsonData.dump();
392 std::vector<unsigned char>
data(str_data.begin(), str_data.end());
398 RestPersistence::writeItemToRest(
const armarx::armem::MemoryID&
id,
401 const std::vector<unsigned char>& data)
403 std::string path = buildPath(
id);
404 std::string mime_type = getMimeType(type);
405 std::string query =
"?item=" + key;
407 std::string body(
data.begin(),
data.end());
409 auto res = client_->Put(path + query, body, mime_type);
418 ARMARX_DEBUG <<
"Request failed. Error: " << res.error();
423 RestPersistence::removeRestItem(
const armarx::armem::MemoryID&
id, std::string& key)
425 std::string path = buildPath(
id);
426 std::string query =
"item=" + key;
427 std::string url = path +
"?" + query;
429 ARMARX_DEBUG <<
"DELETE request: Remove item from MemoryX-REST server: key=" << key
433 auto res = client_->Delete(url);
437 ARMARX_DEBUG <<
"DELETE request successful: " << res->body;
441 ARMARX_DEBUG <<
"DELETE request failed. REST-LTM does not contain any item with key="
446 ARMARX_DEBUG <<
"DELETE request failed. Status: " << res->status;
450 ARMARX_DEBUG <<
"DELETE request failed. No connection could be established to the "
451 "MemoryX-REST server at host="
452 << host_ <<
", port=" << port_;
457 RestPersistence::setPort(
int port)
464 RestPersistence::getPort()
const
470 RestPersistence::setHost(std::string& host)
477 RestPersistence::getHost()
483 RestPersistence::buildPath(
const armarx::armem::MemoryID&
id)
487 if (
id.hasMemoryName())
491 if (
id.hasCoreSegmentName())
496 if (
id.hasProviderSegmentName())
499 id.providerSegmentName);
501 if (
id.hasEntityName())
505 if (
id.hasTimestamp())
507 url +=
"/" +
id.timestampStr();
509 if (
id.hasInstanceIndex())
511 url +=
"/" +
id.instanceIndexStr();
518 RestPersistence::getMimeType(
DataType type)
521 std::string mime_type;
525 mime_type =
"application/json";
529 mime_type =
"image/png";
std::string identifier_
Name of the strategy.
std::string getExportName()
bool enabled_
If false, the strategy is not writing or reading anything.
bool checkConnection()
Checks if the server is up.
bool containsContainer(const armarx::armem::MemoryID &id, std::string key) override
std::vector< std::string > getItemKeys(const armarx::armem::MemoryID &id) override
Keys of the actual items containing data stored for the memory id.
void storeItem(const armarx::armem::MemoryID &id, std::string key, std::vector< unsigned char > &data) override
Stores an item containing actual data for the current memory id.
bool containsItem(const armarx::armem::MemoryID &id, std::string key) override
std::vector< std::string > getContainerKeys(const armarx::armem::MemoryID &id) override
Returns keys that allow use to move a step further in the hierarchy (e.g.
static const std::string DEFAULT_HOST
std::vector< unsigned char > retrieveItem(const armarx::armem::MemoryID &id, std::string key) override
Retrieve the actual data of an item stored for the memory id.
void createPropertyDefinitions(PropertyDefinitionsPtr &defs, const std::string &prefix) override
#define ARMARX_IMPORTANT
The logging level for always important information, but expected behaviour (in contrast to ARMARX_WAR...
#define ARMARX_ERROR
The logging level for unexpected behaviour, that must be fixed.
#define ARMARX_DEBUG
The logging level for output that is only interesting while debugging.
std::string escapeName(const std::string &segmentName)
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.