Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
ModernServer Class Reference

Modern server using new API. More...

Collaboration diagram for ModernServer:
Collaboration graph

Public Member Functions

 ModernServer (const std::string &id)
 
bool start (uint16_t port)
 
bool stop ()
 
void enable_async_processing ()
 
void show_statistics ()
 

Private Member Functions

void process_message (const std::string &client_id, const std::string &message)
 

Private Attributes

std::string server_id_
 
uint16_t port_ = 0
 
bool async_enabled_ = false
 
std::shared_ptr< core::messaging_server > server_
 
std::shared_ptr< integration::messaging_bridge > bridge_
 
std::vector< std::future< void > > futures_
 

Detailed Description

Modern server using new API.

Definition at line 40 of file modern_usage.cpp.

Constructor & Destructor Documentation

◆ ModernServer()

ModernServer::ModernServer ( const std::string & id)
inline

Definition at line 42 of file modern_usage.cpp.

42 : server_id_(id) {
43 server_ = std::make_shared<core::messaging_server>(server_id_);
44 bridge_ = std::make_shared<integration::messaging_bridge>();
45
46 // Set up thread pool interface
47 bridge_->set_thread_pool_interface(
49 );
50
51 std::cout << "[Modern Server] Created with ID: " << server_id_ << std::endl;
52 }
std::shared_ptr< core::messaging_server > server_
std::string server_id_
std::shared_ptr< integration::messaging_bridge > bridge_
static thread_integration_manager & instance()
Get the singleton instance.

References bridge_, kcenon::network::integration::thread_integration_manager::instance(), server_, and server_id_.

Here is the call graph for this function:

Member Function Documentation

◆ enable_async_processing()

void ModernServer::enable_async_processing ( )
inline

Definition at line 79 of file modern_usage.cpp.

79 {
80 async_enabled_ = true;
81 std::cout << "[Modern Server] Async processing enabled" << std::endl;
82 }

References async_enabled_.

◆ process_message()

void ModernServer::process_message ( const std::string & client_id,
const std::string & message )
inlineprivate

Definition at line 103 of file modern_usage.cpp.

103 {
104 std::cout << "[Modern Server] Processing from " << client_id
105 << ": " << message << std::endl;
106
107 if (async_enabled_) {
108 // Process asynchronously using thread pool
110 auto future = thread_mgr.submit_task([message]() {
111 // Simulate complex processing
112 std::this_thread::sleep_for(50ms);
113 std::cout << "[Async Processor] Completed processing: " << message << std::endl;
114 });
115
116 // Don't wait - let it process in background
117 futures_.push_back(std::move(future));
118 }
119
120 // Use container system for demonstration
121 auto& container_mgr = integration::container_manager::instance();
122 std::string response = "Processed: " + message;
123
124 // Serialize and deserialize to demonstrate container usage
125 auto serialized = container_mgr.serialize(std::any(response));
126 auto deserialized = container_mgr.deserialize(serialized);
127
128 if (deserialized.has_value()) {
129 std::cout << "[Modern Server] Container processed: "
130 << std::any_cast<std::string>(deserialized) << std::endl;
131 }
132 }
std::vector< std::future< void > > futures_
static container_manager & instance()
Get the singleton instance.

References async_enabled_, futures_, kcenon::network::integration::container_manager::instance(), kcenon::network::integration::thread_integration_manager::instance(), and kcenon::network::message.

Here is the call graph for this function:

◆ show_statistics()

void ModernServer::show_statistics ( )
inline

Definition at line 84 of file modern_usage.cpp.

84 {
85 auto metrics = bridge_->get_metrics();
87
88 std::cout << "\n=== Server Statistics ===" << std::endl;
89 std::cout << "Network Metrics:" << std::endl;
90 std::cout << " Messages sent: " << metrics.messages_sent << std::endl;
91 std::cout << " Messages received: " << metrics.messages_received << std::endl;
92 std::cout << " Bytes sent: " << metrics.bytes_sent << std::endl;
93 std::cout << " Bytes received: " << metrics.bytes_received << std::endl;
94 std::cout << " Active connections: " << metrics.connections_active << std::endl;
95
96 std::cout << "\nThread Pool Metrics:" << std::endl;
97 std::cout << " Worker threads: " << thread_metrics.worker_threads << std::endl;
98 std::cout << " Pending tasks: " << thread_metrics.pending_tasks << std::endl;
99 std::cout << " Completed tasks: " << thread_metrics.completed_tasks << std::endl;
100 }

References bridge_, kcenon::network::integration::thread_integration_manager::get_metrics(), kcenon::network::integration::thread_integration_manager::instance(), and kcenon::network::integration::thread_integration_manager::metrics::worker_threads.

Here is the call graph for this function:

◆ start()

bool ModernServer::start ( uint16_t port)
inline

Definition at line 54 of file modern_usage.cpp.

54 {
55 auto result = server_->start_server(port);
56 if (result.is_err()) {
57 std::cerr << "[Modern Server] Failed to start: " << result.error().message
58 << " (code: " << result.error().code << ")" << std::endl;
59 return false;
60 }
61
62 port_ = port;
63 std::cout << "[Modern Server] Started on port " << port << std::endl;
64 return true;
65 }
auto start_server(unsigned short port) -> VoidResult
Starts the server on the specified port.

References port_, server_, and kcenon::network::core::messaging_server::start_server().

Here is the call graph for this function:

◆ stop()

bool ModernServer::stop ( )
inline

Definition at line 67 of file modern_usage.cpp.

67 {
68 auto result = server_->stop_server();
69 if (result.is_err()) {
70 std::cerr << "[Modern Server] Failed to stop: " << result.error().message
71 << " (code: " << result.error().code << ")" << std::endl;
72 return false;
73 }
74
75 std::cout << "[Modern Server] Stopped" << std::endl;
76 return true;
77 }
auto stop_server() -> VoidResult
Stops the server and closes all connections.

References server_, and kcenon::network::core::messaging_server::stop_server().

Here is the call graph for this function:

Member Data Documentation

◆ async_enabled_

bool ModernServer::async_enabled_ = false
private

Definition at line 136 of file modern_usage.cpp.

Referenced by enable_async_processing(), and process_message().

◆ bridge_

std::shared_ptr<integration::messaging_bridge> ModernServer::bridge_
private

Definition at line 138 of file modern_usage.cpp.

Referenced by ModernServer(), and show_statistics().

◆ futures_

std::vector<std::future<void> > ModernServer::futures_
private

Definition at line 139 of file modern_usage.cpp.

Referenced by process_message().

◆ port_

uint16_t ModernServer::port_ = 0
private

Definition at line 135 of file modern_usage.cpp.

Referenced by start().

◆ server_

std::shared_ptr<core::messaging_server> ModernServer::server_
private

Definition at line 137 of file modern_usage.cpp.

Referenced by ModernServer(), start(), and stop().

◆ server_id_

std::string ModernServer::server_id_
private

Definition at line 134 of file modern_usage.cpp.

Referenced by ModernServer().


The documentation for this class was generated from the following file: