Line data Source code
1 : /*
2 : ___________________________________________
3 : | _ ___ _ |
4 : | | | |__ \ | | |
5 : | | |__ ) |__ _ __ _ ___ _ __ | |_ |
6 : | | '_ \ / // _` |/ _` |/ _ \ '_ \| __| | HTTP/2 AGENT FOR MOCK TESTING
7 : | | | | |/ /| (_| | (_| | __/ | | | |_ | Version 0.0.z
8 : | |_| |_|____\__,_|\__, |\___|_| |_|\__| | https://github.com/testillano/h2agent
9 : | __/ | |
10 : | |___/ |
11 : |___________________________________________|
12 :
13 : Licensed under the MIT License <http://opensource.org/licenses/MIT>.
14 : SPDX-License-Identifier: MIT
15 : Copyright (c) 2021 Eduardo Ramos
16 :
17 : Permission is hereby granted, free of charge, to any person obtaining a copy
18 : of this software and associated documentation files (the "Software"), to deal
19 : in the Software without restriction, including without limitation the rights
20 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
21 : copies of the Software, and to permit persons to whom the Software is
22 : furnished to do so, subject to the following conditions:
23 :
24 : The above copyright notice and this permission notice shall be included in all
25 : copies or substantial portions of the Software.
26 :
27 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
30 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
32 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 : SOFTWARE.
34 : */
35 :
36 : #pragma once
37 :
38 : #include <nlohmann/json.hpp>
39 :
40 : #include <ert/metrics/Metrics.hpp>
41 :
42 : #include <Map.hpp>
43 : #include <SafeFile.hpp>
44 :
45 :
46 : namespace ert
47 : {
48 : namespace metrics
49 : {
50 : class Metrics;
51 : }
52 : }
53 :
54 : namespace h2agent
55 : {
56 : namespace model
57 : {
58 :
59 : /**
60 : * This class stores a list of safe files.
61 : */
62 : class FileManager : public Map<std::string, std::shared_ptr<SafeFile>>
63 : {
64 : mutable mutex_t rw_mutex_{};
65 : boost::asio::io_context *io_context_{};
66 :
67 : // metrics (will be passed to SafeFile):
68 : ert::metrics::Metrics *metrics_{};
69 :
70 : ert::metrics::counter_t *observed_open_operation_counter_{};
71 : ert::metrics::counter_t *observed_close_operation_counter_{};
72 : ert::metrics::counter_t *observed_write_operation_counter_{};
73 : ert::metrics::counter_t *observed_empty_operation_counter_{};
74 : ert::metrics::counter_t *observed_delayed_close_operation_counter_{};
75 : ert::metrics::counter_t *observed_instant_close_operation_counter_{};
76 : ert::metrics::counter_t *observed_error_open_operation_counter_{};
77 :
78 : bool read_cache_;
79 :
80 : public:
81 : /**
82 : * File manager class
83 : *
84 : * @param timersIoContext timers io context needed to schedule delayed close operations.
85 : * If you never schedule close operations (@see write) it may be 'nullptr'.
86 : * @param metrics underlaying reference for SafeFile in order to compute prometheus metrics
87 : * about I/O operations. It may be 'nullptr' if no metrics are enabled.
88 : *
89 : * @see SafeFile
90 : */
91 161 : FileManager(boost::asio::io_context *timersIoContext = nullptr, ert::metrics::Metrics *metrics = nullptr) : io_context_(timersIoContext), metrics_(metrics), read_cache_(false) {;}
92 158 : ~FileManager() = default;
93 :
94 : /**
95 : * Set metrics reference
96 : *
97 : * @param metrics Optional metrics object to compute counters
98 : * @param source Source label for prometheus metrics
99 : */
100 : void enableMetrics(ert::metrics::Metrics *metrics, const std::string &source);
101 :
102 : /** incrementObservedOpenOperationCounter */
103 : void incrementObservedOpenOperationCounter();
104 :
105 : /** incrementObservedCloseOperationCounter */
106 : void incrementObservedCloseOperationCounter();
107 :
108 : /** incrementObservedEmptyOperationCounter */
109 : void incrementObservedEmptyOperationCounter();
110 :
111 : /** incrementObservedWriteOperationCounter */
112 : void incrementObservedWriteOperationCounter();
113 :
114 : /** incrementObservedDelayedCloseOperationCounter */
115 : void incrementObservedDelayedCloseOperationCounter();
116 :
117 : /** incrementObservedInstantCloseOperationCounter */
118 : void incrementObservedInstantCloseOperationCounter();
119 :
120 : /** incrementObservedErrorOpenOperationCounter */
121 : void incrementObservedErrorOpenOperationCounter();
122 :
123 : /**
124 : * Write file
125 : *
126 : * @param path path file to write. Can be relative (to execution directory) or absolute.
127 : * @param data data string to write.
128 : * @param textOrBinary open file to write text (true) or binary (false) data.
129 : * @param closeDelayUs delay after last write operation, to close the file.
130 : * Zero value means that no planned close is scheduled, so the file is opened,
131 : * written and closed in the same moment.
132 : */
133 : void write(const std::string &path, const std::string &data, bool textOrBinary, unsigned int closeDelayUs);
134 :
135 : /**
136 : * Read the file content.
137 : * There is no close delay. Read transaction is atomic (open, read, close).
138 : *
139 : * @param path path file to read. Can be relative (to execution directory) or absolute.
140 : * @param data data read by reference.
141 : * @param textOrBinary open file to read text (true) or binary (false) data.
142 : *
143 : * @return Boolean about success of the read operation (first read of unexisting file, returns 'false')
144 : */
145 : bool read(const std::string &path, std::string &data, bool textOrBinary);
146 :
147 : /**
148 : * Enables read cache to speed up content load
149 : *
150 : * @param enable Boolean to enable or disable cache. File manager disables cache by default.
151 : */
152 4 : void enableReadCache(bool enable) {
153 4 : read_cache_ = enable;
154 4 : }
155 :
156 : /**
157 : * Empty file
158 : *
159 : * @param path path file to empty. Can be relative (to execution directory) or absolute.
160 : */
161 : void empty(const std::string &path);
162 :
163 : /** Clears list
164 : *
165 : * @return Boolean about success of operation (something removed, nothing removed: already empty)
166 : */
167 : bool clear();
168 :
169 : /**
170 : * Builds json document for class configuration
171 : *
172 : * @return Json object
173 : */
174 : nlohmann::json getConfigurationJson() const;
175 :
176 : /**
177 : * Json string representation for class configuration (json object)
178 : *
179 : * @return Json string representation.
180 : */
181 : std::string configurationAsJsonString() const;
182 :
183 : /**
184 : * Builds json document for class information
185 : *
186 : * @return Json object
187 : */
188 : nlohmann::json getJson() const;
189 :
190 : /**
191 : * Json string representation for class information (json object)
192 : *
193 : * @return Json string representation ('[]' for empty object).
194 : */
195 : std::string asJsonString() const;
196 : };
197 :
198 : }
199 : }
200 :
|