Skip to content

Commit 8a3b4c7

Browse files
authored
Add on disconnect callback to samples (#647)
* add on disconnect callback * update default client id to be mqtt5-sample-* * remove unused parameters * update path for folder strucutres * fix path for test-test-pypi
1 parent d7ccf2f commit 8a3b4c7

22 files changed

+108
-48
lines changed

codebuild/cd/test-prod-pypi.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ phases:
2828
- CURRENT_TAG_VERSION=$(cat $CODEBUILD_SRC_DIR/VERSION)
2929
- python3 codebuild/cd/pip-install-with-retry.py --no-cache-dir --user awsiotsdk==$CURRENT_TAG_VERSION
3030
# Run PubSub sample
31-
- python3 samples/mqtt5_pubsub.py --endpoint ${ENDPOINT} --cert /tmp/certificate.pem --key /tmp/privatekey.pem --ca_file /tmp/AmazonRootCA1.pem --verbosity Trace
31+
- python3 samples/mqtt/mqtt5_x509.py --endpoint ${ENDPOINT} --cert /tmp/certificate.pem --key /tmp/privatekey.pem --ca_file /tmp/AmazonRootCA1.pem --verbosity Trace
3232

3333
post_build:
3434
commands:

codebuild/cd/test-test-pypi.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ phases:
2929
- python3 -m pip install typing
3030
- python3 codebuild/cd/pip-install-with-retry.py -i https://testpypi.python.org/simple --user awsiotsdk==$CURRENT_TAG_VERSION
3131
# Run PubSub sample
32-
- python3 samples/mqtt5_pubsub.py --endpoint ${ENDPOINT} --cert /tmp/certificate.pem --key /tmp/privatekey.pem --ca_file /tmp/AmazonRootCA1.pem --verbosity Trace
32+
- python3 samples/mqtt/mqtt5_x509.py --endpoint ${ENDPOINT} --cert /tmp/certificate.pem --key /tmp/privatekey.pem --ca_file /tmp/AmazonRootCA1.pem --verbosity Trace
3333

3434
post_build:
3535
commands:

documents/FAQ.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ Here is an example launch.json file to run the pubsub sample
102102
"name": "PubSub",
103103
"type": "python",
104104
"request": "launch",
105-
"program": "${workspaceFolder}/samples/pubsub.py",
105+
"program": "${workspaceFolder}/samples/mqtt/mqtt5_x509.py",
106106
"args": [
107107
"--endpoint", "<account-number>-ats.iot.<region>.amazonaws.com",
108108
"--ca_file", "<path to root-CA>",

documents/MIGRATION_GUIDE.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ delta_subscribed_future.result()
783783
For more information, see API documentation for the v2 SDK
784784
[Device Shadow](https://aws.github.io/aws-iot-device-sdk-python-v2/awsiot/iotshadow.html).
785785

786-
For code examples, see the v2 SDK [Device Shadow](https://github.com/aws/aws-iot-device-sdk-python-v2/blob/main/samples/shadow_mqtt5.py).
786+
For code examples, see the v2 SDK [Device Shadow](https://github.com/aws/aws-iot-device-sdk-python-v2/blob/main/samples/service_clients/shadow.py).
787787

788788

789789
### Client for AWS IoT Jobs
@@ -1131,7 +1131,7 @@ For detailed descriptions for the topics used to interact with the service, see
11311131
For more information about the service clients, see API documentation for the v2 SDK
11321132
[Jobs](https://aws.github.io/aws-iot-device-sdk-python-v2/awsiot/iotjobs.html).
11331133

1134-
For code examles, see [Jobs](https://github.com/aws/aws-iot-device-sdk-python/blob/master/samples/jobs/jobsSample.py)
1134+
For code examles, see [Jobs](https://github.com/aws/aws-iot-device-sdk-python/blob/master/samples/service_clients/jobs.py)
11351135
samples.
11361136

11371137

@@ -1155,16 +1155,16 @@ AWS IoT Core documentation for [Fleet Provisioning](https://docs.aws.amazon.com/
11551155
For more information about the Fleet Provisioning service client, See API documentation for the v2 SDK
11561156
[Fleet Provisioning](https://aws.github.io/aws-iot-device-sdk-python-v2/awsiot/iotidentity.html).
11571157

1158-
For code examples, see the v2 SDK [Fleet Provisioning](https://github.com/aws/aws-iot-device-sdk-python-v2/blob/main/samples/fleetprovisioning.md)
1158+
For code examples, see the v2 SDK [Fleet Provisioning](https://github.com/aws/aws-iot-device-sdk-python-v2/blob/main/samples/service_clients/fleet_provisioning_basic.md)
11591159
samples.
11601160

11611161

11621162
### Example
11631163

11641164
It's always helpful to look at a working example to see how new functionality works, to be able to tweak different options,
11651165
to compare with existing code. For that reason, we implemented a
1166-
[Publish/Subscribe example](https://github.com/aws/aws-iot-device-sdk-python-v2/blob/main/samples/mqtt5_pubsub.md)
1167-
([source code](https://github.com/aws/aws-iot-device-sdk-python-v2/blob/main/samples/mqtt5_pubsub.py))
1166+
[X509 Publish/Subscribe example](https://github.com/aws/aws-iot-device-sdk-python-v2/blob/main/samples/mqtt/mqtt5_x509.md)
1167+
([source code](https://github.com/aws/aws-iot-device-sdk-python-v2/blob/main/samples/mqtt/mqtt5_x509.py))
11681168
in the v2 SDK similar to a sample provided by the v1 SDK (see a corresponding
11691169
[readme section](https://github.com/aws/aws-iot-device-sdk-python/blob/master/README.rst#basicpubsub) and
11701170
[source code](https://github.com/aws/aws-iot-device-sdk-python/blob/master/samples/basicPubSub/basicPubSub.py)).

samples/mqtt/mqtt5_aws_websocket.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ The AWS IAM permission policy associated with the AWS credentials resolved by th
4949
"iot:Connect"
5050
],
5151
"Resource": [
52-
"arn:aws:iot:<b>region</b>:<b>account</b>:client/test-*"
52+
"arn:aws:iot:<b>region</b>:<b>account</b>:client/mqtt5-sample-*"
5353
]
5454
}
5555
]
@@ -60,7 +60,7 @@ Replace with the following with the data from your AWS account:
6060
* `<region>`: The AWS IoT Core region where you created your AWS IoT Core thing you wish to use with this sample. For example `us-east-1`.
6161
* `<account>`: Your AWS IoT Core account ID. This is the set of numbers in the top right next to your AWS account name when using the AWS IoT Core website.
6262

63-
Note that in a real application, you may want to avoid the use of wildcards in your ClientID or use them selectively. Please follow best practices when working with AWS on production applications using the SDK. Also, for the purposes of this sample, please make sure your policy allows a client ID of `test-*` to connect or use `--client_id <client ID here>` to send the client ID your policy supports.
63+
Note that in a real application, you may want to avoid the use of wildcards in your ClientID or use them selectively. Please follow best practices when working with AWS on production applications using the SDK. Also, for the purposes of this sample, please make sure your policy allows a client ID of `mqtt5-sample-*` to connect or use `--client_id <client ID here>` to send the client ID your policy supports.
6464

6565
</details>
6666

@@ -92,7 +92,7 @@ required arguments:
9292
--signing_region Signing region for websocket connection (default: None)
9393
9494
optional arguments:
95-
--client_id Client ID (default: mqtt5-sample-809571c8)
95+
--client_id Client ID (default: mqtt5-sample-<uuid>)
9696
--topic Topic (default: test/topic)
9797
--message Message payload (default: Hello from mqtt5 sample)
9898
--count Messages to publish (0 = infinite) (default: 5)

samples/mqtt/mqtt5_aws_websocket.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def on_lifecycle_attempting_connect(lifecycle_attempting_connect_data: mqtt5.Lif
7474
# Callback for the lifecycle event Connection Success
7575
def on_lifecycle_connection_success(lifecycle_connect_success_data: mqtt5.LifecycleConnectSuccessData):
7676
connack_packet = lifecycle_connect_success_data.connack_packet
77-
print("Lifecycle Connection Success with reason_code:{}\n".format(
77+
print("Lifecycle Connection Success with reason code:{}\n".format(
7878
repr(connack_packet.reason_code)))
7979
connection_success_event.set()
8080

@@ -85,6 +85,12 @@ def on_lifecycle_connection_failure(lifecycle_connection_failure: mqtt5.Lifecycl
8585
lifecycle_connection_failure.exception))
8686

8787

88+
# Callback for the lifecycle event Disconnection
89+
def on_lifecycle_disconnection(lifecycle_disconnect_data: mqtt5.LifecycleDisconnectData):
90+
print("Lifecycle Disconnected with reason code:{}".format(
91+
lifecycle_disconnect_data.disconnect_packet.reason_code if lifecycle_disconnect_data.disconnect_packet else "None"))
92+
93+
8894
if __name__ == '__main__':
8995
print("\nStarting MQTT5 Websocket Sample\n")
9096
message_count = args.input_count
@@ -105,6 +111,7 @@ def on_lifecycle_connection_failure(lifecycle_connection_failure: mqtt5.Lifecycl
105111
on_lifecycle_attempting_connect=on_lifecycle_attempting_connect,
106112
on_lifecycle_connection_success=on_lifecycle_connection_success,
107113
on_lifecycle_connection_failure=on_lifecycle_connection_failure,
114+
on_lifecycle_disconnection=on_lifecycle_disconnection,
108115
client_id=args.input_clientId)
109116

110117

samples/mqtt/mqtt5_custom_auth.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Below is a sample policy that provides the necessary privileges.
5151
"iot:Connect"
5252
],
5353
"Resource": [
54-
"arn:aws:iot:<b>region</b>:<b>account</b>:client/test-*"
54+
"arn:aws:iot:<b>region</b>:<b>account</b>:client/mqtt5-sample-*"
5555
]
5656
}
5757
]
@@ -62,7 +62,7 @@ Replace with the following with the data from your AWS account:
6262
* `<region>`: The AWS IoT Core region where you created your AWS IoT Core thing you wish to use with this sample. For example `us-east-1`.
6363
* `<account>`: Your AWS IoT Core account ID. This is the set of numbers in the top right next to your AWS account name when using the AWS IoT Core website.
6464

65-
Note that in a real application, you may want to avoid the use of wildcards in your ClientID or use them selectively. Please follow best practices when working with AWS on production applications using the SDK. Also, for the purposes of this sample, please make sure your policy allows a client ID of `test-*` to connect or use `--client_id <client ID here>` to send the client ID your policy supports.
65+
Note that in a real application, you may want to avoid the use of wildcards in your ClientID or use them selectively. Please follow best practices when working with AWS on production applications using the SDK. Also, for the purposes of this sample, please make sure your policy allows a client ID of `mqtt5-sample-*` to connect or use `--client_id <client ID here>` to send the client ID your policy supports.
6666

6767
</details>
6868

@@ -121,7 +121,7 @@ required arguments:
121121
--auth_password The password to send when connecting through a custom authorizer (optional) (default: None)
122122
123123
optional arguments:
124-
--client_id Client ID (default: test-f3168b42)
124+
--client_id Client ID (default: mqtt5-sample-<uuid>)
125125
--topic Topic (default: test/topic)
126126
--message Message payload (default: Hello from mqtt5 sample)
127127
--count Messages to publish (0 = infinite) (default: 5)

samples/mqtt/mqtt5_custom_auth_signed.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
help="The password to send when connecting through a custom authorizer (optional)")
3434

3535
# Optional Arguments
36-
optional.add_argument("--client_id", metavar="", dest="input_clientId", default=f"test-{uuid.uuid4().hex[:8]}",
36+
optional.add_argument("--client_id", metavar="", dest="input_clientId", default=f"mqtt5-sample-{uuid.uuid4().hex[:8]}",
3737
help="Client ID")
3838
optional.add_argument("--topic", default="test/topic", metavar="", dest="input_topic",
3939
help="Topic")
@@ -86,7 +86,7 @@ def on_lifecycle_attempting_connect(lifecycle_attempting_connect_data: mqtt5.Lif
8686
# Callback for the lifecycle event Connection Success
8787
def on_lifecycle_connection_success(lifecycle_connect_success_data: mqtt5.LifecycleConnectSuccessData):
8888
connack_packet = lifecycle_connect_success_data.connack_packet
89-
print("Lifecycle Connection Success with reason_code:{}\n".format(
89+
print("Lifecycle Connection Success with reason code:{}\n".format(
9090
repr(connack_packet.reason_code)))
9191
connection_success_event.set()
9292

@@ -97,6 +97,12 @@ def on_lifecycle_connection_failure(lifecycle_connection_failure: mqtt5.Lifecycl
9797
lifecycle_connection_failure.exception))
9898

9999

100+
# Callback for the lifecycle event Disconnection
101+
def on_lifecycle_disconnection(lifecycle_disconnect_data: mqtt5.LifecycleDisconnectData):
102+
print("Lifecycle Disconnected with reason code:{}".format(
103+
lifecycle_disconnect_data.disconnect_packet.reason_code if lifecycle_disconnect_data.disconnect_packet else "None"))
104+
105+
100106
if __name__ == '__main__':
101107

102108
# Create MQTT5 Client with a custom authorizer
@@ -114,6 +120,7 @@ def on_lifecycle_connection_failure(lifecycle_connection_failure: mqtt5.Lifecycl
114120
on_lifecycle_attempting_connect=on_lifecycle_attempting_connect,
115121
on_lifecycle_connection_success=on_lifecycle_connection_success,
116122
on_lifecycle_connection_failure=on_lifecycle_connection_failure,
123+
on_lifecycle_disconnection=on_lifecycle_disconnection,
117124
client_id=args.input_clientId)
118125

119126

samples/mqtt/mqtt5_custom_auth_unsigned.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
help="The password to send when connecting through a custom authorizer")
2727

2828
# Optional Arguments
29-
optional.add_argument("--client_id", metavar="", dest="input_clientId", default=f"test-{uuid.uuid4().hex[:8]}",
29+
optional.add_argument("--client_id", metavar="", dest="input_clientId", default=f"mqtt5-sample-{uuid.uuid4().hex[:8]}",
3030
help="Client ID")
3131
optional.add_argument("--topic", default="test/topic", metavar="", dest="input_topic",
3232
help="Topic")
@@ -79,7 +79,7 @@ def on_lifecycle_attempting_connect(lifecycle_attempting_connect_data: mqtt5.Lif
7979
# Callback for the lifecycle event Connection Success
8080
def on_lifecycle_connection_success(lifecycle_connect_success_data: mqtt5.LifecycleConnectSuccessData):
8181
connack_packet = lifecycle_connect_success_data.connack_packet
82-
print("Lifecycle Connection Success with reason_code:{}\n".format(
82+
print("Lifecycle Connection Success with reason code:{}\n".format(
8383
repr(connack_packet.reason_code)))
8484
connection_success_event.set()
8585

@@ -90,6 +90,12 @@ def on_lifecycle_connection_failure(lifecycle_connection_failure: mqtt5.Lifecycl
9090
lifecycle_connection_failure.exception))
9191

9292

93+
# Callback for the lifecycle event Disconnection
94+
def on_lifecycle_disconnection(lifecycle_disconnect_data: mqtt5.LifecycleDisconnectData):
95+
print("Lifecycle Disconnected with reason code:{}".format(
96+
lifecycle_disconnect_data.disconnect_packet.reason_code if lifecycle_disconnect_data.disconnect_packet else "None"))
97+
98+
9399
if __name__ == '__main__':
94100

95101
# Create MQTT5 Client with a custom authorizer
@@ -104,6 +110,7 @@ def on_lifecycle_connection_failure(lifecycle_connection_failure: mqtt5.Lifecycl
104110
on_lifecycle_attempting_connect=on_lifecycle_attempting_connect,
105111
on_lifecycle_connection_success=on_lifecycle_connection_success,
106112
on_lifecycle_connection_failure=on_lifecycle_connection_failure,
113+
on_lifecycle_disconnection=on_lifecycle_disconnection,
107114
client_id=args.input_clientId)
108115

109116

samples/mqtt/mqtt5_pkcs11_connect.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Your IoT Core Thing's [Policy](https://docs.aws.amazon.com/iot/latest/developerg
5252
"iot:Connect"
5353
],
5454
"Resource": [
55-
"arn:aws:iot:<b>region</b>:<b>account</b>:client/test-*"
55+
"arn:aws:iot:<b>region</b>:<b>account</b>:client/mqtt5-sample-*"
5656
]
5757
}
5858
]
@@ -63,7 +63,7 @@ Replace with the following with the data from your AWS account:
6363
* `<region>`: The AWS IoT Core region where you created your AWS IoT Core thing you wish to use with this sample. For example `us-east-1`.
6464
* `<account>`: Your AWS IoT Core account ID. This is the set of numbers in the top right next to your AWS account name when using the AWS IoT Core website.
6565

66-
Note that in a real application, you may want to avoid the use of wildcards in your ClientID or use them selectively. Please follow best practices when working with AWS on production applications using the SDK. Also, for the purposes of this sample, please make sure your policy allows a client ID of `test-*` to connect or use `--client_id <client ID here>` to send the client ID your policy supports.
66+
Note that in a real application, you may want to avoid the use of wildcards in your ClientID or use them selectively. Please follow best practices when working with AWS on production applications using the SDK. Also, for the purposes of this sample, please make sure your policy allows a client ID of `mqtt5-sample-*` to connect or use `--client_id <client ID here>` to send the client ID your policy supports.
6767

6868
</details>
6969

@@ -109,9 +109,8 @@ optional arguments:
109109
--topic Topic (default: test/topic)
110110
--message Message payload (default: Hello from mqtt5 sample)
111111
--count Messages to publish (0 = infinite) (default: 5)
112-
--proxy_host HTTP proxy host (default: None)
113-
--proxy_port HTTP proxy port (default: 0)
114-
--client_id Client ID (default: test-548e4344)
112+
113+
--client_id Client ID (default: mqtt5-sample-<uuid>)
115114
```
116115

117116
### Run sample with SoftHSM

0 commit comments

Comments
 (0)