Skip to content

Commit

Permalink
fix: moved functions to TopicNode
Browse files Browse the repository at this point in the history
  • Loading branch information
guglielmo-boi committed Mar 23, 2024
1 parent 65bfd5a commit 7ec39e4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 56 deletions.
96 changes: 46 additions & 50 deletions src/templates/message_parser.cpp.j2
Original file line number Diff line number Diff line change
Expand Up @@ -15,84 +15,50 @@ MessageParser::Variables::Variables(
{%- if not loop.last %}, {% endif -%}
{%- endfor %} {}

MessageParser::MessageParser() {
this->buildTree();
}

void MessageParser::setMessageParse(Topic topic, parse_t parse, void* argument) {
auto node = this->findNode(this->tree, GetTopic(topic).topic);

if(node != nullptr) {
node->parse = std::make_unique<parse_t>(parse);

if(argument != nullptr) {
node->argument = argument;
}
}
}

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

for(auto& node : nodes) {
if(node->parse != nullptr) {
(*node->parse)(payload, node->argument);
}
}
}

void MessageParser::buildTree() {
{% for topic in topics -%}
this->addTopic(this->tree, "{{ topic['topic'] }}");
{%- if not loop.last%}
{%endif -%}
{%- endfor %}
}

void MessageParser::addTopic(TopicNode& node, const std::string& topic) {
void MessageParser::TopicNode::addNode(const std::string& topic) {
if(topic.empty()) {
return;
}

auto slash = std::find(topic.begin(), topic.end(), '/');

std::string subTopic = std::string(topic.begin(), slash);
auto iter = node.adjacent.find(subTopic);
auto iter = this->adjacent.find(subTopic);

if(iter == node.adjacent.end()) {
iter = node.adjacent.emplace(subTopic, TopicNode()).first;
if(iter == this->adjacent.end()) {
iter = this->adjacent.emplace(subTopic, TopicNode()).first;
}

if(slash != topic.end()) {
this->addTopic(iter->second, std::string(slash + 1, topic.end()));
iter->second.addNode(std::string(slash + 1, topic.end()));
}
}

MessageParser::TopicNode* MessageParser::findNode(TopicNode& node, const std::string& topic) {
MessageParser::TopicNode* MessageParser::TopicNode::findNode(const std::string& topic) {
auto slash = std::find(topic.begin(), topic.end(), '/');

std::string subTopic = std::string(topic.begin(), slash);
auto iter = node.adjacent.find(subTopic);
auto iter = this->adjacent.find(subTopic);

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

if(slash == topic.end()) {
return &(iter->second);
} else {
return findNode(iter->second, std::string(slash + 1, topic.end()));
return iter->second.findNode(std::string(slash + 1, topic.end()));
}
}

std::vector<MessageParser::TopicNode*> MessageParser::findNodesVariables(TopicNode& node, const std::string& topic, const Variables& variables) {
std::vector<MessageParser::TopicNode*> MessageParser::TopicNode::findNodesVariables(const std::string& topic, const Variables& variables) {
std::vector<TopicNode*> ret;
findNodesVariablesRec(node, topic, variables, false, ret);
this->findNodesVariablesRec(topic, variables, false, ret);

return ret;
}

void MessageParser::findNodesVariablesRec(TopicNode& node, const std::string& topic, const Variables& variables, bool hashtag, std::vector<TopicNode*>& ret) {
void MessageParser::TopicNode::findNodesVariablesRec(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);

Expand All @@ -101,12 +67,12 @@ void MessageParser::findNodesVariablesRec(TopicNode& node, const std::string& to
}

if(hashtag) {
for(auto& next : node.adjacent) {
for(auto& next : this->adjacent) {
ret.push_back(&(next.second));
findNodesVariablesRec(next.second, "", variables, hashtag, ret);
next.second.findNodesVariablesRec("", variables, hashtag, ret);
}
} else {
for(auto& next : node.adjacent) {
for(auto& next : this->adjacent) {
bool match = false;

if(subTopic == "+" || subTopic == next.first) {
Expand All @@ -121,10 +87,40 @@ void MessageParser::findNodesVariablesRec(TopicNode& node, const std::string& to
if(slash == topic.end()) {
ret.push_back(&(next.second));
} else {
findNodesVariablesRec(next.second, std::string(slash + 1, topic.end()), variables, hashtag, ret);
next.second.findNodesVariablesRec(std::string(slash + 1, topic.end()), variables, hashtag, ret);
}
}
}
}
}

MessageParser::MessageParser() {
{% for topic in topics -%}
this->tree.addNode("{{ topic['topic'] }}");
{%- if not loop.last%}
{%endif -%}
{%- endfor %}
}

void MessageParser::setMessageParse(Topic topic, parse_t parse, void* argument) {
auto node = this->tree.findNode(GetTopic(topic).topic);

if(node != nullptr) {
node->parse = std::make_unique<parse_t>(parse);

if(argument != nullptr) {
node->argument = argument;
}
}
}

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

for(auto& node : nodes) {
if(node->parse != nullptr) {
(*node->parse)(payload, node->argument);
}
}
}
}
11 changes: 5 additions & 6 deletions src/templates/message_parser.h.j2
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ private:
std::unique_ptr<parse_t> parse;
void* argument = nullptr;
std::unordered_map<std::string, TopicNode> adjacent;

void addNode(const std::string& topic);
TopicNode* findNode(const std::string& topic);
std::vector<TopicNode*> findNodesVariables(const std::string& topic, const Variables& variables);
void findNodesVariablesRec(const std::string& topic, const Variables& variables, bool hashtag, std::vector<TopicNode*>& ret);
};

public:
Expand All @@ -46,12 +51,6 @@ public:
void parseMessage(const Variables& variables, const std::string& topic, const std::string& payload);

private:
void buildTree();
void addTopic(TopicNode& node, const std::string& topic);
static TopicNode* findNode(TopicNode& node, const std::string& topic);
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 7ec39e4

Please sign in to comment.