Debian Codegraph @

crt/aws-c-mqtt/include/aws/mqtt/private/topic_tree.h

raw symbols SCIP
1 #ifndef AWS_MQTT_PRIVATE_TOPIC_TREE_H
2 #define AWS_MQTT_PRIVATE_TOPIC_TREE_H
3
4 /**
5 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
6 * SPDX-License-Identifier: Apache-2.0.
7 */
8
9 #include <aws/common/hash_table.h>
10
11 #include <aws/mqtt/private/packets.h>
12
13 /** Type of function called when a publish received matches a subscription */
14 typedef void(aws_mqtt_publish_received_fn)(
15 const struct aws_byte_cursor *topic,
16 const struct aws_byte_cursor *payload,
17 bool dup,
18 enum aws_mqtt_qos qos,
19 bool retain,
20 void *user_data);
21
22 /**
23 * Function called per subscription when iterating through subscriptions.
24 * Return true to continue iteration, or false to stop.
25 */
26 typedef bool(
27 aws_mqtt_topic_tree_iterator_fn)(const struct aws_byte_cursor *topic, enum aws_mqtt_qos qos, void *user_data);
28
29 struct aws_mqtt_topic_node {
30
31 /* This node's part of the topic filter. If in another node's subtopics, this is the key. */
32 struct aws_byte_cursor topic;
33
34 /**
35 * aws_byte_cursor -> aws_mqtt_topic_node
36 * '#' and '+' are special values in here
37 */
38 struct aws_hash_table subtopics;
39
40 /* The entire topic filter. If !owns_topic_filter, this topic_filter belongs to someone else. */
41 const struct aws_string *topic_filter;
42 bool owns_topic_filter;
43
44 /* The following will only be populated if the node IS a subscription */
45 /* Max QoS to deliver. */
46 enum aws_mqtt_qos qos;
47 /* Callback to call on message received */
48 aws_mqtt_publish_received_fn *callback;
49 aws_mqtt_userdata_cleanup_fn *cleanup;
50 void *userdata;
51 };
52
53 struct aws_mqtt_topic_tree {
54 struct aws_mqtt_topic_node *root;
55 struct aws_allocator *allocator;
56 };
57
58 /**
59 * The size of transaction instances.
60 * When you initialize an aws_array_list for use as a transaction, pass this as the item size.
61 */
62 extern AWS_MQTT_API size_t aws_mqtt_topic_tree_action_size;
63
64 /**
65 * Initialize a topic tree with an allocator to later use.
66 * Note that calling init allocates root.
67 */
68 AWS_MQTT_API int aws_mqtt_topic_tree_init(struct aws_mqtt_topic_tree *tree, struct aws_allocator *allocator);
69 /**
70 * Cleanup and deallocate an entire topic tree.
71 */
72 AWS_MQTT_API void aws_mqtt_topic_tree_clean_up(struct aws_mqtt_topic_tree *tree);
73
74 /**
75 * Iterates through all registered subscriptions, and calls iterator.
76 *
77 * Iterator may return false to stop iterating, or true to continue.
78 */
79 AWS_MQTT_API void aws_mqtt_topic_tree_iterate(
80 const struct aws_mqtt_topic_tree *tree,
81 aws_mqtt_topic_tree_iterator_fn *iterator,
82 void *user_data);
83
84 /**
85 * Gets the total number of subscriptions in the tree.
86 */
87 AWS_MQTT_API size_t aws_mqtt_topic_tree_get_sub_count(const struct aws_mqtt_topic_tree *tree);
88
89 /**
90 * Insert a new topic filter into the subscription tree (subscribe).
91 *
92 * \param[in] tree The tree to insert into.
93 * \param[in] transaction The transaction to add the insert action to.
94 * Must be initialized with aws_mqtt_topic_tree_action_size as item size.
95 * \param[in] topic_filter The topic filter to subscribe on. May contain wildcards.
96 * \param[in] callback The callback to call on a publish with a matching topic.
97 * \param[in] connection The connection object to pass to the callback. This is a void* to support client and server
98 * connections in the future.
99 * \param[in] userdata The userdata to pass to callback.
100 *
101 * \returns AWS_OP_SUCCESS on successful insertion, AWS_OP_ERR with aws_last_error() populated on failure.
102 * If AWS_OP_ERR is returned, aws_mqtt_topic_tree_transaction_rollback should be called to prevent leaks.
103 */
104 AWS_MQTT_API int aws_mqtt_topic_tree_transaction_insert(
105 struct aws_mqtt_topic_tree *tree,
106 struct aws_array_list *transaction,
107 const struct aws_string *topic_filter,
108 enum aws_mqtt_qos qos,
109 aws_mqtt_publish_received_fn *callback,
110 aws_mqtt_userdata_cleanup_fn *cleanup,
111 void *userdata);
112
113 /**
114 * Remove a topic filter from the subscription tree (unsubscribe).
115 *
116 * \param[in] tree The tree to remove from.
117 * \param[in] transaction The transaction to add the insert action to.
118 * Must be initialized with aws_mqtt_topic_tree_action_size as item size.
119 * \param[in] topic_filter The filter to remove (must be exactly the same as the topic_filter passed to insert).
120 * \param[out] old_userdata The userdata assigned to this subscription will be assigned if not NULL.
121 * \NOTE once the transaction is committed, old_userdata may be destroyed,
122 * if a cleanup callback was set on insert.
123 *
124 * \returns AWS_OP_SUCCESS on successful removal, AWS_OP_ERR with aws_last_error() populated on failure.
125 * If AWS_OP_ERR is returned, aws_mqtt_topic_tree_transaction_rollback should be called to prevent leaks.
126 */
127 AWS_MQTT_API int aws_mqtt_topic_tree_transaction_remove(
128 struct aws_mqtt_topic_tree *tree,
129 struct aws_array_list *transaction,
130 const struct aws_byte_cursor *topic_filter,
131 void **old_userdata);
132
133 AWS_MQTT_API void aws_mqtt_topic_tree_transaction_commit(
134 struct aws_mqtt_topic_tree *tree,
135 struct aws_array_list *transaction);
136
137 AWS_MQTT_API void aws_mqtt_topic_tree_transaction_roll_back(
138 struct aws_mqtt_topic_tree *tree,
139 struct aws_array_list *transaction);
140
141 /**
142 * Insert a new topic filter into the subscription tree (subscribe).
143 *
144 * \param[in] tree The tree to insert into.
145 * \param[in] topic_filter The topic filter to subscribe on. May contain wildcards.
146 * \param[in] callback The callback to call on a publish with a matching topic.
147 * \param[in] connection The connection object to pass to the callback. This is a void* to support client and server
148 * connections in the future.
149 * \param[in] userdata The userdata to pass to callback.
150 *
151 * \returns AWS_OP_SUCCESS on successful insertion, AWS_OP_ERR with aws_last_error() populated on failure.
152 */
153 AWS_MQTT_API
154 int aws_mqtt_topic_tree_insert(
155 struct aws_mqtt_topic_tree *tree,
156 const struct aws_string *topic_filter,
157 enum aws_mqtt_qos qos,
158 aws_mqtt_publish_received_fn *callback,
159 aws_mqtt_userdata_cleanup_fn *cleanup,
160 void *userdata);
161
162 AWS_MQTT_API
163 int aws_mqtt_topic_tree_remove(struct aws_mqtt_topic_tree *tree, const struct aws_byte_cursor *topic_filter);
164
165 /**
166 * Dispatches a publish packet to all subscriptions matching the publish topic.
167 *
168 * \param[in] tree The tree to publish on.
169 * \param[in] pub The publish packet to dispatch. The topic MUST NOT contain wildcards.
170 */
171 void AWS_MQTT_API
172 aws_mqtt_topic_tree_publish(const struct aws_mqtt_topic_tree *tree, struct aws_mqtt_packet_publish *pub);
173
174 #endif /* AWS_MQTT_PRIVATE_TOPIC_TREE_H */