Skip to content

Commit f214eca

Browse files
committed
Migrate Mqtt5ClientBuilder from deprecated New* to Create* APIs
1 parent d870ae8 commit f214eca

5 files changed

Lines changed: 37 additions & 33 deletions

File tree

documents/MIGRATION_GUIDE.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,26 +100,25 @@ std::shared_ptr<MqttClient>client = std::shared_ptr<MqttClient>(
100100
mqtt_timeout,
101101
/* disconnect handler */, nullptr,
102102
/* reconnect handler */, nullptr,
103-
/* resubscribe handler */, nullptr);
103+
/* resubscribe handler */, nullptr));
104104
```
105105
106106
#### Example of creating a client in the v2 SDK
107107
108108
The v2 SDK supports different connection types. Given the same input parameters as in the v1 example above,
109-
the most recommended method to create an MQTT5 client will be [NewMqtt5ClientBuilderWithMtlsFromPath](https://aws.github.io/aws-iot-device-sdk-cpp-v2/class_aws_1_1_iot_1_1_mqtt5_client_builder.html#ab595bbc50e08b9d2f78f62e9efeafd65).
109+
the most recommended method to create an MQTT5 client will be [CreateMqtt5ClientBuilderWithMtlsFromPath](https://aws.github.io/aws-iot-device-sdk-cpp-v2/class_aws_1_1_iot_1_1_mqtt5_client_builder.html#a21c365a232be4447b21eb56cc55d4b62).
110110
111111
```cpp
112112
util::String clientEndpoint = "<prefix>-ats.iot.<region>.amazonaws.com";
113113
util::String certificateFile = "<certificate file>"; // X.509 based certificate file
114114
util::String privateKeyFile = "<private key file>"; // PEM encoded private key file
115-
uint32_t clientPort = <port number>
115+
uint32_t clientPort = <port number>;
116116
String client_id = "unique client id";
117117
118-
std::shared_ptr<Aws::Iot::Mqtt5ClientBuilder> builder(
119-
Aws::Iot::Mqtt5ClientBuilder::NewMqtt5ClientBuilderWithMtlsFromPath(
120-
clientEndpoint,
121-
certificateFile,
122-
privateKeyFile));
118+
std::shared_ptr<Aws::Iot::Mqtt5ClientBuilder> builder =
119+
Aws::Iot::Mqtt5ClientBuilder::CreateMqtt5ClientBuilderWithMtlsFromPath(
120+
clientEndpoint, certificateFile.c_str(), privateKeyFile.c_str());
121+
123122
std::shared_ptr<Mqtt5::ConnectPacket> connectOptions =
124123
Aws::Crt::MakeShared<Mqtt5::ConnectPacket>(Aws::Crt::DefaultAllocatorImplementation());
125124
util::String clientId = "client_id";

