|
3 | 3 | * @file NetworkProbe.hpp |
4 | 4 | * @author Gaspard Kirira |
5 | 5 | * |
6 | | - * Copyright 2025, Gaspard Kirira. All rights reserved. |
| 6 | + * Copyright 2025, Gaspard Kirira. |
| 7 | + * All rights reserved. |
7 | 8 | * https://github.com/vixcpp/vix |
| 9 | + * |
8 | 10 | * Use of this source code is governed by a MIT license |
9 | 11 | * that can be found in the License file. |
10 | 12 | * |
|
18 | 20 |
|
19 | 21 | namespace vix::net |
20 | 22 | { |
21 | | - |
| 23 | + /** |
| 24 | + * @brief Lightweight network connectivity probe with caching. |
| 25 | + * |
| 26 | + * NetworkProbe encapsulates a user-provided probe function and adds: |
| 27 | + * - rate limiting (minimum probe interval) |
| 28 | + * - cached online/offline state with separate TTLs |
| 29 | + * - monotonic-time based decisions |
| 30 | + * |
| 31 | + * It is designed to be used by offline-first systems to avoid excessive |
| 32 | + * network checks while still reacting quickly to connectivity changes. |
| 33 | + */ |
22 | 34 | class NetworkProbe |
23 | 35 | { |
24 | 36 | public: |
| 37 | + /** |
| 38 | + * @brief Probe function type. |
| 39 | + * |
| 40 | + * The function should return true if the network is considered online, |
| 41 | + * false otherwise. |
| 42 | + */ |
25 | 43 | using ProbeFn = std::function<bool()>; // returns true if online |
26 | 44 |
|
| 45 | + /** |
| 46 | + * @brief Configuration for the network probe. |
| 47 | + */ |
27 | 48 | struct Config |
28 | 49 | { |
| 50 | + /** |
| 51 | + * @brief Minimum interval between two actual probe executions. |
| 52 | + */ |
29 | 53 | std::int64_t min_interval_ms{1000}; |
30 | | - std::int64_t online_ttl_ms{2000}; // cached online validity |
31 | | - std::int64_t offline_ttl_ms{500}; // cached offline validity |
| 54 | + |
| 55 | + /** |
| 56 | + * @brief Time-to-live for a cached "online" result. |
| 57 | + */ |
| 58 | + std::int64_t online_ttl_ms{2000}; |
| 59 | + |
| 60 | + /** |
| 61 | + * @brief Time-to-live for a cached "offline" result. |
| 62 | + */ |
| 63 | + std::int64_t offline_ttl_ms{500}; |
32 | 64 | }; |
33 | 65 |
|
| 66 | + /** |
| 67 | + * @brief Construct a NetworkProbe. |
| 68 | + * |
| 69 | + * @param cfg Probe configuration. |
| 70 | + * @param fn Probe function invoked to test connectivity. |
| 71 | + */ |
34 | 72 | NetworkProbe(Config cfg, ProbeFn fn); |
35 | 73 |
|
| 74 | + /** |
| 75 | + * @brief Check whether the network is currently considered online. |
| 76 | + * |
| 77 | + * This method may return a cached result if the TTL has not expired. |
| 78 | + * |
| 79 | + * @param now_ms Current time in milliseconds. |
| 80 | + * @return true if online, false otherwise. |
| 81 | + */ |
36 | 82 | bool isOnline(std::int64_t now_ms) const; |
| 83 | + |
| 84 | + /** |
| 85 | + * @brief Force a probe refresh if allowed by rate limiting. |
| 86 | + * |
| 87 | + * @param now_ms Current time in milliseconds. |
| 88 | + * @return Latest online state. |
| 89 | + */ |
37 | 90 | bool refresh(std::int64_t now_ms); |
| 91 | + |
| 92 | + /** |
| 93 | + * @brief Return the last known online state. |
| 94 | + * |
| 95 | + * This does not trigger a probe. |
| 96 | + */ |
38 | 97 | bool lastKnownOnline() const noexcept { return last_online_; } |
| 98 | + |
| 99 | + /** |
| 100 | + * @brief Timestamp of the last probe attempt. |
| 101 | + */ |
39 | 102 | std::int64_t lastProbeAtMs() const noexcept { return last_probe_at_ms_; } |
40 | 103 |
|
41 | 104 | private: |
| 105 | + /** |
| 106 | + * @brief Check whether a new probe is allowed at the given time. |
| 107 | + */ |
42 | 108 | bool canProbe(std::int64_t now_ms) const noexcept; |
43 | 109 |
|
44 | 110 | private: |
| 111 | + /** |
| 112 | + * @brief Stored probe configuration. |
| 113 | + */ |
45 | 114 | Config cfg_; |
| 115 | + |
| 116 | + /** |
| 117 | + * @brief User-provided probe function. |
| 118 | + */ |
46 | 119 | ProbeFn probe_; |
47 | 120 |
|
| 121 | + /** |
| 122 | + * @brief Last known online state. |
| 123 | + */ |
48 | 124 | bool last_online_{false}; |
| 125 | + |
| 126 | + /** |
| 127 | + * @brief Timestamp of the last probe execution. |
| 128 | + */ |
49 | 129 | std::int64_t last_probe_at_ms_{0}; |
| 130 | + |
| 131 | + /** |
| 132 | + * @brief Timestamp of the last cached state update. |
| 133 | + */ |
50 | 134 | std::int64_t last_update_ms_{0}; |
51 | 135 | }; |
52 | 136 |
|
53 | 137 | } // namespace vix::net |
54 | 138 |
|
55 | | -#endif |
| 139 | +#endif // VIX_NETWORK_PROBE_HPP |
0 commit comments