Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/everest/framework/include/utils/message_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@
template <typename HandlerMap, typename ExecuteFn>
void execute_handlers_from_vector(HandlerMap& handlers, const std::string& topic, ExecuteFn execute_fn);

template <typename HandlerMap, typename ExecuteFn>
void execute_handlers_from_vector_with_wildcards(HandlerMap& handlers, const std::string& topic,
ExecuteFn execute_fn);

template <typename HandlerMap, typename ExecuteFn>
void execute_single_handler(HandlerMap& handlers, const std::string& topic, ExecuteFn execute_fn);

Expand Down Expand Up @@ -94,7 +98,8 @@
std::map<MqttTopic, std::vector<std::shared_ptr<TypedHandler>>> var_handlers; // var handlers of module
std::map<MqttTopic, std::shared_ptr<TypedHandler>> cmd_handlers; // cmd handlers of module
std::map<CmdId, std::shared_ptr<TypedHandler>> cmd_result_handlers; // cmd result handlers of module
std::map<MqttTopic, std::shared_ptr<TypedHandler>> error_handlers; // error handlers of module
std::map<MqttTopic, std::vector<std::shared_ptr<TypedHandler>>>
error_handlers; // error handlers with wildcard support

Check warning on line 102 in lib/everest/framework/include/utils/message_handler.hpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

lib/everest/framework/include/utils/message_handler.hpp#L102

class member 'MessageHandler::error_handlers' is never used.
std::map<MqttTopic, std::shared_ptr<TypedHandler>>
get_module_config_handlers; // get module config handler of manager
std::shared_ptr<TypedHandler> config_response_handler; // get module config response handler of module
Expand Down
37 changes: 22 additions & 15 deletions lib/everest/framework/lib/message_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ void MessageHandler::register_handler(const std::string& topic, std::shared_ptr<
}
case HandlerType::SubscribeError: {
std::lock_guard<std::mutex> lg(handler_mutex);
error_handlers[topic] = handler;
error_handlers[topic].push_back(handler);
break;
}
case HandlerType::ExternalMQTT: {
Expand Down Expand Up @@ -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<std::pair<std::string, std::shared_ptr<TypedHandler>>> matching_handlers;
{
std::lock_guard<std::mutex> 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) {
Expand Down Expand Up @@ -383,6 +373,23 @@ void MessageHandler::execute_handlers_from_vector(HandlerMap& handlers, const st
}
}

template <typename HandlerMap, typename ExecuteFn>
void MessageHandler::execute_handlers_from_vector_with_wildcards(HandlerMap& handlers, const std::string& topic,
ExecuteFn execute_fn) {
std::vector<std::shared_ptr<TypedHandler>> handlers_copy;
{
std::lock_guard<std::mutex> 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 <typename HandlerMap, typename ExecuteFn>
void MessageHandler::execute_single_handler(HandlerMap& handlers, const std::string& topic, ExecuteFn execute_fn) {
std::shared_ptr<TypedHandler> handler_copy;
Expand Down
Loading