|
| 1 | +/***************************************************************************** |
| 2 | +BSD 3-Clause License |
| 3 | +
|
| 4 | +Copyright (c) 2024, 🍀☀🌕🌥 🌊 |
| 5 | +All rights reserved. |
| 6 | +
|
| 7 | +Redistribution and use in source and binary forms, with or without |
| 8 | +modification, are permitted provided that the following conditions are met: |
| 9 | +
|
| 10 | +1. Redistributions of source code must retain the above copyright notice, this |
| 11 | + list of conditions and the following disclaimer. |
| 12 | +
|
| 13 | +2. Redistributions in binary form must reproduce the above copyright notice, |
| 14 | + this list of conditions and the following disclaimer in the documentation |
| 15 | + and/or other materials provided with the distribution. |
| 16 | +
|
| 17 | +3. Neither the name of the copyright holder nor the names of its |
| 18 | + contributors may be used to endorse or promote products derived from |
| 19 | + this software without specific prior written permission. |
| 20 | +
|
| 21 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 22 | +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 | +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 24 | +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 25 | +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 26 | +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 27 | +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 28 | +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 29 | +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | +*****************************************************************************/ |
| 32 | + |
| 33 | +#pragma once |
| 34 | + |
| 35 | +#include <chrono> |
| 36 | +#include <cstdint> |
| 37 | +#include <memory> |
| 38 | +#include <string> |
| 39 | + |
| 40 | +namespace kcenon::network::core |
| 41 | +{ |
| 42 | + class http_client; |
| 43 | + class http_server; |
| 44 | +} // namespace kcenon::network::core |
| 45 | + |
| 46 | +namespace kcenon::network::facade |
| 47 | +{ |
| 48 | + |
| 49 | +/*! |
| 50 | + * \class http_facade |
| 51 | + * \brief Simplified facade for creating HTTP clients and servers. |
| 52 | + * |
| 53 | + * This facade provides a simple, unified API for creating HTTP/1.1 protocol |
| 54 | + * clients and servers, hiding the complexity of underlying implementation |
| 55 | + * from the user. |
| 56 | + * |
| 57 | + * ### Design Goals |
| 58 | + * - **Simplicity**: No template parameters or protocol tags |
| 59 | + * - **Consistency**: Same API pattern across all protocol facades |
| 60 | + * - **Type Safety**: Returns standard HTTP client/server types |
| 61 | + * - **Zero Cost**: No performance overhead compared to direct instantiation |
| 62 | + * |
| 63 | + * ### Thread Safety |
| 64 | + * All methods are thread-safe and can be called concurrently. |
| 65 | + * |
| 66 | + * ### Usage Example |
| 67 | + * \code |
| 68 | + * using namespace kcenon::network::facade; |
| 69 | + * |
| 70 | + * // Create HTTP client |
| 71 | + * http_facade facade; |
| 72 | + * auto client = facade.create_client({ |
| 73 | + * .timeout = std::chrono::seconds(10) |
| 74 | + * }); |
| 75 | + * |
| 76 | + * // Create HTTP server |
| 77 | + * auto server = facade.create_server({ |
| 78 | + * .port = 8080, |
| 79 | + * .server_id = "my-http-server" |
| 80 | + * }); |
| 81 | + * \endcode |
| 82 | + * |
| 83 | + * \see core::http_client |
| 84 | + * \see core::http_server |
| 85 | + */ |
| 86 | +class http_facade |
| 87 | +{ |
| 88 | +public: |
| 89 | + /*! |
| 90 | + * \struct client_config |
| 91 | + * \brief Configuration for creating an HTTP client. |
| 92 | + */ |
| 93 | + struct client_config |
| 94 | + { |
| 95 | + //! Request timeout |
| 96 | + std::chrono::milliseconds timeout = std::chrono::seconds(30); |
| 97 | + }; |
| 98 | + |
| 99 | + /*! |
| 100 | + * \struct server_config |
| 101 | + * \brief Configuration for creating an HTTP server. |
| 102 | + */ |
| 103 | + struct server_config |
| 104 | + { |
| 105 | + //! Port number to listen on |
| 106 | + uint16_t port = 0; |
| 107 | + |
| 108 | + //! Server identifier (optional, auto-generated if not provided) |
| 109 | + std::string server_id; |
| 110 | + }; |
| 111 | + |
| 112 | + /*! |
| 113 | + * \brief Creates an HTTP client with the specified configuration. |
| 114 | + * \param config Client configuration. |
| 115 | + * \return Shared pointer to http_client. |
| 116 | + * \throws std::invalid_argument if configuration is invalid. |
| 117 | + * |
| 118 | + * ### Behavior |
| 119 | + * - Creates an http_client instance with the specified timeout |
| 120 | + * - Default timeout is 30 seconds if not specified |
| 121 | + * |
| 122 | + * ### Error Conditions |
| 123 | + * - Throws if timeout is zero or negative |
| 124 | + */ |
| 125 | + [[nodiscard]] auto create_client(const client_config& config) const |
| 126 | + -> std::shared_ptr<core::http_client>; |
| 127 | + |
| 128 | + /*! |
| 129 | + * \brief Creates an HTTP server with the specified configuration. |
| 130 | + * \param config Server configuration. |
| 131 | + * \return Shared pointer to http_server. |
| 132 | + * \throws std::invalid_argument if configuration is invalid. |
| 133 | + * |
| 134 | + * ### Behavior |
| 135 | + * - Creates an http_server instance |
| 136 | + * - Server ID is auto-generated if not provided |
| 137 | + * - Server must be started manually using start() method |
| 138 | + * |
| 139 | + * ### Error Conditions |
| 140 | + * - Throws if port is 0 or > 65535 |
| 141 | + */ |
| 142 | + [[nodiscard]] auto create_server(const server_config& config) const |
| 143 | + -> std::shared_ptr<core::http_server>; |
| 144 | + |
| 145 | +private: |
| 146 | + //! \brief Generates a unique server ID |
| 147 | + [[nodiscard]] static auto generate_server_id() -> std::string; |
| 148 | + |
| 149 | + //! \brief Validates client configuration |
| 150 | + static auto validate_client_config(const client_config& config) -> void; |
| 151 | + |
| 152 | + //! \brief Validates server configuration |
| 153 | + static auto validate_server_config(const server_config& config) -> void; |
| 154 | +}; |
| 155 | + |
| 156 | +} // namespace kcenon::network::facade |
0 commit comments