documents/MQTT5_Userguide.md

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
- [Direct MQTT with Custom Authentication](#direct-mqtt-with-custom-authentication)
1818
- [MQTT over Websockets with Cognito](#mqtt-over-websockets-with-cognito)
1919
- [Direct MQTT with Windows Certificate Store Method](#direct-mqtt-with-windows-certificate-store-method)
20-
- [Direct MQTT with PKCS11 Method](#direct-mqtt-with-pkcs11-method)
21-
- [Direct MQTT with pkcs12 method](#direct-mqtt-with-pkcs12-method)
20+
- [Direct MQTT with PKCS11 Method (Unix Only)](#direct-mqtt-with-pkcs11-method-unix-only)
21+
- [Direct MQTT with pkcs12 method (macOS Only)](#direct-mqtt-with-pkcs12-method-macos-only)
2222
+ [Adding an HTTP Proxy](#adding-an-http-proxy)
2323
+ [Client Operations](#client-operations)
2424
- [Subscribe](#subscribe)
@@ -81,7 +81,7 @@ Example:
8181
std::shared_ptr<Mqtt5Client> client = nullptr;
8282

8383
// Create Mqtt5Client Builder
84-
Aws::Iot::Mqtt5ClientBuilder *builder = Aws::Iot::Mqtt5ClientBuilder::NewMqtt5ClientBuilderWithMtlsFromPath(...);
84+
std::shared_ptr<Aws::Iot::Mqtt5ClientBuilder> builder = Aws::Iot::Mqtt5ClientBuilder::CreateMqtt5ClientBuilderWithMtlsFromPath(...);
8585

8686
// Setup lifecycle callbacks
8787
builder->WithClientConnectionSuccessCallback(
@@ -115,10 +115,10 @@ Once a MQTT5 client builder has been created, it is ready to make a [MQTT5 clien
115115
```cpp
116116

117117
// Create Mqtt5Client Builder
118-
Aws::Iot::Mqtt5ClientBuilder *builder = Aws::Iot::Mqtt5ClientBuilder::NewMqtt5ClientBuilderWithMtlsFromPath(...);
118+
std::shared_ptr<Aws::Iot::Mqtt5ClientBuilder> builder = Aws::Iot::Mqtt5ClientBuilder::CreateMqtt5ClientBuilderWithMtlsFromPath(...);
119119

120120
// Build Mqtt5Client
121-
std::shared_ptr<Aws::Crt::Mqtt5Client> client = builder->Build();
121+
std::shared_ptr<Aws::Crt::Mqtt5::Mqtt5Client> client = builder->Build();
122122

123123
if (mqtt5Client == nullptr)
124124
{
@@ -142,7 +142,7 @@ The MQTT5 client emits a set of events related to state and network status chang
142142
```cpp
143143
144144
// Create Mqtt5Client Builder
145-
Aws::Iot::Mqtt5ClientBuilder *builder = Aws::Iot::Mqtt5ClientBuilder::NewMqtt5ClientBuilderWithMtlsFromPath(...);
145+
std::shared_ptr<Aws::Iot::Mqtt5ClientBuilder> builder = Aws::Iot::Mqtt5ClientBuilder::CreateMqtt5ClientBuilderWithMtlsFromPath(...);
146146
147147
148148
/* setup lifecycle event callbacks */
@@ -181,7 +181,7 @@ The MQTT5 client emits a set of events related to state and network status chang
181181
});
182182
183183
// Build Mqtt5Client
184-
std::shared_ptr<Aws::Crt::Mqtt5Client> client = builder->Build();
184+
std::shared_ptr<Aws::Crt::Mqtt::Mqtt5Client> client = builder->Build();
185185
186186
if (mqtt5Client == nullptr)
187187
{
@@ -253,7 +253,7 @@ Emitted once the client has shutdown any associated network connection and enter
253253

254254
```cpp
255255
// Create Mqtt5Client Builder
256-
Aws::Iot::Mqtt5ClientBuilder *builder = Aws::Iot::Mqtt5ClientBuilder::NewMqtt5ClientBuilderWithMtlsFromPath(...);
256+
std::shared_ptr<Aws::Iot::Mqtt5ClientBuilder> builder = Aws::Iot::Mqtt5ClientBuilder::CreateMqtt5ClientBuilderWithMtlsFromPath(...);
257257

258258
builder->WithPublishReceivedCallback([](const Mqtt5::PublishReceivedEventData &eventData) {
259259
if (eventData.publishPacket == nullptr)
@@ -263,7 +263,7 @@ Emitted once the client has shutdown any associated network connection and enter
263263
fprintf(stdout, "\n");
264264
});
265265

266-
std::shared_ptr<Aws::Crt::Mqtt5Client> client = builder->Build();
266+
std::shared_ptr<Aws::Crt::Mqtt::Mqtt5Client> client = builder->Build();
267267
```
268268
269269
@@ -277,10 +277,10 @@ Invoking `start()` on the client will put it into an active state where it recur
277277
```cpp
278278
279279
// Create Mqtt5Client Builder
280-
Aws::Iot::Mqtt5ClientBuilder *builder = Aws::Iot::Mqtt5ClientBuilder::NewMqtt5ClientBuilderWithMtlsFromPath(...);
280+
std::shared_ptr<Aws::Iot::Mqtt5ClientBuilder> builder = Aws::Iot::Mqtt5ClientBuilder::CreateMqtt5ClientBuilderWithMtlsFromPath(...);
281281
282282
// Build Mqtt5Client
283-
std::shared_ptr<Aws::Crt::Mqtt5Client> client = builder->Build();
283+
std::shared_ptr<Aws::Crt::Mqtt::Mqtt5Client> client = builder->Build();
284284
285285
if (mqtt5Client == nullptr)
286286
{
@@ -322,16 +322,17 @@ For X509 based mutual TLS, you can create a client where the certificate and pri
322322
323323
```cpp
324324
// Create a Client using Mqtt5ClientBuilder
325-
Aws::Iot::Mqtt5ClientBuilder *builder = Aws::Iot::Mqtt5ClientBuilder::NewMqtt5ClientBuilderWithMtlsFromPath(
326-
"<clientEndpoint>", "<certificateFilePath>", "<privateKeyFilePath>");
325+
std::shared_ptr<Aws::Iot::Mqtt5ClientBuilder> builder =
326+
Aws::Iot::Mqtt5ClientBuilder::CreateMqtt5ClientBuilderWithMtlsFromPath(
327+
"<clientEndpoint>", "<certificateFilePath>", "<privateKeyFilePath>");
327328
328329
/* You can setup other client options and lifecycle event callbacks before call builder->Build().
329330
** Once the the client get built, you could no longer update the client options or connection options
330331
** on the created client.
331332
*/
332333
333334
// Build Mqtt5Client
334-
std::shared_ptr<Aws::Crt::Mqtt5Client> client = builder->Build();
335+
std::shared_ptr<Aws::Crt::Mqtt5::Mqtt5Client> client = builder->Build();
335336
336337
if (client == nullptr)
337338
{
@@ -369,16 +370,17 @@ If the default credentials provider chain and AWS region are specified, you do n
369370
Aws::Iot::WebsocketConfig websocketConfig(<signing region>, provider);
370371

371372
// Create a Client using Mqtt5ClientBuilder
372-
Aws::Iot::Mqtt5ClientBuilder *builder = Aws::Iot::Mqtt5ClientBuilder::NewMqtt5ClientBuilderWithWebsocket(
373-
"<clientEndpoint>", websocketConfig);
373+
std::shared_ptr<Aws::Iot::Mqtt5ClientBuilder> builder =
374+
Aws::Iot::Mqtt5ClientBuilder::CreateMqtt5ClientBuilderWithWebsocket(
375+
"<clientEndpoint>", websocketConfig);
374376

375377
/* You can setup other client options and lifecycle event callbacks before call builder->Build().
376378
** Once the the client get built, you could no longer update the client options or connection options
377379
** on the created client.
378380
*/
379381

380382
// Build Mqtt5Client
381-
std::shared_ptr<Aws::Crt::Mqtt5Client> mqtt5Client = builder->Build();
383+
std::shared_ptr<Aws::Crt::Mqtt5::Mqtt5Client> mqtt5Client = builder->Build();
382384

383385
if (mqtt5Client == nullptr)
384386
{
@@ -545,7 +547,7 @@ store, rather than simply being files on disk. To create a MQTT5 builder configu
545547
Note: This is the primary way to use HSM/TPMs on Windows.
546548
Note: Windows Certificate Store connection support is only available on Windows devices.
547549
548-
### Direct MQTT with PKCS11 Method
550+
### Direct MQTT with PKCS11 Method (Unix Only)
549551
550552
A MQTT5 direct connection can be made using a PKCS11 device rather than using a PEM encoded private key,
551553
the private key for mutual TLS is stored on a PKCS#11 compatible smart card or Hardware Security Module (HSM).
@@ -581,7 +583,7 @@ the private key for mutual TLS is stored on a PKCS#11 compatible smart card or H
581583
```
582584
Note: Currently, TLS integration with PKCS#11 is only available on Unix devices.
583585

584-
### Direct MQTT with PKCS12 Method
586+
### Direct MQTT with PKCS12 Method (macOS Only)
585587
A MQTT5 direct connection can be made using a PKCS12 file rather than using a PEM encoded private key.
586588
To create a MQTT5 builder configured for this connection, see the following code:
587589
```cpp

samples/mqtt/mqtt5_aws_websocket/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ Note that in a real application, you may want to avoid the use of wildcards in y
6666

6767
</details>
6868

69+
### Determining your signing region
70+
71+
The `signing_region` parameter specifies the AWS region used to sign WebSocket connection requests via [SigV4 authentication](https://docs.aws.amazon.com/general/latest/gr/signature-version-4.html). It must match the region of your AWS IoT Core endpoint.
72+
For example, if your endpoint is `abcdef12345-ats.iot.us-west-2.amazonaws.com`, the signing region is `us-west-2`.
73+
6974
## How to build
7075

7176
To build the sample, change directory into the sample's folder and run the cmake commands. The sample executable will be built into the `samples/mqtt/mqtt5_aws_websocket/build` folder.

samples/mqtt/mqtt5_aws_websocket/main.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,8 @@ int main(int argc, char *argv[])
134134
Aws::Iot::WebsocketConfig websocketConfig(cmdData.signingRegion, provider);
135135

136136
// Create a Client using Mqtt5ClientBuilder
137-
auto builder = std::unique_ptr<Aws::Iot::Mqtt5ClientBuilder>(
138-
Aws::Iot::Mqtt5ClientBuilder::NewMqtt5ClientBuilderWithWebsocket(
139-
cmdData.endpoint, websocketConfig));
137+
auto builder = Aws::Iot::Mqtt5ClientBuilder::CreateMqtt5ClientBuilderWithWebsocket(
138+
cmdData.endpoint, websocketConfig);
140139

141140
// Check if the builder setup correctly.
142141
if (builder == nullptr)

samples/mqtt/mqtt5_x509/main.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,8 @@ int main(int argc, char *argv[])
126126
* Create MQTT5 client builder using mutual TLS via X509 Certificate and Private Key,
127127
* The builder will be used to create the final client
128128
*/
129-
auto builder = std::unique_ptr<Aws::Iot::Mqtt5ClientBuilder>(
130-
Aws::Iot::Mqtt5ClientBuilder::NewMqtt5ClientBuilderWithMtlsFromPath(
131-
cmdData.endpoint, cmdData.cert.c_str(), cmdData.key.c_str()));
129+
auto builder = Aws::Iot::Mqtt5ClientBuilder::CreateMqtt5ClientBuilderWithMtlsFromPath(
130+
cmdData.endpoint, cmdData.cert.c_str(), cmdData.key.c_str());
132131

133132
// Check if the builder setup correctly.
134133
if (builder == nullptr)

0 commit comments

Comments
 (0)