Search in sources :

Example 11 with CommandLineUtils

use of utils.commandlineutils.CommandLineUtils in project aws-iot-device-sdk-java-v2 by aws.

the class X509CredentialsProviderConnect method main.

public static void main(String[] args) {
    cmdUtils = new CommandLineUtils();
    cmdUtils.registerProgramName("x509CredentialsProviderConnect");
    cmdUtils.addCommonMQTTCommands();
    cmdUtils.addCommonProxyCommands();
    cmdUtils.addCommonX509Commands();
    cmdUtils.registerCommand("signing_region", "<str>", "AWS IoT service region.");
    cmdUtils.registerCommand("client_id", "<int>", "Client id to use (optional, default='test-*').");
    cmdUtils.registerCommand("port", "<int>", "Port to connect to on the endpoint (optional, default='8883').");
    cmdUtils.sendArguments(args);
    MqttClientConnectionEvents callbacks = new MqttClientConnectionEvents() {

        @Override
        public void onConnectionInterrupted(int errorCode) {
            if (errorCode != 0) {
                System.out.println("Connection interrupted: " + errorCode + ": " + CRT.awsErrorString(errorCode));
            }
        }

        @Override
        public void onConnectionResumed(boolean sessionPresent) {
            System.out.println("Connection resumed: " + (sessionPresent ? "existing session" : "clean session"));
        }
    };
    try {
        // Create a connection using a certificate and key
        // Note: The data for the connection is gotten from cmdUtils.
        // (see buildWebsocketX509MQTTConnection for implementation)
        MqttClientConnection connection = cmdUtils.buildWebsocketX509MQTTConnection(callbacks);
        if (connection == null) {
            onApplicationFailure(new RuntimeException("MQTT connection creation failed!"));
        }
        // Connect and disconnect using the connection we created
        // (see sampleConnectAndDisconnect for implementation)
        cmdUtils.sampleConnectAndDisconnect(connection);
    } catch (CrtRuntimeException | InterruptedException | ExecutionException ex) {
        onApplicationFailure(ex);
    }
    CrtResource.waitForNoResources();
    System.out.println("Complete!");
}
Also used : CrtRuntimeException(software.amazon.awssdk.crt.CrtRuntimeException) MqttClientConnection(software.amazon.awssdk.crt.mqtt.MqttClientConnection) CommandLineUtils(utils.commandlineutils.CommandLineUtils) CrtRuntimeException(software.amazon.awssdk.crt.CrtRuntimeException) MqttClientConnectionEvents(software.amazon.awssdk.crt.mqtt.MqttClientConnectionEvents) ExecutionException(java.util.concurrent.ExecutionException)

Example 12 with CommandLineUtils

use of utils.commandlineutils.CommandLineUtils in project aws-iot-device-sdk-java-v2 by aws.

the class PubSub method main.

public static void main(String[] args) {
    cmdUtils = new CommandLineUtils();
    cmdUtils.registerProgramName("PubSub");
    cmdUtils.addCommonMQTTCommands();
    cmdUtils.addCommonTopicMessageCommands();
    cmdUtils.registerCommand("key", "<path>", "Path to your key in PEM format.");
    cmdUtils.registerCommand("cert", "<path>", "Path to your client certificate in PEM format.");
    cmdUtils.registerCommand("client_id", "<int>", "Client id to use (optional, default='test-*').");
    cmdUtils.registerCommand("port", "<int>", "Port to connect to on the endpoint (optional, default='8883').");
    cmdUtils.registerCommand("count", "<int>", "Number of messages to publish (optional, default='10').");
    cmdUtils.sendArguments(args);
    topic = cmdUtils.getCommandOrDefault("topic", topic);
    message = cmdUtils.getCommandOrDefault("message", message);
    messagesToPublish = Integer.parseInt(cmdUtils.getCommandOrDefault("count", String.valueOf(messagesToPublish)));
    MqttClientConnectionEvents callbacks = new MqttClientConnectionEvents() {

        @Override
        public void onConnectionInterrupted(int errorCode) {
            if (errorCode != 0) {
                System.out.println("Connection interrupted: " + errorCode + ": " + CRT.awsErrorString(errorCode));
            }
        }

        @Override
        public void onConnectionResumed(boolean sessionPresent) {
            System.out.println("Connection resumed: " + (sessionPresent ? "existing session" : "clean session"));
        }
    };
    try {
        MqttClientConnection connection = cmdUtils.buildMQTTConnection(callbacks);
        if (connection == null) {
            onApplicationFailure(new RuntimeException("MQTT connection creation failed!"));
        }
        CompletableFuture<Boolean> connected = connection.connect();
        try {
            boolean sessionPresent = connected.get();
            System.out.println("Connected to " + (!sessionPresent ? "new" : "existing") + " session!");
        } catch (Exception ex) {
            throw new RuntimeException("Exception occurred during connect", ex);
        }
        CountDownLatch countDownLatch = new CountDownLatch(messagesToPublish);
        CompletableFuture<Integer> subscribed = connection.subscribe(topic, QualityOfService.AT_LEAST_ONCE, (message) -> {
            String payload = new String(message.getPayload(), StandardCharsets.UTF_8);
            System.out.println("MESSAGE: " + payload);
            countDownLatch.countDown();
        });
        subscribed.get();
        int count = 0;
        while (count++ < messagesToPublish) {
            CompletableFuture<Integer> published = connection.publish(new MqttMessage(topic, message.getBytes(), QualityOfService.AT_LEAST_ONCE, false));
            published.get();
            Thread.sleep(1000);
        }
        countDownLatch.await();
        CompletableFuture<Void> disconnected = connection.disconnect();
        disconnected.get();
    } catch (CrtRuntimeException | InterruptedException | ExecutionException ex) {
        onApplicationFailure(ex);
    }
    CrtResource.waitForNoResources();
    System.out.println("Complete!");
}
Also used : MqttMessage(software.amazon.awssdk.crt.mqtt.MqttMessage) MqttClientConnection(software.amazon.awssdk.crt.mqtt.MqttClientConnection) MqttClientConnectionEvents(software.amazon.awssdk.crt.mqtt.MqttClientConnectionEvents) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutionException(java.util.concurrent.ExecutionException) CrtRuntimeException(software.amazon.awssdk.crt.CrtRuntimeException) CrtRuntimeException(software.amazon.awssdk.crt.CrtRuntimeException) CommandLineUtils(utils.commandlineutils.CommandLineUtils) CrtRuntimeException(software.amazon.awssdk.crt.CrtRuntimeException) ExecutionException(java.util.concurrent.ExecutionException)

