File: request_response_client.h

package info (click to toggle)
aws-crt-python 0.28.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 78,428 kB
  • sloc: ansic: 437,955; python: 27,657; makefile: 5,855; sh: 4,289; ruby: 208; java: 82; perl: 73; cpp: 25; xml: 11
file content (290 lines) | stat: -rw-r--r-- 9,909 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#ifndef AWS_MQTT_REQUEST_RESPONSE_REQUEST_RESPONSE_CLIENT_H
#define AWS_MQTT_REQUEST_RESPONSE_REQUEST_RESPONSE_CLIENT_H

/**
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */

#include <aws/mqtt/mqtt.h>

struct aws_event_loop;
struct aws_mqtt_request_response_client;
struct aws_mqtt_client_connection;
struct aws_mqtt5_client;

/*
 * A response path is a pair of values - MQTT topic and a JSON path - that describe where a response to
 * an MQTT-based request may arrive.  For a given request type, there may be multiple response paths and each
 * one is associated with a separate JSON schema for the response body.
 */
struct aws_mqtt_request_operation_response_path {

    /*
     * MQTT topic that a response may arrive on.
     */
    struct aws_byte_cursor topic;

    /*
     * JSON path for finding correlation tokens within payloads that arrive on this path's topic.
     */
    struct aws_byte_cursor correlation_token_json_path;
};

/*
 * An event emitted by a streaming operation's subscription.
 */
struct aws_mqtt_rr_incoming_publish_event {
    struct aws_byte_cursor payload;
    struct aws_byte_cursor topic;
    /* Below are MQTT optional fields. For MQTT3, they will always be empty, as MQTT3 does not support them. For MQTT5,
     * they will be set if they are present in a packet. */
    const struct aws_byte_cursor *content_type;
    size_t user_property_count;
    const struct aws_mqtt5_user_property *user_properties;
    /* Even though this field is supposed to be used by MQTT broker to determine if a message-to-be-sent is expired,
     * certain services use this field to specify client-side timeouts. */
    const uint32_t *message_expiry_interval_seconds;
};

/*
 * Callback signature for request-response completion.
 *
 * Invariants:
 *   If error_code is non-zero then publish_event will be NULL.
 *   If publish_event is not NULL, then error_code will be 0.
 */
typedef void(aws_mqtt_request_operation_completion_fn)(
    const struct aws_mqtt_rr_incoming_publish_event *publish_event,
    int error_code,
    void *user_data);

/*
 * Configuration options for a request-response operation.
 */
struct aws_mqtt_request_operation_options {

    /*
     * Set of topic filters that should be subscribed to in order to cover all possible response paths.  Sometimes
     * we can use wildcards to cut down on the subscriptions needed; sometimes we can't.
     */
    struct aws_byte_cursor *subscription_topic_filters;
    size_t subscription_topic_filter_count;

    /*
     * Set of all possible response paths associated with this request type
     */
    struct aws_mqtt_request_operation_response_path *response_paths;
    size_t response_path_count;

    /*
     * Topic to publish the request to once response subscriptions have been established.
     */
    struct aws_byte_cursor publish_topic;

    /*
     * Payload to publish in order to initiate the request
     */
    struct aws_byte_cursor serialized_request;

    /*
     * Correlation token embedded in the request that must be found in a response message.  This can be the
     * empty cursor to support certain services which don't use correlation tokens.  In this case, the client
     * only allows one request at a time to use the associated subscriptions; no concurrency is possible.  There
     * are some optimizations we could make here but for now, they're not worth the complexity.
     */
    struct aws_byte_cursor correlation_token;

    /*
     * Callback (and associated user data) to invoke when the request is completed.
     */
    aws_mqtt_request_operation_completion_fn *completion_callback;
    void *user_data;
};

/*
 * Describes a change to the state of a streaming operation subscription
 */
enum aws_rr_streaming_subscription_event_type {

    /*
     * The streaming operation is successfully subscribed to its topic (filter)
     */
    ARRSSET_SUBSCRIPTION_ESTABLISHED,

    /*
     * The streaming operation has temporarily lost its subscription to its topic (filter)
     */
    ARRSSET_SUBSCRIPTION_LOST,

    /*
     * The streaming operation has entered a terminal state where it has given up trying to subscribe
     * to its topic (filter).  This is always due to user error (bad topic filter or IoT Core permission policy).
     */
    ARRSSET_SUBSCRIPTION_HALTED,
};

/*
 * Callback signature for when the subscription status of a streaming operation changes.
 */
typedef void(aws_mqtt_streaming_operation_subscription_status_fn)(
    enum aws_rr_streaming_subscription_event_type status,
    int error_code,
    void *user_data);

/*
 * Callback signature for when a publish arrives that matches a streaming operation's subscription
 */
typedef void(aws_mqtt_streaming_operation_incoming_publish_fn)(
    const struct aws_mqtt_rr_incoming_publish_event *publish_event,
    void *user_data);

