From 257b7ad13c3484ee60c58036a314922478ea49af Mon Sep 17 00:00:00 2001 From: Steve Kim Date: Mon, 19 Jun 2023 11:08:32 -0700 Subject: [PATCH 1/9] add extra mqtt3 callbacks --- awsiot/mqtt_connection_builder.py | 3 +++ samples/pubsub.py | 18 +++++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/awsiot/mqtt_connection_builder.py b/awsiot/mqtt_connection_builder.py index 785cd774..0f3e46d7 100644 --- a/awsiot/mqtt_connection_builder.py +++ b/awsiot/mqtt_connection_builder.py @@ -230,6 +230,9 @@ def _builder( use_websockets=use_websockets, websocket_handshake_transform=websocket_handshake_transform, proxy_options=proxy_options, + on_connection_success=_get(kwargs, 'on_connection_success'), + on_connection_failure=_get(kwargs, 'on_connection_failure'), + on_connection_closed=_get(kwargs, 'on_connection_closed'), ) diff --git a/samples/pubsub.py b/samples/pubsub.py index b71dbf2b..9a2ada25 100644 --- a/samples/pubsub.py +++ b/samples/pubsub.py @@ -58,6 +58,19 @@ def on_message_received(topic, payload, dup, qos, retain, **kwargs): if received_count == cmdData.input_count: received_all_event.set() +# Callback when the connection successfully connects +def on_connection_success(connection, callback_data): + assert isinstance(callback_data, mqtt.OnConnectionSuccessData) + print("Connection Successfull with return code: {} session present: {}".format(callback_data.return_code, callback_data.session_present)) + +# Callback when a connection attempt fails +def on_connection_failure(connection, callback_data): + assert isinstance(callback_data, mqtt.OnConnectionFailuredata) + print("Connection failed with error code: {}".format(callback_data.error)) + +# Callback when a connection has been disconnected or shutdown successfully +def on_connection_closed(connection, callback_data): + print("Connection closed") if __name__ == '__main__': # Create the proxy options if the data is present in cmdData @@ -79,7 +92,10 @@ def on_message_received(topic, payload, dup, qos, retain, **kwargs): client_id=cmdData.input_clientId, clean_session=False, keep_alive_secs=30, - http_proxy_options=proxy_options) + http_proxy_options=proxy_options, + on_connection_success=on_connection_success, + on_connection_failure=on_connection_failure, + on_connection_closed=on_connection_closed) if not cmdData.input_is_ci: print(f"Connecting to {cmdData.input_endpoint} with client ID '{cmdData.input_clientId}'...") From 9b41623e8053126553a19b8103d4de416aba4442 Mon Sep 17 00:00:00 2001 From: Steve Kim Date: Mon, 19 Jun 2023 11:13:59 -0700 Subject: [PATCH 2/9] callback documentation --- awsiot/mqtt_connection_builder.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/awsiot/mqtt_connection_builder.py b/awsiot/mqtt_connection_builder.py index 0f3e46d7..1a1f93c7 100644 --- a/awsiot/mqtt_connection_builder.py +++ b/awsiot/mqtt_connection_builder.py @@ -39,6 +39,28 @@ * `**kwargs` (dict): Forward-compatibility kwargs. + **on_connection_success** (`Callable`): Callback invoked whenever the MQTT connection + successfully connects. Function should take the following argumenta and return nothing: + + * `connection` (:class:`awscrt.mqtt.Connection`): This MQTT Connection + + * `callback_data` (:class:`awscrt.mqtt.OnConnectionSuccessData) + + **on_connection_failure** (`Callable`): Callback invoked whenever the MQTT connection + attempt fails. Function should take the following argumenta and return nothing: + + * `connection` (:class:`awscrt.mqtt.Connection`): This MQTT Connection + + * `callback_data` (:class:`awscrt.mqtt.OnConnectionFailureData) + + **on_connection_closed** (`Callable`): Callback invoked whenever the MQTT connection + has been disconnected and shutdown successfully. Function should take the following argumenta + and return nothing: + + * `connection` (:class:`awscrt.mqtt.Connection`): This MQTT Connection + + * `callback_data` (:class:`awscrt.mqtt.OnConnectionClosedData) + **clean_session** (`bool`): Whether or not to start a clean session with each reconnect. If True, the server will forget all subscriptions with each reconnect. Set False to request that the server resume an existing session From bb477ea2e4b3d985daeeb8a8b8d1368ce652d651 Mon Sep 17 00:00:00 2001 From: Steve Kim Date: Mon, 19 Jun 2023 11:18:17 -0700 Subject: [PATCH 3/9] doc edit --- awsiot/mqtt_connection_builder.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/awsiot/mqtt_connection_builder.py b/awsiot/mqtt_connection_builder.py index 1a1f93c7..1a681f2a 100644 --- a/awsiot/mqtt_connection_builder.py +++ b/awsiot/mqtt_connection_builder.py @@ -44,14 +44,16 @@ * `connection` (:class:`awscrt.mqtt.Connection`): This MQTT Connection - * `callback_data` (:class:`awscrt.mqtt.OnConnectionSuccessData) + * `callback_data` (:class:`awscrt.mqtt.OnConnectionSuccessData): The data returned from the + connection success. **on_connection_failure** (`Callable`): Callback invoked whenever the MQTT connection attempt fails. Function should take the following argumenta and return nothing: * `connection` (:class:`awscrt.mqtt.Connection`): This MQTT Connection - * `callback_data` (:class:`awscrt.mqtt.OnConnectionFailureData) + * `callback_data` (:class:`awscrt.mqtt.OnConnectionFailureData): The data returned from the + connection failure. **on_connection_closed** (`Callable`): Callback invoked whenever the MQTT connection has been disconnected and shutdown successfully. Function should take the following argumenta @@ -59,7 +61,8 @@ * `connection` (:class:`awscrt.mqtt.Connection`): This MQTT Connection - * `callback_data` (:class:`awscrt.mqtt.OnConnectionClosedData) + * `callback_data` (:class:`awscrt.mqtt.OnConnectionClosedData): The data returned from the + connection close. **clean_session** (`bool`): Whether or not to start a clean session with each reconnect. If True, the server will forget all subscriptions with each reconnect. From 79f71a8c95576c4c9eb5304e2fe1a6577eca4c1d Mon Sep 17 00:00:00 2001 From: Steve Kim Date: Mon, 19 Jun 2023 11:27:39 -0700 Subject: [PATCH 4/9] formatting check --- awsiot/mqtt_connection_builder.py | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/awsiot/mqtt_connection_builder.py b/awsiot/mqtt_connection_builder.py index 1a681f2a..91ea7de6 100644 --- a/awsiot/mqtt_connection_builder.py +++ b/awsiot/mqtt_connection_builder.py @@ -39,30 +39,29 @@ * `**kwargs` (dict): Forward-compatibility kwargs. - **on_connection_success** (`Callable`): Callback invoked whenever the MQTT connection - successfully connects. Function should take the following argumenta and return nothing: + **on_connection_success** (`Callable`): Callback invoked whenever the MQTT connection is lost. + The MQTT client will automatically attempt to reconnect. + The function should take the following arguments return nothing: - * `connection` (:class:`awscrt.mqtt.Connection`): This MQTT Connection + * `connection` (:class:`awscrt.mqtt.Connection`): This MQTT Connection. - * `callback_data` (:class:`awscrt.mqtt.OnConnectionSuccessData): The data returned from the - connection success. + * `error` (:class:`awscrt.exceptions.AwsCrtError`): Exception which caused connection loss. - **on_connection_failure** (`Callable`): Callback invoked whenever the MQTT connection - attempt fails. Function should take the following argumenta and return nothing: + **on_connection_failure** (`Callable`): Callback invoked whenever the MQTT connection is lost. + The MQTT client will automatically attempt to reconnect. + The function should take the following arguments return nothing: - * `connection` (:class:`awscrt.mqtt.Connection`): This MQTT Connection + * `connection` (:class:`awscrt.mqtt.Connection`): This MQTT Connection. - * `callback_data` (:class:`awscrt.mqtt.OnConnectionFailureData): The data returned from the - connection failure. + * `error` (:class:`awscrt.exceptions.AwsCrtError`): Exception which caused connection loss. - **on_connection_closed** (`Callable`): Callback invoked whenever the MQTT connection - has been disconnected and shutdown successfully. Function should take the following argumenta - and return nothing: + **on_connection_closed** (`Callable`): Callback invoked whenever the MQTT connection is lost. + The MQTT client will automatically attempt to reconnect. + The function should take the following arguments return nothing: - * `connection` (:class:`awscrt.mqtt.Connection`): This MQTT Connection + * `connection` (:class:`awscrt.mqtt.Connection`): This MQTT Connection. - * `callback_data` (:class:`awscrt.mqtt.OnConnectionClosedData): The data returned from the - connection close. + * `error` (:class:`awscrt.exceptions.AwsCrtError`): Exception which caused connection loss. **clean_session** (`bool`): Whether or not to start a clean session with each reconnect. If True, the server will forget all subscriptions with each reconnect. From 78f289ca000af4613ff782ee4e54d25c23516978 Mon Sep 17 00:00:00 2001 From: Steve Kim Date: Mon, 19 Jun 2023 11:29:39 -0700 Subject: [PATCH 5/9] incremental change --- awsiot/mqtt_connection_builder.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/awsiot/mqtt_connection_builder.py b/awsiot/mqtt_connection_builder.py index 91ea7de6..50a95bd9 100644 --- a/awsiot/mqtt_connection_builder.py +++ b/awsiot/mqtt_connection_builder.py @@ -45,7 +45,7 @@ * `connection` (:class:`awscrt.mqtt.Connection`): This MQTT Connection. - * `error` (:class:`awscrt.exceptions.AwsCrtError`): Exception which caused connection loss. + * `callback_data` (:class:`awscrt.OnConnectionSuccessData`): Exception which caused connection loss. **on_connection_failure** (`Callable`): Callback invoked whenever the MQTT connection is lost. The MQTT client will automatically attempt to reconnect. @@ -53,7 +53,7 @@ * `connection` (:class:`awscrt.mqtt.Connection`): This MQTT Connection. - * `error` (:class:`awscrt.exceptions.AwsCrtError`): Exception which caused connection loss. + * `callback_data` (:class:`awscrt.OnConnectionFailureData`): Exception which caused connection loss. **on_connection_closed** (`Callable`): Callback invoked whenever the MQTT connection is lost. The MQTT client will automatically attempt to reconnect. @@ -61,7 +61,7 @@ * `connection` (:class:`awscrt.mqtt.Connection`): This MQTT Connection. - * `error` (:class:`awscrt.exceptions.AwsCrtError`): Exception which caused connection loss. + * `callback_data` (:class:`awscrt.OnConnectionClosedData`): Exception which caused connection loss. **clean_session** (`bool`): Whether or not to start a clean session with each reconnect. If True, the server will forget all subscriptions with each reconnect. From 3f3086872b61230f85439e11c3ffeb84c92be3f0 Mon Sep 17 00:00:00 2001 From: Steve Kim Date: Mon, 19 Jun 2023 11:32:52 -0700 Subject: [PATCH 6/9] formatting --- awsiot/mqtt_connection_builder.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/awsiot/mqtt_connection_builder.py b/awsiot/mqtt_connection_builder.py index 50a95bd9..dd7c13ce 100644 --- a/awsiot/mqtt_connection_builder.py +++ b/awsiot/mqtt_connection_builder.py @@ -45,7 +45,7 @@ * `connection` (:class:`awscrt.mqtt.Connection`): This MQTT Connection. - * `callback_data` (:class:`awscrt.OnConnectionSuccessData`): Exception which caused connection loss. + * `callback_data` (:class:`awscrt.mqtt.OnConnectionSuccessData`): Exception which caused connection loss. **on_connection_failure** (`Callable`): Callback invoked whenever the MQTT connection is lost. The MQTT client will automatically attempt to reconnect. @@ -53,7 +53,7 @@ * `connection` (:class:`awscrt.mqtt.Connection`): This MQTT Connection. - * `callback_data` (:class:`awscrt.OnConnectionFailureData`): Exception which caused connection loss. + * `callback_data` (:class:`awscrt.mqtt.OnConnectionFailureData`): Exception which caused connection loss. **on_connection_closed** (`Callable`): Callback invoked whenever the MQTT connection is lost. The MQTT client will automatically attempt to reconnect. @@ -61,7 +61,7 @@ * `connection` (:class:`awscrt.mqtt.Connection`): This MQTT Connection. - * `callback_data` (:class:`awscrt.OnConnectionClosedData`): Exception which caused connection loss. + * `callback_data` (:class:`awscrt.mqtt.OnConnectionClosedData`): Exception which caused connection loss. **clean_session** (`bool`): Whether or not to start a clean session with each reconnect. If True, the server will forget all subscriptions with each reconnect. From 8b977f3000f81043e4694ce09f149576ffebb7a9 Mon Sep 17 00:00:00 2001 From: Steve Kim Date: Mon, 19 Jun 2023 11:37:02 -0700 Subject: [PATCH 7/9] doc edit --- awsiot/mqtt_connection_builder.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/awsiot/mqtt_connection_builder.py b/awsiot/mqtt_connection_builder.py index dd7c13ce..7fb65b54 100644 --- a/awsiot/mqtt_connection_builder.py +++ b/awsiot/mqtt_connection_builder.py @@ -45,7 +45,7 @@ * `connection` (:class:`awscrt.mqtt.Connection`): This MQTT Connection. - * `callback_data` (:class:`awscrt.mqtt.OnConnectionSuccessData`): Exception which caused connection loss. + * `callback_data` (:class:`awscrt.mqtt.OnConnectionSuccessData`): The data returned from the connection success. **on_connection_failure** (`Callable`): Callback invoked whenever the MQTT connection is lost. The MQTT client will automatically attempt to reconnect. @@ -53,7 +53,7 @@ * `connection` (:class:`awscrt.mqtt.Connection`): This MQTT Connection. - * `callback_data` (:class:`awscrt.mqtt.OnConnectionFailureData`): Exception which caused connection loss. + * `callback_data` (:class:`awscrt.mqtt.OnConnectionFailureData`): The data returned from the connection failure. **on_connection_closed** (`Callable`): Callback invoked whenever the MQTT connection is lost. The MQTT client will automatically attempt to reconnect. @@ -61,7 +61,7 @@ * `connection` (:class:`awscrt.mqtt.Connection`): This MQTT Connection. - * `callback_data` (:class:`awscrt.mqtt.OnConnectionClosedData`): Exception which caused connection loss. + * `callback_data` (:class:`awscrt.mqtt.OnConnectionClosedData`): The data returned from the connection close. **clean_session** (`bool`): Whether or not to start a clean session with each reconnect. If True, the server will forget all subscriptions with each reconnect. From 1a6d93cebc2db3dddd513267aaac846e4dda4dd7 Mon Sep 17 00:00:00 2001 From: Steve Kim Date: Mon, 19 Jun 2023 11:44:39 -0700 Subject: [PATCH 8/9] proper documentation description for new callbacks >,< --- awsiot/mqtt_connection_builder.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/awsiot/mqtt_connection_builder.py b/awsiot/mqtt_connection_builder.py index 7fb65b54..468f9f54 100644 --- a/awsiot/mqtt_connection_builder.py +++ b/awsiot/mqtt_connection_builder.py @@ -39,7 +39,7 @@ * `**kwargs` (dict): Forward-compatibility kwargs. - **on_connection_success** (`Callable`): Callback invoked whenever the MQTT connection is lost. + **on_connection_success** (`Callable`): Optional callback invoked whenever the connection successfully connects. The MQTT client will automatically attempt to reconnect. The function should take the following arguments return nothing: @@ -47,7 +47,7 @@ * `callback_data` (:class:`awscrt.mqtt.OnConnectionSuccessData`): The data returned from the connection success. - **on_connection_failure** (`Callable`): Callback invoked whenever the MQTT connection is lost. + **on_connection_failure** (`Callable`): Optional callback invoked whenever the connection fails to connect. The MQTT client will automatically attempt to reconnect. The function should take the following arguments return nothing: @@ -55,7 +55,7 @@ * `callback_data` (:class:`awscrt.mqtt.OnConnectionFailureData`): The data returned from the connection failure. - **on_connection_closed** (`Callable`): Callback invoked whenever the MQTT connection is lost. + **on_connection_closed** (`Callable`): Optional callback invoked whenever the connection has been disconnected and shutdown successfully. The MQTT client will automatically attempt to reconnect. The function should take the following arguments return nothing: From 972327602ca9a52259c72712bf7ecb4c14740f29 Mon Sep 17 00:00:00 2001 From: Steve Kim Date: Mon, 19 Jun 2023 12:58:16 -0700 Subject: [PATCH 9/9] cr fixes --- awsiot/mqtt_connection_builder.py | 9 +++------ samples/pubsub.py | 2 +- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/awsiot/mqtt_connection_builder.py b/awsiot/mqtt_connection_builder.py index 468f9f54..a78a1850 100644 --- a/awsiot/mqtt_connection_builder.py +++ b/awsiot/mqtt_connection_builder.py @@ -40,24 +40,21 @@ * `**kwargs` (dict): Forward-compatibility kwargs. **on_connection_success** (`Callable`): Optional callback invoked whenever the connection successfully connects. - The MQTT client will automatically attempt to reconnect. - The function should take the following arguments return nothing: + The function should take the following arguments and return nothing: * `connection` (:class:`awscrt.mqtt.Connection`): This MQTT Connection. * `callback_data` (:class:`awscrt.mqtt.OnConnectionSuccessData`): The data returned from the connection success. **on_connection_failure** (`Callable`): Optional callback invoked whenever the connection fails to connect. - The MQTT client will automatically attempt to reconnect. - The function should take the following arguments return nothing: + The function should take the following arguments and return nothing: * `connection` (:class:`awscrt.mqtt.Connection`): This MQTT Connection. * `callback_data` (:class:`awscrt.mqtt.OnConnectionFailureData`): The data returned from the connection failure. **on_connection_closed** (`Callable`): Optional callback invoked whenever the connection has been disconnected and shutdown successfully. - The MQTT client will automatically attempt to reconnect. - The function should take the following arguments return nothing: + The function should take the following arguments and return nothing: * `connection` (:class:`awscrt.mqtt.Connection`): This MQTT Connection. diff --git a/samples/pubsub.py b/samples/pubsub.py index 9a2ada25..14b50dc0 100644 --- a/samples/pubsub.py +++ b/samples/pubsub.py @@ -61,7 +61,7 @@ def on_message_received(topic, payload, dup, qos, retain, **kwargs): # Callback when the connection successfully connects def on_connection_success(connection, callback_data): assert isinstance(callback_data, mqtt.OnConnectionSuccessData) - print("Connection Successfull with return code: {} session present: {}".format(callback_data.return_code, callback_data.session_present)) + print("Connection Successful with return code: {} session present: {}".format(callback_data.return_code, callback_data.session_present)) # Callback when a connection attempt fails def on_connection_failure(connection, callback_data):