|
| 1 | +/** |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0. |
| 4 | + */ |
| 5 | + |
| 6 | +package windowscertpubsub; |
| 7 | + |
| 8 | +import software.amazon.awssdk.crt.*; |
| 9 | +import software.amazon.awssdk.crt.io.*; |
| 10 | +import software.amazon.awssdk.crt.mqtt.*; |
| 11 | +import software.amazon.awssdk.iot.AwsIotMqttConnectionBuilder; |
| 12 | + |
| 13 | +import java.nio.charset.StandardCharsets; |
| 14 | +import java.util.UUID; |
| 15 | +import java.util.concurrent.CompletableFuture; |
| 16 | +import java.util.concurrent.CountDownLatch; |
| 17 | +import java.util.concurrent.ExecutionException; |
| 18 | + |
| 19 | +import utils.commandlineutils.CommandLineUtils; |
| 20 | + |
| 21 | +public class WindowsCertPubSub { |
| 22 | + |
| 23 | + // When run normally, we want to exit nicely even if something goes wrong |
| 24 | + // When run from CI, we want to let an exception escape which in turn causes the |
| 25 | + // exec:java task to return a non-zero exit code |
| 26 | + static String ciPropValue = System.getProperty("aws.crt.ci"); |
| 27 | + static boolean isCI = ciPropValue != null && Boolean.valueOf(ciPropValue); |
| 28 | + |
| 29 | + static String clientId = "test-" + UUID.randomUUID().toString(); |
| 30 | + static String rootCaPath; |
| 31 | + static String windowsCertStorePath; |
| 32 | + static String endpoint; |
| 33 | + static String topic = "test/topic"; |
| 34 | + static String message = "Hello World!"; |
| 35 | + static int messagesToPublish = 10; |
| 36 | + static int port = 8883; |
| 37 | + |
| 38 | + static CommandLineUtils cmdUtils; |
| 39 | + |
| 40 | + /* |
| 41 | + * When called during a CI run, throw an exception that will escape and fail the |
| 42 | + * exec:java task When called otherwise, print what went wrong (if anything) and |
| 43 | + * just continue (return from main) |
| 44 | + */ |
| 45 | + static void onApplicationFailure(Throwable cause) { |
| 46 | + if (isCI) { |
| 47 | + throw new RuntimeException("execution failure", cause); |
| 48 | + } else if (cause != null) { |
| 49 | + System.out.println("Exception encountered: " + cause.toString()); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + public static void main(String[] args) { |
| 54 | + |
| 55 | + cmdUtils = new CommandLineUtils(); |
| 56 | + cmdUtils.registerProgramName("WindowsCertPubSub"); |
| 57 | + cmdUtils.addCommonMQTTCommands(); |
| 58 | + cmdUtils.removeCommand("cert"); |
| 59 | + cmdUtils.removeCommand("key"); |
| 60 | + cmdUtils.registerCommand("cert", "<str>", "Path to certificate in Windows cert store. " + |
| 61 | + "e.g. \"CurrentUser\\MY\\6ac133ac58f0a88b83e9c794eba156a98da39b4c\""); |
| 62 | + cmdUtils.registerCommand("client_id", "<int>", "Client id to use (optional, default='test-*')."); |
| 63 | + cmdUtils.registerCommand("port", "<int>", "Port to connect to on the endpoint (optional, default='8883')."); |
| 64 | + cmdUtils.registerCommand("topic", "<str>", "Topic to subscribe/publish to (optional, default='test/topic')."); |
| 65 | + cmdUtils.registerCommand("message", "<str>", "Message to publish (optional, default='Hello World')."); |
| 66 | + cmdUtils.registerCommand("count", "<int>", "Number of messages to publish (optional, default='10')."); |
| 67 | + cmdUtils.registerCommand("help", "", "Prints this message"); |
| 68 | + cmdUtils.sendArguments(args); |
| 69 | + |
| 70 | + if (cmdUtils.hasCommand("help")) { |
| 71 | + cmdUtils.printHelp(); |
| 72 | + System.exit(1); |
| 73 | + } |
| 74 | + |
| 75 | + endpoint = cmdUtils.getCommandRequired("endpoint", ""); |
| 76 | + windowsCertStorePath = cmdUtils.getCommandRequired("cert", ""); |
| 77 | + rootCaPath = cmdUtils.getCommandOrDefault("root_ca", rootCaPath); |
| 78 | + clientId = cmdUtils.getCommandOrDefault("client_id", clientId); |
| 79 | + port = Integer.parseInt(cmdUtils.getCommandOrDefault("port", String.valueOf(port))); |
| 80 | + topic = cmdUtils.getCommandOrDefault("topic", topic); |
| 81 | + message = cmdUtils.getCommandOrDefault("message", message); |
| 82 | + messagesToPublish = Integer.parseInt(cmdUtils.getCommandOrDefault("count", String.valueOf(messagesToPublish))); |
| 83 | + |
| 84 | + MqttClientConnectionEvents callbacks = new MqttClientConnectionEvents() { |
| 85 | + @Override |
| 86 | + public void onConnectionInterrupted(int errorCode) { |
| 87 | + if (errorCode != 0) { |
| 88 | + System.out.println("Connection interrupted: " + errorCode + ": " + CRT.awsErrorString(errorCode)); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public void onConnectionResumed(boolean sessionPresent) { |
| 94 | + System.out.println("Connection resumed: " + (sessionPresent ? "existing session" : "clean session")); |
| 95 | + } |
| 96 | + }; |
| 97 | + |
| 98 | + try (AwsIotMqttConnectionBuilder builder = |
| 99 | + AwsIotMqttConnectionBuilder.newMtlsWindowsCertStorePathBuilder(windowsCertStorePath)) { |
| 100 | + |
| 101 | + if (rootCaPath != null) { |
| 102 | + builder.withCertificateAuthorityFromPath(null, rootCaPath); |
| 103 | + } |
| 104 | + |
| 105 | + builder.withConnectionEventCallbacks(callbacks) |
| 106 | + .withClientId(clientId) |
| 107 | + .withEndpoint(endpoint) |
| 108 | + .withPort((short) port) |
| 109 | + .withCleanSession(true) |
| 110 | + .withProtocolOperationTimeoutMs(60000); |
| 111 | + |
| 112 | + try (MqttClientConnection connection = builder.build()) { |
| 113 | + |
| 114 | + CompletableFuture<Boolean> connected = connection.connect(); |
| 115 | + try { |
| 116 | + boolean sessionPresent = connected.get(); |
| 117 | + System.out.println("Connected to " + (!sessionPresent ? "new" : "existing") + " session!"); |
| 118 | + } catch (Exception ex) { |
| 119 | + throw new RuntimeException("Exception occurred during connect", ex); |
| 120 | + } |
| 121 | + |
| 122 | + CountDownLatch countDownLatch = new CountDownLatch(messagesToPublish); |
| 123 | + |
| 124 | + CompletableFuture<Integer> subscribed = connection.subscribe(topic, QualityOfService.AT_LEAST_ONCE, (message) -> { |
| 125 | + String payload = new String(message.getPayload(), StandardCharsets.UTF_8); |
| 126 | + System.out.println("MESSAGE: " + payload); |
| 127 | + countDownLatch.countDown(); |
| 128 | + }); |
| 129 | + |
| 130 | + subscribed.get(); |
| 131 | + |
| 132 | + int count = 0; |
| 133 | + while (count++ < messagesToPublish) { |
| 134 | + CompletableFuture<Integer> published = connection.publish(new MqttMessage(topic, message.getBytes(), QualityOfService.AT_LEAST_ONCE, false)); |
| 135 | + published.get(); |
| 136 | + Thread.sleep(1000); |
| 137 | + } |
| 138 | + |
| 139 | + countDownLatch.await(); |
| 140 | + |
| 141 | + CompletableFuture<Void> disconnected = connection.disconnect(); |
| 142 | + disconnected.get(); |
| 143 | + } |
| 144 | + } catch (CrtRuntimeException | InterruptedException | ExecutionException ex) { |
| 145 | + onApplicationFailure(ex); |
| 146 | + } |
| 147 | + |
| 148 | + CrtResource.waitForNoResources(); |
| 149 | + |
| 150 | + System.out.println("Complete!"); |
| 151 | + } |
| 152 | +} |
0 commit comments