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 <memory>
39 : #include <string>
40 : #include <vector>
41 : #include <regex>
42 : #include <cstdint>
43 : #include <atomic>
44 :
45 : #include <nlohmann/json.hpp>
46 :
47 : #include <AdminSchema.hpp>
48 : #include <Transformation.hpp>
49 : #include <TypeConverter.hpp>
50 : #include <DataPart.hpp>
51 :
52 : #include <MyTrafficHttp2Client.hpp>
53 :
54 : #include <ert/metrics/Metrics.hpp>
55 :
56 :
57 :
58 : namespace h2agent
59 : {
60 : namespace model
61 : {
62 :
63 : // Client endpoint key:
64 : typedef std::string admin_client_endpoint_key_t;
65 :
66 : class AdminClientEndpoint
67 : {
68 : nlohmann::json json_{}; // client endpoint reference
69 :
70 : admin_client_endpoint_key_t key_{};
71 :
72 : // Cached information:
73 : std::string host_{};
74 : int port_{};
75 : bool secure_{};
76 : bool permit_{};
77 :
78 : std::shared_ptr<h2agent::http2::MyTrafficHttp2Client> client_{};
79 : std::vector<std::shared_ptr<h2agent::http2::MyTrafficHttp2Client>> clients_{}; // worker clients (index 0 == client_)
80 : std::atomic<std::uint64_t> send_seq_{0};
81 : size_t num_workers_{1};
82 :
83 : // Metrics for client:
84 : std::string application_name_{};
85 : ert::metrics::Metrics *metrics_{};
86 : ert::metrics::bucket_boundaries_t response_delay_seconds_histogram_bucket_boundaries_{};
87 : ert::metrics::bucket_boundaries_t message_size_bytes_histogram_bucket_boundaries_{};
88 :
89 : public:
90 :
91 23 : AdminClientEndpoint() {}
92 23 : ~AdminClientEndpoint() {}
93 :
94 : // setters:
95 :
96 : /**
97 : * Load client endpoint information
98 : *
99 : * @param j Json client endpoint object
100 : *
101 : * @return Operation success
102 : */
103 : bool load(const nlohmann::json &j);
104 :
105 : /**
106 : * Connects the endpoint
107 : *
108 : * @param fromScratch Indicates if the previous client must be dropped
109 : * @param numWorkers Number of worker connections to create
110 : */
111 : void connect(bool fromScratch = false, size_t numWorkers = 1);
112 :
113 : /**
114 : * Set metrics data
115 : *
116 : * @param metrics Optional metrics object to compute counters and histograms
117 : * @param responseDelaySecondsHistogramBucketBoundaries Optional bucket boundaries for response delay seconds histogram
118 : * @param messageSizeBytesHistogramBucketBoundaries Optional bucket boundaries for message size bytes histogram
119 : * @param applicationName Application/process name used for metrics source label
120 : */
121 : void setMetricsData(ert::metrics::Metrics *metrics, const ert::metrics::bucket_boundaries_t &responseDelaySecondsHistogramBucketBoundaries,
122 : const ert::metrics::bucket_boundaries_t &messageSizeBytesHistogramBucketBoundaries, const std::string &applicationName);
123 :
124 : // getters:
125 :
126 : /**
127 : * Gets the client endpoint key
128 : *
129 : * @return Client endpoint key
130 : */
131 20 : const admin_client_endpoint_key_t &getKey() const {
132 20 : return key_;
133 : }
134 :
135 : /**
136 : * Json for class information and also current connection status
137 : *
138 : * @return Json object
139 : */
140 : nlohmann::json asJson() const;
141 :
142 : /**
143 : * Configured host
144 : *
145 : * @return Host
146 : */
147 11 : const std::string &getHost() const {
148 11 : return host_;
149 : }
150 :
151 : /**
152 : * Configured port
153 : *
154 : * @return Port
155 : */
156 11 : int getPort() const {
157 11 : return port_;
158 : }
159 :
160 : /*
161 : * Configured secure field
162 : *
163 : * @return Secure boolean
164 : */
165 7 : bool getSecure() const {
166 7 : return secure_;
167 : }
168 :
169 : /*
170 : * Configured permit field
171 : *
172 : * @return Permit boolean
173 : */
174 1 : bool getPermit() const {
175 1 : return permit_;
176 : }
177 :
178 : /*
179 : * Get the associated http2 client for a specific worker
180 : *
181 : * @param workerIndex Worker index (0-based), defaults to 0
182 : * @return Http2 client
183 : */
184 0 : std::shared_ptr<h2agent::http2::MyTrafficHttp2Client> getClient(size_t workerIndex = 0) {
185 0 : if (workerIndex < clients_.size()) return clients_[workerIndex];
186 0 : return client_;
187 : }
188 :
189 : /*
190 : * Get number of worker connections
191 : *
192 : * @return Number of workers
193 : */
194 0 : size_t getNumWorkers() const {
195 0 : return clients_.empty() ? (client_ ? 1 : 0) : clients_.size();
196 : }
197 :
198 : /*
199 : * Increment send sequence (thread-safe)
200 : *
201 : * @return New sequence value
202 : */
203 0 : std::uint64_t incrementSendSeq() {
204 0 : return ++send_seq_;
205 : }
206 :
207 : /*
208 : * Get send sequence
209 : *
210 : * @return Send sequence (1..N)
211 : */
212 : std::uint64_t getSendSeq() const;
213 : };
214 :
215 : }
216 : }
217 :
|