IRSOL
C++ code implementing socket server for interacting with Baumer camera.
message_handler.cpp
Go to the documentation of this file.
2
3#include "irsol/logging.hpp"
4#include "irsol/protocol.hpp"
5
6namespace irsol {
7namespace server {
8namespace handlers {
9
12 const
13{
14 IRSOL_LOG_TRACE("Handling message: '{}' for client '{}'", protocol::toString(message), clientId);
16 if(!handler) {
17 IRSOL_LOG_ERROR("No handler found for message: '{}'", irsol::protocol::toString(message));
18 std::vector<protocol::OutMessage> result;
19 result.emplace_back(
20 protocol::Error::from(std::move(message), "No handler registered for this message."));
21 return result;
22 }
23 // Dispatch the message to the appropriate handler and return the response
24 const std::string messageString = protocol::toString(message);
25
26 try {
27 return std::visit(
29 using T = std::decay_t<decltype(msg)>;
31 return std::get<handler_function_t>(*handler)(clientId, std::move(msg));
32 },
33 std::move(message));
34
35 } catch(const std::exception& e) {
36 IRSOL_LOG_ERROR("Error handling message: '{}': {}", messageString, e.what());
37 return {};
38 }
39}
40
41std::optional<MessageHandler::any_handler_function_t>
43{
44 return std::visit(
45 [this](auto&& value) -> std::optional<any_handler_function_t> {
46 using T = std::decay_t<decltype(value)>;
47 auto res = this->findHandler<T>(value.identifier);
48 if(!res) {
49 return std::nullopt;
50 }
51 return std::make_optional<any_handler_function_t>(*res);
52 },
53 msg);
54}
55
56} // namespace handlers
57} // namespace server
58
59} // namespace irsol
std::function< handling_function_response_t(const irsol::types::client_id_t &, T &&)> handler_function_t
handling_function_response_t handle(const irsol::types::client_id_t &clientId, protocol::InMessage &&message) const
Dispatches an incoming message to the correct user-defined handler.
std::optional< any_handler_function_t > findHandlerForMessage(const protocol::InMessage &msg) const
Locates a registered handler (of any message type) for a given message.
std::vector< protocol::OutMessage > handling_function_response_t
#define IRSOL_LOG_ERROR(...)
Logs an error-level message using the default logger.
Definition logging.hpp:94
#define IRSOL_LOG_TRACE(...)
Logs a trace-level message using the default logger.
Definition logging.hpp:90
std::variant< Assignment, Inquiry, Command > InMessage
Variant type representing any incoming message.
Definition variants.hpp:86
std::string toString(const InMessage &msg)
Converts an incoming message variant to a human-readable string.
Definition variants.cpp:19
Logging utilities and configuration for the irsol library.
Message routing layer between protocol and application logic.
constexpr auto makeHandler(std::shared_ptr< Context > ctx, Args &&... args)
Constructs a handler instance of the given type.
Definition factory.hpp:28
std::string client_id_t
Represents a unique client identifier. Typically used to identify connected clients by string IDs.
Definition types.hpp:55
static Error from(const T &msg, const std::string &description)
Creates an error from a specific incoming message type.
Definition error.hpp:63
auto value
auto result
auto msg