| 1 | /** |
| 2 | * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | * SPDX-License-Identifier: Apache-2.0. |
| 4 | */ |
| 5 | |
| 6 | #ifndef AWS_MQTT_MQTT3_TO_MQTT5_ADAPTER_SUBSCRIPTION_SET_H |
| 7 | #define AWS_MQTT_MQTT3_TO_MQTT5_ADAPTER_SUBSCRIPTION_SET_H |
| 8 | |
| 9 | #include "aws/mqtt/mqtt.h" |
| 10 | |
| 11 | #include "aws/mqtt/client.h" |
| 12 | #include "aws/mqtt/v5/mqtt5_types.h" |
| 13 | #include <aws/common/hash_table.h> |
| 14 | |
| 15 | /** |
| 16 | * (Transient) configuration options about a single persistent MQTT topic filter subscription |
| 17 | */ |
| 18 | struct aws_mqtt_subscription_set_subscription_options { |
| 19 | struct aws_byte_cursor topic_filter; |
| 20 | |
| 21 | enum aws_mqtt5_qos qos; |
| 22 | |
| 23 | bool no_local; |
| 24 | bool retain_as_published; |
| 25 | enum aws_mqtt5_retain_handling_type retain_handling_type; |
| 26 | |
| 27 | /* Callback invoked when this subscription matches an incoming publish */ |
| 28 | aws_mqtt_client_publish_received_fn *on_publish_received; |
| 29 | |
| 30 | /* Callback invoked when this subscription is removed from the set */ |
| 31 | aws_mqtt_userdata_cleanup_fn *on_cleanup; |
| 32 | |
| 33 | void *callback_user_data; |
| 34 | }; |
| 35 | |
| 36 | /** |
| 37 | * Persistent structure to track a single MQTT topic filter subscription |
| 38 | */ |
| 39 | struct aws_mqtt_subscription_set_subscription_record { |
| 40 | struct aws_allocator *allocator; |
| 41 | struct aws_byte_buf topic_filter; |
| 42 | |
| 43 | struct aws_mqtt_subscription_set_subscription_options subscription_view; |
| 44 | }; |
| 45 | |
| 46 | /** |
| 47 | * (Transient) configuration options about an incoming publish message |
| 48 | */ |
| 49 | struct aws_mqtt_subscription_set_publish_received_options { |
| 50 | struct aws_mqtt_client_connection *connection; |
| 51 | |
| 52 | struct aws_byte_cursor topic; |
| 53 | enum aws_mqtt_qos qos; |
| 54 | bool retain; |
| 55 | bool dup; |
| 56 | |
| 57 | struct aws_byte_cursor payload; |
| 58 | }; |
| 59 | |
| 60 | /** |
| 61 | * A node in the topic trie maintained by the subscription set. Each node represents a single "path segment" in a |
| 62 | * topic filter "path." Segments can be empty. |
| 63 | * |
| 64 | * Some examples (topic filter -> path segments): |
| 65 | * |
| 66 | * "hello/world" -> [ "hello", "world" ] |
| 67 | * "a/b/" -> [ "a", "b", "" ] |
| 68 | * "/b/" -> [ "", "b", "" ] |
| 69 | * "a/#/c" -> [ "a", "#", "c" ] |
| 70 | * |
| 71 | * On incoming publish, we walk the tree invoking callbacks based on topic vs. topic filter matching, segment by |
| 72 | * segment. |
| 73 | * |
| 74 | */ |
| 75 | struct aws_mqtt_subscription_set_topic_tree_node { |
| 76 | struct aws_allocator *allocator; |
| 77 | |
| 78 | struct aws_byte_cursor topic_segment_cursor; /* segment can be empty */ |
| 79 | struct aws_byte_buf topic_segment; |
| 80 | |
| 81 | struct aws_mqtt_subscription_set_topic_tree_node *parent; |
| 82 | struct aws_hash_table children; /* (embedded topic_segment -> containing node) */ |
| 83 | |
| 84 | /* |
| 85 | * A node starts with a ref count of one and is incremented every time a new, overlapping path is added |
| 86 | * to the subscription set. When the ref count goes to zero, that means there are not subscriptions using the |
| 87 | * segment (or path suffix) represented by this node and therefor it can be deleted without any additional |
| 88 | * analysis. |
| 89 | * |
| 90 | * Replacing an existing path does not change the ref count. |
| 91 | */ |
| 92 | size_t ref_count; |
| 93 | |
| 94 | bool is_subscription; |
| 95 | |
| 96 | aws_mqtt_client_publish_received_fn *on_publish_received; |
| 97 | aws_mqtt_userdata_cleanup_fn *on_cleanup; |
| 98 | |
| 99 | void *callback_user_data; |
| 100 | }; |
| 101 | |
| 102 | /** |
| 103 | * Utility type to track a client's subscriptions. |
| 104 | * |
| 105 | * The topic tree supports per-subscription callbacks as used by the MQTT311 implementation. |
| 106 | * |
| 107 | * The subscriptions table supports resubscribe APIs for both MQTT311 and MQTT5 by tracking the subscription |
| 108 | * details on a per-topic-filter basis. |
| 109 | */ |
| 110 | struct aws_mqtt_subscription_set { |
| 111 | struct aws_allocator *allocator; |
| 112 | |
| 113 | /* a permanent ref */ |
| 114 | struct aws_mqtt_subscription_set_topic_tree_node *root; |
| 115 | |
| 116 | /* embedded topic_filter_cursor -> persistent subscription */ |
| 117 | struct aws_hash_table subscriptions; |
| 118 | }; |
| 119 | |
| 120 | AWS_EXTERN_C_BEGIN |
| 121 | |
| 122 | /** |
| 123 | * Creates a new subscription set |
| 124 | * |
| 125 | * @param allocator allocator to use |
| 126 | * @return a new subscription set or NULL |
| 127 | */ |
| 128 | AWS_MQTT_API struct aws_mqtt_subscription_set *aws_mqtt_subscription_set_new(struct aws_allocator *allocator); |
| 129 | |
| 130 | /** |
| 131 | * Destroys a subscription set |
| 132 | * |
| 133 | * @param subscription_set subscription set to destroy |
| 134 | */ |
| 135 | AWS_MQTT_API void aws_mqtt_subscription_set_destroy(struct aws_mqtt_subscription_set *subscription_set); |
| 136 | |
| 137 | /** |
| 138 | * Checks if a topic filter exists in the subscription set's hash table of subscriptions |
| 139 | * |
| 140 | * @param subscription_set subscription set to check |
| 141 | * @param topic_filter topic filter to check for existence in the set |
| 142 | * @return true if the topic filter exists in the table of subscriptions, false otherwise |
| 143 | */ |
| 144 | AWS_MQTT_API bool aws_mqtt_subscription_set_is_subscribed( |
| 145 | const struct aws_mqtt_subscription_set *subscription_set, |
| 146 | struct aws_byte_cursor topic_filter); |
| 147 | |
| 148 | /** |
| 149 | * Checks if a topic filter exists as a subscription (has a publish received handler) in the set's topic tree |
| 150 | * |
| 151 | * @param subscription_set subscription set to check |
| 152 | * @param topic_filter topic filter to check for existence in the set's topic tree |
| 153 | * @return true if the set's topic tree contains a publish received callback for the topic filter, false otherwise |
| 154 | */ |
| 155 | AWS_MQTT_API bool aws_mqtt_subscription_set_is_in_topic_tree( |
| 156 | const struct aws_mqtt_subscription_set *subscription_set, |
| 157 | struct aws_byte_cursor topic_filter); |
| 158 | |
| 159 | /** |
| 160 | * Adds a subscription to the subscription set. If a subscription already exists with a matching topic filter, it |
| 161 | * will be overwritten. |
| 162 | * |
| 163 | * @param subscription_set subscription set to add a subscription to |
| 164 | * @param subscription_options options for the new subscription |
| 165 | */ |
| 166 | AWS_MQTT_API void aws_mqtt_subscription_set_add_subscription( |
| 167 | struct aws_mqtt_subscription_set *subscription_set, |
| 168 | const struct aws_mqtt_subscription_set_subscription_options *subscription_options); |
| 169 | |
| 170 | /** |
| 171 | * Removes a subscription from the subscription set |
| 172 | * |
| 173 | * @param subscription_set subscription set to remove a subscription from |
| 174 | * @param topic_filter topic filter to remove subscription information for |
| 175 | */ |
| 176 | AWS_MQTT_API void aws_mqtt_subscription_set_remove_subscription( |
| 177 | struct aws_mqtt_subscription_set *subscription_set, |
| 178 | struct aws_byte_cursor topic_filter); |
| 179 | |
| 180 | /** |
| 181 | * Given a publish message, invokes all publish received handlers for matching subscriptions in the subscription set |
| 182 | * |
| 183 | * @param subscription_set subscription set to invoke publish received callbacks for |
| 184 | * @param publish_options received publish message properties |
| 185 | */ |
| 186 | AWS_MQTT_API void aws_mqtt_subscription_set_on_publish_received( |
| 187 | const struct aws_mqtt_subscription_set *subscription_set, |
| 188 | const struct aws_mqtt_subscription_set_publish_received_options *publish_options); |
| 189 | |
| 190 | /** |
| 191 | * Queries the properties of all subscriptions tracked by this subscription set. Used to implement re-subscribe |
| 192 | * behavior. |
| 193 | * |
| 194 | * @param subscription_set subscription set to query the subscriptions on |
| 195 | * @param subscriptions uninitialized array list to hold the subscriptions. |
| 196 | * |
| 197 | * The caller must invoke the cleanup function for array lists on the result. The list elements are of type |
| 198 | * 'struct aws_mqtt_subscription_set_subscription_options' and the topic filter cursor points to the subscription set's |
| 199 | * internal record. This means that the result must be used and cleaned up in local scope. |
| 200 | */ |
| 201 | AWS_MQTT_API void aws_mqtt_subscription_set_get_subscriptions( |
| 202 | struct aws_mqtt_subscription_set *subscription_set, |
| 203 | struct aws_array_list *subscriptions); |
| 204 | |
| 205 | /** |
| 206 | * Creates a new subscription record. A subscription record tracks all information about a single MQTT topic filter |
| 207 | * subscription |
| 208 | * |
| 209 | * @param allocator memory allocator to use |
| 210 | * @param subscription all relevant information about the subscription |
| 211 | * @return a new persistent subscription record |
| 212 | */ |
| 213 | AWS_MQTT_API struct aws_mqtt_subscription_set_subscription_record *aws_mqtt_subscription_set_subscription_record_new( |
| 214 | struct aws_allocator *allocator, |
| 215 | const struct aws_mqtt_subscription_set_subscription_options *subscription); |
| 216 | |
| 217 | /** |
| 218 | * Destroys a subscription record |
| 219 | * |
| 220 | * @param record subscription record to destroy |
| 221 | */ |
| 222 | AWS_MQTT_API void aws_mqtt_subscription_set_subscription_record_destroy( |
| 223 | struct aws_mqtt_subscription_set_subscription_record *record); |
| 224 | |
| 225 | AWS_EXTERN_C_END |
| 226 | |
| 227 | #endif /* AWS_MQTT_MQTT3_TO_MQTT5_ADAPTER_SUBSCRIPTION_SET_H */ |