| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/mqtt/private/request-response/request_response_subscription_set.h> |
| 7 |
|
| 8 |
#include <aws/mqtt/mqtt.h> |
| 9 |
#include <aws/mqtt/private/client_impl_shared.h> |
| 10 |
#include <aws/mqtt/request-response/request_response_client.h> |
| 11 |
|
| 12 |
#define MQTT_RR_CLIENT_RESPONSE_TABLE_DEFAULT_SIZE 50 |
| 13 |
#define MQTT_RR_CLIENT_OPERATION_TABLE_DEFAULT_SIZE 50 |
| 14 |
|
| 15 |
static void s_aws_rr_operation_list_topic_filter_entry_destroy(struct aws_rr_operation_list_topic_filter_entry *entry) { |
| 16 |
if (entry == NULL) { |
| 17 |
return; |
| 18 |
} |
| 19 |
|
| 20 |
aws_byte_buf_clean_up(&entry->topic_filter); |
| 21 |
|
| 22 |
aws_mem_release(entry->allocator, entry); |
| 23 |
} |
| 24 |
|
| 25 |
static void s_aws_rr_operation_list_topic_filter_entry_hash_element_destroy(void *value) { |
| 26 |
s_aws_rr_operation_list_topic_filter_entry_destroy(value); |
| 27 |
} |
| 28 |
|
| 29 |
static struct aws_rr_response_path_entry *s_aws_rr_response_path_entry_new( |
| 30 |
struct aws_allocator *allocator, |
| 31 |
struct aws_byte_cursor topic, |
| 32 |
struct aws_byte_cursor correlation_token_json_path) { |
| 33 |
struct aws_rr_response_path_entry *entry = aws_mem_calloc(allocator, 1, sizeof(struct aws_rr_response_path_entry)); |
| 34 |
|
| 35 |
entry->allocator = allocator; |
| 36 |
entry->ref_count = 1; |
| 37 |
aws_byte_buf_init_copy_from_cursor(&entry->topic, allocator, topic); |
| 38 |
entry->topic_cursor = aws_byte_cursor_from_buf(&entry->topic); |
| 39 |
|
| 40 |
aws_byte_buf_init_copy_from_cursor(&entry->correlation_token_json_path, allocator, correlation_token_json_path); |
| 41 |
|
| 42 |
return entry; |
| 43 |
} |
| 44 |
|
| 45 |
static void s_aws_rr_response_path_entry_destroy(struct aws_rr_response_path_entry *entry) { |
| 46 |
if (entry == NULL) { |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
aws_byte_buf_clean_up(&entry->topic); |
| 51 |
aws_byte_buf_clean_up(&entry->correlation_token_json_path); |
| 52 |
|
| 53 |
aws_mem_release(entry->allocator, entry); |
| 54 |
} |
| 55 |
|
| 56 |
static void s_aws_rr_response_path_table_hash_element_destroy(void *value) { |
| 57 |
s_aws_rr_response_path_entry_destroy(value); |
| 58 |
} |
| 59 |
|
| 60 |
int aws_mqtt_request_response_client_subscriptions_init( |
| 61 |
struct aws_request_response_subscriptions *subscriptions, |
| 62 |
struct aws_allocator *allocator) { |
| 63 |
AWS_FATAL_ASSERT(subscriptions); |
| 64 |
|
| 65 |
subscriptions->allocator = allocator; |
| 66 |
|
| 67 |
if (aws_hash_table_init( |
| 68 |
&subscriptions->streaming_operation_subscription_lists, |
| 69 |
allocator, |
| 70 |
MQTT_RR_CLIENT_OPERATION_TABLE_DEFAULT_SIZE, |
| 71 |
aws_hash_byte_cursor_ptr, |
| 72 |
aws_mqtt_byte_cursor_hash_equality, |
| 73 |
NULL, |
| 74 |
s_aws_rr_operation_list_topic_filter_entry_hash_element_destroy)) { |
| 75 |
goto clean_up; |
| 76 |
} |
| 77 |
|
| 78 |
if (aws_hash_table_init( |
| 79 |
&subscriptions->streaming_operation_wildcards_subscription_lists, |
| 80 |
allocator, |
| 81 |
MQTT_RR_CLIENT_OPERATION_TABLE_DEFAULT_SIZE, |
| 82 |
aws_hash_byte_cursor_ptr, |
| 83 |
aws_mqtt_byte_cursor_hash_equality, |
| 84 |
NULL, |
| 85 |
s_aws_rr_operation_list_topic_filter_entry_hash_element_destroy)) { |
| 86 |
goto clean_up; |
| 87 |
} |
| 88 |
|
| 89 |
if (aws_hash_table_init( |
| 90 |
&subscriptions->request_response_paths, |
| 91 |
allocator, |
| 92 |
MQTT_RR_CLIENT_RESPONSE_TABLE_DEFAULT_SIZE, |
| 93 |
aws_hash_byte_cursor_ptr, |
| 94 |
aws_mqtt_byte_cursor_hash_equality, |
| 95 |
NULL, |
| 96 |
s_aws_rr_response_path_table_hash_element_destroy)) { |
| 97 |
goto clean_up; |
| 98 |
} |
| 99 |
|
| 100 |
return AWS_OP_SUCCESS; |
| 101 |
|
| 102 |
clean_up: |
| 103 |
aws_mqtt_request_response_client_subscriptions_clean_up(subscriptions); |
| 104 |
return AWS_OP_ERR; |
| 105 |
} |
| 106 |
|
| 107 |
void aws_mqtt_request_response_client_subscriptions_clean_up(struct aws_request_response_subscriptions *subscriptions) { |
| 108 |
if (subscriptions == NULL) { |
| 109 |
return; |
| 110 |
} |
| 111 |
|
| 112 |
if (aws_hash_table_is_valid(&subscriptions->streaming_operation_subscription_lists)) { |
| 113 |
aws_hash_table_clean_up(&subscriptions->streaming_operation_subscription_lists); |
| 114 |
} |
| 115 |
if (aws_hash_table_is_valid(&subscriptions->streaming_operation_wildcards_subscription_lists)) { |
| 116 |
aws_hash_table_clean_up(&subscriptions->streaming_operation_wildcards_subscription_lists); |
| 117 |
} |
| 118 |
if (aws_hash_table_is_valid(&subscriptions->request_response_paths)) { |
| 119 |
aws_hash_table_clean_up(&subscriptions->request_response_paths); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
static struct aws_rr_operation_list_topic_filter_entry *s_aws_rr_operation_list_topic_filter_entry_new( |
| 124 |
struct aws_allocator *allocator, |
| 125 |
struct aws_byte_cursor topic_filter) { |
| 126 |
struct aws_rr_operation_list_topic_filter_entry *entry = |
| 127 |
aws_mem_calloc(allocator, 1, sizeof(struct aws_rr_operation_list_topic_filter_entry)); |
| 128 |
|
| 129 |
entry->allocator = allocator; |
| 130 |
aws_byte_buf_init_copy_from_cursor(&entry->topic_filter, allocator, topic_filter); |
| 131 |
entry->topic_filter_cursor = aws_byte_cursor_from_buf(&entry->topic_filter); |
| 132 |
|
| 133 |
aws_linked_list_init(&entry->operations); |
| 134 |
|
| 135 |
return entry; |
| 136 |
} |
| 137 |
|
| 138 |
static bool s_is_topic_filter_with_wildcard(const struct aws_byte_cursor *topic_filter) { |
| 139 |
bool res = (memchr(topic_filter->ptr, '+', topic_filter->len) || memchr(topic_filter->ptr, '#', topic_filter->len)); |
| 140 |
return res; |
| 141 |
} |
| 142 |
|
| 143 |
struct aws_rr_operation_list_topic_filter_entry *aws_mqtt_request_response_client_subscriptions_add_stream_subscription( |
| 144 |
struct aws_request_response_subscriptions *subscriptions, |
| 145 |
const struct aws_byte_cursor *topic_filter) { |
| 146 |
AWS_FATAL_ASSERT(subscriptions); |
| 147 |
|
| 148 |
struct aws_hash_table *subscription_lists = s_is_topic_filter_with_wildcard(topic_filter) |
| 149 |
? &subscriptions->streaming_operation_wildcards_subscription_lists |
| 150 |
: &subscriptions->streaming_operation_subscription_lists; |
| 151 |
|
| 152 |
struct aws_hash_element *element = NULL; |
| 153 |
if (aws_hash_table_find(subscription_lists, topic_filter, &element)) { |
| 154 |
aws_raise_error(AWS_ERROR_MQTT_REQUEST_RESPONSE_INTERNAL_ERROR); |
| 155 |
return NULL; |
| 156 |
} |
| 157 |
|
| 158 |
struct aws_rr_operation_list_topic_filter_entry *entry = NULL; |
| 159 |
if (element == NULL) { |
| 160 |
entry = s_aws_rr_operation_list_topic_filter_entry_new(subscriptions->allocator, *topic_filter); |
| 161 |
aws_hash_table_put(subscription_lists, &entry->topic_filter_cursor, entry, NULL); |
| 162 |
} else { |
| 163 |
entry = element->value; |
| 164 |
} |
| 165 |
|
| 166 |
AWS_FATAL_ASSERT(entry != NULL); |
| 167 |
|
| 168 |
return entry; |
| 169 |
} |
| 170 |
|
| 171 |
int aws_mqtt_request_response_client_subscriptions_add_request_subscription( |
| 172 |
struct aws_request_response_subscriptions *subscriptions, |
| 173 |
const struct aws_byte_cursor *topic_filter, |
| 174 |
const struct aws_byte_cursor *correlation_token_json_path) { |
| 175 |
struct aws_hash_element *element = NULL; |
| 176 |
if (aws_hash_table_find(&subscriptions->request_response_paths, topic_filter, &element)) { |
| 177 |
return aws_raise_error(AWS_ERROR_MQTT_REQUEST_RESPONSE_INTERNAL_ERROR); |
| 178 |
} |
| 179 |
|
| 180 |
if (element != NULL) { |
| 181 |
struct aws_rr_response_path_entry *entry = element->value; |
| 182 |
++entry->ref_count; |
| 183 |
return AWS_OP_SUCCESS; |
| 184 |
} |
| 185 |
|
| 186 |
struct aws_rr_response_path_entry *entry = |
| 187 |
s_aws_rr_response_path_entry_new(subscriptions->allocator, *topic_filter, *correlation_token_json_path); |
| 188 |
if (aws_hash_table_put(&subscriptions->request_response_paths, &entry->topic_cursor, entry, NULL)) { |
| 189 |
s_aws_rr_response_path_entry_destroy(entry); |
| 190 |
return aws_raise_error(AWS_ERROR_MQTT_REQUEST_RESPONSE_INTERNAL_ERROR); |
| 191 |
} |
| 192 |
|
| 193 |
return AWS_OP_SUCCESS; |
| 194 |
} |
| 195 |
|
| 196 |
int aws_mqtt_request_response_client_subscriptions_remove_request_subscription( |
| 197 |
struct aws_request_response_subscriptions *subscriptions, |
| 198 |
const struct aws_byte_cursor *topic_filter) { |
| 199 |
|
| 200 |
AWS_FATAL_ASSERT(subscriptions); |
| 201 |
AWS_FATAL_ASSERT(topic_filter); |
| 202 |
|
| 203 |
struct aws_hash_element *element = NULL; |
| 204 |
if (aws_hash_table_find(&subscriptions->request_response_paths, topic_filter, &element) || element == NULL) { |
| 205 |
return AWS_OP_ERR; |
| 206 |
} |
| 207 |
|
| 208 |
struct aws_rr_response_path_entry *entry = element->value; |
| 209 |
--entry->ref_count; |
| 210 |
|
| 211 |
if (entry->ref_count == 0) { |
| 212 |
aws_hash_table_remove(&subscriptions->request_response_paths, topic_filter, NULL, NULL); |
| 213 |
} |
| 214 |
|
| 215 |
return AWS_OP_SUCCESS; |
| 216 |
} |
| 217 |
|
| 218 |
static void s_match_stream_subscriptions( |
| 219 |
const struct aws_hash_table *subscriptions, |
| 220 |
const struct aws_mqtt_rr_incoming_publish_event *publish_event, |
| 221 |
aws_mqtt_stream_operation_subscription_match_fn *on_stream_operation_subscription_match, |
| 222 |
void *user_data) { |
| 223 |
struct aws_hash_element *subscription_filter_element = NULL; |
| 224 |
if (aws_hash_table_find(subscriptions, &publish_event->topic, &subscription_filter_element) == AWS_OP_SUCCESS && |
| 225 |
subscription_filter_element != NULL) { |
| 226 |
|
| 227 |
struct aws_rr_operation_list_topic_filter_entry *entry = subscription_filter_element->value; |
| 228 |
on_stream_operation_subscription_match( |
| 229 |
&entry->operations, &entry->topic_filter_cursor, publish_event, user_data); |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
static bool s_is_topic_matched_to_subscription( |
| 234 |
struct aws_byte_cursor topic, |
| 235 |
struct aws_byte_cursor topic_filter_cursor) { |
| 236 |
bool is_matched = true; |
| 237 |
bool multi_level_wildcard = false; |
| 238 |
|
| 239 |
struct aws_byte_cursor subscription_topic_filter_segment; |
| 240 |
AWS_ZERO_STRUCT(subscription_topic_filter_segment); |
| 241 |
|
| 242 |
struct aws_byte_cursor topic_segment; |
| 243 |
AWS_ZERO_STRUCT(topic_segment); |
| 244 |
|
| 245 |
while (aws_byte_cursor_next_split(&topic_filter_cursor, '/', &subscription_topic_filter_segment)) { |
| 246 |
if (!aws_byte_cursor_next_split(&topic, '/', &topic_segment)) { |
| 247 |
is_matched = false; |
| 248 |
break; |
| 249 |
} |
| 250 |
|
| 251 |
if (aws_byte_cursor_eq_c_str(&subscription_topic_filter_segment, "#")) { |
| 252 |
multi_level_wildcard = true; |
| 253 |
is_matched = true; |
| 254 |
break; |
| 255 |
} |
| 256 |
|
| 257 |
if (!aws_byte_cursor_eq_c_str(&subscription_topic_filter_segment, "+") && |
| 258 |
!aws_byte_cursor_eq(&topic_segment, &subscription_topic_filter_segment)) { |
| 259 |
is_matched = false; |
| 260 |
break; |
| 261 |
} |
| 262 |
} |
| 263 |
if (!multi_level_wildcard && aws_byte_cursor_next_split(&topic, '/', &topic_segment)) { |
| 264 |
is_matched = false; |
| 265 |
} |
| 266 |
|
| 267 |
return is_matched; |
| 268 |
} |
| 269 |
|
| 270 |
static void s_match_wildcard_stream_subscriptions( |
| 271 |
const struct aws_hash_table *subscriptions, |
| 272 |
const struct aws_mqtt_rr_incoming_publish_event *publish_event, |
| 273 |
aws_mqtt_stream_operation_subscription_match_fn *on_stream_operation_subscription_match, |
| 274 |
void *user_data) { |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
for (struct aws_hash_iter iter = aws_hash_iter_begin(subscriptions); !aws_hash_iter_done(&iter); |
| 282 |
aws_hash_iter_next(&iter)) { |
| 283 |
struct aws_rr_operation_list_topic_filter_entry *entry = iter.element.value; |
| 284 |
struct aws_byte_cursor topic_filter_cursor = entry->topic_filter_cursor; |
| 285 |
|
| 286 |
bool is_matched = s_is_topic_matched_to_subscription(publish_event->topic, topic_filter_cursor); |
| 287 |
if (is_matched) { |
| 288 |
on_stream_operation_subscription_match( |
| 289 |
&entry->operations, &entry->topic_filter_cursor, publish_event, user_data); |
| 290 |
} |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
void s_match_request_response_subscriptions( |
| 295 |
const struct aws_hash_table *request_response_paths, |
| 296 |
const struct aws_mqtt_rr_incoming_publish_event *publish_event, |
| 297 |
aws_mqtt_request_operation_subscription_match_fn *on_request_operation_subscription_match, |
| 298 |
void *user_data) { |
| 299 |
|
| 300 |
struct aws_hash_element *response_path_element = NULL; |
| 301 |
if (aws_hash_table_find(request_response_paths, &publish_event->topic, &response_path_element) == AWS_OP_SUCCESS && |
| 302 |
response_path_element != NULL) { |
| 303 |
|
| 304 |
on_request_operation_subscription_match(response_path_element->value, publish_event, user_data); |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
void aws_mqtt_request_response_client_subscriptions_handle_incoming_publish( |
| 309 |
const struct aws_request_response_subscriptions *subscriptions, |
| 310 |
const struct aws_mqtt_rr_incoming_publish_event *publish_event, |
| 311 |
aws_mqtt_stream_operation_subscription_match_fn *on_stream_operation_subscription_match, |
| 312 |
aws_mqtt_request_operation_subscription_match_fn *on_request_operation_subscription_match, |
| 313 |
void *user_data) { |
| 314 |
|
| 315 |
AWS_FATAL_PRECONDITION(subscriptions); |
| 316 |
AWS_FATAL_PRECONDITION(publish_event); |
| 317 |
AWS_FATAL_PRECONDITION(on_stream_operation_subscription_match); |
| 318 |
AWS_FATAL_PRECONDITION(on_request_operation_subscription_match); |
| 319 |
|
| 320 |
|
| 321 |
s_match_stream_subscriptions( |
| 322 |
&subscriptions->streaming_operation_subscription_lists, |
| 323 |
publish_event, |
| 324 |
on_stream_operation_subscription_match, |
| 325 |
user_data); |
| 326 |
|
| 327 |
s_match_wildcard_stream_subscriptions( |
| 328 |
&subscriptions->streaming_operation_wildcards_subscription_lists, |
| 329 |
publish_event, |
| 330 |
on_stream_operation_subscription_match, |
| 331 |
user_data); |
| 332 |
|
| 333 |
|
| 334 |
s_match_request_response_subscriptions( |
| 335 |
&subscriptions->request_response_paths, publish_event, on_request_operation_subscription_match, user_data); |
| 336 |
} |