Search in sources :

Example 1 with DiscoveryClientConfig

use of software.amazon.awssdk.iot.discovery.DiscoveryClientConfig in project aws-iot-device-sdk-java-v2 by aws.

the class BasicDiscovery method main.

public static void main(String[] args) {
    cmdUtils = new CommandLineUtils();
    cmdUtils.registerProgramName("BasicDiscovery");
    cmdUtils.addCommonMQTTCommands();
    cmdUtils.removeCommand("endpoint");
    cmdUtils.registerCommand("thing_name", "<str>", "The name of the IoT thing.");
    cmdUtils.registerCommand("region", "<str>", "AWS IoT service region (optional, default='us-east-1').");
    cmdUtils.registerCommand("topic", "<str>", "Topic to subscribe/publish to (optional, default='test/topic').");
    cmdUtils.registerCommand("mode", "<str>", "Mode options: 'both', 'publish', or 'subscribe' (optional, default='both').");
    cmdUtils.registerCommand("proxy_host", "<str>", "Websocket proxy host to use (optional, required if --proxy_port is set).");
    cmdUtils.registerCommand("proxy_port", "<int>", "Websocket proxy port to use (optional, required if --proxy_host is set).");
    cmdUtils.registerCommand("help", "", "Prints this message");
    cmdUtils.sendArguments(args);
    if (cmdUtils.hasCommand("help")) {
        cmdUtils.printHelp();
        System.exit(1);
    }
    thingName = cmdUtils.getCommandRequired("thing_name", "");
    region = cmdUtils.getCommandOrDefault("region", region);
    rootCaPath = cmdUtils.getCommandOrDefault("ca_file", rootCaPath);
    certPath = cmdUtils.getCommandRequired("cert", "");
    keyPath = cmdUtils.getCommandRequired("key", "");
    topic = cmdUtils.getCommandOrDefault("topic", topic);
    mode = cmdUtils.getCommandOrDefault("mode", mode);
    proxyHost = cmdUtils.getCommandOrDefault("proxy_host", proxyHost);
    proxyPort = Integer.parseInt(cmdUtils.getCommandOrDefault("proxy_port", String.valueOf(proxyPort)));
    // ---- Verify file loads ----
    // Get the absolute CA file path
    final File rootCaFile = new File(rootCaPath);
    if (!rootCaFile.isFile()) {
        throw new RuntimeException("Cannot load root CA from path: " + rootCaFile.getAbsolutePath());
    }
    rootCaPath = rootCaFile.getAbsolutePath();
    final File certFile = new File(certPath);
    if (!certFile.isFile()) {
        throw new RuntimeException("Cannot load certificate from path: " + certFile.getAbsolutePath());
    }
    certPath = certFile.getAbsolutePath();
    final File keyFile = new File(keyPath);
    if (!keyFile.isFile()) {
        throw new RuntimeException("Cannot load private key from path: " + keyFile.getAbsolutePath());
    }
    keyPath = keyFile.getAbsolutePath();
    try (final TlsContextOptions tlsCtxOptions = TlsContextOptions.createWithMtlsFromPath(certPath, keyPath)) {
        if (TlsContextOptions.isAlpnSupported()) {
            tlsCtxOptions.withAlpnList(TLS_EXT_ALPN);
        }
        if (rootCaPath != null) {
            tlsCtxOptions.overrideDefaultTrustStoreFromPath(null, rootCaPath);
        }
        HttpProxyOptions proxyOptions = null;
        if (proxyHost != null && proxyPort > 0) {
            proxyOptions = new HttpProxyOptions();
            proxyOptions.setHost(proxyHost);
            proxyOptions.setPort(proxyPort);
        }
        try (final DiscoveryClientConfig discoveryClientConfig = new DiscoveryClientConfig(tlsCtxOptions, new SocketOptions(), region, 1, proxyOptions);
            final DiscoveryClient discoveryClient = new DiscoveryClient(discoveryClientConfig);
            final MqttClientConnection connection = getClientFromDiscovery(discoveryClient)) {
            if ("subscribe".equals(mode) || "both".equals(mode)) {
                final CompletableFuture<Integer> subFuture = connection.subscribe(topic, QualityOfService.AT_MOST_ONCE, message -> {
                    System.out.println(String.format("Message received on topic %s: %s", message.getTopic(), new String(message.getPayload(), StandardCharsets.UTF_8)));
                });
                subFuture.get();
            }
            final Scanner scanner = new Scanner(System.in);
            while (true) {
                String input = null;
                if ("publish".equals(mode) || "both".equals(mode)) {
                    System.out.println("Enter the message you want to publish to topic " + topic + " and press Enter. " + "Type 'exit' or 'quit' to exit this program: ");
                    input = scanner.nextLine();
                }
                if ("exit".equals(input) || "quit".equals(input)) {
                    System.out.println("Terminating...");
                    break;
                }
                if ("publish".equals(mode) || "both".equals(mode)) {
                    final CompletableFuture<Integer> publishResult = connection.publish(new MqttMessage(topic, input.getBytes(StandardCharsets.UTF_8), QualityOfService.AT_MOST_ONCE, false));
                    Integer result = publishResult.get();
                }
            }
        }
    } catch (CrtRuntimeException | InterruptedException | ExecutionException ex) {
        System.out.println("Exception thrown: " + ex.toString());
        ex.printStackTrace();
    }
    CrtResource.waitForNoResources();
    System.out.println("Complete!");
}
Also used : HttpProxyOptions(software.amazon.awssdk.crt.http.HttpProxyOptions) CrtRuntimeException(software.amazon.awssdk.crt.CrtRuntimeException) DiscoveryClientConfig(software.amazon.awssdk.iot.discovery.DiscoveryClientConfig) CommandLineUtils(utils.commandlineutils.CommandLineUtils) CrtRuntimeException(software.amazon.awssdk.crt.CrtRuntimeException) DiscoveryClient(software.amazon.awssdk.iot.discovery.DiscoveryClient) ExecutionException(java.util.concurrent.ExecutionException) File(java.io.File)

Aggregations

File (java.io.File)1 ExecutionException (java.util.concurrent.ExecutionException)1 CrtRuntimeException (software.amazon.awssdk.crt.CrtRuntimeException)1 HttpProxyOptions (software.amazon.awssdk.crt.http.HttpProxyOptions)1 DiscoveryClient (software.amazon.awssdk.iot.discovery.DiscoveryClient)1 DiscoveryClientConfig (software.amazon.awssdk.iot.discovery.DiscoveryClientConfig)1 CommandLineUtils (utils.commandlineutils.CommandLineUtils)1