Skip to content

Commit

Permalink
Do not use mqtt topic wildcard comparison for everest internal topics (
Browse files Browse the repository at this point in the history
…#75)

Topic and payload can be const std::string&

Use a structured binding to access topic key of message_handlers map

Cleanup and bump version to 0.4.1

Signed-off-by: Kai-Uwe Hermann <kai-uwe.hermann@pionix.de>
  • Loading branch information
hikinggrass authored Mar 20, 2023
1 parent 0576ad1 commit dbdd9bf
Showing 4 changed files with 19 additions and 12 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.14)

project(everest-framework
VERSION 0.4
VERSION 0.4.1
DESCRIPTION "The open operating system for e-mobility charging stations"
LANGUAGES CXX C
)
3 changes: 1 addition & 2 deletions lib/message_queue.cpp
Original file line number Diff line number Diff line change
@@ -58,14 +58,13 @@ MessageQueue::~MessageQueue() {
MessageHandler::MessageHandler() : running(true) {
this->handler_thread = std::thread([this]() {
while (true) {
std::shared_ptr<json> message;
std::unique_lock<std::mutex> lock(this->handler_ctrl_mutex);
this->cv.wait(lock, [this]() { return !this->message_queue.empty() || this->running == false; });
if (!this->running) {
return;
}

message = this->message_queue.front();
auto message = std::move(this->message_queue.front());
this->message_queue.pop();
lock.unlock();

25 changes: 17 additions & 8 deletions lib/mqtt_abstraction_impl.cpp
Original file line number Diff line number Diff line change
@@ -191,13 +191,15 @@ std::future<void> MQTTAbstractionImpl::spawn_main_loop_thread() {
void MQTTAbstractionImpl::on_mqtt_message(std::shared_ptr<Message> message) {
BOOST_LOG_FUNCTION();

std::string topic = message->topic;
std::string payload = message->payload;
const std::string& topic = message->topic;
const std::string& payload = message->payload;

try {
std::shared_ptr<json> data;
bool is_everest_topic = false;
if (topic.find(mqtt_everest_prefix) == 0) {
EVLOG_debug << fmt::format("topic {} starts with {}", topic, mqtt_everest_prefix);
is_everest_topic = true;
try {
data = std::make_shared<json>(json::parse(payload));
} catch (nlohmann::detail::parse_error& e) {
@@ -214,11 +216,19 @@ void MQTTAbstractionImpl::on_mqtt_message(std::shared_ptr<Message> message) {

std::unique_lock<std::mutex> lock(handlers_mutex);
std::vector<Handler> local_handlers;
for (auto& ha : this->message_handlers) {
std::string handler_topic = ha.first;
if (MQTTAbstractionImpl::check_topic_matches(topic, handler_topic)) {
for (auto& [handler_topic, handler] : this->message_handlers) {
bool topic_matches = false;
if (is_everest_topic) {
// everest topics never contain wildcards, so a direct comparison is enough
if (topic == handler_topic) {
topic_matches = true;
}
} else {
topic_matches = MQTTAbstractionImpl::check_topic_matches(topic, handler_topic);
}
if (topic_matches) {
found = true;
ha.second.add(data);
handler.add(data);
}
}
lock.unlock();
@@ -246,8 +256,7 @@ void MQTTAbstractionImpl::on_mqtt_connect() {
// subscribe to all topics needed by currently registered handlers
EVLOG_debug << "Subscribing to needed MQTT topics...";
const std::lock_guard<std::mutex> lock(handlers_mutex);
for (auto const& ha : this->message_handlers) {
std::string topic = ha.first;
for (auto const& [topic, handler] : this->message_handlers) {
EVLOG_debug << fmt::format("Subscribing to {}", topic);
subscribe(topic); // FIXME(kai): get QOS from handler
}
1 change: 0 additions & 1 deletion src/controller/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -18,7 +18,6 @@ add_executable(controller

target_include_directories(controller PRIVATE
${PROJECT_SOURCE_DIR}/include
${websocketpp_SOURCE_DIR}
)

target_link_libraries(controller

0 comments on commit dbdd9bf

Please sign in to comment.