24 std::string_view session_id,
25 std::string_view client_address,
27 std::weak_ptr<core::http_server>
server)
28 : session_id_(session_id)
29 , client_address_(client_address)
30 , client_port_(client_port)
31 , server_(std::move(
server))
47 if (!is_connected_.load(std::memory_order_acquire))
51 "Session is not connected",
52 "http_request_session::send"
56 std::lock_guard<std::mutex> lock(data_mutex_);
57 response_data_ = std::move(data);
64 is_connected_.store(
false, std::memory_order_release);
69 std::lock_guard<std::mutex> lock(data_mutex_);
70 response_data_ = std::move(data);
84 : server_id_(server_id)
85 , server_(std::make_shared<core::http_server>(std::string(server_id)))
104 return is_running_.load(std::memory_order_acquire);
111 server_->wait_for_stop();
125 "Server not initialized",
126 "http_server_adapter::start"
130 auto result = server_->start(port);
133 is_running_.store(
true, std::memory_order_release);
144 "Server not initialized",
145 "http_server_adapter::stop"
149 auto result = server_->stop();
150 is_running_.store(
false, std::memory_order_release);
154 std::lock_guard<std::mutex> lock(sessions_mutex_);
169 std::lock_guard<std::mutex> lock(callbacks_mutex_);
170 connection_callback_ = std::move(callback);
175 std::lock_guard<std::mutex> lock(callbacks_mutex_);
176 disconnection_callback_ = std::move(callback);
181 std::lock_guard<std::mutex> lock(callbacks_mutex_);
182 receive_callback_ = std::move(callback);
187 std::lock_guard<std::mutex> lock(callbacks_mutex_);
188 error_callback_ = std::move(callback);
206 const auto request_id = request_counter_.fetch_add(1, std::memory_order_relaxed);
207 const auto session_id = make_session_id(
"client", 0, request_id);
208 auto session = std::make_shared<http_request_session>(
209 session_id,
"client", 0, server_);
213 std::lock_guard<std::mutex> lock(sessions_mutex_);
214 sessions_[session_id] = session;
221 std::lock_guard<std::mutex> lock(callbacks_mutex_);
222 callback_copy = connection_callback_;
226 callback_copy(session);
234 std::lock_guard<std::mutex> lock(callbacks_mutex_);
235 callback_copy = receive_callback_;
246 response.
body = session->get_response_data();
247 response.
set_header(
"Content-Type",
"application/octet-stream");
253 std::lock_guard<std::mutex> lock(callbacks_mutex_);
254 callback_copy = disconnection_callback_;
258 callback_copy(session_id);
263 remove_session(session_id);
270 const auto request_id = request_counter_.fetch_add(1, std::memory_order_relaxed);
271 const auto session_id = make_session_id(
"client", 0, request_id);
272 auto session = std::make_shared<http_request_session>(
273 session_id,
"client", 0, server_);
276 std::lock_guard<std::mutex> lock(sessions_mutex_);
277 sessions_[session_id] = session;
283 std::lock_guard<std::mutex> lock(callbacks_mutex_);
284 callback_copy = connection_callback_;
288 callback_copy(session);
295 std::lock_guard<std::mutex> lock(callbacks_mutex_);
296 callback_copy = receive_callback_;
306 response.
body = session->get_response_data();
307 response.
set_header(
"Content-Type",
"application/octet-stream");
312 std::lock_guard<std::mutex> lock(callbacks_mutex_);
313 callback_copy = disconnection_callback_;
317 callback_copy(session_id);
321 remove_session(session_id);
328 const std::string& address,
330 uint64_t request_id) -> std::string
332 std::ostringstream oss;
333 oss << address <<
":" << port <<
"#" << request_id;
338 const std::string& address,
339 uint16_t port) -> std::shared_ptr<http_request_session>
341 const auto request_id = request_counter_.fetch_add(1, std::memory_order_relaxed);
342 const auto session_id = make_session_id(address, port, request_id);
344 std::lock_guard<std::mutex> lock(sessions_mutex_);
346 auto session = std::make_shared<http_request_session>(
347 session_id, address, port, server_);
348 sessions_[session_id] = session;
355 std::lock_guard<std::mutex> lock(sessions_mutex_);
356 sessions_.erase(session_id);
std::function< void(std::string_view)> disconnection_callback_t
Callback type for disconnections (session_id)
std::function< void(std::string_view, const std::vector< uint8_t > &)> receive_callback_t
Callback type for received data (session_id, data)
std::function< void(std::string_view, std::error_code)> error_callback_t
Callback type for errors (session_id, error)
std::function< void(std::shared_ptr< i_session >)> connection_callback_t
Callback type for new connections.
auto is_connected() const -> bool override
Checks if the session is currently connected.
http_request_session(std::string_view session_id, std::string_view client_address, uint16_t client_port, std::weak_ptr< core::http_server > server)
std::vector< uint8_t > response_data_
std::atomic< bool > is_connected_
auto send(std::vector< uint8_t > &&data) -> VoidResult override
Sends data to the client.
auto id() const -> std::string_view override
Gets the unique identifier for this session.
auto get_response_data() const -> const std::vector< uint8_t > &
Gets the stored response data.
auto close() -> void override
Closes the session.
auto set_response_data(std::vector< uint8_t > data) -> void
Stores the response data to be sent back to client.
std::unordered_map< std::string, std::shared_ptr< http_request_session > > sessions_
auto wait_for_stop() -> void override
Blocks until the component has stopped.
http_server_adapter(std::string_view server_id)
Constructs an adapter with a unique server ID.
~http_server_adapter() override
Destructor ensures proper cleanup.
std::mutex sessions_mutex_
auto set_connection_callback(connection_callback_t callback) -> void override
Sets the callback for new connections.
auto is_running() const -> bool override
Checks if the component is currently running.
auto setup_internal_routes() -> void
Sets up internal HTTP routes to bridge to i_protocol_server callbacks.
auto remove_session(const std::string &session_id) -> void
Removes a session after request processing.
static auto make_session_id(const std::string &address, uint16_t port, uint64_t request_id) -> std::string
Creates a unique session ID from request info.
auto set_disconnection_callback(disconnection_callback_t callback) -> void override
Sets the callback for disconnections.
auto get_or_create_session(const std::string &address, uint16_t port) -> std::shared_ptr< http_request_session >
Gets or creates a session for the given request.
std::shared_ptr< core::http_server > server_
std::atomic< bool > is_running_
auto set_error_callback(error_callback_t callback) -> void override
Sets the callback for errors.
auto set_receive_callback(receive_callback_t callback) -> void override
Sets the callback for received data.
auto stop() -> VoidResult override
Stops the server and closes all connections.
auto connection_count() const -> size_t override
Gets the number of active client connections.
auto start(uint16_t port) -> VoidResult override
Starts the server and begins listening for connections.
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.
Context for an HTTP request with parsed components.
internal::http_request request
std::vector< uint8_t > body
Represents an HTTP response message.
auto set_header(const std::string &name, const std::string &value) -> void
Set a header value.
std::vector< uint8_t > body