| 1 | #ifndef AWS_MQTT_PRIVATE_REQUEST_RESPONSE_SUBSCRIPTION_MANAGER_H |
| 2 | #define AWS_MQTT_PRIVATE_REQUEST_RESPONSE_SUBSCRIPTION_MANAGER_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/mqtt/mqtt.h> |
| 10 | |
| 11 | #include <aws/common/hash_table.h> |
| 12 | |
| 13 | struct aws_mqtt_protocol_adapter; |
| 14 | struct aws_protocol_adapter_connection_event; |
| 15 | struct aws_protocol_adapter_subscription_event; |
| 16 | |
| 17 | /* |
| 18 | * Describes a change to the state of a request operation subscription |
| 19 | */ |
| 20 | enum aws_rr_subscription_event_type { |
| 21 | |
| 22 | /* |
| 23 | * A request subscription subscribe succeeded |
| 24 | */ |
| 25 | ARRSET_REQUEST_SUBSCRIBE_SUCCESS, |
| 26 | |
| 27 | /* |
| 28 | * A request subscription subscribe failed |
| 29 | */ |
| 30 | ARRSET_REQUEST_SUBSCRIBE_FAILURE, |
| 31 | |
| 32 | /* |
| 33 | * A previously successful request subscription has ended. |
| 34 | * |
| 35 | * Under normal circumstances this can happen when |
| 36 | * |
| 37 | * (1) failure to rejoin a session |
| 38 | */ |
| 39 | ARRSET_REQUEST_SUBSCRIPTION_ENDED, |
| 40 | |
| 41 | /* |
| 42 | * A streaming subscription subscribe succeeded |
| 43 | */ |
| 44 | ARRSET_STREAMING_SUBSCRIPTION_ESTABLISHED, |
| 45 | |
| 46 | /* |
| 47 | * The protocol client failed to rejoin a session containing a previously-established streaming subscription |
| 48 | */ |
| 49 | ARRSET_STREAMING_SUBSCRIPTION_LOST, |
| 50 | |
| 51 | /* |
| 52 | * A streaming subscription subscribe attempt resulted in an error or reason code that the client has determined |
| 53 | * will result in indefinite failures to subscribe. In this case, we stop attempting to resubscribe. |
| 54 | * |
| 55 | * Situations that can lead to this: |
| 56 | * (1) Permission failures |
| 57 | * (2) Invalid topic filter |
| 58 | */ |
| 59 | ARRSET_STREAMING_SUBSCRIPTION_HALTED, |
| 60 | |
| 61 | /* |
| 62 | * A subscription has lost its last listener and can be purged |
| 63 | * |
| 64 | * This event is global; operation_id will always be zero. |
| 65 | */ |
| 66 | ARRSET_SUBSCRIPTION_EMPTY, |
| 67 | |
| 68 | /* |
| 69 | * A subscription has been unsubscribed from |
| 70 | * |
| 71 | * This event is global; operation_id will always be zero. |
| 72 | */ |
| 73 | ARRSET_UNSUBSCRIBE_COMPLETE, |
| 74 | }; |
| 75 | |
| 76 | struct aws_rr_subscription_status_event { |
| 77 | enum aws_rr_subscription_event_type type; |
| 78 | struct aws_byte_cursor topic_filter; |
| 79 | uint64_t operation_id; |
| 80 | }; |
| 81 | |
| 82 | /* |
| 83 | * Invariant: despite being on the same thread, these callbacks must be queued as cross-thread tasks on the native |
| 84 | * request-response client. This allows us to iterate internal collections without worrying about external |
| 85 | * callers disrupting things by invoking APIs back on us. |
| 86 | */ |
| 87 | typedef void( |
| 88 | aws_rr_subscription_status_event_callback_fn)(const struct aws_rr_subscription_status_event *event, void *userdata); |
| 89 | |
| 90 | struct aws_rr_subscription_manager_options { |
| 91 | |
| 92 | /* |
| 93 | * Maximum number of request-response subscriptions allowed. Must be at least two. |
| 94 | */ |
| 95 | size_t max_request_response_subscriptions; |
| 96 | |
| 97 | /* |
| 98 | * Maximum number of streaming subscriptions allowed. |
| 99 | */ |
| 100 | size_t max_streaming_subscriptions; |
| 101 | |
| 102 | /* |
| 103 | * Ack timeout to use for all subscribe and unsubscribe operations |
| 104 | */ |
| 105 | uint32_t operation_timeout_seconds; |
| 106 | |
| 107 | aws_rr_subscription_status_event_callback_fn *subscription_status_callback; |
| 108 | void *userdata; |
| 109 | }; |
| 110 | |
| 111 | /* |
| 112 | * The subscription manager works with the request-response client to handle subscriptions in an eager manner. |
| 113 | * Subscription purges are checked with every client service call. Unsubscribe failures don't trigger anything special, |
| 114 | * we'll just try again next time we look for subscription space. Subscribes are attempted on idle subscriptions |
| 115 | * that still need them, either in response to a new operation listener or a connection resumption event. |
| 116 | * |
| 117 | * We only allow one subscribe or unsubscribe to be outstanding at once for a given topic. If an operation requires a |
| 118 | * subscription while an unsubscribe is in progress the operation is blocked until the unsubscribe resolves. |
| 119 | * |
| 120 | * These invariants are dropped during shutdown. In that case, we immediately send unsubscribes for everything |
| 121 | * that is not already unsubscribing. |
| 122 | */ |
| 123 | struct aws_rr_subscription_manager { |
| 124 | struct aws_allocator *allocator; |
| 125 | |
| 126 | struct aws_rr_subscription_manager_options config; |
| 127 | |
| 128 | /* non-owning reference; the client is responsible for destroying this asynchronously (listener detachment) */ |
| 129 | struct aws_mqtt_protocol_adapter *protocol_adapter; |
| 130 | |
| 131 | /* &aws_rr_subscription_record.topic_filter_cursor -> aws_rr_subscription_record * */ |
| 132 | struct aws_hash_table subscriptions; |
| 133 | |
| 134 | bool is_protocol_client_connected; |
| 135 | }; |
| 136 | |
| 137 | enum aws_rr_subscription_type { |
| 138 | ARRST_EVENT_STREAM, |
| 139 | ARRST_REQUEST_RESPONSE, |
| 140 | }; |
| 141 | |
| 142 | struct aws_rr_acquire_subscription_options { |
| 143 | struct aws_byte_cursor *topic_filters; |
| 144 | size_t topic_filter_count; |
| 145 | |
| 146 | uint64_t operation_id; |
| 147 | enum aws_rr_subscription_type type; |
| 148 | }; |
| 149 | |
| 150 | struct aws_rr_release_subscription_options { |
| 151 | struct aws_byte_cursor *topic_filters; |
| 152 | size_t topic_filter_count; |
| 153 | |
| 154 | uint64_t operation_id; |
| 155 | }; |
| 156 | |
| 157 | enum aws_acquire_subscription_result_type { |
| 158 | |
| 159 | /* |
| 160 | * All requested subscriptions already exist and are active. The operation can proceed to the next stage. |
| 161 | */ |
| 162 | AASRT_SUBSCRIBED, |
| 163 | |
| 164 | /* |
| 165 | * The requested subscriptions now exist but at least one is not yet active. The operation must wait for subscribes |
| 166 | * to complete as success or failure. |
| 167 | */ |
| 168 | AASRT_SUBSCRIBING, |
| 169 | |
| 170 | /* |
| 171 | * At least one subscription does not exist and there is no room for it currently. Room may open up in the future, |
| 172 | * so the operation should wait. |
| 173 | */ |
| 174 | AASRT_BLOCKED, |
| 175 | |
| 176 | /* |
| 177 | * At least one subscription does not exist and there is no room for it. Unless an event stream subscription gets |
| 178 | * closed, no room will be available in the future. The operation should be failed. |
| 179 | */ |
| 180 | AASRT_NO_CAPACITY, |
| 181 | |
| 182 | /* |
| 183 | * An internal failure occurred while trying to establish subscriptions. The operation should be failed. |
| 184 | */ |
| 185 | AASRT_FAILURE |
| 186 | }; |
| 187 | |
| 188 | AWS_EXTERN_C_BEGIN |
| 189 | |
| 190 | /* |
| 191 | * Initializes a subscription manager. Every native request-response client owns a single subscription manager. |
| 192 | */ |
| 193 | AWS_MQTT_API void aws_rr_subscription_manager_init( |
| 194 | struct aws_rr_subscription_manager *manager, |
| 195 | struct aws_allocator *allocator, |
| 196 | struct aws_mqtt_protocol_adapter *protocol_adapter, |
| 197 | const struct aws_rr_subscription_manager_options *options); |
| 198 | |
| 199 | /* |
| 200 | * Cleans up a subscription manager. This is done early in the native request-response client shutdown process. |
| 201 | * After this API is called, no other subscription manager APIs will be called by the request-response client (during |
| 202 | * the rest of the asynchronous shutdown process). |
| 203 | */ |
| 204 | AWS_MQTT_API void aws_rr_subscription_manager_clean_up(struct aws_rr_subscription_manager *manager); |
| 205 | |
| 206 | /* |
| 207 | * Requests the the subscription manager unsubscribe from all currently-unused subscriptions |
| 208 | */ |
| 209 | AWS_MQTT_API void aws_rr_subscription_manager_purge_unused(struct aws_rr_subscription_manager *manager); |
| 210 | |
| 211 | /* |
| 212 | * Signals to the subscription manager that the native request-response client is processing an operation that |
| 213 | * needs a subscription to a particular topic. Return value indicates to the request-response client how it should |
| 214 | * proceed with processing the operation. |
| 215 | */ |
| 216 | AWS_MQTT_API enum aws_acquire_subscription_result_type aws_rr_subscription_manager_acquire_subscription( |
| 217 | struct aws_rr_subscription_manager *manager, |
| 218 | const struct aws_rr_acquire_subscription_options *options); |
| 219 | |
| 220 | /* |
| 221 | * Signals to the subscription manager that the native request-response client operation no longer |
| 222 | * needs a subscription to a particular topic. |
| 223 | */ |
| 224 | AWS_MQTT_API void aws_rr_subscription_manager_release_subscription( |
| 225 | struct aws_rr_subscription_manager *manager, |
| 226 | const struct aws_rr_release_subscription_options *options); |
| 227 | |
| 228 | /* |
| 229 | * Notifies the subscription manager of a subscription status event. Invoked by the native request-response client |
| 230 | * that owns the subscription manager. The native request-response client also owns the protocol adapter that |
| 231 | * the subscription event originates from, so the control flow looks like: |
| 232 | * |
| 233 | * [Subscribe] |
| 234 | * subscription manager -> protocol adapter Subscribe -> protocol client Subscribe -> network... |
| 235 | * |
| 236 | * [Result] |
| 237 | * protocol client Suback/Timeout/Error -> protocol adapter -> native request-response client -> |
| 238 | * subscription manager (this API) |
| 239 | */ |
| 240 | AWS_MQTT_API void aws_rr_subscription_manager_on_protocol_adapter_subscription_event( |
| 241 | struct aws_rr_subscription_manager *manager, |
| 242 | const struct aws_protocol_adapter_subscription_event *event); |
| 243 | |
| 244 | /* |
| 245 | * Notifies the subscription manager of a connection status event. Invoked by the native request-response client |
| 246 | * that owns the subscription manager. The native request-response client also owns the protocol adapter that |
| 247 | * the connection event originates from. The control flow looks like: |
| 248 | * |
| 249 | * protocol client connect/disconnect -> protocol adapter -> native request-response client -> |
| 250 | * Subscription manager (this API) |
| 251 | */ |
| 252 | AWS_MQTT_API void aws_rr_subscription_manager_on_protocol_adapter_connection_event( |
| 253 | struct aws_rr_subscription_manager *manager, |
| 254 | const struct aws_protocol_adapter_connection_event *event); |
| 255 | |
| 256 | /* |
| 257 | * Checks subscription manager options for validity. |
| 258 | */ |
| 259 | AWS_MQTT_API bool aws_rr_subscription_manager_are_options_valid( |
| 260 | const struct aws_rr_subscription_manager_options *options); |
| 261 | |
| 262 | AWS_EXTERN_C_END |
| 263 | |
| 264 | #endif /* AWS_MQTT_PRIVATE_REQUEST_RESPONSE_SUBSCRIPTION_MANAGER_H */ |