LCOV - code coverage report
Current view: top level - model - SocketManager.cpp (source / functions) Coverage Total Hit
Test: final-coverage.info Lines: 89.1 % 46 41
Test Date: 2025-12-16 15:55:14 Functions: 90.0 % 10 9

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

Generated by: LCOV version 2.0-1