20 std::string_view client_id,
21 std::chrono::milliseconds timeout)
22 : client_id_(client_id)
24 , client_(std::make_shared<core::http_client>(timeout))
30 is_running_.store(
false, std::memory_order_release);
39 path_ = std::string(path);
71 "Client not initialized",
72 "http_client_adapter::start"
77 host_ = std::string(host);
79 is_running_.store(
true, std::memory_order_release);
83 std::shared_ptr<interfaces::connection_observer> observer_copy;
86 std::lock_guard<std::mutex> lock(callbacks_mutex_);
87 observer_copy = observer_;
88 callback_copy = connected_callback_;
93 observer_copy->on_connected();
106 if (!is_running_.load(std::memory_order_acquire))
110 "Client is not running",
111 "http_client_adapter::stop"
115 is_running_.store(
false, std::memory_order_release);
119 std::shared_ptr<interfaces::connection_observer> observer_copy;
122 std::lock_guard<std::mutex> lock(callbacks_mutex_);
123 observer_copy = observer_;
124 callback_copy = disconnected_callback_;
129 observer_copy->on_disconnected();
146 "Client not initialized",
147 "http_client_adapter::send"
151 if (!is_running_.load(std::memory_order_acquire))
155 "Client is not running - call start() first",
156 "http_client_adapter::send"
161 const std::string url = build_url();
164 auto result = client_->post(url, data);
167 notify_error(std::make_error_code(std::errc::io_error));
170 "HTTP POST request failed: " + result.error().message,
171 "http_client_adapter::send"
176 const auto& response = result.value();
177 notify_receive(response.body);
189 std::lock_guard<std::mutex> lock(callbacks_mutex_);
190 observer_ = std::move(observer);
195 std::lock_guard<std::mutex> lock(callbacks_mutex_);
196 receive_callback_ = std::move(callback);
201 std::lock_guard<std::mutex> lock(callbacks_mutex_);
202 connected_callback_ = std::move(callback);
207 std::lock_guard<std::mutex> lock(callbacks_mutex_);
208 disconnected_callback_ = std::move(callback);
213 std::lock_guard<std::mutex> lock(callbacks_mutex_);
214 error_callback_ = std::move(callback);
223 std::ostringstream oss;
224 oss << (
use_ssl_ ?
"https://" :
"http://")
231 std::shared_ptr<interfaces::connection_observer> observer_copy;
234 std::lock_guard<std::mutex> lock(callbacks_mutex_);
235 observer_copy = observer_;
236 callback_copy = receive_callback_;
241 observer_copy->on_receive(data);
251 std::shared_ptr<interfaces::connection_observer> observer_copy;
254 std::lock_guard<std::mutex> lock(callbacks_mutex_);
255 observer_copy = observer_;
256 callback_copy = error_callback_;
261 observer_copy->on_error(ec);
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.
auto notify_error(std::error_code ec) -> void
Notifies observers/callbacks of errors.
std::atomic< bool > is_running_
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 send(std::vector< uint8_t > &&data) -> VoidResult override
Sends data to the connected server.
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(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.
auto set_connected_callback(connected_callback_t callback) -> void override
Sets the callback for connection established.
auto set_observer(std::shared_ptr< interfaces::connection_observer > observer) -> void override
Sets the connection observer for unified event handling.
constexpr int invalid_argument
constexpr int internal_error
VoidResult error_void(int code, const std::string &message, const std::string &source="network_system", const std::string &details="")
Network-specific error and result type definitions.