From 06d55fd30c9a85a49c3497092c98dfa8ec9dff49 Mon Sep 17 00:00:00 2001 From: Justin Boswell Date: Tue, 19 Nov 2019 16:31:16 -0800 Subject: [PATCH] Bumped version for new pypi package (#95) * Bumped version for new pypi package * Use uuid for client id * Applied PR feedback --- setup.py | 2 +- source/mqtt_client_connection.c | 29 ++++++++++++++++++++--------- test/test_mqtt.py | 3 ++- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/setup.py b/setup.py index 06d1a68a5..20bb9429b 100644 --- a/setup.py +++ b/setup.py @@ -224,7 +224,7 @@ def awscrt_ext(): setuptools.setup( name="awscrt", - version="0.3.3", + version="0.4.1", author="Amazon Web Services, Inc", author_email="aws-sdk-common-runtime@amazon.com", description="A common runtime for AWS Python projects", diff --git a/source/mqtt_client_connection.c b/source/mqtt_client_connection.c index df3affb86..9b8706ff5 100644 --- a/source/mqtt_client_connection.c +++ b/source/mqtt_client_connection.c @@ -48,6 +48,7 @@ struct mqtt_connection_binding { /* Dependencies that must outlive this */ PyObject *on_connection_interrupted; PyObject *on_connection_resumed; + PyObject *on_any_publish; PyObject *client; }; @@ -56,6 +57,7 @@ static void s_mqtt_python_connection_finish_destruction(struct mqtt_connection_b Py_XDECREF(py_connection->on_connection_interrupted); Py_XDECREF(py_connection->on_connection_resumed); + Py_XDECREF(py_connection->on_any_publish); Py_DECREF(py_connection->client); aws_mem_release(aws_py_get_allocator(), py_connection); @@ -184,7 +186,9 @@ PyObject *aws_py_mqtt_client_connection_new(PyObject *self, PyObject *args) { /* From hereon, nothing will fail */ py_connection->on_connection_interrupted = PyWeakref_NewProxy(on_connection_interrupted, NULL); + AWS_FATAL_ASSERT(py_connection->on_connection_interrupted); py_connection->on_connection_resumed = PyWeakref_NewProxy(on_connection_resumed, NULL); + AWS_FATAL_ASSERT(py_connection->on_connection_resumed); py_connection->client = client_py; Py_INCREF(py_connection->client); @@ -602,10 +606,6 @@ static void s_subscribe_callback( (void)connection; PyObject *callback = user_data; - if (!callback) { - return; - } - PyGILState_STATE state = PyGILState_Ensure(); PyObject *result = PyObject_CallFunction( @@ -644,7 +644,7 @@ static void s_suback_callback( (void)connection; PyObject *callback = userdata; - if (!callback) { + if (callback == Py_None) { return; } @@ -683,8 +683,8 @@ PyObject *aws_py_mqtt_client_connection_subscribe(PyObject *self, PyObject *args return NULL; } - Py_XINCREF(callback); - Py_XINCREF(suback_callback); + Py_INCREF(callback); + Py_INCREF(suback_callback); struct aws_byte_cursor topic_filter = aws_byte_cursor_from_array(topic, topic_len); uint16_t msg_id = aws_mqtt_client_connection_subscribe( @@ -698,8 +698,8 @@ PyObject *aws_py_mqtt_client_connection_subscribe(PyObject *self, PyObject *args suback_callback); if (msg_id == 0) { - Py_XDECREF(callback); - Py_XDECREF(suback_callback); + Py_DECREF(callback); + Py_DECREF(suback_callback); return PyErr_AwsLastError(); } @@ -721,12 +721,23 @@ PyObject *aws_py_mqtt_client_connection_on_message(PyObject *self, PyObject *arg return NULL; } + Py_CLEAR(py_connection->on_any_publish); + + if (callback == Py_None) { + aws_mqtt_client_connection_set_on_any_publish_handler(py_connection->native, NULL, NULL); + Py_RETURN_NONE; + } + callback = PyWeakref_NewProxy(callback, NULL); + AWS_FATAL_ASSERT(callback); + if (aws_mqtt_client_connection_set_on_any_publish_handler(py_connection->native, s_subscribe_callback, callback)) { Py_DECREF(callback); return PyErr_AwsLastError(); } + py_connection->on_any_publish = callback; + Py_RETURN_NONE; } diff --git a/test/test_mqtt.py b/test/test_mqtt.py index 0df9faba2..5fcdcab9d 100644 --- a/test/test_mqtt.py +++ b/test/test_mqtt.py @@ -20,6 +20,7 @@ import unittest import boto3 import time +import uuid import warnings @@ -75,7 +76,7 @@ def _test_connection(self): tls = ClientTlsContext(tls_opts) client = Client(ClientBootstrap(EventLoopGroup()), tls) connection = Connection(client) - connection.connect('aws-crt-python-unit-test-'.format(time.gmtime()), config.endpoint, 8883).result() + connection.connect('aws-crt-python-unit-test-{0}'.format(uuid.uuid4()), config.endpoint, 8883).result() return connection except Exception as ex: self.assertFalse(ex)