Skip to content

Commit ae15113

Browse files
committed
refactor
1 parent bff0221 commit ae15113

3 files changed

Lines changed: 107 additions & 72 deletions

File tree

CMakeLists.txt

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
# ====================================================================
2-
# Vix.cpp — Net Module
3-
# ====================================================================
1+
# @file CMakeLists.cpp
2+
# @author Gaspard Kirira
3+
#
4+
# Copyright 2025, Gaspard Kirira. All rights reserved.
5+
# https://github.com/vixcpp/vix
6+
# Use of this source code is governed by a MIT license
7+
# that can be found in the License file.
8+
#
9+
# Vix.cpp
10+
# NET MODULE
411
# Purpose:
512
# Low-level networking primitives for Vix (connectivity probing,
613
# reachability checks, lightweight network utilities). Builds as
@@ -20,20 +27,21 @@
2027
# Installs into the umbrella export-set `VixTargets`.
2128
# ====================================================================
2229

30+
2331
cmake_minimum_required(VERSION 3.20)
2432
project(vix_net VERSION 0.1.0 LANGUAGES CXX)
2533

2634
include(GNUInstallDirs)
2735

28-
# ------------------------ Global settings ----------------------------
36+
# Global settings
2937
set(CMAKE_CXX_STANDARD 20)
3038
set(CMAKE_CXX_STANDARD_REQUIRED ON)
3139
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
3240

33-
# ------------------------ Sources discovery --------------------------
41+
# Sources discovery
3442
file(GLOB_RECURSE NET_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp")
3543

36-
# ------------------------ Utils dependency (robust) ------------------
44+
# Utils dependency
3745
option(VIX_NET_FETCH_UTILS "Auto-fetch vix::utils if missing" ON)
3846

3947
if (NOT TARGET vix::utils)
@@ -45,7 +53,7 @@ if (NOT TARGET vix::utils)
4553
message(STATUS "[net] Fetching vix::utils via FetchContent")
4654
FetchContent_Declare(vix_utils
4755
GIT_REPOSITORY https://github.com/vixcpp/utils.git
48-
GIT_TAG dev
56+
GIT_TAG dev
4957
)
5058
FetchContent_MakeAvailable(vix_utils)
5159
else()
@@ -58,7 +66,7 @@ if (NOT TARGET ${VIX_UTILS_TARGET} AND TARGET vix::utils)
5866
set(VIX_UTILS_TARGET vix::utils)
5967
endif()
6068

61-
# ============================== STATIC ===============================
69+
# STATIC
6270
if (NET_SOURCES)
6371
message(STATUS "[net] Building STATIC library with detected sources.")
6472

@@ -105,7 +113,7 @@ if (NET_SOURCES)
105113
install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
106114
FILES_MATCHING PATTERN "*.hpp" PATTERN "*.h")
107115

108-
# ============================ HEADER-ONLY ============================
116+
# HEADER-ONLY
109117
else()
110118
message(STATUS "[net] Building HEADER-ONLY library (no sources).")
111119

@@ -134,7 +142,7 @@ else()
134142
FILES_MATCHING PATTERN "*.hpp" PATTERN "*.h")
135143
endif()
136144

137-
# ----------------------------- Tests ---------------------------------
145+
# Tests
138146
option(VIX_NET_BUILD_TESTS "Build net module tests" OFF)
139147

140148
if (VIX_NET_BUILD_TESTS)
@@ -143,7 +151,7 @@ if (VIX_NET_BUILD_TESTS)
143151
add_subdirectory(tests)
144152
endif()
145153

146-
# ----------------------------- Summary -------------------------------
154+
# Summary
147155
message(STATUS "------------------------------------------------------")
148156
message(STATUS "vix::net configured (${PROJECT_VERSION})")
149157
if (NET_SOURCES)

include/vix/net/NetworkProbe.hpp

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,55 @@
1-
#pragma once
1+
/**
2+
*
3+
* @file NetworkProbe.hpp
4+
* @author Gaspard Kirira
5+
*
6+
* Copyright 2025, Gaspard Kirira. All rights reserved.
7+
* https://github.com/vixcpp/vix
8+
* Use of this source code is governed by a MIT license
9+
* that can be found in the License file.
10+
*
11+
* Vix.cpp
12+
*/
13+
#ifndef VIX_NETWORK_PROBE_HPP
14+
#define VIX_NETWORK_PROBE_HPP
215

316
#include <cstdint>
417
#include <functional>
518

