Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions codebuild/samples/connect-linux.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash

set -e

env

pushd $CODEBUILD_SRC_DIR/samples/BasicConnect

ENDPOINT=$(aws secretsmanager get-secret-value --secret-id "unit-test/endpoint" --query "SecretString" | cut -f2 -d":" | sed -e 's/[\\\"\}]//g')

mvn compile

echo "Basic Mqtt (Direct) Connect test"
mvn exec:java -Dexec.mainClass="basicconnect.BasicConnect" -Daws.crt.ci="True" -Dexec.arguments="--endpoint,$ENDPOINT,--key,/tmp/privatekey.pem,--cert,/tmp/certificate.pem"

popd

pushd $CODEBUILD_SRC_DIR/samples/WebsocketConnect

mvn compile

echo "Websocket Connect test"
mvn exec:java -Dexec.mainClass="websocketconnect.WebsocketConnect" -Daws.crt.ci="True" -Dexec.arguments="--endpoint,$ENDPOINT,--signing_region,us-east-1,--port,443"

popd
1 change: 1 addition & 0 deletions codebuild/samples/linux-smoke-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ phases:
- echo Build started on `date`
- $CODEBUILD_SRC_DIR/codebuild/samples/setup-linux.sh
- $CODEBUILD_SRC_DIR/codebuild/samples/pubsub-linux.sh
- $CODEBUILD_SRC_DIR/codebuild/samples/connect-linux.sh
post_build:
commands:
- echo Build completed on `date`
Expand Down
5 changes: 1 addition & 4 deletions codebuild/samples/pubsub-linux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ ENDPOINT=$(aws secretsmanager get-secret-value --secret-id "unit-test/endpoint"

mvn compile

echo "Mqtt Direct test"
echo "Basic PubSub test"
mvn exec:java -Dexec.mainClass="pubsub.PubSub" -Daws.crt.ci="True" -Dexec.arguments="--endpoint,$ENDPOINT,--key,/tmp/privatekey.pem,--cert,/tmp/certificate.pem"

echo "Websocket test"
mvn exec:java -Dexec.mainClass="pubsub.PubSub" -Daws.crt.ci="True" -Dexec.arguments="--endpoint,$ENDPOINT,--use_websocket,--region,us-east-1,--port,443"

popd
9 changes: 6 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
<modules>
<module>sdk</module>
<module>samples/BasicPubSub</module>
<module>samples/BasicConnect</module>
<module>samples/WebsocketConnect</module>
<module>samples/X509CredentialsProviderConnect</module>
<module>samples/RawConnect</module>
<module>samples/Pkcs11Connect</module>
<module>samples/Greengrass</module>
<module>samples/Jobs</module>
<module>samples/PubSubStress</module>
<module>samples/RawPubSub</module>
<module>samples/Pkcs11PubSub</module>
<module>samples/WindowsCertPubSub</module>
<module>samples/WindowsCertConnect</module>
<module>samples/Shadow</module>
<module>samples/Identity</module>
</modules>
Expand Down
54 changes: 54 additions & 0 deletions samples/BasicConnect/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>software.amazon.awssdk.iotdevicesdk</groupId>
<artifactId>BasicConnect</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>${project.groupId}:${project.artifactId}</name>
<description>Java bindings for the AWS IoT Core Service</description>
<url>https://github.com/awslabs/aws-iot-device-sdk-java-v2</url>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>software.amazon.awssdk.iotdevicesdk</groupId>
<artifactId>aws-iot-device-sdk</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
<configuration>
<mainclass>main</mainclass>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>../Utils/CommandLineUtils</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
95 changes: 95 additions & 0 deletions samples/BasicConnect/src/main/java/basicconnect/BasicConnect.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/

package basicconnect;

import software.amazon.awssdk.crt.CRT;
import software.amazon.awssdk.crt.CrtResource;
import software.amazon.awssdk.crt.CrtRuntimeException;
import software.amazon.awssdk.crt.io.ClientBootstrap;
import software.amazon.awssdk.crt.mqtt.MqttClientConnection;
import software.amazon.awssdk.crt.mqtt.MqttClientConnectionEvents;
import software.amazon.awssdk.iot.iotjobs.model.RejectedError;

import java.util.concurrent.ExecutionException;

import utils.commandlineutils.CommandLineUtils;

public class BasicConnect {

// When run normally, we want to exit nicely even if something goes wrong
// When run from CI, we want to let an exception escape which in turn causes the
// exec:java task to return a non-zero exit code
static String ciPropValue = System.getProperty("aws.crt.ci");
static boolean isCI = ciPropValue != null && Boolean.valueOf(ciPropValue);

static CommandLineUtils cmdUtils;

static void onRejectedError(RejectedError error) {
System.out.println("Request rejected: " + error.code.toString() + ": " + error.message);
}

/*
* When called during a CI run, throw an exception that will escape and fail the exec:java task
* When called otherwise, print what went wrong (if anything) and just continue (return from main)
*/
static void onApplicationFailure(Throwable cause) {
if (isCI) {
throw new RuntimeException("BasicConnect execution failure", cause);
} else if (cause != null) {
System.out.println("Exception encountered: " + cause.toString());
}
}

public static void main(String[] args) {

cmdUtils = new CommandLineUtils();
cmdUtils.registerProgramName("BasicConnect");
cmdUtils.addCommonMQTTCommands();
cmdUtils.addCommonProxyCommands();
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.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 buildDirectMQTTConnection for implementation)
MqttClientConnection connection = cmdUtils.buildDirectMQTTConnection(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!");
}

}
Loading