/*
 * Callback signature for when a streaming operation is fully destroyed and no more events will be emitted.
 */
typedef void(aws_mqtt_streaming_operation_terminated_fn)(void *user_data);

/*
 * Configuration options for a streaming operation.
 */
struct aws_mqtt_streaming_operation_options {

    /*
     * Topic filter that the streaming operation should listen on
     */
    struct aws_byte_cursor topic_filter;

    /*
     * Callback for subscription status events
     */
    aws_mqtt_streaming_operation_subscription_status_fn *subscription_status_callback;

    /*
     * Callback for publish messages that match the operation's topic filter
     */
    aws_mqtt_streaming_operation_incoming_publish_fn *incoming_publish_callback;

    /*
     * Callback for streaming operation final shutdown
     */
    aws_mqtt_streaming_operation_terminated_fn *terminated_callback;

    /*
     * Data passed to all streaming operation callbacks
     */
    void *user_data;
};

typedef void(aws_mqtt_request_response_client_initialized_callback_fn)(void *user_data);
typedef void(aws_mqtt_request_response_client_terminated_callback_fn)(void *user_data);

/*
 * Request-response client configuration options
 */
struct aws_mqtt_request_response_client_options {

    /*
     * Maximum number of subscriptions that the client will concurrently use for request-response operations
     */
    size_t max_request_response_subscriptions;

    /*
     * Maximum number of subscriptions that the client will concurrently use for streaming operations
     */
    size_t max_streaming_subscriptions;

    /*
     * Duration, in seconds, that a request-response operation will wait for completion before giving up
     */
    uint32_t operation_timeout_seconds;

    /*
     * Request-response client initialization is asynchronous.  This callback is invoked when the client is fully
     * initialized.
     *
     * Do not bind the initialized callback; it exists mostly for tests and should not be exposed
     */
    aws_mqtt_request_response_client_initialized_callback_fn *initialized_callback;

    /*
     * Callback invoked when the client's asynchronous destruction process has fully completed.
     */
    aws_mqtt_request_response_client_terminated_callback_fn *terminated_callback;

    /*
     * Arbitrary data to pass to the client callbacks
     */
    void *user_data;
};

AWS_EXTERN_C_BEGIN

/*
 * Create a new request-response client that uses an MQTT311 client.
 */
AWS_MQTT_API struct aws_mqtt_request_response_client *aws_mqtt_request_response_client_new_from_mqtt311_client(
    struct aws_allocator *allocator,
    struct aws_mqtt_client_connection *client,
    const struct aws_mqtt_request_response_client_options *options);

/*
 * Create a new request-response client that uses an MQTT5 client.
 */
AWS_MQTT_API struct aws_mqtt_request_response_client *aws_mqtt_request_response_client_new_from_mqtt5_client(
    struct aws_allocator *allocator,
    struct aws_mqtt5_client *client,
    const struct aws_mqtt_request_response_client_options *options);

/*
 * Add a reference to a request-response client
 */
AWS_MQTT_API struct aws_mqtt_request_response_client *aws_mqtt_request_response_client_acquire(
    struct aws_mqtt_request_response_client *client);

/*
 * Remove a reference from a request-response client
 */
AWS_MQTT_API struct aws_mqtt_request_response_client *aws_mqtt_request_response_client_release(
    struct aws_mqtt_request_response_client *client);

/*
 * Submits a request operation to the client
 */
AWS_MQTT_API int aws_mqtt_request_response_client_submit_request(
    struct aws_mqtt_request_response_client *client,
    const struct aws_mqtt_request_operation_options *request_options);

/*
 * Creates a new streaming operation.  Streaming operations start in an inactive state and must be
 * activated before its subscription can be established.
 */
AWS_MQTT_API struct aws_mqtt_rr_client_operation *aws_mqtt_request_response_client_create_streaming_operation(
    struct aws_mqtt_request_response_client *client,
    const struct aws_mqtt_streaming_operation_options *streaming_options);

/*
 * Returns the event loop used by the request-response client's protocol client
 */
AWS_MQTT_API struct aws_event_loop *aws_mqtt_request_response_client_get_event_loop(
    struct aws_mqtt_request_response_client *client);

/*
 * Initiates a streaming operation's subscription process.
 */
AWS_MQTT_API int aws_mqtt_rr_client_operation_activate(struct aws_mqtt_rr_client_operation *operation);

/*
 * Add a reference to a streaming operation.
 */
AWS_MQTT_API struct aws_mqtt_rr_client_operation *aws_mqtt_rr_client_operation_acquire(
    struct aws_mqtt_rr_client_operation *operation);

/*
 * Remove a reference from a streaming operation
 */
AWS_MQTT_API struct aws_mqtt_rr_client_operation *aws_mqtt_rr_client_operation_release(
    struct aws_mqtt_rr_client_operation *operation);

AWS_EXTERN_C_END

#endif /* AWS_MQTT_REQUEST_RESPONSE_REQUEST_RESPONSE_CLIENT_H */