| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include "mqtt_request_response.h" |
| 6 |
|
| 7 |
#include "mqtt5_client.h" |
| 8 |
#include "mqtt_client_connection.h" |
| 9 |
|
| 10 |
#include "aws/mqtt/request-response/request_response_client.h" |
| 11 |
|
| 12 |
static const char *s_capsule_name_mqtt_request_response_client = "aws_mqtt_request_response_client"; |
| 13 |
static const char *s_capsule_name_mqtt_streaming_operation = "aws_mqtt_streaming_operation"; |
| 14 |
|
| 15 |
static const char *AWS_PYOBJECT_KEY_REQUEST_RESPONSE_CLIENT_OPTIONS = "ClientOptions"; |
| 16 |
|
| 17 |
static const char *AWS_PYOBJECT_KEY_MAX_REQUEST_RESPONSE_SUBSCRIPTIONS = "max_request_response_subscriptions"; |
| 18 |
static const char *AWS_PYOBJECT_KEY_MAX_STREAMING_SUBSCRIPTIONS = "max_streaming_subscriptions"; |
| 19 |
static const char *AWS_PYOBJECT_KEY_OPERATION_TIMEOUT_IN_SECONDS = "operation_timeout_in_seconds"; |
| 20 |
|
| 21 |
struct mqtt_request_response_client_binding { |
| 22 |
struct aws_mqtt_request_response_client *native; |
| 23 |
}; |
| 24 |
|
| 25 |
static void s_mqtt_request_response_python_client_destructor(PyObject *client_capsule) { |
| 26 |
struct mqtt_request_response_client_binding *client_binding = |
| 27 |
PyCapsule_GetPointer(client_capsule, s_capsule_name_mqtt_request_response_client); |
| 28 |
assert(client_binding); |
| 29 |
|
| 30 |
client_binding->native = aws_mqtt_request_response_client_release(client_binding->native); |
| 31 |
|
| 32 |
aws_mem_release(aws_py_get_allocator(), client_binding); |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
static bool s_init_mqtt_request_response_client_options( |
| 39 |
struct aws_mqtt_request_response_client_options *client_options, |
| 40 |
PyObject *client_options_py) { |
| 41 |
AWS_ZERO_STRUCT(*client_options); |
| 42 |
|
| 43 |
uint32_t max_request_response_subscriptions = PyObject_GetAttrAsUint32( |
| 44 |
client_options_py, |
| 45 |
AWS_PYOBJECT_KEY_REQUEST_RESPONSE_CLIENT_OPTIONS, |
| 46 |
AWS_PYOBJECT_KEY_MAX_REQUEST_RESPONSE_SUBSCRIPTIONS); |
| 47 |
if (PyErr_Occurred()) { |
| 48 |
PyErr_Format(PyErr_Occurred(), "Cannot convert max_request_response_subscriptions to a C uint32"); |
| 49 |
return false; |
| 50 |
} |
| 51 |
|
| 52 |
uint32_t max_streaming_subscriptions = PyObject_GetAttrAsUint32( |
| 53 |
client_options_py, |
| 54 |
AWS_PYOBJECT_KEY_REQUEST_RESPONSE_CLIENT_OPTIONS, |
| 55 |
AWS_PYOBJECT_KEY_MAX_STREAMING_SUBSCRIPTIONS); |
| 56 |
if (PyErr_Occurred()) { |
| 57 |
PyErr_Format(PyErr_Occurred(), "Cannot convert max_streaming_subscriptions to a C uint32"); |
| 58 |
return false; |
| 59 |
} |
| 60 |
|
| 61 |
uint32_t timeout_in_seconds = PyObject_GetAttrAsUint32( |
| 62 |
client_options_py, |
| 63 |
AWS_PYOBJECT_KEY_REQUEST_RESPONSE_CLIENT_OPTIONS, |
| 64 |
AWS_PYOBJECT_KEY_OPERATION_TIMEOUT_IN_SECONDS); |
| 65 |
if (PyErr_Occurred()) { |
| 66 |
PyErr_Format(PyErr_Occurred(), "Cannot convert operation_timeout_in_seconds to a C uint32_t"); |
| 67 |
return false; |
| 68 |
} |
| 69 |
|
| 70 |
client_options->max_request_response_subscriptions = (size_t)max_request_response_subscriptions; |
| 71 |
client_options->max_streaming_subscriptions = (size_t)max_streaming_subscriptions; |
| 72 |
client_options->operation_timeout_seconds = (uint32_t)timeout_in_seconds; |
| 73 |
|
| 74 |
return true; |
| 75 |
} |
| 76 |
|
| 77 |
PyObject *aws_py_mqtt_request_response_client_new_from_5(PyObject *self, PyObject *args) { |
| 78 |
(void)self; |
| 79 |
|
| 80 |
PyObject *mqtt5_client_py = NULL; |
| 81 |
PyObject *client_options_py = NULL; |
| 82 |
|
| 83 |
if (!PyArg_ParseTuple( |
| 84 |
args, |
| 85 |
"OO", |
| 86 |
&mqtt5_client_py, |
| 87 |
&client_options_py)) { |
| 88 |
return NULL; |
| 89 |
} |
| 90 |
|
| 91 |
struct aws_mqtt5_client *protocol_client = aws_py_get_mqtt5_client(mqtt5_client_py); |
| 92 |
if (protocol_client == NULL) { |
| 93 |
return NULL; |
| 94 |
} |
| 95 |
|
| 96 |
struct aws_mqtt_request_response_client_options client_options; |
| 97 |
if (!s_init_mqtt_request_response_client_options(&client_options, client_options_py)) { |
| 98 |
return NULL; |
| 99 |
} |
| 100 |
|
| 101 |
struct aws_allocator *allocator = aws_py_get_allocator(); |
| 102 |
|
| 103 |
struct aws_mqtt_request_response_client *rr_client = |
| 104 |
aws_mqtt_request_response_client_new_from_mqtt5_client(allocator, protocol_client, &client_options); |
| 105 |
if (rr_client == NULL) { |
| 106 |
PyErr_SetAwsLastError(); |
| 107 |
return NULL; |
| 108 |
} |
| 109 |
|
| 110 |
struct mqtt_request_response_client_binding *client_binding = |
| 111 |
aws_mem_calloc(allocator, 1, sizeof(struct mqtt_request_response_client_binding)); |
| 112 |
|
| 113 |
PyObject *capsule = PyCapsule_New( |
| 114 |
client_binding, s_capsule_name_mqtt_request_response_client, s_mqtt_request_response_python_client_destructor); |
| 115 |
if (!capsule) { |
| 116 |
aws_mem_release(allocator, client_binding); |
| 117 |
aws_mqtt_request_response_client_release(rr_client); |
| 118 |
return NULL; |
| 119 |
} |
| 120 |
|
| 121 |
client_binding->native = rr_client; |
| 122 |
|
| 123 |
return capsule; |
| 124 |
} |
| 125 |
|
| 126 |
PyObject *aws_py_mqtt_request_response_client_new_from_311(PyObject *self, PyObject *args) { |
| 127 |
(void)self; |
| 128 |
|
| 129 |
PyObject *mqtt_connection_py = NULL; |
| 130 |
PyObject *client_options_py = NULL; |
| 131 |
|
| 132 |
if (!PyArg_ParseTuple( |
| 133 |
args, |
| 134 |
"OO", |
| 135 |
&mqtt_connection_py, |
| 136 |
&client_options_py)) { |
| 137 |
return NULL; |
| 138 |
} |
| 139 |
|
| 140 |
struct aws_mqtt_client_connection *protocol_client = aws_py_get_mqtt_client_connection(mqtt_connection_py); |
| 141 |
if (protocol_client == NULL) { |
| 142 |
return NULL; |
| 143 |
} |
| 144 |
|
| 145 |
struct aws_mqtt_request_response_client_options client_options; |
| 146 |
if (!s_init_mqtt_request_response_client_options(&client_options, client_options_py)) { |
| 147 |
return NULL; |
| 148 |
} |
| 149 |
|
| 150 |
struct aws_allocator *allocator = aws_py_get_allocator(); |
| 151 |
|
| 152 |
struct aws_mqtt_request_response_client *rr_client = |
| 153 |
aws_mqtt_request_response_client_new_from_mqtt311_client(allocator, protocol_client, &client_options); |
| 154 |
if (rr_client == NULL) { |
| 155 |
PyErr_SetAwsLastError(); |
| 156 |
return NULL; |
| 157 |
} |
| 158 |
|
| 159 |
struct mqtt_request_response_client_binding *client_binding = |
| 160 |
aws_mem_calloc(allocator, 1, sizeof(struct mqtt_request_response_client_binding)); |
| 161 |
|
| 162 |
PyObject *capsule = PyCapsule_New( |
| 163 |
client_binding, s_capsule_name_mqtt_request_response_client, s_mqtt_request_response_python_client_destructor); |
| 164 |
if (!capsule) { |
| 165 |
aws_mem_release(allocator, client_binding); |
| 166 |
aws_mqtt_request_response_client_release(rr_client); |
| 167 |
return NULL; |
| 168 |
} |
| 169 |
|
| 170 |
client_binding->native = rr_client; |
| 171 |
|
| 172 |
return capsule; |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
static const char *AWS_PYOBJECT_KEY_TOPIC = "topic"; |
| 178 |
static const char *AWS_PYOBJECT_KEY_CORRELATION_TOKEN_JSON_PATH = "correlation_token_json_path"; |
| 179 |
|
| 180 |
static void s_cleanup_subscription_topic_filters(struct aws_array_list *subscription_topic_filters) { |
| 181 |
size_t filter_count = aws_array_list_length(subscription_topic_filters); |
| 182 |
for (size_t i = 0; i < filter_count; ++i) { |
| 183 |
struct aws_byte_buf filter_buffer; |
| 184 |
aws_array_list_get_at(subscription_topic_filters, &filter_buffer, i); |
| 185 |
|
| 186 |
aws_byte_buf_clean_up(&filter_buffer); |
| 187 |
} |
| 188 |
|
| 189 |
aws_array_list_clean_up(subscription_topic_filters); |
| 190 |
} |
| 191 |
|
| 192 |
static bool s_init_subscription_topic_filters( |
| 193 |
struct aws_array_list *subscription_topic_filters, |
| 194 |
PyObject *subscription_topic_filters_py) { |
| 195 |
AWS_ZERO_STRUCT(*subscription_topic_filters); |
| 196 |
|
| 197 |
if (!PySequence_Check(subscription_topic_filters_py)) { |
| 198 |
PyErr_Format(PyExc_TypeError, "subscription_topic_filters must be a sequence type"); |
| 199 |
return false; |
| 200 |
} |
| 201 |
|
| 202 |
Py_ssize_t filter_count = PySequence_Size(subscription_topic_filters_py); |
| 203 |
if (filter_count <= 0) { |
| 204 |
PyErr_Format(PyExc_TypeError, "subscription_topic_filters must have at least one element"); |
| 205 |
return false; |
| 206 |
} |
| 207 |
|
| 208 |
bool success = false; |
| 209 |
struct aws_allocator *allocator = aws_py_get_allocator(); |
| 210 |
aws_array_list_init_dynamic( |
| 211 |
subscription_topic_filters, allocator, (size_t)filter_count, sizeof(struct aws_byte_buf)); |
| 212 |
|
| 213 |
for (size_t i = 0; i < (size_t)filter_count; ++i) { |
| 214 |
PyObject *entry_py = PySequence_GetItem(subscription_topic_filters_py, i); |
| 215 |
if (entry_py == NULL) { |
| 216 |
goto done; |
| 217 |
} |
| 218 |
|
| 219 |
struct aws_byte_cursor topic_filter_cursor = aws_byte_cursor_from_pyunicode(entry_py); |
| 220 |
|
| 221 |
struct aws_byte_buf topic_filter; |
| 222 |
aws_byte_buf_init_copy_from_cursor(&topic_filter, allocator, topic_filter_cursor); |
| 223 |
|
| 224 |
aws_array_list_push_back(subscription_topic_filters, &topic_filter); |
| 225 |
|
| 226 |
Py_XDECREF(entry_py); |
| 227 |
|
| 228 |
if (PyErr_Occurred()) { |
| 229 |
goto done; |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
success = true; |
| 234 |
|
| 235 |
done: |
| 236 |
|
| 237 |
if (!success) { |
| 238 |
s_cleanup_subscription_topic_filters(subscription_topic_filters); |
| 239 |
AWS_ZERO_STRUCT(*subscription_topic_filters); |
| 240 |
} |
| 241 |
|
| 242 |
return success; |
| 243 |
} |
| 244 |
|
| 245 |
struct aws_request_response_path { |
| 246 |
struct aws_byte_buf topic; |
| 247 |
struct aws_byte_buf correlation_token_json_path; |
| 248 |
}; |
| 249 |
|
| 250 |
static void s_cleanup_response_paths(struct aws_array_list *response_paths) { |
| 251 |
size_t path_count = aws_array_list_length(response_paths); |
| 252 |
for (size_t i = 0; i < path_count; ++i) { |
| 253 |
struct aws_request_response_path response_path; |
| 254 |
aws_array_list_get_at(response_paths, &response_path, i); |
| 255 |
|
| 256 |
aws_byte_buf_clean_up(&response_path.topic); |
| 257 |
aws_byte_buf_clean_up(&response_path.correlation_token_json_path); |
| 258 |
} |
| 259 |
|
| 260 |
aws_array_list_clean_up(response_paths); |
| 261 |
} |
| 262 |
|
| 263 |
static bool s_init_response_paths(struct aws_array_list *response_paths, PyObject *response_paths_py) { |
| 264 |
AWS_ZERO_STRUCT(*response_paths); |
| 265 |
|
| 266 |
if (!PySequence_Check(response_paths_py)) { |
| 267 |
PyErr_Format(PyExc_TypeError, "response_paths must be a sequence type"); |
| 268 |
return false; |
| 269 |
} |
| 270 |
|
| 271 |
Py_ssize_t path_count = PySequence_Size(response_paths_py); |
| 272 |
if (path_count <= 0) { |
| 273 |
PyErr_Format(PyExc_TypeError, "response_paths must have at least one element"); |
| 274 |
return false; |
| 275 |
} |
| 276 |
|
| 277 |
bool success = false; |
| 278 |
struct aws_allocator *allocator = aws_py_get_allocator(); |
| 279 |
aws_array_list_init_dynamic( |
| 280 |
response_paths, allocator, (size_t)path_count, sizeof(struct aws_request_response_path)); |
| 281 |
|
| 282 |
for (size_t i = 0; i < (size_t)path_count; ++i) { |
| 283 |
PyObject *entry_py = PySequence_GetItem(response_paths_py, i); |
| 284 |
if (entry_py == NULL) { |
| 285 |
goto done; |
| 286 |
} |
| 287 |
|
| 288 |
PyObject *topic_py = PyObject_GetAttrString(entry_py, AWS_PYOBJECT_KEY_TOPIC); |
| 289 |
PyObject *correlation_token_json_path_py = |
| 290 |
PyObject_GetAttrString(entry_py, AWS_PYOBJECT_KEY_CORRELATION_TOKEN_JSON_PATH); |
| 291 |
if (topic_py != NULL && correlation_token_json_path_py != NULL) { |
| 292 |
struct aws_byte_cursor topic_cursor = aws_byte_cursor_from_pyunicode(topic_py); |
| 293 |
struct aws_byte_cursor correlation_token_json_path_cursor; |
| 294 |
AWS_ZERO_STRUCT(correlation_token_json_path_cursor); |
| 295 |
if (correlation_token_json_path_py != Py_None) { |
| 296 |
correlation_token_json_path_cursor = aws_byte_cursor_from_pyunicode(correlation_token_json_path_py); |
| 297 |
} |
| 298 |
|
| 299 |
struct aws_request_response_path response_path; |
| 300 |
aws_byte_buf_init_copy_from_cursor(&response_path.topic, allocator, topic_cursor); |
| 301 |
aws_byte_buf_init_copy_from_cursor( |
| 302 |
&response_path.correlation_token_json_path, allocator, correlation_token_json_path_cursor); |
| 303 |
|
| 304 |
aws_array_list_push_back(response_paths, &response_path); |
| 305 |
} |
| 306 |
|
| 307 |
Py_XDECREF(topic_py); |
| 308 |
Py_XDECREF(correlation_token_json_path_py); |
| 309 |
Py_XDECREF(entry_py); |
| 310 |
|
| 311 |
if (PyErr_Occurred()) { |
| 312 |
PyErr_Format(PyExc_TypeError, "invalid response path"); |
| 313 |
goto done; |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
success = true; |
| 318 |
|
| 319 |
done: |
| 320 |
|
| 321 |
if (!success) { |
| 322 |
s_cleanup_response_paths(response_paths); |
| 323 |
AWS_ZERO_STRUCT(*response_paths); |
| 324 |
} |
| 325 |
|
| 326 |
return success; |
| 327 |
} |
| 328 |
|
| 329 |
struct aws_mqtt_make_request_binding { |
| 330 |
PyObject *on_request_complete_callback; |
| 331 |
}; |
| 332 |
|
| 333 |
static struct aws_mqtt_make_request_binding *s_aws_mqtt_make_request_binding_new( |
| 334 |
PyObject *on_request_complete_callable_py) { |
| 335 |
struct aws_mqtt_make_request_binding *binding = |
| 336 |
aws_mem_calloc(aws_py_get_allocator(), 1, sizeof(struct aws_mqtt_make_request_binding)); |
| 337 |
|
| 338 |
binding->on_request_complete_callback = on_request_complete_callable_py; |
| 339 |
Py_XINCREF(binding->on_request_complete_callback); |
| 340 |
|
| 341 |
return binding; |
| 342 |
} |
| 343 |
|
| 344 |
static void s_aws_mqtt_make_request_binding_destroy(struct aws_mqtt_make_request_binding *binding) { |
| 345 |
if (binding == NULL) { |
| 346 |
return; |
| 347 |
} |
| 348 |
|
| 349 |
Py_XDECREF(binding->on_request_complete_callback); |
| 350 |
|
| 351 |
aws_mem_release(aws_py_get_allocator(), binding); |
| 352 |
} |
| 353 |
|
| 354 |
static void s_on_mqtt_request_complete( |
| 355 |
const struct aws_mqtt_rr_incoming_publish_event *publish_event, |
| 356 |
int error_code, |
| 357 |
void *user_data) { |
| 358 |
|
| 359 |
struct aws_mqtt_make_request_binding *request_binding = user_data; |
| 360 |
|
| 361 |
PyGILState_STATE state; |
| 362 |
if (aws_py_gilstate_ensure(&state)) { |
| 363 |
return; |
| 364 |
} |
| 365 |
|
| 366 |
PyObject *result = PyObject_CallFunction( |
| 367 |
request_binding->on_request_complete_callback, |
| 368 |
"(is#y#)", |
| 369 |
error_code, |
| 370 |
publish_event ? publish_event->topic.ptr : NULL, |
| 371 |
publish_event ? publish_event->topic.len : 0, |
| 372 |
publish_event ? publish_event->payload.ptr : NULL, |
| 373 |
publish_event ? publish_event->payload.len : 0); |
| 374 |
if (!result) { |
| 375 |
PyErr_WriteUnraisable(PyErr_Occurred()); |
| 376 |
} |
| 377 |
|
| 378 |
Py_XDECREF(result); |
| 379 |
|
| 380 |
s_aws_mqtt_make_request_binding_destroy(request_binding); |
| 381 |
|
| 382 |
PyGILState_Release(state); |
| 383 |
} |
| 384 |
|
| 385 |
PyObject *aws_py_mqtt_request_response_client_make_request(PyObject *self, PyObject *args) { |
| 386 |
(void)self; |
| 387 |
|
| 388 |
PyObject *client_capsule_py; |
| 389 |
PyObject *subscription_topic_filters_py; |
| 390 |
PyObject *response_paths_py; |
| 391 |
struct aws_byte_cursor publish_topic; |
| 392 |
struct aws_byte_cursor payload; |
| 393 |
struct aws_byte_cursor correlation_token; |
| 394 |
PyObject *on_request_complete_callable_py; |
| 395 |
|
| 396 |
if (!PyArg_ParseTuple( |
| 397 |
args, |
| 398 |
"OOOs#y#z#O", |
| 399 |
&client_capsule_py, |
| 400 |
&subscription_topic_filters_py, |
| 401 |
&response_paths_py, |
| 402 |
&publish_topic.ptr, |
| 403 |
&publish_topic.len, |
| 404 |
&payload.ptr, |
| 405 |
&payload.len, |
| 406 |
&correlation_token.ptr, |
| 407 |
&correlation_token.len, |
| 408 |
&on_request_complete_callable_py)) { |
| 409 |
return NULL; |
| 410 |
} |
| 411 |
|
| 412 |
struct mqtt_request_response_client_binding *client_binding = |
| 413 |
PyCapsule_GetPointer(client_capsule_py, s_capsule_name_mqtt_request_response_client); |
| 414 |
if (!client_binding) { |
| 415 |
return NULL; |
| 416 |
} |
| 417 |
|
| 418 |
struct aws_array_list subscription_topic_filters; |
| 419 |
AWS_ZERO_STRUCT(subscription_topic_filters); |
| 420 |
|
| 421 |
struct aws_array_list response_paths; |
| 422 |
AWS_ZERO_STRUCT(response_paths); |
| 423 |
|
| 424 |
if (!s_init_subscription_topic_filters(&subscription_topic_filters, subscription_topic_filters_py) || |
| 425 |
!s_init_response_paths(&response_paths, response_paths_py)) { |
| 426 |
goto done; |
| 427 |
} |
| 428 |
|
| 429 |
{ |
| 430 |
size_t subscription_count = aws_array_list_length(&subscription_topic_filters); |
| 431 |
AWS_VARIABLE_LENGTH_ARRAY(struct aws_byte_cursor, subscription_topic_filter_cursors, subscription_count); |
| 432 |
|
| 433 |
for (size_t i = 0; i < subscription_count; ++i) { |
| 434 |
struct aws_byte_buf topic_filter; |
| 435 |
aws_array_list_get_at(&subscription_topic_filters, &topic_filter, i); |
| 436 |
|
| 437 |
subscription_topic_filter_cursors[i] = aws_byte_cursor_from_buf(&topic_filter); |
| 438 |
} |
| 439 |
|
| 440 |
size_t response_path_count = aws_array_list_length(&response_paths); |
| 441 |
AWS_VARIABLE_LENGTH_ARRAY( |
| 442 |
struct aws_mqtt_request_operation_response_path, response_path_values, response_path_count); |
| 443 |
|
| 444 |
for (size_t i = 0; i < response_path_count; ++i) { |
| 445 |
struct aws_request_response_path response_path; |
| 446 |
aws_array_list_get_at(&response_paths, &response_path, i); |
| 447 |
|
| 448 |
response_path_values[i].topic = aws_byte_cursor_from_buf(&response_path.topic); |
| 449 |
response_path_values[i].correlation_token_json_path = |
| 450 |
aws_byte_cursor_from_buf(&response_path.correlation_token_json_path); |
| 451 |
} |
| 452 |
|
| 453 |
struct aws_mqtt_make_request_binding *request_binding = |
| 454 |
s_aws_mqtt_make_request_binding_new(on_request_complete_callable_py); |
| 455 |
|
| 456 |
struct aws_mqtt_request_operation_options request_options = { |
| 457 |
.subscription_topic_filters = subscription_topic_filter_cursors, |
| 458 |
.subscription_topic_filter_count = subscription_count, |
| 459 |
.response_paths = response_path_values, |
| 460 |
.response_path_count = response_path_count, |
| 461 |
.publish_topic = publish_topic, |
| 462 |
.serialized_request = payload, |
| 463 |
.correlation_token = correlation_token, |
| 464 |
.completion_callback = s_on_mqtt_request_complete, |
| 465 |
.user_data = request_binding, |
| 466 |
}; |
| 467 |
|
| 468 |
if (aws_mqtt_request_response_client_submit_request(client_binding->native, &request_options)) { |
| 469 |
s_on_mqtt_request_complete(NULL, aws_last_error(), request_binding); |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
done: |
| 474 |
|
| 475 |
s_cleanup_subscription_topic_filters(&subscription_topic_filters); |
| 476 |
s_cleanup_response_paths(&response_paths); |
| 477 |
|
| 478 |
Py_RETURN_NONE; |
| 479 |
} |
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
struct mqtt_streaming_operation_binding { |
| 484 |
struct aws_mqtt_rr_client_operation *native; |
| 485 |
PyObject *subscription_status_changed_callable; |
| 486 |
PyObject *incoming_publish_callable; |
| 487 |
}; |
| 488 |
|
| 489 |
static struct mqtt_streaming_operation_binding *s_mqtt_streaming_operation_binding_new( |
| 490 |
PyObject *subscription_status_changed_callable_py, |
| 491 |
PyObject *incoming_publish_callable_py) { |
| 492 |
struct mqtt_streaming_operation_binding *binding = |
| 493 |
aws_mem_calloc(aws_py_get_allocator(), 1, sizeof(struct mqtt_streaming_operation_binding)); |
| 494 |
|
| 495 |
binding->subscription_status_changed_callable = subscription_status_changed_callable_py; |
| 496 |
Py_XINCREF(binding->subscription_status_changed_callable); |
| 497 |
|
| 498 |
binding->incoming_publish_callable = incoming_publish_callable_py; |
| 499 |
Py_XINCREF(binding->incoming_publish_callable); |
| 500 |
|
| 501 |
return binding; |
| 502 |
} |
| 503 |
|
| 504 |
static void s_mqtt_streaming_operation_binding_on_terminated(void *user_data) { |
| 505 |
struct mqtt_streaming_operation_binding *stream_binding = user_data; |
| 506 |
|
| 507 |
PyGILState_STATE state; |
| 508 |
if (!aws_py_gilstate_ensure(&state)) { |
| 509 |
Py_XDECREF(stream_binding->subscription_status_changed_callable); |
| 510 |
Py_XDECREF(stream_binding->incoming_publish_callable); |
| 511 |
|
| 512 |
PyGILState_Release(state); |
| 513 |
} |
| 514 |
|
| 515 |
aws_mem_release(aws_py_get_allocator(), stream_binding); |
| 516 |
} |
| 517 |
|
| 518 |
static void s_mqtt_streaming_operation_binding_destructor(PyObject *stream_capsule) { |
| 519 |
struct mqtt_streaming_operation_binding *stream_binding = |
| 520 |
PyCapsule_GetPointer(stream_capsule, s_capsule_name_mqtt_streaming_operation); |
| 521 |
assert(stream_binding); |
| 522 |
|
| 523 |
stream_binding->native = aws_mqtt_rr_client_operation_release(stream_binding->native); |
| 524 |
} |
| 525 |
|
| 526 |
static void s_aws_mqtt_streaming_operation_subscription_status_callback_python( |
| 527 |
enum aws_rr_streaming_subscription_event_type status, |
| 528 |
int error_code, |
| 529 |
void *user_data) { |
| 530 |
|
| 531 |
struct mqtt_streaming_operation_binding *stream_binding = user_data; |
| 532 |
|
| 533 |
PyGILState_STATE state; |
| 534 |
if (aws_py_gilstate_ensure(&state)) { |
| 535 |
return; |
| 536 |
} |
| 537 |
|
| 538 |
PyObject *result = PyObject_CallFunction( |
| 539 |
stream_binding->subscription_status_changed_callable, |
| 540 |
"(ii)", |
| 541 |
(int)status, |
| 542 |
error_code); |
| 543 |
if (!result) { |
| 544 |
PyErr_WriteUnraisable(PyErr_Occurred()); |
| 545 |
} |
| 546 |
|
| 547 |
Py_XDECREF(result); |
| 548 |
|
| 549 |
PyGILState_Release(state); |
| 550 |
} |
| 551 |
|
| 552 |
static void s_aws_mqtt_streaming_operation_incoming_publish_callback_python( |
| 553 |
const struct aws_mqtt_rr_incoming_publish_event *publish_event, |
| 554 |
void *user_data) { |
| 555 |
|
| 556 |
AWS_FATAL_ASSERT(publish_event != NULL); |
| 557 |
|
| 558 |
struct mqtt_streaming_operation_binding *stream_binding = user_data; |
| 559 |
|
| 560 |
PyGILState_STATE state; |
| 561 |
if (aws_py_gilstate_ensure(&state)) { |
| 562 |
return; |
| 563 |
} |
| 564 |
|
| 565 |
PyObject *result = PyObject_CallFunction( |
| 566 |
stream_binding->incoming_publish_callable, |
| 567 |
"(s#y#)", |
| 568 |
publish_event->topic.ptr, |
| 569 |
publish_event->topic.len, |
| 570 |
publish_event->payload.ptr, |
| 571 |
publish_event->payload.len); |
| 572 |
if (!result) { |
| 573 |
PyErr_WriteUnraisable(PyErr_Occurred()); |
| 574 |
} |
| 575 |
|
| 576 |
Py_XDECREF(result); |
| 577 |
|
| 578 |
PyGILState_Release(state); |
| 579 |
} |
| 580 |
|
| 581 |
PyObject *aws_py_mqtt_request_response_client_create_stream(PyObject *self, PyObject *args) { |
| 582 |
(void)self; |
| 583 |
|
| 584 |
PyObject *client_capsule_py; |
| 585 |
struct aws_byte_cursor subscription_topic_filter; |
| 586 |
PyObject *subscription_status_changed_callable_py; |
| 587 |
PyObject *incoming_publish_callable_py; |
| 588 |
|
| 589 |
if (!PyArg_ParseTuple( |
| 590 |
args, |
| 591 |
"Os#OO", |
| 592 |
&client_capsule_py, |
| 593 |
&subscription_topic_filter.ptr, |
| 594 |
&subscription_topic_filter.len, |
| 595 |
&subscription_status_changed_callable_py, |
| 596 |
&incoming_publish_callable_py)) { |
| 597 |
return NULL; |
| 598 |
} |
| 599 |
|
| 600 |
struct mqtt_request_response_client_binding *client_binding = |
| 601 |
PyCapsule_GetPointer(client_capsule_py, s_capsule_name_mqtt_request_response_client); |
| 602 |
if (!client_binding) { |
| 603 |
return NULL; |
| 604 |
} |
| 605 |
|
| 606 |
struct mqtt_streaming_operation_binding *stream_binding = |
| 607 |
s_mqtt_streaming_operation_binding_new(subscription_status_changed_callable_py, incoming_publish_callable_py); |
| 608 |
|
| 609 |
struct aws_mqtt_streaming_operation_options stream_options = { |
| 610 |
.topic_filter = subscription_topic_filter, |
| 611 |
.subscription_status_callback = s_aws_mqtt_streaming_operation_subscription_status_callback_python, |
| 612 |
.incoming_publish_callback = s_aws_mqtt_streaming_operation_incoming_publish_callback_python, |
| 613 |
.terminated_callback = s_mqtt_streaming_operation_binding_on_terminated, |
| 614 |
.user_data = stream_binding, |
| 615 |
}; |
| 616 |
stream_binding->native = |
| 617 |
aws_mqtt_request_response_client_create_streaming_operation(client_binding->native, &stream_options); |
| 618 |
if (stream_binding->native == NULL) { |
| 619 |
PyErr_SetAwsLastError(); |
| 620 |
s_mqtt_streaming_operation_binding_on_terminated(stream_binding); |
| 621 |
return NULL; |
| 622 |
} |
| 623 |
|
| 624 |
PyObject *capsule = PyCapsule_New( |
| 625 |
stream_binding, s_capsule_name_mqtt_streaming_operation, s_mqtt_streaming_operation_binding_destructor); |
| 626 |
if (!capsule) { |
| 627 |
stream_binding->native = aws_mqtt_rr_client_operation_release(stream_binding->native); |
| 628 |
return NULL; |
| 629 |
} |
| 630 |
|
| 631 |
return capsule; |
| 632 |
} |
| 633 |
|
| 634 |
PyObject *aws_py_mqtt_streaming_operation_open(PyObject *self, PyObject *args) { |
| 635 |
(void)self; |
| 636 |
|
| 637 |
PyObject *stream_capsule_py; |
| 638 |
|
| 639 |
if (!PyArg_ParseTuple( |
| 640 |
args, |
| 641 |
"O", |
| 642 |
&stream_capsule_py)) { |
| 643 |
return NULL; |
| 644 |
} |
| 645 |
|
| 646 |
struct mqtt_streaming_operation_binding *stream_binding = |
| 647 |
PyCapsule_GetPointer(stream_capsule_py, s_capsule_name_mqtt_streaming_operation); |
| 648 |
if (!stream_binding) { |
| 649 |
return NULL; |
| 650 |
} |
| 651 |
|
| 652 |
if (aws_mqtt_rr_client_operation_activate(stream_binding->native)) { |
| 653 |
PyErr_SetAwsLastError(); |
| 654 |
return NULL; |
| 655 |
} |
| 656 |
|
| 657 |
Py_RETURN_NONE; |
| 658 |
} |
| 659 |
|
| 660 |
struct aws_mqtt_request_response_client *aws_py_get_mqtt_request_response_client( |
| 661 |
PyObject *mqtt_request_response_client) { |
| 662 |
AWS_PY_RETURN_NATIVE_FROM_BINDING( |
| 663 |
mqtt_request_response_client, |
| 664 |
s_capsule_name_mqtt_request_response_client, |
| 665 |
"Client", |
| 666 |
mqtt_request_response_client_binding); |
| 667 |
} |
| 668 |
|
| 669 |
struct aws_mqtt_rr_client_operation *aws_py_get_mqtt_streaming_operation(PyObject *mqtt_streaming_operation) { |
| 670 |
|
| 671 |
AWS_PY_RETURN_NATIVE_FROM_BINDING( |
| 672 |
mqtt_streaming_operation, |
| 673 |
s_capsule_name_mqtt_streaming_operation, |
| 674 |
"StreamingOperation", |
| 675 |
mqtt_streaming_operation_binding); |
| 676 |
} |