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 : #include <memory>
37 :
38 : #include <ert/tracing/Logger.hpp>
39 :
40 : #include <SocketManager.hpp>
41 :
42 : namespace h2agent
43 : {
44 : namespace model
45 : {
46 :
47 :
48 1 : void SocketManager::enableMetrics(ert::metrics::Metrics *metrics, const std::string &source) {
49 :
50 1 : metrics_ = metrics;
51 :
52 1 : if (metrics_) {
53 0 : ert::metrics::labels_t familyLabels = {{"source", source}};
54 :
55 0 : ert::metrics::counter_family_t& cf = metrics->addCounterFamily("h2agent_socket_manager_operations_counter", "UDP socket operations counter in h2agent_socket_manager", familyLabels);
56 0 : observed_open_operation_counter_ = &(cf.Add({{"operation", "open"}}));
57 0 : observed_write_operation_counter_ = &(cf.Add({{"operation", "write"}}));
58 0 : observed_delayed_write_operation_counter_ = &(cf.Add({{"operation", "delayedWrite"}}));
59 0 : observed_instant_write_operation_counter_ = &(cf.Add({{"operation", "instantWrite"}}));
60 0 : observed_error_open_operation_counter_ = &(cf.Add({{"result", "failed"}, {"operation", "open"}}));
61 0 : }
62 1 : }
63 :
64 5 : void SocketManager::incrementObservedOpenOperationCounter() {
65 5 : if (metrics_) observed_open_operation_counter_->Increment();
66 5 : }
67 :
68 6 : void SocketManager::incrementObservedWriteOperationCounter() {
69 6 : if (metrics_) observed_write_operation_counter_->Increment();
70 6 : }
71 :
72 1 : void SocketManager::incrementObservedDelayedWriteOperationCounter() {
73 1 : if (metrics_) observed_delayed_write_operation_counter_->Increment();
74 1 : }
75 :
76 5 : void SocketManager::incrementObservedInstantWriteOperationCounter() {
77 5 : if (metrics_) observed_instant_write_operation_counter_->Increment();
78 5 : }
79 :
80 0 : void SocketManager::incrementObservedErrorOpenOperationCounter() {
81 0 : if (metrics_) observed_error_open_operation_counter_->Increment();
82 0 : }
83 :
84 4 : void SocketManager::write(const std::string &path, const std::string &data, unsigned int writeDelayUs) {
85 :
86 4 : std::shared_ptr<SafeSocket> safeSocket;
87 4 : bool exists{};
88 4 : auto result = get(path, exists);
89 4 : if (exists) {
90 1 : safeSocket = result;
91 : }
92 : else {
93 3 : safeSocket = std::make_shared<SafeSocket>(this, path, io_context_);
94 3 : add(path, safeSocket);
95 : }
96 :
97 4 : safeSocket->write(data, writeDelayUs);
98 4 : }
99 :
100 0 : nlohmann::json SocketManager::getJson() const {
101 :
102 0 : nlohmann::json result;
103 :
104 0 : this->forEach([&](const KeyType& k, const ValueType& value) {
105 0 : result.push_back(value->getJson());
106 0 : });
107 :
108 0 : return result;
109 0 : }
110 :
111 0 : std::string SocketManager::asJsonString() const {
112 :
113 0 : return ((size() != 0) ? getJson().dump() : "[]");
114 : }
115 :
116 :
117 : }
118 : }
119 :
|