topic_matcher.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. /////////////////////////////////////////////////////////////////////////////
  2. /// @file topic_matcher.h
  3. /// Declaration of MQTT topic_matcher class
  4. /// @date April 23, 2022
  5. /// @author Frank Pagliughi
  6. /////////////////////////////////////////////////////////////////////////////
  7. /*******************************************************************************
  8. * Copyright (c) 2022-2025 Frank Pagliughi <fpagliughi@mindspring.com>
  9. *
  10. * All rights reserved. This program and the accompanying materials
  11. * are made available under the terms of the Eclipse Public License v2.0
  12. * and Eclipse Distribution License v1.0 which accompany this distribution.
  13. *
  14. * The Eclipse Public License is available at
  15. * http://www.eclipse.org/legal/epl-v20.html
  16. * and the Eclipse Distribution License is available at
  17. * http://www.eclipse.org/org/documents/edl-v10.php.
  18. *
  19. * Contributors:
  20. * Frank Pagliughi - initial implementation and documentation
  21. *******************************************************************************/
  22. #ifndef __mqtt_topic_matcher_h
  23. #define __mqtt_topic_matcher_h
  24. #include <forward_list>
  25. #include <initializer_list>
  26. #include <map>
  27. #include <memory>
  28. #include <string>
  29. #include <vector>
  30. #include "mqtt/topic.h"
  31. #include "mqtt/types.h"
  32. namespace mqtt {
  33. /////////////////////////////////////////////////////////////////////////////
  34. /**
  35. * A collection of MQTT topic filters mapped to arbitrary values.
  36. *
  37. * This can be used to get an iterator to all filters in the collection that
  38. * match a topic. A typical use case might be to match incoming messages to
  39. * specific callback functions based on topics.
  40. *
  41. * To test against a single filter, see
  42. * [`TopicFilter`](crate::TopicFilter). This collection is more commonly
  43. * used when there are a number of filters and each needs to be associated
  44. * with a particular action or piece of data. Note, however, that a single
  45. * incoming topic could match against several items in the collection. For
  46. * example, the topic:
  47. *
  48. * @code
  49. * data/temperature/engine
  50. * @endcode
  51. *
  52. * Could match against the filters:
  53. * @code
  54. * data/temperature/engine
  55. * data/temperature/#
  56. * data/+/engine
  57. * @endcode
  58. *
  59. * Thus, the collection gives an iterator for the items matching a topic.
  60. *
  61. * A common use for this would be to store callbacks to process incoming
  62. * messages based on topics.
  63. *
  64. * Note, however, that MQTT v5 has Subscription Identifiers. These are
  65. * integer values that can be mapped to specific filters when subscribing.
  66. * The server will include the identifier when sending a message to the
  67. * client to indicate which filter the message matched. This can be used to
  68. * make a simple, efficient, lookup-table for callbacks, etc. It could be a
  69. * prefereble way to handle subscriptions when available.
  70. *
  71. * This code was adapted from the Eclipse Paho Python `MQTTMatcher` class:
  72. *
  73. *<https://github.com/eclipse/paho.mqtt.python/blob/master/src/paho/mqtt/matcher.py>
  74. *
  75. * which use a prefix tree (trie) to store the values.
  76. *
  77. * For example, if you had a `topic_mapper<int>` and you inserted:
  78. * @code
  79. * topic_matcher<int> tm{
  80. * {"some/random/topic", 42},
  81. * {"some/#", 99},
  82. * {"some/+/topic", 33}
  83. * };
  84. * @endcode
  85. *
  86. * The collection would be built like:
  87. * @code
  88. * "some" -> <null>
  89. * "random" -> <null>
  90. * "topic" -> <42>
  91. * "#" -> <99>
  92. * "+" -> <null>
  93. * "topic" -> <33>
  94. * @endcode
  95. *
  96. * Note that the collection has two types of iterators. The basic `iterator`
  97. * is a normal C++ iterator over *all* the items in the collection. It will
  98. * visit every node in the collection and produce all items. This is not the
  99. * typical use case for the collection, but can be used for diagnostics,
  100. * etc, to show the full contents of the collection.
  101. *
  102. * The more common use case is the `match_iterator`, returned by the
  103. * `topic_matcher::matches(string)` method. This is an optimized search
  104. * iterator for finding all the filters and values that match the specified
  105. * topic string.
  106. */
  107. template <typename T>
  108. class topic_matcher
  109. {
  110. public:
  111. using key_type = string;
  112. using mapped_type = T;
  113. using value_type = std::pair<key_type, mapped_type>;
  114. using reference = value_type;
  115. using const_reference = const value_type&;
  116. using value_ptr = std::unique_ptr<value_type>;
  117. using mapped_ptr = std::unique_ptr<mapped_type>;
  118. private:
  119. /**
  120. * The nodes of the collection.
  121. */
  122. struct node
  123. {
  124. using ptr_t = std::unique_ptr<node>;
  125. using map_t = std::map<string, ptr_t>;
  126. /** The value that matches the topic at this node, if any */
  127. value_ptr content;
  128. /** Child nodes mapped by the next field of the topic */
  129. map_t children;
  130. /** Creates a new, empty node */
  131. static ptr_t create() { return std::make_unique<node>(); }
  132. /** Determines if this node is empty (no content or children) */
  133. bool empty() const { return !content && children.empty(); }
  134. /** Removes the empty nodes under this one. */
  135. void prune() {
  136. for (auto& child : children) {
  137. child.second->prune();
  138. }
  139. for (auto child = children.cbegin(); child != children.cend();) {
  140. if (child->second->empty()) {
  141. child = children.erase(child);
  142. }
  143. else {
  144. ++child;
  145. }
  146. }
  147. }
  148. };
  149. using node_ptr = typename node::ptr_t;
  150. using node_map = typename node::map_t;
  151. /** The root node of the collection */
  152. node_ptr root_;
  153. public:
  154. /** Generic iterator over all items in the collection. */
  155. class iterator
  156. {
  157. /** The last-found value */
  158. value_type* pval_;
  159. /** The nodes still to be checked, used as a stack */
  160. std::vector<node*> nodes_;
  161. void next() {
  162. // If there are no nodes left to search, we're done.
  163. if (nodes_.empty()) {
  164. pval_ = nullptr;
  165. return;
  166. }
  167. // Get the next node to search.
  168. auto snode = std::move(nodes_.back());
  169. nodes_.pop_back();
  170. // Push the children onto the stack for later
  171. for (auto const& child : snode->children) {
  172. nodes_.push_back(child.second.get());
  173. }
  174. // If there's a value in this node, use it;
  175. // otherwise keep looking.
  176. pval_ = snode->content.get();
  177. if (!pval_)
  178. this->next();
  179. }
  180. friend class topic_matcher;
  181. iterator(value_type* pval) : pval_{pval} {}
  182. iterator(node* root) : pval_{nullptr} {
  183. nodes_.push_back(root);
  184. next();
  185. }
  186. public:
  187. /**
  188. * Gets a reference to the current value.
  189. * @return A reference to the current value.
  190. */
  191. reference operator*() noexcept { return *pval_; }
  192. /**
  193. * Gets a const reference to the current value.
  194. * @return A const reference to the current value.
  195. */
  196. const_reference operator*() const noexcept { return *pval_; }
  197. /**
  198. * Get a pointer to the current value.
  199. * @return A pointer to the current value.
  200. */
  201. value_type* operator->() noexcept { return pval_; }
  202. /**
  203. * Get a const pointer to the current value.
  204. * @return A const pointer to the current value.
  205. */
  206. const value_type* operator->() const noexcept { return pval_; }
  207. /**
  208. * Postfix increment operator.
  209. * @return An iterator pointing to the previous matching item.
  210. */
  211. iterator operator++(int) noexcept {
  212. auto tmp = *this;
  213. this->next();
  214. return tmp;
  215. }
  216. /**
  217. * Prefix increment operator.
  218. * @return An iterator pointing to the next matching item.
  219. */
  220. iterator& operator++() noexcept {
  221. this->next();
  222. return *this;
  223. }
  224. /**
  225. * Compares two iterators to see if they don't refer to the same
  226. * node.
  227. *
  228. * @param other The other iterator to compare against this one.
  229. * @return @em true if they don't match, @em false if they do
  230. */
  231. bool operator!=(const iterator& other) const noexcept { return pval_ != other.pval_; }
  232. };
  233. /** A const iterator over all itemsin the collection. */
  234. class const_iterator : public iterator
  235. {
  236. using base = iterator;
  237. friend class topic_matcher;
  238. const_iterator(iterator it) : base(it) {}
  239. public:
  240. /**
  241. * Gets a const reference to the current value.
  242. * @return A const reference to the current value.
  243. */
  244. const_reference operator*() const noexcept { return base::operator*(); }
  245. /**
  246. * Get a const pointer to the current value.
  247. * @return A const pointer to the current value.
  248. */
  249. const value_type* operator->() const noexcept { return base::operator->(); }
  250. };
  251. /**
  252. * Iterator that efficiently searches the collection for topic
  253. * matches.
  254. */
  255. class match_iterator
  256. {
  257. /** Information about a node that needs to be searched. */
  258. struct search_node
  259. {
  260. /** The current node being searched. */
  261. node* node_;
  262. /** The fields of the topic still to be searched. */
  263. std::forward_list<string> fields_;
  264. /** Whether this is the first/root node */
  265. bool first_;
  266. search_node(node* nd, const std::forward_list<string>& sy, bool first = false)
  267. : node_{nd}, fields_{sy}, first_{first} {}
  268. search_node(node* nd, std::forward_list<string>&& sy, bool first = false)
  269. : node_{nd}, fields_{std::move(sy)}, first_{first} {}
  270. };
  271. /** The last-found value */
  272. value_type* pval_;
  273. /** The nodes still to be checked, used as a stack */
  274. std::vector<search_node> nodes_;
  275. /**
  276. * Move the next iterator to the next value, or to end(), if none
  277. * left.
  278. *
  279. * This will keep recursing until it finds a matching node that
  280. * contains a value or it reaches the end.
  281. */
  282. void next() {
  283. pval_ = nullptr;
  284. // If there are no nodes left to search, we're done.
  285. if (nodes_.empty())
  286. return;
  287. // Get the next node to search.
  288. auto snode = std::move(nodes_.back());
  289. nodes_.pop_back();
  290. const auto map_end = snode.node_->children.end();
  291. typename node_map::iterator child;
  292. // If we're at the end of the topic fields, we either have a value,
  293. // or need to move on to the next node to search.
  294. if (snode.fields_.empty()) {
  295. pval_ = snode.node_->content.get();
  296. if (!pval_) {
  297. // ...but a '#' matches the parent topic
  298. if ((child = snode.node_->children.find("#")) != map_end) {
  299. pval_ = child->second->content.get();
  300. return;
  301. }
  302. this->next();
  303. }
  304. return;
  305. }
  306. // Get the next field of the topic to search
  307. auto field = std::move(snode.fields_.front());
  308. snode.fields_.pop_front();
  309. // Look for an exact match
  310. if ((child = snode.node_->children.find(field)) != map_end) {
  311. nodes_.push_back({child->second.get(), snode.fields_});
  312. }
  313. // Topics starting with '$' don't match wildcards in the first field
  314. // MQTT v5 Spec, Section 4.7.2:
  315. // https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901246
  316. if (!snode.first_ || field.empty() || field[0] != '$') {
  317. // Look for a single-field wildcard match
  318. if ((child = snode.node_->children.find("+")) != map_end) {
  319. nodes_.push_back({child->second.get(), snode.fields_});
  320. }
  321. // Look for a terminating match
  322. if ((child = snode.node_->children.find("#")) != map_end) {
  323. // By definition, a '#' is a terminating leaf
  324. pval_ = child->second->content.get();
  325. return;
  326. }
  327. }
  328. this->next();
  329. }
  330. friend class topic_matcher;
  331. match_iterator() : pval_{nullptr} {}
  332. match_iterator(value_type* pval) : pval_{pval} {}
  333. match_iterator(node* root, const string& topic) : pval_{nullptr} {
  334. auto v = topic::split(topic);
  335. std::forward_list<string> fields{v.begin(), v.end()};
  336. nodes_.push_back(search_node{root, std::move(fields), true});
  337. next();
  338. }
  339. public:
  340. /**
  341. * Gets a reference to the current value.
  342. * @return A reference to the current value.
  343. */
  344. reference operator*() noexcept { return *pval_; }
  345. /**
  346. * Gets a const reference to the current value.
  347. * @return A const reference to the current value.
  348. */
  349. const_reference operator*() const noexcept { return *pval_; }
  350. /**
  351. * Get a pointer to the current value.
  352. * @return A pointer to the current value.
  353. */
  354. value_type* operator->() noexcept { return pval_; }
  355. /**
  356. * Get a const pointer to the current value.
  357. * @return A const pointer to the current value.
  358. */
  359. const value_type* operator->() const noexcept { return pval_; }
  360. /**
  361. * Postfix increment operator.
  362. * @return An iterator pointing to the previous matching item.
  363. */
  364. match_iterator operator++(int) noexcept {
  365. auto tmp = *this;
  366. this->next();
  367. return tmp;
  368. }
  369. /**
  370. * Prefix increment operator.
  371. * @return An iterator pointing to the next matching item.
  372. */
  373. match_iterator& operator++() noexcept {
  374. this->next();
  375. return *this;
  376. }
  377. /**
  378. * Compares two iterators to see if they don't refer to the same
  379. * node.
  380. *
  381. * @param other The other iterator to compare against this one.
  382. * @return @em true if they don't match, @em false if they do
  383. */
  384. bool operator!=(const match_iterator& other) const noexcept {
  385. return pval_ != other.pval_;
  386. }
  387. };
  388. /**
  389. * A const match iterator.
  390. */
  391. class const_match_iterator : public match_iterator
  392. {
  393. using base = match_iterator;
  394. friend class topic_matcher;
  395. const_match_iterator(match_iterator it) : base(it) {}
  396. public:
  397. /**
  398. * Gets a const reference to the current value.
  399. * @return A const reference to the current value.
  400. */
  401. const_reference operator*() const noexcept { return base::operator*(); }
  402. /**
  403. * Get a const pointer to the current value.
  404. * @return A const pointer to the current value.
  405. */
  406. const value_type* operator->() const noexcept { return base::operator->(); }
  407. };
  408. /**
  409. * Creates new, empty collection.
  410. */
  411. topic_matcher() : root_(node::create()) {}
  412. /**
  413. * Creates a new collection with a list of key/value pairs.
  414. *
  415. * This can be used to create a connection from a table of entries, as
  416. * key/value pairs, like:
  417. *
  418. * topic_matcher<int> matcher {
  419. * { "#", -1 },
  420. * { "some/random/topic", 42 },
  421. * { "some/#", 99 }
  422. * }
  423. *
  424. * @param lst The list of key/value pairs to populate the collection.
  425. */
  426. topic_matcher(std::initializer_list<value_type> lst) : root_(node::create()) {
  427. for (const auto& v : lst) {
  428. insert(v);
  429. }
  430. }
  431. /**
  432. * Determines if the collection is empty.
  433. * @return @em true if the collection is empty, @em false if it contains
  434. * any filters.
  435. */
  436. bool empty() const { return root_.empty(); }
  437. /**
  438. * Inserts a new key/value pair into the collection.
  439. * @param val The value to place in the collection.
  440. */
  441. void insert(value_type&& val) {
  442. auto nd = root_.get();
  443. auto fields = topic::split(val.first);
  444. for (const auto& field : fields) {
  445. auto it = nd->children.find(field);
  446. if (it == nd->children.end()) {
  447. nd->children[field] = node::create();
  448. it = nd->children.find(field);
  449. }
  450. nd = it->second.get();
  451. }
  452. nd->content = std::make_unique<value_type>(std::move(val));
  453. }
  454. /**
  455. * Inserts a new value into the collection.
  456. * @param key The topic/filter entry
  457. * @param val The value to associate with that entry.
  458. */
  459. void insert(const value_type& val) {
  460. value_type v{val};
  461. this->insert(std::move(v));
  462. }
  463. /**
  464. * Removes an entry from the collection.
  465. *
  466. * This removes the value from the internal node, but leaves the node in
  467. * the collection, even if it is empty.
  468. * @param filter The topic filter to remove.
  469. * @return A unique pointer to the value, if any.
  470. */
  471. mapped_ptr remove(const key_type& filter) {
  472. auto nd = root_.get();
  473. auto fields = topic::split(filter);
  474. for (auto& field : fields) {
  475. auto it = nd->children.find(field);
  476. if (it == nd->children.end())
  477. return mapped_ptr{};
  478. nd = it->second.get();
  479. }
  480. value_ptr valpair;
  481. nd->content.swap(valpair);
  482. return (valpair) ? std::make_unique<mapped_type>(valpair->second) : mapped_ptr{};
  483. }
  484. /**
  485. * Removes the empty nodes in the collection.
  486. */
  487. void prune() { root_->prune(); }
  488. /**
  489. * Gets an iterator to the full collection of filters.
  490. * @return An iterator to the full collection of filters.
  491. */
  492. iterator begin() { return iterator{root_.get()}; }
  493. /**
  494. * Gets an iterator to the end of the collection of filters.
  495. * @return An iterator to the end of collection of filters.
  496. */
  497. iterator end() { return iterator{static_cast<value_type*>(nullptr)}; }
  498. /**
  499. * Gets an iterator to the end of the collection of filters.
  500. * @return An iterator to the end of collection of filters.
  501. */
  502. const_iterator end() const noexcept {
  503. return const_iterator{static_cast<value_type*>(nullptr)};
  504. }
  505. /**
  506. * Gets a const iterator to the full collection of filters.
  507. * @return A const iterator to the full collection of filters.
  508. */
  509. const_iterator cbegin() const { return const_iterator{root_.get()}; }
  510. /**
  511. * Gets a const iterator to the end of the collection of filters.
  512. * @return A const iterator to the end of collection of filters.
  513. */
  514. const_iterator cend() const noexcept { return end(); }
  515. /**
  516. * Gets a pointer to the value at the requested key.
  517. * @param filter The topic filter entry to find.
  518. * @return An iterator to the value if found, @em end() if not found.
  519. */
  520. iterator find(const key_type& filter) {
  521. auto nd = root_.get();
  522. auto fields = topic::split(filter);
  523. for (auto& field : fields) {
  524. auto it = nd->children.find(field);
  525. if (it == nd->children.end())
  526. return end();
  527. nd = it->second.get();
  528. }
  529. return iterator{nd->content.get()};
  530. }
  531. /**
  532. * Gets a const pointer to the value at the requested key.
  533. * @param filter The topic filter entry to find.
  534. * @return A const pointer to the value if found, @em nullptr if not
  535. * found.
  536. */
  537. const_iterator find(const key_type& filter) const {
  538. return const_cast<topic_matcher*>(this)->find(filter);
  539. }
  540. /**
  541. * Gets an match_iterator that can find the matches to the topic.
  542. * @param topic The topic to search for matches.
  543. * @return An iterator that can find the matches to the topic.
  544. */
  545. match_iterator matches(const string& topic) { return match_iterator(root_.get(), topic); }
  546. /**
  547. * Gets a const iterator that can find the matches to the topic.
  548. * @param topic The topic to search for matches.
  549. * @return A const iterator that can find the matches to the topic.
  550. */
  551. const_match_iterator matches(const string& topic) const {
  552. return match_iterator(root_.get(), topic);
  553. }
  554. /**
  555. * Gets an iterator for the end of the collection.
  556. *
  557. * This simply returns an empty/null iterator which we can use to signal
  558. * the end of the collection.
  559. *
  560. * @return An empty/null iterator indicating the end of the collection.
  561. */
  562. const_match_iterator matches_end() const noexcept { return match_iterator{}; }
  563. /**
  564. * Gets an iterator for the end of the collection.
  565. *
  566. * This simply returns an empty/null iterator which we can use to signal
  567. * the end of the collection.
  568. *
  569. * @return An empty/null iterator indicating the end of the collection.
  570. */
  571. const_match_iterator matches_cend() const noexcept { return match_iterator{}; }
  572. /**
  573. * Determines if there are any matches for the specified topic.
  574. * @param topic The topic to search for matches.
  575. * @return Whether there are any matches for the topic in the
  576. * collection.
  577. */
  578. bool has_match(const string& topic) { return matches(topic) != matches_cend(); }
  579. };
  580. /////////////////////////////////////////////////////////////////////////////
  581. } // namespace mqtt
  582. #endif // __mqtt_topic_matcher_h