RestPersistence.h
Go to the documentation of this file.
1#pragma once
2
3#include <SimoxUtility/json.h>
4
8
10{
12 {
15 };
16
17 /**
18 * Persistence strategy that uses a rest server as its data sink.
19 * How the data is stored on the rest server is up to the server.
20 *
21 * The structure of rest endpoints are very similar to a directory structure.
22 *
23 * General endpoint:
24 * /endpoint
25 * GET: /endpoint?containers: return all container keys of the endpoint
26 * GET: /endpoint?container=key: check if a container with key exists (true = Status Code 200, false = Status Code 404)
27 * GET: /endpoint?items: return all item keys of the endpoint
28 * GET: /endpoint?item=key: return Status Code 200 with item data OR Status Code 404 if the item is not inside of the container
29 * PUT: /endpoint?item: store item data (data = http body content)
30 * DELETE: /endpoint?item=key: removes item with key, return Status Code 200 on success and Status Code 404 if item not found
31 * DELETE: /endpoint?container=key: removes container with key, return Status Code 200 on success and Status Code 404 if container not found
32 *
33 * NOTE: DELETE-methods are implemented, but the curren memory system does not use it in general.
34 *
35 * Available endpoints:
36 * /ltm/:exportName
37 * -> memories
38 * /ltm/:exportName/:memoryName
39 * -> core segments
40 * /ltm/:exportName/:memoryName/:coreSegmentName
41 * -> provider segments
42 * /ltm/:exportName/:memoryName/:coreSegmentName/:providerSegmentName
43 * -> entities
44 * /ltm/:exportName/:memoryName/:coreSegmentName/:providerSegmentName/:entityName
45 * -> entity snapshots
46 * /ltm/:exportName/:memoryName/:coreSegmentName/:providerSegmentName/:entityName/:timestamp
47 * -> entity instances
48 * /ltm/:exportName/:memoryName/:coreSegmentName/:providerSegmentName/:entityName/:timestamp/:instanceIndex
49 * -> entity instance (here you will definitely find some items)
50 */
52 {
53 public:
54 RestPersistence() : RestPersistence("Rest", "DefaultExport")
55 {
56 }
57
58 RestPersistence(const std::string& identifier, const std::string& exportName) :
59 MemoryPersistenceStrategy(identifier, exportName)
60 {
61 }
62
63 /**
64 * @param identifier basically a unique name for the strategy (important if you use different strategies @see RedundantPersistenceStrategy)
65 * @param exportName identifier for the exported memory where any items are written. Changes http endpoints.
66 * @param id memory id, gives us the hierarchy depth to store some items for it (e.g. a core segment)
67 * @param host rest server address (e.g. localhost or a specific ip or a domain)
68 * @param port network port where the rest server runs
69 */
70 RestPersistence(const std::string& identifier,
71 const std::string& exportName,
72 const std::string& host,
73 int port,
74 bool disableIfNotAvailable = false) :
75 MemoryPersistenceStrategy(identifier, exportName), host_(host), port_(port)
76 {
77 client_ = std::make_shared<httplib::Client>(host, port);
78
79 bool available = checkConnection();
80
81 if (disableIfNotAvailable && !available)
82 {
83 ARMARX_IMPORTANT << "REST Server not available. Disabling...";
84 disable();
85 }
86 }
87
88 const static std::string DEFAULT_HOST;
89 const static int DEFAULT_PORT = 8080;
90
91 std::vector<std::string> getContainerKeys(const armarx::armem::MemoryID& id) override;
92
93 std::vector<std::string> getItemKeys(const armarx::armem::MemoryID& id) override;
94
95 bool containsContainer(const armarx::armem::MemoryID& id, std::string key) override;
96
97 bool containsItem(const armarx::armem::MemoryID& id, std::string key) override;
98
100 std::string key,
101 std::vector<unsigned char>& data) override;
102
103 std::vector<unsigned char> retrieveItem(const armarx::armem::MemoryID& id,
104 std::string key) override;
105
107 const std::string& prefix) override;
108
109
110 /**
111 * Checks if the server is up
112 */
113 bool checkConnection();
114
115 private:
116 std::shared_ptr<httplib::Client> client_;
117
118 std::string host_ = "localhost";
119 int port_ = DEFAULT_PORT;
120
121
122 /* Internal rest logic */
123
124 /**
125 * Stops and restarts the client (e.g. if some parameters like the port are changed)
126 */
127 void resetClient();
128
129 bool restContainsItem(const armarx::armem::MemoryID& id, std::string& key);
130
131 bool restContainsContainer(const armarx::armem::MemoryID& id, std::string& key);
132
133 std::vector<std::string> getRestItems(const armarx::armem::MemoryID& id);
134
135 std::vector<std::string> getRestContainers(const armarx::armem::MemoryID& id);
136
137 std::vector<unsigned char> readItemFromRest(const armarx::armem::MemoryID& id,
138 std::string& key);
139
140 void writeItemToRest(const armarx::armem::MemoryID& id,
141 std::string& key,
142 const std::vector<unsigned char>& data);
143
144 void writeItemToRest(const armarx::armem::MemoryID& id,
145 std::string& key,
146 nlohmann::json& jsonData);
147
148 void writeItemToRest(const armarx::armem::MemoryID& id,
149 std::string& key,
150 DataType type,
151 const std::vector<unsigned char>& data);
152
153 void removeRestItem(const armarx::armem::MemoryID& id, std::string& key);
154
155 void setPort(int port);
156
157 int getPort() const;
158
159 void setHost(std::string& host);
160
161 std::string getHost();
162
163 /**
164 * Creates our endpoint resp. http request path using the export name and memory id
165 */
166 std::string buildPath(const armarx::armem::MemoryID& id);
167
168 std::string getMimeType(DataType type);
169 };
170} // namespace armarx::armem::server::ltm::persistence
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.
RestPersistence(const std::string &identifier, const std::string &exportName)
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.
RestPersistence(const std::string &identifier, const std::string &exportName, const std::string &host, int port, bool disableIfNotAvailable=false)
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...
Definition Logging.h:190
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.