diff --git a/lib/everest/framework/include/utils/message_handler.hpp b/lib/everest/framework/include/utils/message_handler.hpp index 59310470fa..30be66b5ac 100644 --- a/lib/everest/framework/include/utils/message_handler.hpp +++ b/lib/everest/framework/include/utils/message_handler.hpp @@ -61,6 +61,10 @@ class MessageHandler { template void execute_handlers_from_vector(HandlerMap& handlers, const std::string& topic, ExecuteFn execute_fn); + template + void execute_handlers_from_vector_with_wildcards(HandlerMap& handlers, const std::string& topic, + ExecuteFn execute_fn); + template void execute_single_handler(HandlerMap& handlers, const std::string& topic, ExecuteFn execute_fn); @@ -94,7 +98,8 @@ class MessageHandler { std::map>> var_handlers; // var handlers of module std::map> cmd_handlers; // cmd handlers of module std::map> cmd_result_handlers; // cmd result handlers of module - std::map> error_handlers; // error handlers of module + std::map>> + error_handlers; // error handlers with wildcard support std::map> get_module_config_handlers; // get module config handler of manager std::shared_ptr config_response_handler; // get module config response handler of module diff --git a/lib/everest/framework/lib/message_handler.cpp b/lib/everest/framework/lib/message_handler.cpp index 481d10571f..c011617fb5 100644 --- a/lib/everest/framework/lib/message_handler.cpp +++ b/lib/everest/framework/lib/message_handler.cpp @@ -260,7 +260,7 @@ void MessageHandler::register_handler(const std::string& topic, std::shared_ptr< } case HandlerType::SubscribeError: { std::lock_guard lg(handler_mutex); - error_handlers[topic] = handler; + error_handlers[topic].push_back(handler); break; } case HandlerType::ExternalMQTT: { @@ -305,23 +305,13 @@ void MessageHandler::handle_cmd_message(const std::string& topic, const json& da } void MessageHandler::handle_external_mqtt_message(const std::string& topic, const json& data) { - execute_handlers_from_vector(external_var_handlers, topic, - [&](const auto& handler) { (*handler->handler)(topic, data); }); + execute_handlers_from_vector_with_wildcards(external_var_handlers, topic, + [&](const auto& handler) { (*handler->handler)(topic, data); }); } void MessageHandler::handle_error_message(const std::string& topic, const json& data) { - std::vector>> matching_handlers; - { - std::lock_guard lock(handler_mutex); - for (const auto& [_topic, error_handler] : error_handlers) { - if (check_topic_matches(topic, _topic)) { - matching_handlers.emplace_back(_topic, error_handler); - } - } - } - for (const auto& [_topic, handler] : matching_handlers) { - (*handler->handler)(topic, data); - } + execute_handlers_from_vector_with_wildcards(error_handlers, topic, + [&](const auto& handler) { (*handler->handler)(topic, data); }); } void MessageHandler::handle_get_config_message(const std::string& topic, const json& data) { @@ -383,6 +373,23 @@ void MessageHandler::execute_handlers_from_vector(HandlerMap& handlers, const st } } +template +void MessageHandler::execute_handlers_from_vector_with_wildcards(HandlerMap& handlers, const std::string& topic, + ExecuteFn execute_fn) { + std::vector> handlers_copy; + { + std::lock_guard lock(handler_mutex); + for (const auto& [wildcard_topic, handlers_vec] : handlers) { + if (check_topic_matches(topic, wildcard_topic)) { + handlers_copy.insert(handlers_copy.end(), handlers_vec.begin(), handlers_vec.end()); + } + } + } + for (const auto& handler : handlers_copy) { + execute_fn(handler); + } +} + template void MessageHandler::execute_single_handler(HandlerMap& handlers, const std::string& topic, ExecuteFn execute_fn) { std::shared_ptr handler_copy;