Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
network_system.cpp
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024-2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
6
12
13#if KCENON_WITH_MONITORING_SYSTEM
15#endif
16
17#include <atomic>
18
19namespace kcenon::network {
20
24
25static std::atomic<bool> g_initialized{false};
26
28
30 if (g_initialized.load()) {
31 NETWORK_LOG_WARN("[network_system] Already initialized");
32 return error_void(already_exists, "Network system already initialized", "network_system");
33 }
34
35 try {
37
38 // Initialize thread pool
39 if (config.thread_pool.worker_count >= 0) {
40 auto thread_pool = std::make_shared<integration::basic_thread_pool>(
41 config.thread_pool.worker_count == 0
42 ? std::thread::hardware_concurrency()
43 : config.thread_pool.worker_count);
44 ctx.set_thread_pool(thread_pool);
45 ctx.initialize(config.thread_pool.worker_count);
46 }
47
48 // Initialize logger
49 // Issue #285: Uses common_system_logger_adapter when available, basic_logger otherwise
50#if KCENON_WITH_COMMON_SYSTEM
51 auto logger = std::make_shared<integration::common_system_logger_adapter>();
52#else
53 auto logger = std::make_shared<integration::basic_logger>(config.logger.min_level);
54#endif
55 ctx.set_logger(logger);
57
58 // Initialize monitoring
59#if KCENON_WITH_MONITORING_SYSTEM
60 if (config.monitoring.enabled) {
61 auto monitoring =
62 std::make_shared<integration::monitoring_system_adapter>(
63 config.monitoring.service_name);
64 // Note: monitoring_system_adapter needs start() method
65 // monitoring->start();
66 ctx.set_monitoring(monitoring);
67 }
68#else
69 (void)config.monitoring; // Suppress unused parameter warning
70#endif
71
72 g_initialized.store(true);
73 NETWORK_LOG_INFO("[network_system] Initialized successfully");
74
75 return ok();
76 } catch (const std::exception &e) {
77 NETWORK_LOG_ERROR("[network_system] Initialization failed: " +
78 std::string(e.what()));
79 return error_void(internal_error, std::string("Initialization failed: ") + e.what(), "network_system");
80 }
81}
82
83VoidResult initialize(const config::network_system_config &config_with_dependencies) {
84#if KCENON_WITH_COMMON_SYSTEM
86
87 if (config_with_dependencies.executor) {
88 auto pool_adapter =
89 std::make_shared<integration::common_thread_pool_adapter>(
90 config_with_dependencies.executor);
91 ctx.set_thread_pool(pool_adapter);
92 }
93
94 if (config_with_dependencies.logger) {
95 auto logger_adapter = std::make_shared<integration::common_logger_adapter>(
96 config_with_dependencies.logger);
97 ctx.set_logger(logger_adapter);
98 }
99
100#if KCENON_WITH_MONITORING_SYSTEM
101 if (config_with_dependencies.monitor) {
102 auto monitoring_adapter =
103 std::make_shared<integration::common_monitoring_adapter>(
104 config_with_dependencies.monitor);
105 ctx.set_monitoring(monitoring_adapter);
106 }
107#endif
108#else
109 (void)config_with_dependencies;
110#endif
111
112 return initialize(config_with_dependencies.runtime);
113}
114
116 if (!g_initialized.load()) {
117 return error_void(not_initialized, "Network system not initialized", "network_system");
118 }
119
120 try {
121 NETWORK_LOG_INFO("[network_system] Shutting down...");
122
124 ctx.shutdown();
125
126 g_initialized.store(false);
127 NETWORK_LOG_INFO("[network_system] Shutdown complete");
128
129 return ok();
130 } catch (const std::exception &e) {
131 NETWORK_LOG_ERROR("[network_system] Shutdown error: " +
132 std::string(e.what()));
133 return error_void(internal_error, std::string("Shutdown failed: ") + e.what(), "network_system");
134 }
135}
136
137bool is_initialized() { return g_initialized.load(); }
138
139} // namespace kcenon::network
static network_context & instance()
Get the singleton instance.
static logger_integration_manager & instance()
Get the singleton instance.
void set_logger(std::shared_ptr< logger_interface > logger)
Set the logger implementation.
Adapter for common_system integration.
Feature flags for network_system.
tracing_config config
Definition exporters.cpp:29
Logger system integration interface for network_system.
#define NETWORK_LOG_WARN(msg)
#define NETWORK_LOG_INFO(msg)
#define NETWORK_LOG_ERROR(msg)
Monitoring system integration interface for network_system.
Main namespace for all Network System components.
bool is_initialized()
Check if network system is initialized.
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.
static std::atomic< bool > g_initialized
VoidResult shutdown()
Shutdown the network system.
Global context for shared network system resources.
Main header for the Network System.
Configuration for standalone network_system initialization.
static network_config production()
Create production configuration.
Configuration for network_system with external dependencies.
std::shared_ptr< kcenon::common::interfaces::IExecutor > executor
External executor/thread pool (nullptr = create internal)
network_config runtime
Runtime configuration settings (thread pool, logger, monitoring config)
std::shared_ptr< kcenon::common::interfaces::ILogger > logger
External logger instance (nullptr = create internal)
std::shared_ptr< kcenon::common::interfaces::IMonitor > monitor
External monitoring instance (nullptr = create internal if enabled)
Thread system integration interface for network_system.