Skip to content

Commit

Permalink
feat: added parseMessage wildcards support
Browse files Browse the repository at this point in the history
  • Loading branch information
guglielmo-boi committed Mar 22, 2024
1 parent 2c30701 commit a24d384
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 24 deletions.
59 changes: 36 additions & 23 deletions src/templates/message_parser.cpp.j2
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ void MessageParser::setMessageParse(Topic topic, parse_t parse, void* argument)
}

void MessageParser::parseMessage(const Variables& variables, const std::string& topic, const std::string& payload) {
auto node = this->findNode(this->tree, topic, variables);
auto nodes = this->findNodesVariables(this->tree, topic, variables);

if(node != nullptr) {
for(auto& node : nodes) {
(*node->parse)(payload, node->argument);
}
}
Expand Down Expand Up @@ -83,33 +83,46 @@ MessageParser::TopicNode* MessageParser::findNode(TopicNode& node, const std::st
}
}

MessageParser::TopicNode* MessageParser::findNode(TopicNode& node, const std::string& topic, const Variables& variables) {
auto slash = std::find(topic.begin(), topic.end(), '/');
std::vector<MessageParser::TopicNode*> MessageParser::findNodesVariables(TopicNode& node, const std::string& topic, const Variables& variables) {
std::vector<TopicNode*> ret;
findNodesVariablesRec(node, topic, variables, false, ret);

return ret;
}

void MessageParser::findNodesVariablesRec(TopicNode& node, const std::string& topic, const Variables& variables, bool hashtag, std::vector<TopicNode*>& ret) {
auto slash = std::find(topic.begin(), topic.end(), '/');
std::string subTopic = std::string(topic.begin(), slash);
auto iter = node.adjacent.find(subTopic);

if(iter == node.adjacent.end()) {
iter = std::find_if(node.adjacent.begin(), node.adjacent.end(), [&subTopic, &variables](auto& next) {
{% for variable in variables -%}
if(next.first == "<{{ variable }}>" && subTopic == variables.{{ variable }}) {
return true;
}
{%- if not loop.last %} else {% endif -%}
{%- endfor %} else {
return false;
}
});
if(subTopic == "#") {
hashtag = true;
}

if(iter == node.adjacent.end()) {
return nullptr;
}

if(slash == topic.end()) {
return &(iter->second);
if(hashtag) {
for(auto& next : node.adjacent) {
ret.push_back(&(next.second));
findNodesVariablesRec(next.second, "", variables, hashtag, ret);
}
} else {
return findNode(iter->second, std::string(slash + 1, topic.end()), variables);
for(auto& next : node.adjacent) {
bool match = false;

if(subTopic == "+" || subTopic == next.first) {
match = true;
}
{%- for variable in variables %} else if(next.first == "<{{ variable }}>" && subTopic == variables.{{ variable }}) {
match = true;
}
{%- endfor %}

if(match) {
if(slash == topic.end()) {
ret.push_back(&(next.second));
} else {
findNodesVariablesRec(next.second, std::string(slash + 1, topic.end()), variables, hashtag, ret);
}
}
}
}
}
}
3 changes: 2 additions & 1 deletion src/templates/message_parser.h.j2
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ private:
void buildTree();
void addTopic(TopicNode& node, const std::string& topic);
static TopicNode* findNode(TopicNode& node, const std::string& topic);
static TopicNode* findNode(TopicNode& node, const std::string& topic, const Variables& variables);
static std::vector<TopicNode*> findNodesVariables(TopicNode& node, const std::string& topic, const Variables& variables);
static void findNodesVariablesRec(TopicNode& node, const std::string& topic, const Variables& variables, bool hashtag, std::vector<TopicNode*>& ret);

MessageParser::TopicNode tree;
};
Expand Down

0 comments on commit a24d384

Please sign in to comment.