LCOV - code coverage report
Current view: top level - src/network - NetworkStateMonitor.h (source / functions) Coverage Total Hit
Test: coverage.info.cleaned Lines: 100.0 % 4 4
Test Date: 2026-01-27 22:31:25 Functions: 100.0 % 4 4

            Line data    Source code
       1              : #ifndef NETWORKSTATEMONITOR_H
       2              : #define NETWORKSTATEMONITOR_H
       3              : 
       4              : #include <QObject>
       5              : #include <QNetworkInformation>
       6              : #include <QElapsedTimer>
       7              : #include <QTimer>
       8              : 
       9              : /**
      10              :  * @brief Implements circuit breaker for network requests.
      11              :  */
      12              : class NetworkStateMonitor : public QObject
      13              : {
      14              :     Q_OBJECT
      15              : 
      16              : public:
      17              :     enum CircuitState {
      18              :         Closed,     // Good: allow requests as normal
      19              :         Open,       // Fail: block requests for now
      20              :         HalfOpen    // Check state: allow a request to test for connectivity
      21              :     };
      22              : 
      23              :     static NetworkStateMonitor& instance();
      24              : 
      25              :     /**
      26              :      * @brief Checks if network is available.
      27              :      */
      28            1 :     bool isOnline() const { return isCurrentlyOnline; }
      29              : 
      30              :     /**
      31              :      * @brief Informs online state.
      32              :      */
      33              :     bool shouldAllowRequest() const;
      34              : 
      35              :     /**
      36              :      * @brief Returns current circuit breaker state.
      37              :      */
      38           21 :     CircuitState circuitState() const { return currentCircuitState; }
      39              : 
      40              :     /**
      41              :      * @brief Records a successful network operation.
      42              :      */
      43              :     void recordSuccess();
      44              : 
      45              :     /**
      46              :      * @brief Records a failed network operation.
      47              :      */
      48              :     void recordFailure();
      49              : 
      50              :     /**
      51              :      * @brief Returns failure rate (0.0 to 1.0)
      52              :      */
      53              :     float failureRate() const;
      54              : 
      55              :     /**
      56              :      * @brief Returns total request count in current window.
      57              :      */
      58              :     int requestCount() const { return successCountMetric + failureCountMetric; }
      59              : 
      60              :     /**
      61              :      * @brief Returns success count in current window.
      62              :      */
      63            8 :     int successCount() const { return successCountMetric; }
      64              : 
      65              :     /**
      66              :      * @brief Returns failure count in current window.
      67              :      */
      68            8 :     int failureCount() const { return failureCountMetric; }
      69              : 
      70              :     /**
      71              :      * @brief USE WITH CAUTION: Manually resets the breaker.
      72              :      */
      73              :     void resetCircuit();
      74              : 
      75              :     /**
      76              :      * @brief Configures circuit breaker.
      77              :      */
      78              :     void configure(int failureThreshold, int openDuration, int windowSize);
      79              : 
      80              : signals:
      81              : 
      82              :     void networkAvailable();
      83              : 
      84              :     void networkUnavailable();
      85              : 
      86              :     void circuitOpened();
      87              : 
      88              :     void circuitClosed();
      89              : 
      90              :     void circuitHalfOpen();
      91              : 
      92              : private slots:
      93              :     void onReachabilityChanged(QNetworkInformation::Reachability reachability);
      94              :     void onCircuitTimerTimeout();
      95              :     void onWindowTimerTimeout();
      96              : 
      97              : private:
      98              :     NetworkStateMonitor();
      99              :     ~NetworkStateMonitor();
     100              :     Q_DISABLE_COPY(NetworkStateMonitor)
     101              : 
     102              :     void updateCircuitState();
     103              :     void tripCircuit();
     104              :     void closeCircuit();
     105              : 
     106              :     bool isCurrentlyOnline;
     107              :     CircuitState currentCircuitState;
     108              : 
     109              :     // Metrics
     110              :     int successCountMetric;
     111              :     int failureCountMetric;
     112              :     int halfOpenSuccessesMetric;
     113              : 
     114              :     // Config
     115              :     int failureThreshold;
     116              :     int openDuration;
     117              :     int windowSize;
     118              :     int halfOpenRequests;
     119              : 
     120              :     QTimer circuitTimer;
     121              :     QTimer windowTimer;
     122              :     QElapsedTimer windowElapsed;
     123              : };
     124              : 
     125              : #endif // NETWORKSTATEMONITOR_H
        

Generated by: LCOV version 2.0-1