Example 13 with CommandLineUtils

use of utils.commandlineutils.CommandLineUtils in project aws-iot-device-sdk-java-v2 by aws.

the class CustomAuthorizerConnect method main.

public static void main(String[] args) {
    cmdUtils = new CommandLineUtils();
    cmdUtils.registerProgramName("CustomAuthorizerConnect");
    cmdUtils.addCommonMQTTCommands();
    cmdUtils.registerCommand("client_id", "<int>", "Client id to use (optional, default='test-*').");
    cmdUtils.registerCommand("custom_auth_username", "<str>", "Username for connecting to custom authorizer (optional, default=null).");
    cmdUtils.registerCommand("custom_auth_authorizer_name", "<str>", "Name of custom authorizer (optional, default=null).");
    cmdUtils.registerCommand("custom_auth_authorizer_signature", "<str>", "Signature passed when connecting to custom authorizer (optional, default=null).");
    cmdUtils.registerCommand("custom_auth_password", "<str>", "Password for connecting to custom authorizer (optional, default=null).");
    cmdUtils.sendArguments(args);
    MqttClientConnectionEvents callbacks = new MqttClientConnectionEvents() {

        @Override
        public void onConnectionInterrupted(int errorCode) {
            if (errorCode != 0) {
                System.out.println("Connection interrupted: " + errorCode + ": " + CRT.awsErrorString(errorCode));
            }
        }

        @Override
        public void onConnectionResumed(boolean sessionPresent) {
            System.out.println("Connection resumed: " + (sessionPresent ? "existing session" : "clean session"));
        }
    };
    try {
        // Create a connection using a certificate and key, but through a custom authorizer.
        // Note: The data for the connection is gotten from cmdUtils.
        // (see buildDirectMQTTConnectionWithCustomAuthorizer for implementation)
        MqttClientConnection connection = cmdUtils.buildDirectMQTTConnectionWithCustomAuthorizer(callbacks);
        if (connection == null) {
            onApplicationFailure(new RuntimeException("MQTT connection creation (through custom authorizer) failed!"));
        }
        // Connect and disconnect using the connection we created
        // (see sampleConnectAndDisconnect for implementation)
        cmdUtils.sampleConnectAndDisconnect(connection);
    } catch (CrtRuntimeException | InterruptedException | ExecutionException ex) {
        onApplicationFailure(ex);
    }
    CrtResource.waitForNoResources();
    System.out.println("Complete!");
}
Also used : CrtRuntimeException(software.amazon.awssdk.crt.CrtRuntimeException) MqttClientConnection(software.amazon.awssdk.crt.mqtt.MqttClientConnection) CommandLineUtils(utils.commandlineutils.CommandLineUtils) CrtRuntimeException(software.amazon.awssdk.crt.CrtRuntimeException) MqttClientConnectionEvents(software.amazon.awssdk.crt.mqtt.MqttClientConnectionEvents) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

CommandLineUtils (utils.commandlineutils.CommandLineUtils)13 ExecutionException (java.util.concurrent.ExecutionException)12 CrtRuntimeException (software.amazon.awssdk.crt.CrtRuntimeException)10 MqttClientConnection (software.amazon.awssdk.crt.mqtt.MqttClientConnection)9 MqttClientConnectionEvents (software.amazon.awssdk.crt.mqtt.MqttClientConnectionEvents)8 AwsIotMqttConnectionBuilder (software.amazon.awssdk.iot.AwsIotMqttConnectionBuilder)3 CompletableFuture (java.util.concurrent.CompletableFuture)2 MqttMessage (software.amazon.awssdk.crt.mqtt.MqttMessage)2 File (java.io.File)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Scanner (java.util.Scanner)1 UUID (java.util.UUID)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 CRT (software.amazon.awssdk.crt.CRT)1 CrtResource (software.amazon.awssdk.crt.CrtResource)1 Log (software.amazon.awssdk.crt.Log)1 HttpProxyOptions (software.amazon.awssdk.crt.http.HttpProxyOptions)1 TlsContext (software.amazon.awssdk.crt.io.TlsContext)1