Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
common_system_adapter.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2021-2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#pragma once
6
8
14#if KCENON_WITH_COMMON_SYSTEM
15#include <kcenon/common/interfaces/executor_interface.h>
16#include <kcenon/common/interfaces/logger_interface.h>
17#include <kcenon/common/interfaces/monitoring_interface.h>
18#include <kcenon/common/patterns/result.h>
22#endif
23
24#include <chrono>
25#include <future>
26#include <functional>
27#include <map>
28#include <memory>
29#include <stdexcept>
30#include <unordered_map>
31
33
34#if KCENON_WITH_COMMON_SYSTEM
35
36namespace detail {
37inline std::future<void> make_error_future(const std::string& message) {
38 std::promise<void> promise;
39 promise.set_exception(std::make_exception_ptr(std::runtime_error(message)));
40 auto future = promise.get_future();
41 return future;
42}
43} // namespace detail
44
45class executor_job final : public ::kcenon::common::interfaces::IJob {
46public:
47 explicit executor_job(std::function<void()> task, std::string name = "network_job")
48 : task_(std::move(task)), name_(std::move(name)) {}
49
50 ::kcenon::common::VoidResult execute() override {
51 try {
52 if (task_) {
53 task_();
54 }
55 return ::kcenon::common::ok();
56 } catch (const std::exception& ex) {
57 return ::kcenon::common::VoidResult(::kcenon::common::error_info{
58 ::kcenon::common::error_codes::INTERNAL_ERROR,
59 ex.what(),
60 "network_system"});
61 }
62 }
63
64 std::string get_name() const override { return name_; }
65
66private:
67 std::function<void()> task_;
68 std::string name_;
69};
70
71class common_thread_pool_adapter : public thread_pool_interface {
72public:
73 explicit common_thread_pool_adapter(
74 std::shared_ptr<::kcenon::common::interfaces::IExecutor> executor)
75 : executor_(std::move(executor)) {}
76
77 std::future<void> submit(std::function<void()> task) override {
78 if (!executor_) {
79 auto future = detail::make_error_future("Executor not initialized");
80 return future;
81 }
82 auto result = executor_->execute(std::make_unique<executor_job>(std::move(task)));
83 if (result.is_err()) {
84 auto future = detail::make_error_future(result.error().message);
85 return future;
86 }
87 auto future = std::move(result.value());
88 return future;
89 }
90
91 std::future<void> submit_delayed(
92 std::function<void()> task,
93 std::chrono::milliseconds delay) override {
94 if (!executor_) {
95 auto future = detail::make_error_future("Executor not initialized");
96 return future;
97 }
98 auto result = executor_->execute_delayed(
99 std::make_unique<executor_job>(std::move(task)), delay);
100 if (result.is_err()) {
101 auto future = detail::make_error_future(result.error().message);
102 return future;
103 }
104 auto future = std::move(result.value());
105 return future;
106 }
107
108 size_t worker_count() const override {
109 return executor_ ? executor_->worker_count() : 0;
110 }
111
112 bool is_running() const override {
113 return executor_ ? executor_->is_running() : false;
114 }
115
116 size_t pending_tasks() const override {
117 return executor_ ? executor_->pending_tasks() : 0;
118 }
119
120 void shutdown(bool wait_for_tasks = true) {
121 if (executor_) {
122 executor_->shutdown(wait_for_tasks);
123 }
124 }
125
126private:
127 std::shared_ptr<::kcenon::common::interfaces::IExecutor> executor_;
128};
129
130inline ::kcenon::common::interfaces::log_level to_common_log_level(log_level level) {
131 using common_level = ::kcenon::common::interfaces::log_level;
132 switch (level) {
133 case log_level::trace: return common_level::trace;
134 case log_level::debug: return common_level::debug;
135 case log_level::info: return common_level::info;
136 case log_level::warn: return common_level::warning;
137 case log_level::error: return common_level::error;
138 case log_level::fatal: return common_level::critical;
139 default: return common_level::info;
140 }
141}
142
143class common_logger_adapter : public logger_interface {
144public:
145 explicit common_logger_adapter(std::shared_ptr<::kcenon::common::interfaces::ILogger> logger)
146 : logger_(std::move(logger)) {}
147
148 void log(log_level level, const std::string& message) override {
149 if (!logger_) {
150 return;
151 }
152 logger_->log(to_common_log_level(level), message);
153 }
154
155 void log(log_level level, const std::string& message,
156 const std::string& file, int line,
157 const std::string& function) override {
158 if (!logger_) {
159 return;
160 }
161 ::kcenon::common::interfaces::log_entry entry;
162 entry.level = to_common_log_level(level);
163 entry.message = message;
164 entry.file = file;
165 entry.line = line;
166 entry.function = function;
167 entry.timestamp = std::chrono::system_clock::now();
168 logger_->log(entry);
169 }
170
171 bool is_level_enabled(log_level level) const override {
172 return logger_
173 ? logger_->is_enabled(to_common_log_level(level))
174 : false;
175 }
176
177 void flush() override {
178 if (logger_) {
179 logger_->flush();
180 }
181 }
182
183private:
184 std::shared_ptr<::kcenon::common::interfaces::ILogger> logger_;
185};
186
187class common_monitoring_adapter : public monitoring_interface {
188public:
189 explicit common_monitoring_adapter(std::shared_ptr<::kcenon::common::interfaces::IMonitor> monitor)
190 : monitor_(std::move(monitor)) {}
191
192 void report_counter(const std::string& name, double value,
193 const std::map<std::string, std::string>& labels = {}) override {
194 record_with_type(name, value, "counter", labels);
195 }
196
197 void report_gauge(const std::string& name, double value,
198 const std::map<std::string, std::string>& labels = {}) override {
199 record_with_type(name, value, "gauge", labels);
200 }
201
202 void report_histogram(const std::string& name, double value,
203 const std::map<std::string, std::string>& labels = {}) override {
204 record_with_type(name, value, "histogram", labels);
205 }
206
207 void report_health(const std::string& connection_id, bool is_alive,
208 double response_time_ms, size_t missed_heartbeats,
209 double packet_loss_rate) override {
210 if (!monitor_) {
211 return;
212 }
213 std::unordered_map<std::string, std::string> tags{
214 {"connection_id", connection_id}
215 };
216 monitor_->record_metric("network.connection.alive", is_alive ? 1.0 : 0.0, tags);
217 monitor_->record_metric("network.connection.rtt_ms", response_time_ms, tags);
218 monitor_->record_metric("network.connection.missed_heartbeats",
219 static_cast<double>(missed_heartbeats), tags);
220 monitor_->record_metric("network.connection.packet_loss",
221 packet_loss_rate, tags);
222 }
223
224private:
225 void record_with_type(const std::string& name,
226 double value,
227 const std::string& type,
228 const std::map<std::string, std::string>& labels) {
229 if (!monitor_) {
230 return;
231 }
232 std::unordered_map<std::string, std::string> enriched(labels.begin(), labels.end());
233 enriched["metric_type"] = type;
234 monitor_->record_metric(name, value, enriched);
235 }
236
237 std::shared_ptr<::kcenon::common::interfaces::IMonitor> monitor_;
238};
239
240#endif // KCENON_WITH_COMMON_SYSTEM
241
242} // namespace kcenon::network::integration
243
244// Backward compatibility namespace alias is defined in thread_integration.h
Feature flags for network_system.
Logger system integration interface for network_system.
Monitoring system integration interface for network_system.
VoidResult shutdown()
Shutdown the network system.
Thread system integration interface for network_system.