Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
http_facade.cpp
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
6
7#include <atomic>
8#include <iomanip>
9#include <sstream>
10#include <stdexcept>
11
14
16{
17
18namespace
19{
21 std::atomic<uint64_t> g_client_id_counter{0};
22
24 std::atomic<uint64_t> g_server_id_counter{0};
25} // namespace
26
28{
29 const auto id = g_client_id_counter.fetch_add(1, std::memory_order_relaxed);
30 std::ostringstream oss;
31 oss << "http_client_" << std::setfill('0') << std::setw(8) << id;
32 return oss.str();
33}
34
36{
37 const auto id = g_server_id_counter.fetch_add(1, std::memory_order_relaxed);
38 std::ostringstream oss;
39 oss << "http_server_" << std::setfill('0') << std::setw(8) << id;
40 return oss.str();
41}
42
44{
45 if (config.timeout.count() <= 0)
46 {
47 return error_void(-1, "http_facade: timeout must be positive", "http_facade");
48 }
49
50 return ok();
51}
52
54{
55 if (config.port == 0 || config.port > 65535)
56 {
57 return error_void(-1, "http_facade: port must be between 1 and 65535", "http_facade");
58 }
59
60 return ok();
61}
62
65{
66 auto validation = validate_client_config(config);
67 if (validation.is_err())
68 {
70 validation.error().code, validation.error().message, "http_facade");
71 }
72
73 const auto client_id = config.client_id.empty() ? generate_client_id() : config.client_id;
74
75 // Create adapter with timeout from config
76 auto adapter = std::make_shared<internal::adapters::http_client_adapter>(
77 client_id, config.timeout);
78
79 // Configure path and SSL
80 adapter->set_path(config.path);
81 adapter->set_use_ssl(config.use_ssl);
82
83 return ok(std::shared_ptr<interfaces::i_protocol_client>(adapter));
84}
85
88{
89 auto validation = validate_server_config(config);
90 if (validation.is_err())
91 {
93 validation.error().code, validation.error().message, "http_facade");
94 }
95
96 const auto server_id = config.server_id.empty() ? generate_server_id() : config.server_id;
97
98 // Create adapter
99 return ok(std::shared_ptr<interfaces::i_protocol_server>(
100 std::make_shared<internal::adapters::http_server_adapter>(server_id)));
101}
102
103} // namespace kcenon::network::facade
static auto validate_server_config(const server_config &config) -> VoidResult
Validates server configuration.
static auto validate_client_config(const client_config &config) -> VoidResult
Validates client configuration.
auto create_server(const server_config &config) const -> Result< std::shared_ptr< interfaces::i_protocol_server > >
Creates an HTTP server with the specified configuration.
static auto generate_server_id() -> std::string
Generates a unique server ID.
auto create_client(const client_config &config) const -> Result< std::shared_ptr< interfaces::i_protocol_client > >
Creates an HTTP client with the specified configuration.
static auto generate_client_id() -> std::string
Generates a unique client ID.
tracing_config config
Definition exporters.cpp:29
Simplified facade for creating HTTP clients and servers.
VoidResult error_void(int code, const std::string &message, const std::string &source="network_system", const std::string &details="")
VoidResult ok()
Configuration for creating an HTTP client.
Definition http_facade.h:84
Configuration for creating an HTTP server.