File: mqtt_client.c

package info (click to toggle)
aws-crt-python 0.24.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 75,932 kB
  • sloc: ansic: 418,984; python: 23,626; makefile: 6,035; sh: 4,075; ruby: 208; java: 82; perl: 73; cpp: 25; xml: 11
file content (81 lines) | stat: -rw-r--r-- 2,313 bytes parent folder | download | duplicates (3)
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
/**
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */
#include "mqtt_client.h"

#include "io.h"

static const char *s_capsule_name_mqtt_client = "aws_mqtt_client";

struct mqtt_client_binding {
    struct aws_mqtt_client *native;

    /* Dependencies that must outlive this */
    PyObject *bootstrap;
    PyObject *tls_ctx;
};

static void s_mqtt_python_client_destructor(PyObject *client_capsule) {

    struct mqtt_client_binding *client = PyCapsule_GetPointer(client_capsule, s_capsule_name_mqtt_client);
    assert(client);

    aws_mqtt_client_release(client->native);
    Py_DECREF(client->bootstrap);
    Py_DECREF(client->tls_ctx);
    aws_mem_release(aws_py_get_allocator(), client);
}

PyObject *aws_py_mqtt_client_new(PyObject *self, PyObject *args) {
    (void)self;

    struct aws_allocator *allocator = aws_py_get_allocator();

    PyObject *bootstrap_py;
    PyObject *tls_ctx_py;
    if (!PyArg_ParseTuple(args, "OO", &bootstrap_py, &tls_ctx_py)) {
        return NULL;
    }

    struct aws_client_bootstrap *bootstrap = aws_py_get_client_bootstrap(bootstrap_py);
    if (!bootstrap) {
        return NULL;
    }

    struct mqtt_client_binding *client = aws_mem_calloc(allocator, 1, sizeof(struct mqtt_client_binding));
    if (!client) {
        PyErr_SetAwsLastError();
        return NULL;
    }

    /* From hereon, we need to clean up if errors occur */
    client->native = aws_mqtt_client_new(allocator, bootstrap);
    if (client->native == NULL) {
        PyErr_SetAwsLastError();
        goto client_init_failed;
    }

    PyObject *capsule = PyCapsule_New(client, s_capsule_name_mqtt_client, s_mqtt_python_client_destructor);
    if (!capsule) {
        goto capsule_new_failed;
    }

    /* From hereon, nothing will fail */

    client->bootstrap = bootstrap_py;
    Py_INCREF(client->bootstrap);
    client->tls_ctx = tls_ctx_py;
    Py_INCREF(client->tls_ctx);
    return capsule;

capsule_new_failed:
    aws_mqtt_client_release(client->native);
client_init_failed:
    aws_mem_release(allocator, client);
    return NULL;
}

struct aws_mqtt_client *aws_py_get_mqtt_client(PyObject *mqtt_client) {
    AWS_PY_RETURN_NATIVE_FROM_BINDING(mqtt_client, s_capsule_name_mqtt_client, "Client", mqtt_client_binding);
}