619
namespace vix::net
720
{
821

9-
class NetworkProbe
22+
class NetworkProbe
23+
{
24+
public:
25+
using ProbeFn = std::function<bool()>; // returns true if online
26+
27+
struct Config
1028
{
11-
public:
12-
using ProbeFn = std::function<bool()>; // returns true if online
29+
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
32+
};
1333

14-
struct Config
15-
{
16-
std::int64_t min_interval_ms{1000};
17-
std::int64_t online_ttl_ms{2000}; // cached online validity
18-
std::int64_t offline_ttl_ms{500}; // cached offline validity
19-
};
34+
NetworkProbe(Config cfg, ProbeFn fn);
2035

21-
NetworkProbe(Config cfg, ProbeFn fn);
36+
bool isOnline(std::int64_t now_ms) const;
37+
bool refresh(std::int64_t now_ms);
38+
bool lastKnownOnline() const noexcept { return last_online_; }
39+
std::int64_t lastProbeAtMs() const noexcept { return last_probe_at_ms_; }
2240

23-
bool isOnline(std::int64_t now_ms) const;
24-
bool refresh(std::int64_t now_ms);
25-
bool lastKnownOnline() const noexcept { return last_online_; }
26-
std::int64_t lastProbeAtMs() const noexcept { return last_probe_at_ms_; }
41+
private:
42+
bool canProbe(std::int64_t now_ms) const noexcept;
2743

28-
private:
29-
bool canProbe(std::int64_t now_ms) const noexcept;
44+
private:
45+
Config cfg_;
46+
ProbeFn probe_;
3047

31-
private:
32-
Config cfg_;
33-
ProbeFn probe_;
34-
35-
bool last_online_{false};
36-
std::int64_t last_probe_at_ms_{0};
37-
std::int64_t last_update_ms_{0};
38-
};
48+
bool last_online_{false};
49+
std::int64_t last_probe_at_ms_{0};
50+
std::int64_t last_update_ms_{0};
51+
};
3952

4053
} // namespace vix::net
54+
55+
#endif

src/NetworkProbe.cpp

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,64 @@
1+
/**
2+
*
3+
* @file NetworkProbe.cpp
4+
* @author Gaspard Kirira
5+
*
6+
* Copyright 2025, Gaspard Kirira. All rights reserved.
7+
* https://github.com/vixcpp/vix
8+
* Use of this source code is governed by a MIT license
9+
* that can be found in the License file.
10+
*
11+
* Vix.cpp
12+
*/
113
#include <vix/net/NetworkProbe.hpp>
214

315
namespace vix::net
416
{
517

6-
NetworkProbe::NetworkProbe(Config cfg, ProbeFn fn)
7-
: cfg_(cfg), probe_(std::move(fn))
8-
{
9-
}
18+
NetworkProbe::NetworkProbe(Config cfg, ProbeFn fn)
19+
: cfg_(cfg), probe_(std::move(fn))
20+
{
21+
}
22+
23+
bool NetworkProbe::canProbe(std::int64_t now_ms) const noexcept
24+
{
25+
return (now_ms - last_probe_at_ms_) >= cfg_.min_interval_ms;
26+
}
1027

11-
bool NetworkProbe::canProbe(std::int64_t now_ms) const noexcept
28+
bool NetworkProbe::isOnline(std::int64_t now_ms) const
29+
{
30+
const auto age = now_ms - last_update_ms_;
31+
const auto ttl = last_online_ ? cfg_.online_ttl_ms : cfg_.offline_ttl_ms;
32+
33+
if (age <= ttl)
1234
{
13-
return (now_ms - last_probe_at_ms_) >= cfg_.min_interval_ms;
35+
return last_online_;
1436
}
37+
// cache expired -> caller should call refresh()
38+
return last_online_;
39+
}
1540

16-
bool NetworkProbe::isOnline(std::int64_t now_ms) const
41+
bool NetworkProbe::refresh(std::int64_t now_ms)
42+
{
43+
if (!probe_)
1744
{
18-
const auto age = now_ms - last_update_ms_;
19-
const auto ttl = last_online_ ? cfg_.online_ttl_ms : cfg_.offline_ttl_ms;
20-
21-
if (age <= ttl)
22-
{
23-
return last_online_;
24-
}
25-
// cache expired -> caller should call refresh()
26-
return last_online_;
45+
// No probe function -> assume online (or false). Choose conservative?
46+
last_online_ = true;
47+
last_update_ms_ = now_ms;
48+
return last_online_;
2749
}
2850

29-
bool NetworkProbe::refresh(std::int64_t now_ms)
51+
if (!canProbe(now_ms))
3052
{
31-
if (!probe_)
32-
{
33-
// No probe function -> assume online (or false). Choose conservative?
34-
last_online_ = true;
35-
last_update_ms_ = now_ms;
36-
return last_online_;
37-
}
38-
39-
if (!canProbe(now_ms))
40-
{
41-
// Too soon: return cached
42-
return last_online_;
43-
}
44-
45-
last_probe_at_ms_ = now_ms;
46-
const bool ok = probe_();
47-
last_online_ = ok;
48-
last_update_ms_ = now_ms;
49-
return last_online_;
53+
// Too soon: return cached
54+
return last_online_;
5055
}
5156

57+
last_probe_at_ms_ = now_ms;
58+
const bool ok = probe_();
59+
last_online_ = ok;
60+
last_update_ms_ = now_ms;
61+
return last_online_;
62+
}
63+
5264
} // namespace vix::net

0 commit comments

Comments
 (0)