Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
http_client_adapter.h
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
5#pragma once
6
8
9#include <atomic>
10#include <chrono>
11#include <memory>
12#include <mutex>
13#include <string>
14
15namespace kcenon::network::core {
16 class http_client;
17}
18
20
54public:
60 explicit http_client_adapter(
61 std::string_view client_id,
62 std::chrono::milliseconds timeout = std::chrono::seconds(30));
63
67 ~http_client_adapter() override;
68
69 // Non-copyable, non-movable
74
75 // =========================================================================
76 // Path Configuration
77 // =========================================================================
78
86 auto set_path(std::string_view path) -> void;
87
95 auto set_use_ssl(bool use_ssl) -> void;
96
97 // =========================================================================
98 // i_network_component interface implementation
99 // =========================================================================
100
101 [[nodiscard]] auto is_running() const -> bool override;
102 auto wait_for_stop() -> void override;
103
104 // =========================================================================
105 // i_protocol_client interface implementation
106 // =========================================================================
107
108 [[nodiscard]] auto start(std::string_view host, uint16_t port) -> VoidResult override;
109 [[nodiscard]] auto stop() -> VoidResult override;
110 [[nodiscard]] auto send(std::vector<uint8_t>&& data) -> VoidResult override;
111 [[nodiscard]] auto is_connected() const -> bool override;
112
113 auto set_observer(std::shared_ptr<interfaces::connection_observer> observer) -> void override;
114
115 auto set_receive_callback(receive_callback_t callback) -> void override;
116 auto set_connected_callback(connected_callback_t callback) -> void override;
117 auto set_disconnected_callback(disconnected_callback_t callback) -> void override;
118 auto set_error_callback(error_callback_t callback) -> void override;
119
120private:
124 [[nodiscard]] auto build_url() const -> std::string;
125
129 auto notify_receive(const std::vector<uint8_t>& data) -> void;
130
134 auto notify_error(std::error_code ec) -> void;
135
136 std::string client_id_;
137 std::chrono::milliseconds timeout_;
138 std::shared_ptr<core::http_client> client_;
139
140 // URL components
141 std::string host_;
142 uint16_t port_{0};
143 std::string path_{"/"};
144 bool use_ssl_{false};
145
146 std::atomic<bool> is_running_{false};
147
148 mutable std::mutex callbacks_mutex_;
149 std::shared_ptr<interfaces::connection_observer> observer_;
154};
155
156} // namespace kcenon::network::internal::adapters
Unified interface for all protocol client implementations.
std::function< void(const std::vector< uint8_t > &)> receive_callback_t
Callback type for received data.
std::function< void()> connected_callback_t
Callback type for connection established.
std::function< void(std::error_code)> error_callback_t
Callback type for errors.
std::function< void()> disconnected_callback_t
Callback type for disconnection.
Adapter that wraps http_client to implement i_protocol_client.
auto notify_error(std::error_code ec) -> void
Notifies observers/callbacks of errors.
auto set_receive_callback(receive_callback_t callback) -> void override
Sets the callback for received data.
auto wait_for_stop() -> void override
Blocks until the component has stopped.
auto notify_receive(const std::vector< uint8_t > &data) -> void
Notifies observers/callbacks of received data.
auto set_use_ssl(bool use_ssl) -> void
Sets whether to use HTTPS.
auto set_error_callback(error_callback_t callback) -> void override
Sets the callback for errors.
auto build_url() const -> std::string
Builds the full URL from stored components.
auto set_path(std::string_view path) -> void
Sets the HTTP path for requests.
http_client_adapter(const http_client_adapter &)=delete
http_client_adapter(std::string_view client_id, std::chrono::milliseconds timeout=std::chrono::seconds(30))
Constructs an adapter with timeout configuration.
auto is_connected() const -> bool override
Checks if the client is connected to the server.
auto set_disconnected_callback(disconnected_callback_t callback) -> void override
Sets the callback for disconnection.
auto stop() -> VoidResult override
Stops the client and closes the connection.
auto start(std::string_view host, uint16_t port) -> VoidResult override
Starts the client and connects to the specified server.
~http_client_adapter() override
Destructor ensures proper cleanup.
auto is_running() const -> bool override
Checks if the component is currently running.
std::shared_ptr< interfaces::connection_observer > observer_
auto set_connected_callback(connected_callback_t callback) -> void override
Sets the callback for connection established.
http_client_adapter & operator=(http_client_adapter &&)=delete
auto set_observer(std::shared_ptr< interfaces::connection_observer > observer) -> void override
Sets the connection observer for unified event handling.
http_client_adapter & operator=(const http_client_adapter &)=delete
Protocol-specific client interface extending i_client.