Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
network_system_bridge.cpp
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
7
8#if KCENON_WITH_COMMON_SYSTEM
10#include <kcenon/common/interfaces/monitoring_interface.h>
11#endif
12
13#include <algorithm>
14#include <vector>
15
17
19public:
20 Impl() = default;
21
23 if (initialized_.load()) {
24 shutdown();
25 }
26 }
27
29 std::lock_guard<std::mutex> lock(mutex_);
30
31 if (initialized_.load()) {
32 return error_void(
34 "NetworkSystemBridge already initialized",
35 "NetworkSystemBridge::initialize");
36 }
37
38 // Store configuration
40
41 // Initialize thread pool bridge if enabled
42 if (config.enable_thread_pool && thread_pool_bridge_) {
43 BridgeConfig thread_config;
44 thread_config.integration_name = config.integration_name;
45 thread_config.properties = config.thread_pool_properties;
46
47 auto result = thread_pool_bridge_->initialize(thread_config);
48 if (result.is_err()) {
49 return error_void(
51 "Failed to initialize thread pool bridge: " + result.error().message,
52 "NetworkSystemBridge::initialize");
53 }
54 initialized_bridges_.push_back("thread_pool");
55 }
56
57 // Future: Initialize logger bridge if enabled
58 if (config.enable_logger && logger_) {
59 // Logger initialization logic here
60 initialized_bridges_.push_back("logger");
61 }
62
63 // Future: Initialize monitoring bridge if enabled
64 if (config.enable_monitoring && monitoring_) {
65 // Monitoring initialization logic here
66 initialized_bridges_.push_back("monitoring");
67 }
68
69 initialized_.store(true);
70 return ok();
71 }
72
74 std::lock_guard<std::mutex> lock(mutex_);
75
76 if (!initialized_.load()) {
77 return ok(); // Idempotent
78 }
79
80 // Shutdown in reverse order
81 std::reverse(initialized_bridges_.begin(), initialized_bridges_.end());
82
83 for (const auto& bridge_name : initialized_bridges_) {
84 if (bridge_name == "thread_pool" && thread_pool_bridge_) {
85 thread_pool_bridge_->shutdown();
86 }
87 // Future: Handle other bridges
88 }
89
91 initialized_.store(false);
92 return ok();
93 }
94
95 bool is_initialized() const {
96 return initialized_.load();
97 }
98
100 std::lock_guard<std::mutex> lock(mutex_);
101
102 BridgeMetrics aggregated;
103 aggregated.is_healthy = true;
104 aggregated.last_activity = std::chrono::steady_clock::now();
105
106 // Aggregate thread pool metrics
107 if (thread_pool_bridge_ && thread_pool_bridge_->is_initialized()) {
108 auto thread_metrics = thread_pool_bridge_->get_metrics();
109 aggregated.is_healthy &= thread_metrics.is_healthy;
110
111 for (const auto& [key, value] : thread_metrics.custom_metrics) {
112 aggregated.custom_metrics["thread_pool." + key] = value;
113 }
114 }
115
116 // Future: Aggregate logger and monitoring metrics
117
118 return aggregated;
119 }
120
121 std::shared_ptr<ThreadPoolBridge> get_thread_pool_bridge() const {
122 std::lock_guard<std::mutex> lock(mutex_);
123 return thread_pool_bridge_;
124 }
125
126 std::shared_ptr<thread_pool_interface> get_thread_pool() const {
127 std::lock_guard<std::mutex> lock(mutex_);
129 return thread_pool_bridge_->get_thread_pool();
130 }
131 return nullptr;
132 }
133
134 std::shared_ptr<logger_interface> get_logger() const {
135 std::lock_guard<std::mutex> lock(mutex_);
136 return logger_;
137 }
138
139 std::shared_ptr<monitoring_interface> get_monitoring() const {
140 std::lock_guard<std::mutex> lock(mutex_);
141 return monitoring_;
142 }
143
144 VoidResult set_thread_pool_bridge(std::shared_ptr<ThreadPoolBridge> bridge) {
145 std::lock_guard<std::mutex> lock(mutex_);
146
147 if (initialized_.load()) {
148 return error_void(
150 "Cannot set thread pool bridge after initialization",
151 "NetworkSystemBridge::set_thread_pool_bridge");
152 }
153
154 if (!bridge) {
155 return error_void(
157 "Thread pool bridge cannot be null",
158 "NetworkSystemBridge::set_thread_pool_bridge");
159 }
160
161 thread_pool_bridge_ = std::move(bridge);
162 return ok();
163 }
164
165 VoidResult set_logger(std::shared_ptr<logger_interface> logger) {
166 std::lock_guard<std::mutex> lock(mutex_);
167
168 if (initialized_.load()) {
169 return error_void(
171 "Cannot set logger after initialization",
172 "NetworkSystemBridge::set_logger");
173 }
174
175 logger_ = std::move(logger);
176 return ok();
177 }
178
179 VoidResult set_monitoring(std::shared_ptr<monitoring_interface> monitoring) {
180 std::lock_guard<std::mutex> lock(mutex_);
181
182 if (initialized_.load()) {
183 return error_void(
185 "Cannot set monitoring after initialization",
186 "NetworkSystemBridge::set_monitoring");
187 }
188
189 monitoring_ = std::move(monitoring);
190 return ok();
191 }
192
193private:
194 mutable std::mutex mutex_;
195 std::atomic<bool> initialized_{false};
197 std::vector<std::string> initialized_bridges_;
198
199 std::shared_ptr<ThreadPoolBridge> thread_pool_bridge_;
200 std::shared_ptr<logger_interface> logger_;
201 std::shared_ptr<monitoring_interface> monitoring_;
202};
203
204// NetworkSystemBridge implementation
205
207 : pimpl_(std::make_unique<Impl>()) {
208}
209
210NetworkSystemBridge::NetworkSystemBridge(std::shared_ptr<ThreadPoolBridge> thread_pool)
211 : pimpl_(std::make_unique<Impl>()) {
212 pimpl_->set_thread_pool_bridge(std::move(thread_pool));
213}
214
216
218NetworkSystemBridge& NetworkSystemBridge::operator=(NetworkSystemBridge&&) noexcept = default;
219
221 if (!pimpl_) {
222 return error_void(
224 "Bridge has been moved",
225 "NetworkSystemBridge::initialize");
226 }
227 return pimpl_->initialize(config);
228}
229
231 if (!pimpl_) {
232 return ok(); // Already moved or destroyed
233 }
234 return pimpl_->shutdown();
235}
236
238 if (!pimpl_) {
239 return false;
240 }
241 return pimpl_->is_initialized();
242}
243
245 if (!pimpl_) {
246 return BridgeMetrics{};
247 }
248 return pimpl_->get_metrics();
249}
250
251std::shared_ptr<ThreadPoolBridge> NetworkSystemBridge::get_thread_pool_bridge() const {
252 if (!pimpl_) {
253 return nullptr;
254 }
255 return pimpl_->get_thread_pool_bridge();
256}
257
258std::shared_ptr<thread_pool_interface> NetworkSystemBridge::get_thread_pool() const {
259 if (!pimpl_) {
260 return nullptr;
261 }
262 return pimpl_->get_thread_pool();
263}
264
265std::shared_ptr<logger_interface> NetworkSystemBridge::get_logger() const {
266 if (!pimpl_) {
267 return nullptr;
268 }
269 return pimpl_->get_logger();
270}
271
272std::shared_ptr<monitoring_interface> NetworkSystemBridge::get_monitoring() const {
273 if (!pimpl_) {
274 return nullptr;
275 }
276 return pimpl_->get_monitoring();
277}
278
280 if (!pimpl_) {
281 return error_void(
283 "Bridge has been moved",
284 "NetworkSystemBridge::set_thread_pool_bridge");
285 }
286 return pimpl_->set_thread_pool_bridge(std::move(bridge));
287}
288
289VoidResult NetworkSystemBridge::set_logger(std::shared_ptr<logger_interface> logger) {
290 if (!pimpl_) {
291 return error_void(
293 "Bridge has been moved",
294 "NetworkSystemBridge::set_logger");
295 }
296 return pimpl_->set_logger(std::move(logger));
297}
298
299VoidResult NetworkSystemBridge::set_monitoring(std::shared_ptr<monitoring_interface> monitoring) {
300 if (!pimpl_) {
301 return error_void(
303 "Bridge has been moved",
304 "NetworkSystemBridge::set_monitoring");
305 }
306 return pimpl_->set_monitoring(std::move(monitoring));
307}
308
309// Factory Methods
310
311std::shared_ptr<NetworkSystemBridge> NetworkSystemBridge::create_default() {
312 auto bridge = std::make_shared<NetworkSystemBridge>();
313
314 // Try to create thread pool from thread_system if available
315#if KCENON_WITH_THREAD_SYSTEM
316 auto thread_pool_bridge = ThreadPoolBridge::from_thread_system("network_pool");
317 bridge->set_thread_pool_bridge(thread_pool_bridge);
318#endif
319
320 return bridge;
321}
322
323std::shared_ptr<NetworkSystemBridge> NetworkSystemBridge::with_thread_system(
324 const std::string& pool_name) {
325 auto bridge = std::make_shared<NetworkSystemBridge>();
326
327#if KCENON_WITH_THREAD_SYSTEM
328 auto thread_pool_bridge = ThreadPoolBridge::from_thread_system(pool_name);
329 bridge->set_thread_pool_bridge(thread_pool_bridge);
330#endif
331
332 return bridge;
333}
334
335#if KCENON_WITH_COMMON_SYSTEM
336std::shared_ptr<NetworkSystemBridge> NetworkSystemBridge::with_common_system(
337 std::shared_ptr<::kcenon::common::interfaces::IExecutor> executor,
338 std::shared_ptr<::kcenon::common::interfaces::ILogger> logger,
339 std::shared_ptr<::kcenon::common::interfaces::IMonitor> monitor) {
340
341 auto bridge = std::make_shared<NetworkSystemBridge>();
342
343 // Set up thread pool bridge
344 if (executor) {
345 auto thread_pool_bridge = ThreadPoolBridge::from_common_system(executor);
346 bridge->set_thread_pool_bridge(thread_pool_bridge);
347 }
348
349 // Set up logger
350 if (logger) {
351 auto logger_adapter = std::make_shared<common_logger_adapter>(logger);
352 bridge->set_logger(logger_adapter);
353 }
354
355 // Set up monitoring
356 if (monitor) {
357 auto monitoring_adapter = std::make_shared<common_monitoring_adapter>(monitor);
358 bridge->set_monitoring(monitoring_adapter);
359 }
360
361 return bridge;
362}
363#endif
364
365std::shared_ptr<NetworkSystemBridge> NetworkSystemBridge::with_custom(
366 std::shared_ptr<thread_pool_interface> thread_pool,
367 std::shared_ptr<logger_interface> logger,
368 std::shared_ptr<monitoring_interface> monitoring) {
369
370 auto bridge = std::make_shared<NetworkSystemBridge>();
371
372 if (thread_pool) {
373 auto thread_pool_bridge = std::make_shared<ThreadPoolBridge>(
375 bridge->set_thread_pool_bridge(thread_pool_bridge);
376 }
377
378 if (logger) {
379 bridge->set_logger(logger);
380 }
381
382 if (monitoring) {
383 bridge->set_monitoring(monitoring);
384 }
385
386 return bridge;
387}
388
389} // namespace kcenon::network::integration
std::shared_ptr< thread_pool_interface > get_thread_pool() const
std::shared_ptr< monitoring_interface > get_monitoring() const
VoidResult initialize(const NetworkSystemBridgeConfig &config)
VoidResult set_thread_pool_bridge(std::shared_ptr< ThreadPoolBridge > bridge)
std::shared_ptr< logger_interface > get_logger() const
VoidResult set_monitoring(std::shared_ptr< monitoring_interface > monitoring)
std::shared_ptr< ThreadPoolBridge > get_thread_pool_bridge() const
VoidResult set_logger(std::shared_ptr< logger_interface > logger)
Unified facade for all network_system integration bridges.
static std::shared_ptr< NetworkSystemBridge > create_default()
Create bridge with default configuration.
std::shared_ptr< thread_pool_interface > get_thread_pool() const
Get thread pool interface.
std::shared_ptr< logger_interface > get_logger() const
Get logger interface.
static std::shared_ptr< NetworkSystemBridge > with_custom(std::shared_ptr< thread_pool_interface > thread_pool=nullptr, std::shared_ptr< logger_interface > logger=nullptr, std::shared_ptr< monitoring_interface > monitoring=nullptr)
Create bridge with custom components.
VoidResult set_thread_pool_bridge(std::shared_ptr< ThreadPoolBridge > bridge)
Set custom thread pool bridge.
bool is_initialized() const
Check if the bridge is initialized.
std::shared_ptr< ThreadPoolBridge > get_thread_pool_bridge() const
Get thread pool bridge.
BridgeMetrics get_metrics() const
Get aggregated metrics from all bridges.
static std::shared_ptr< NetworkSystemBridge > with_thread_system(const std::string &pool_name="network_pool")
Create bridge with thread_system integration.
std::shared_ptr< monitoring_interface > get_monitoring() const
Get monitoring interface.
VoidResult set_monitoring(std::shared_ptr< monitoring_interface > monitoring)
Set custom monitoring.
VoidResult set_logger(std::shared_ptr< logger_interface > logger)
Set custom logger.
static std::shared_ptr< ThreadPoolBridge > from_thread_system(const std::string &pool_name="network_pool")
Create bridge from thread_system.
Adapter for common_system integration.
tracing_config config
Definition exporters.cpp:29
VoidResult error_void(int code, const std::string &message, const std::string &source="network_system", const std::string &details="")
VoidResult ok()
VoidResult initialize()
Initialize the network system with default production configuration.
Unified facade for all network_system integration bridges.
Configuration for bridge initialization.
std::map< std::string, std::string > properties
Key-value properties for bridge-specific configuration.
std::string integration_name
Name identifying the external system being integrated.
Metrics and health information for a bridge.
std::chrono::steady_clock::time_point last_activity
Timestamp of last activity or health check.
std::map< std::string, double > custom_metrics
Bridge-specific custom metrics.
bool is_healthy
Overall health status of the bridge.
Adapter that bridges thread_system::thread_pool to thread_pool_interface.