35using namespace std::chrono_literals;
44 bridge_ = std::make_shared<integration::messaging_bridge>();
47 bridge_->set_thread_pool_interface(
51 std::cout <<
"[Modern Server] Created with ID: " <<
server_id_ << std::endl;
56 if (result.is_err()) {
57 std::cerr <<
"[Modern Server] Failed to start: " << result.error().message
58 <<
" (code: " << result.error().code <<
")" << std::endl;
63 std::cout <<
"[Modern Server] Started on port " << port << std::endl;
69 if (result.is_err()) {
70 std::cerr <<
"[Modern Server] Failed to stop: " << result.error().message
71 <<
" (code: " << result.error().code <<
")" << std::endl;
75 std::cout <<
"[Modern Server] Stopped" << std::endl;
81 std::cout <<
"[Modern Server] Async processing enabled" << std::endl;
85 auto metrics =
bridge_->get_metrics();
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;
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;
104 std::cout <<
"[Modern Server] Processing from " << client_id
105 <<
": " <<
message << std::endl;
110 auto future = thread_mgr.submit_task([
message]() {
112 std::this_thread::sleep_for(50ms);
113 std::cout <<
"[Async Processor] Completed processing: " <<
message << std::endl;
117 futures_.push_back(std::move(future));
122 std::string response =
"Processed: " +
message;
125 auto serialized = container_mgr.serialize(std::any(response));
126 auto deserialized = container_mgr.deserialize(serialized);
128 if (deserialized.has_value()) {
129 std::cout <<
"[Modern Server] Container processed: "
130 << std::any_cast<std::string>(deserialized) << std::endl;
137 std::shared_ptr<core::messaging_server>
server_;
138 std::shared_ptr<integration::messaging_bridge>
bridge_;
149 std::cout <<
"[Modern Client] Created with ID: " <<
client_id_ << std::endl;
152 bool connect(
const std::string& host, uint16_t port) {
154 if (result.is_err()) {
155 std::cerr <<
"[Modern Client] Failed to connect: " << result.error().message
156 <<
" (code: " << result.error().code <<
")" << std::endl;
160 std::cout <<
"[Modern Client] Connecting to " << host <<
":" << port << std::endl;
161 std::this_thread::sleep_for(200ms);
165 size_t send_batch(
const std::vector<std::string>& messages) {
166 std::cout <<
"[Modern Client] Sending batch of " << messages.size()
167 <<
" messages" << std::endl;
169 size_t successful = 0;
170 for (
const auto& msg : messages) {
171 std::vector<uint8_t> data(msg.begin(), msg.end());
174 if (result.is_err()) {
175 std::cerr <<
"[Modern Client] Failed to send message: " << result.error().message
176 <<
" (code: " << result.error().code <<
")" << std::endl;
181 std::this_thread::sleep_for(50ms);
184 std::cout <<
"[Modern Client] Successfully sent " << successful <<
"/" << messages.size()
185 <<
" messages" << std::endl;
191 thread_mgr.submit_task([
this,
message]() {
195 if (result.is_err()) {
196 std::cerr <<
"[Modern Client] Async send failed: " << result.error().message
197 <<
" (code: " << result.error().code <<
")" << std::endl;
199 std::cout <<
"[Modern Client] Async sent: " <<
message << std::endl;
206 if (result.is_err()) {
207 std::cerr <<
"[Modern Client] Failed to disconnect: " << result.error().message
208 <<
" (code: " << result.error().code <<
")" << std::endl;
212 std::cout <<
"[Modern Client] Disconnected" << std::endl;
218 std::shared_ptr<core::messaging_client>
client_;
225 std::cout <<
"\n=== Advanced Features Demo ===" << std::endl;
228 auto custom_container = std::make_shared<integration::basic_container>();
231 custom_container->set_serializer([](
const std::any& data) {
232 std::vector<uint8_t> result;
233 if (data.type() ==
typeid(std::vector<int>)) {
234 auto vec = std::any_cast<std::vector<int>>(data);
235 result.reserve(vec.size() *
sizeof(
int));
236 for (
int val : vec) {
237 auto bytes =
reinterpret_cast<const uint8_t*
>(&val);
238 result.insert(result.end(), bytes, bytes +
sizeof(
int));
246 "custom_vector_serializer", custom_container
250 std::vector<int> test_data = {1, 2, 3, 4, 5};
251 auto serialized = custom_container->serialize(std::any(test_data));
252 std::cout <<
"Custom serialized " << test_data.size()
253 <<
" integers to " << serialized.size() <<
" bytes" << std::endl;
257 std::vector<std::future<void>> tasks;
260 for (
int i = 1; i <= 3; ++i) {
261 auto future = thread_mgr.submit_delayed_task(
263 std::cout <<
"[Delayed Task " << i <<
"] Executed after delay" << std::endl;
265 std::chrono::milliseconds(i * 100)
267 tasks.push_back(std::move(future));
271 for (
auto& future : tasks) {
275 std::cout <<
"All advanced features demonstrated successfully" << std::endl;
281int main(
int argc,
char* argv[]) {
282 std::cout <<
"=== Modern Network System Usage Demo ===" << std::endl;
283 std::cout <<
"Demonstrating the new API with all integration features" << std::endl;
286 kcenon::network::compat::initialize();
287 std::cout <<
"\nā Network system initialized" << std::endl;
292 server.enable_async_processing();
294 if (!server.start(9090)) {
295 std::cerr <<
"Failed to start server, aborting demo" << std::endl;
300 std::this_thread::sleep_for(500ms);
305 if (!client.connect(
"127.0.0.1", 9090)) {
306 std::cerr <<
"Failed to connect client, stopping server" << std::endl;
312 std::vector<std::string> batch = {
317 size_t sent = client.send_batch(batch);
319 if (sent < batch.size()) {
320 std::cout <<
"[Warning] Not all messages were sent successfully" << std::endl;
324 for (
int i = 1; i <= 3; ++i) {
325 client.send_async(
"Async message " + std::to_string(i));
329 std::this_thread::sleep_for(500ms);
332 server.show_statistics();
335 if (!client.disconnect()) {
336 std::cout <<
"[Warning] Client disconnection had issues" << std::endl;
343 if (!server.stop()) {
344 std::cout <<
"[Warning] Server shutdown had issues" << std::endl;
347 }
catch (
const std::exception& e) {
348 std::cerr <<
"Error: " << e.what() << std::endl;
353 kcenon::network::compat::shutdown();
354 std::cout <<
"\nā Network system shutdown complete" << std::endl;
356 std::cout <<
"\n=== Modern Usage Demo Complete ===" << std::endl;
357 std::cout <<
"All modern features working perfectly!" << std::endl;
Modern client using new API.
size_t send_batch(const std::vector< std::string > &messages)
bool connect(const std::string &host, uint16_t port)
std::shared_ptr< core::messaging_client > client_
ModernClient(const std::string &id)
void send_async(const std::string &message)
Modern server using new API.
void process_message(const std::string &client_id, const std::string &message)
std::shared_ptr< core::messaging_server > server_
void enable_async_processing()
ModernServer(const std::string &id)
std::shared_ptr< integration::messaging_bridge > bridge_
bool start(uint16_t port)
std::vector< std::future< void > > futures_
auto start_client(std::string_view host, unsigned short port) -> VoidResult
Starts the client and connects to the specified host and port.
auto stop_client() -> VoidResult
Stops the client and disconnects from the server.
auto send_packet(std::vector< uint8_t > &&data) -> VoidResult
Sends data to the connected server.
auto start_server(unsigned short port) -> VoidResult
Starts the server on the specified port.
auto stop_server() -> VoidResult
Stops the server and closes all connections.
void register_container(const std::string &name, std::shared_ptr< container_interface > container)
Register a container implementation.
static container_manager & instance()
Get the singleton instance.
static thread_integration_manager & instance()
Get the singleton instance.
metrics get_metrics() const
Get current metrics.
void demonstrate_advanced_features()
Demonstrate advanced features.
Main namespace for all Network System components.
Main header for the Network System.