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 <SafeSocket.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 SocketManager : public Map<std::string, std::shared_ptr<SafeSocket>>
63 : {
64 : boost::asio::io_context *io_context_{};
65 :
66 : // metrics (will be passed to SafeSocket):
67 : ert::metrics::Metrics *metrics_{};
68 :
69 : ert::metrics::counter_t *observed_open_operation_counter_{};
70 : ert::metrics::counter_t *observed_write_operation_counter_{};
71 : ert::metrics::counter_t *observed_delayed_write_operation_counter_{};
72 : ert::metrics::counter_t *observed_instant_write_operation_counter_{};
73 : ert::metrics::counter_t *observed_error_open_operation_counter_{};
74 :
75 : public:
76 : using KeyType = std::string;
77 : using ValueType = std::shared_ptr<SafeSocket>;
78 :
79 : /**
80 : * Socket manager class
81 : *
82 : * @param timersIoContext timers io context needed to schedule delayed write operations.
83 : * If you never schedule write operations (@see write) it may be 'nullptr'.
84 : * @param metrics underlaying reference for SafeSocket in order to compute prometheus metrics
85 : * about I/O operations. It may be 'nullptr' if no metrics are enabled.
86 : *
87 : * @see SafeSocket
88 : */
89 160 : SocketManager(boost::asio::io_context *timersIoContext = nullptr, ert::metrics::Metrics *metrics = nullptr) : io_context_(timersIoContext), metrics_(metrics) {;}
90 158 : ~SocketManager() = default;
91 :
92 : /**
93 : * Set metrics reference
94 : *
95 : * @param metrics Optional metrics object to compute counters
96 : * @param source Source label for prometheus metrics
97 : */
98 : void enableMetrics(ert::metrics::Metrics *metrics, const std::string &source);
99 :
100 :
101 : /** incrementObservedOpenOperationCounter */
102 : void incrementObservedOpenOperationCounter();
103 :
104 : /** incrementObservedWriteOperationCounter */
105 : void incrementObservedWriteOperationCounter();
106 :
107 : /** incrementObservedDelayedWriteOperationCounter */
108 : void incrementObservedDelayedWriteOperationCounter();
109 :
110 : /** incrementObservedInstantWriteOperationCounter */
111 : void incrementObservedInstantWriteOperationCounter();
112 :
113 : /** incrementObservedErrorOpenOperationCounter */
114 : void incrementObservedErrorOpenOperationCounter();
115 :
116 : /**
117 : * Write socket
118 : *
119 : * @param path path file to write. Can be relative (to execution directory) or absolute.
120 : * @param data data string to write.
121 : * @param writeDelayUs delay for write operation.
122 : * Zero value means that no planned write is scheduled, so the socket is written instantly.
123 : */
124 : void write(const std::string &path, const std::string &data, unsigned int writeDelayUs);
125 :
126 : /**
127 : * Builds json document for class configuration
128 : *
129 : * @return Json object
130 : */
131 : nlohmann::json getConfigurationJson() const;
132 :
133 : /**
134 : * Builds json document for class information
135 : *
136 : * @return Json object
137 : */
138 : nlohmann::json getJson() const;
139 :
140 : /**
141 : * Json string representation for class information (json object)
142 : *
143 : * @return Json string representation ('[]' for empty object).
144 : */
145 : std::string asJsonString() const;
146 : };
147 :
148 : }
149 : }
150 :
|