|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0. |
| 3 | + |
| 4 | +from awscrt import mqtt5 |
| 5 | +from awsiot import mqtt5_client_builder |
| 6 | +from uuid import uuid4 |
| 7 | +import threading |
| 8 | +from concurrent.futures import Future |
| 9 | +import time |
| 10 | + |
| 11 | +# MQTT5 support is currently in <b>developer preview</b>. We encourage feedback at all times, but feedback during the |
| 12 | +# preview window is especially valuable in shaping the final product. During the preview period we may make |
| 13 | +# backwards-incompatible changes to the public API, but in general, this is something we will try our best to avoid. |
| 14 | + |
| 15 | +# For the purposes of this sample, we need to associate certain variables with a particular MQTT5 client |
| 16 | +# and to do so we use this class to hold all the data for a particular client used in the sample. |
| 17 | +class sample_mqtt5_client: |
| 18 | + client : mqtt5.Client |
| 19 | + name : str |
| 20 | + count : int |
| 21 | + received_count : int |
| 22 | + received_all_event = threading.Event() |
| 23 | + future_stopped : Future |
| 24 | + future_connection_success : Future |
| 25 | + |
| 26 | + # Creates a MQTT5 client using direct MQTT5 via mTLS with the passed input data. |
| 27 | + def __init__(self, input_endpoint, input_cert, input_key, input_ca, input_client_id, input_count, input_client_name) -> None: |
| 28 | + try: |
| 29 | + self.count = input_count |
| 30 | + self.received_count = 0 |
| 31 | + self.name = input_client_name |
| 32 | + self.future_stopped = Future() |
| 33 | + self.future_connection_success = Future() |
| 34 | + self.client = mqtt5_client_builder.mtls_from_path( |
| 35 | + endpoint=input_endpoint, |
| 36 | + cert_filepath=input_cert, |
| 37 | + pri_key_filepath=input_key, |
| 38 | + client_id=input_client_id, |
| 39 | + ca_filepath=input_ca, |
| 40 | + on_publish_received=self.on_publish_received, |
| 41 | + on_lifecycle_stopped=self.on_lifecycle_stopped, |
| 42 | + on_lifecycle_connection_success=self.on_lifecycle_connection_success, |
| 43 | + on_lifecycle_connection_failure=self.on_lifecycle_connection_failure, |
| 44 | + on_lifecycle_disconnection=self.on_lifecycle_disconnection, |
| 45 | + ) |
| 46 | + except Exception as ex: |
| 47 | + print (f"Client creation failed with exception: {ex}") |
| 48 | + raise ex |
| 49 | + |
| 50 | + # Callback when any publish is received |
| 51 | + def on_publish_received(self, publish_packet_data): |
| 52 | + print(f"[{self.name}] Received a publish") |
| 53 | + |
| 54 | + publish_packet = publish_packet_data.publish_packet |
| 55 | + assert isinstance(publish_packet, mqtt5.PublishPacket) |
| 56 | + print(f"\tPublish received message on topic: {publish_packet.topic}") |
| 57 | + print(f"\tMessage: {publish_packet.payload}") |
| 58 | + |
| 59 | + if (publish_packet.user_properties != None): |
| 60 | + if (publish_packet.user_properties.count > 0): |
| 61 | + for i in range(0, publish_packet.user_properties.count): |
| 62 | + user_property = publish_packet.user_properties[i] |
| 63 | + print(f"\t\twith UserProperty ({user_property.name}, {user_property.value})") |
| 64 | + |
| 65 | + self.received_count += 1 |
| 66 | + if self.received_count == self.count: |
| 67 | + self.received_all_event.set() |
| 68 | + |
| 69 | + # Callback for the lifecycle event Stopped |
| 70 | + def on_lifecycle_stopped(self, lifecycle_stopped_data: mqtt5.LifecycleStoppedData): |
| 71 | + print(f"[{self.name}]: Lifecycle Stopped") |
| 72 | + self.future_stopped.set_result(lifecycle_stopped_data) |
| 73 | + |
| 74 | + # Callback for the lifecycle event Connection Success |
| 75 | + def on_lifecycle_connection_success(self, lifecycle_connect_success_data: mqtt5.LifecycleConnectSuccessData): |
| 76 | + print(f"{self.name}]: Lifecycle Connection Success") |
| 77 | + self.future_connection_success.set_result(lifecycle_connect_success_data) |
| 78 | + |
| 79 | + # Callback for the lifecycle event Connection Failure |
| 80 | + def on_lifecycle_connection_failure(self, lifecycle_connection_failure: mqtt5.LifecycleConnectFailureData): |
| 81 | + print(f"{self.name}]: Lifecycle Connection Failure") |
| 82 | + print(f"{self.name}]: Connection failed with exception:{lifecycle_connection_failure.exception}") |
| 83 | + |
| 84 | + # Callback for the lifecycle event Disconnection |
| 85 | + def on_lifecycle_disconnection(self, disconnect_data: mqtt5.LifecycleDisconnectData): |
| 86 | + print(f"{self.name}]: Lifecycle Disconnected") |
| 87 | + |
| 88 | + if (disconnect_data.disconnect_packet != None): |
| 89 | + print(f"\tDisconnection packet code: {disconnect_data.disconnect_packet.reason_code}") |
| 90 | + print(f"\tDisconnection packet reason: {disconnect_data.disconnect_packet.reason_string}") |
| 91 | + if (disconnect_data.disconnect_packet.reason_code == mqtt5.DisconnectReasonCode.SHARED_SUBSCRIPTIONS_NOT_SUPPORTED): |
| 92 | + # Stop the client, which will interrupt the subscription and stop the sample |
| 93 | + self.client.stop() |
| 94 | + |
| 95 | +# Register arguments that can be parsed from the command line |
| 96 | +import utils.command_line_utils as command_line_utils |
| 97 | +cmdUtils = command_line_utils.CommandLineUtils("SharedSubscription - Send and receive messages through a MQTT5 shared subscription") |
| 98 | +cmdUtils.add_common_mqtt5_commands() |
| 99 | +cmdUtils.add_common_topic_message_commands() |
| 100 | +cmdUtils.add_common_proxy_commands() |
| 101 | +cmdUtils.add_common_logging_commands() |
| 102 | +cmdUtils.register_command("key", "<path>", "Path to your key in PEM format.", True, str) |
| 103 | +cmdUtils.register_command("cert", "<path>", "Path to your client certificate in PEM format.", True, str) |
| 104 | +cmdUtils.register_command( |
| 105 | + "port", |
| 106 | + "<int>", |
| 107 | + "Connection port. AWS IoT supports 433 and 8883 (optional, default=auto).", |
| 108 | + type=int) |
| 109 | +cmdUtils.register_command( |
| 110 | + "client_id", |
| 111 | + "<str>", |
| 112 | + "Client ID to use for MQTT5 connection (optional, default=None)." |
| 113 | + "Note that '1', '2', and '3' will be added for to the given clientIDs since this sample uses 3 clients.", |
| 114 | + default="test-" + str(uuid4())) |
| 115 | +cmdUtils.register_command( |
| 116 | + "count", |
| 117 | + "<int>", |
| 118 | + "The number of messages to send (optional, default='10').", |
| 119 | + default=10, |
| 120 | + type=int) |
| 121 | +cmdUtils.register_command( |
| 122 | + "group_identifier", |
| 123 | + "<str>", |
| 124 | + "The group identifier to use in the shared subscription (optional, default='python-sample')", |
| 125 | + default="python-sample", |
| 126 | + type=str) |
| 127 | +cmdUtils.register_command("is_ci", "<str>", "If present the sample will run in CI mode (optional, default='None')") |
| 128 | +# Needs to be called so the command utils parse the commands |
| 129 | +cmdUtils.get_args() |
| 130 | + |
| 131 | +# Pull all the data from the command line |
| 132 | +input_endpoint = cmdUtils.get_command_required("endpoint") |
| 133 | +input_cert = cmdUtils.get_command_required("cert") |
| 134 | +input_key = cmdUtils.get_command_required("key") |
| 135 | +input_ca = cmdUtils.get_command("ca_file") |
| 136 | +input_client_id = cmdUtils.get_command("client_id", "test-" + str(uuid4())) |
| 137 | +input_count = cmdUtils.get_command("count", 10) |
| 138 | +input_topic = cmdUtils.get_command("topic", "test/topic") |
| 139 | +input_message = cmdUtils.get_command("message", "Hello World!") |
| 140 | +input_group_identifier = cmdUtils.get_command("group_identifier", "python-sample") |
| 141 | +input_is_ci = cmdUtils.get_command("is_ci", None) |
| 142 | +input_is_ci_boolean = (input_is_ci != None and input_is_ci != "None") |
| 143 | + |
| 144 | +# If this is CI, append a UUID to the topic |
| 145 | +if (input_is_ci_boolean): |
| 146 | + input_topic += "/" + str(uuid4()) |
| 147 | + |
| 148 | +# Construct the shared topic |
| 149 | +input_shared_topic = f"$share/{input_group_identifier}/{input_topic}" |
| 150 | + |
| 151 | +# Make sure the message count is even |
| 152 | +if (input_count % 2 > 0): |
| 153 | + exit(ValueError("Error: '--count' is an odd number. '--count' must be even or zero for this sample.")) |
| 154 | + |
| 155 | +if __name__ == '__main__': |
| 156 | + try: |
| 157 | + # Create the MQTT5 clients: one publisher and two subscribers |
| 158 | + publisher = sample_mqtt5_client( |
| 159 | + input_endpoint, input_cert, input_key, input_ca, |
| 160 | + input_client_id + "1", input_count/2, "Publisher") |
| 161 | + subscriber_one = sample_mqtt5_client( |
| 162 | + input_endpoint, input_cert, input_key, input_ca, |
| 163 | + input_client_id + "2", input_count/2, "Subscriber One") |
| 164 | + subscriber_two = sample_mqtt5_client( |
| 165 | + input_endpoint, input_cert, input_key, input_ca, |
| 166 | + input_client_id + "3", input_count, "Subscriber Two") |
| 167 | + |
| 168 | + # Connect all the clients |
| 169 | + publisher.client.start() |
| 170 | + publisher.future_connection_success.result(60) |
| 171 | + print (f"[{publisher.name}]: Connected") |
| 172 | + subscriber_one.client.start() |
| 173 | + subscriber_one.future_connection_success.result(60) |
| 174 | + print (f"[{subscriber_one.name}]: Connected") |
| 175 | + subscriber_two.client.start() |
| 176 | + subscriber_two.future_connection_success.result(60) |
| 177 | + print (f"[{subscriber_two.name}]: Connected") |
| 178 | + |
| 179 | + # Subscribe to the shared topic on the two subscribers |
| 180 | + subscribe_packet = mqtt5.SubscribePacket( |
| 181 | + subscriptions=[mqtt5.Subscription( |
| 182 | + topic_filter=input_shared_topic, |
| 183 | + qos=mqtt5.QoS.AT_LEAST_ONCE)] |
| 184 | + ) |
| 185 | + try: |
| 186 | + subscribe_one_future = subscriber_one.client.subscribe(subscribe_packet) |
| 187 | + suback_one = subscribe_one_future.result(60) |
| 188 | + print(f"[{subscriber_one.name}]: Subscribed with: {suback_one.reason_codes}") |
| 189 | + subscribe_two_future = subscriber_two.client.subscribe(subscribe_packet) |
| 190 | + suback_two = subscribe_two_future.result(60) |
| 191 | + print(f"[{subscriber_two.name}]: Subscribed with: {suback_two.reason_codes}") |
| 192 | + except Exception as ex: |
| 193 | + # TMP: If this fails subscribing in CI, just exit the sample gracefully. |
| 194 | + if (input_is_ci != None and input_is_ci != "None"): |
| 195 | + exit(0) |
| 196 | + else: |
| 197 | + raise ex |
| 198 | + |
| 199 | + # Publish using the publisher client |
| 200 | + if (input_count > 0): |
| 201 | + publish_count = 1 |
| 202 | + while (publish_count <= input_count): |
| 203 | + publish_message = f"{input_message} [{publish_count}]" |
| 204 | + publish_future = publisher.client.publish(mqtt5.PublishPacket( |
| 205 | + topic=input_topic, |
| 206 | + payload=publish_message, |
| 207 | + qos=mqtt5.QoS.AT_LEAST_ONCE |
| 208 | + )) |
| 209 | + publish_completion_data = publish_future.result(60) |
| 210 | + print(f"[{publisher.name}]: Sent publish and got PubAck with {repr(publish_completion_data.puback.reason_code)}") |
| 211 | + time.sleep(1) |
| 212 | + publish_count += 1 |
| 213 | + |
| 214 | + # Make sure all the messages were gotten on the subscribers |
| 215 | + subscriber_one.received_all_event.wait(60) |
| 216 | + subscriber_two.received_all_event.wait(60) |
| 217 | + else: |
| 218 | + print("Skipping publishing messages due to message count being zero...") |
| 219 | + |
| 220 | + # Unsubscribe from the shared topic on the two subscribers |
| 221 | + unsubscribe_packet = mqtt5.UnsubscribePacket(topic_filters=[input_shared_topic]) |
| 222 | + unsubscribe_one_future = subscriber_one.client.unsubscribe(unsubscribe_packet) |
| 223 | + unsuback_one = unsubscribe_one_future.result(60) |
| 224 | + print(f"[{subscriber_one.name}]: Unsubscribed with {unsuback_one.reason_codes}") |
| 225 | + unsubscribe_two_future = subscriber_two.client.unsubscribe(unsubscribe_packet) |
| 226 | + unsuback_two = unsubscribe_two_future.result(60) |
| 227 | + print(f"[{subscriber_two.name}]: Unsubscribed with {unsuback_two.reason_codes}") |
| 228 | + |
| 229 | + # Disconnect all the clients |
| 230 | + publisher.client.stop() |
| 231 | + publisher.future_stopped.result(60) |
| 232 | + print(f"[{publisher.name}]: Fully stopped") |
| 233 | + subscriber_one.client.stop() |
| 234 | + subscriber_one.future_stopped.result(60) |
| 235 | + print(f"[{subscriber_one.name}]: Fully stopped") |
| 236 | + subscriber_two.client.stop() |
| 237 | + subscriber_two.future_stopped.result(60) |
| 238 | + print(f"[{subscriber_two.name}]: Fully stopped") |
| 239 | + |
| 240 | + except Exception as ex: |
| 241 | + print (f"An exception ocurred while running sample! Exception: {ex}") |
| 242 | + exit(ex) |
| 243 | + |
| 244 | + print ("Complete!") |
0 commit comments