Skip to content

Commit

Permalink
Use newer MQTT on-message callback in samples (#238)
Browse files Browse the repository at this point in the history
Also update dependencies
  • Loading branch information
graebm committed Feb 17, 2021
1 parent 8b095fd commit bb70677
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 10 deletions.
2 changes: 1 addition & 1 deletion crt/aws-crt-cpp
9 changes: 6 additions & 3 deletions samples/greengrass/basic_discovery/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,12 @@ int main(int argc, char *argv[])

if (mode == "both" || mode == "subscribe")
{
auto onPublish = [&](Mqtt::MqttConnection & /*connection*/,
auto onMessage = [&](Mqtt::MqttConnection & /*connection*/,
const String &receivedOnTopic,
const ByteBuf &payload) {
const ByteBuf &payload,
bool /*dup*/,
Mqtt::QOS /*qos*/,
bool /*retain*/) {
fprintf(stdout, "Publish received on topic %s\n", receivedOnTopic.c_str());
fprintf(stdout, "Message: \n");
fwrite(payload.buffer, 1, payload.len, stdout);
Expand Down Expand Up @@ -267,7 +270,7 @@ int main(int argc, char *argv[])
}
};

conn.Subscribe(topic.c_str(), AWS_MQTT_QOS_AT_MOST_ONCE, onPublish, onSubAck);
conn.Subscribe(topic.c_str(), AWS_MQTT_QOS_AT_MOST_ONCE, onMessage, onSubAck);
}
else
{
Expand Down
16 changes: 13 additions & 3 deletions samples/mqtt/basic_pub_sub/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,12 @@ int main(int argc, char *argv[])
connection->OnConnectionInterrupted = std::move(onInterrupted);
connection->OnConnectionResumed = std::move(onResumed);

connection->SetOnMessageHandler([](Mqtt::MqttConnection &, const String &topic, const ByteBuf &payload) {
connection->SetOnMessageHandler([](Mqtt::MqttConnection &,
const String &topic,
const ByteBuf &payload,
bool /*dup*/,
Mqtt::QOS /*qos*/,
bool /*retain*/) {
fprintf(stdout, "Generic Publish received on topic %s, payload:\n", topic.c_str());
fwrite(payload.buffer, 1, payload.len, stdout);
fprintf(stdout, "\n");
Expand All @@ -479,7 +484,12 @@ int main(int argc, char *argv[])
/*
* This is invoked upon the receipt of a Publish on a subscribed topic.
*/
auto onPublish = [&](Mqtt::MqttConnection &, const String &topic, const ByteBuf &byteBuf) {
auto onMessage = [&](Mqtt::MqttConnection &,
const String &topic,
const ByteBuf &byteBuf,
bool /*dup*/,
Mqtt::QOS /*qos*/,
bool /*retain*/) {
fprintf(stdout, "Publish received on topic %s\n", topic.c_str());
fprintf(stdout, "\n Message:\n");
fwrite(byteBuf.buffer, 1, byteBuf.len, stdout);
Expand Down Expand Up @@ -512,7 +522,7 @@ int main(int argc, char *argv[])
subscribeFinishedPromise.set_value();
};

connection->Subscribe(topic.c_str(), AWS_MQTT_QOS_AT_LEAST_ONCE, onPublish, onSubAck);
connection->Subscribe(topic.c_str(), AWS_MQTT_QOS_AT_LEAST_ONCE, onMessage, onSubAck);
subscribeFinishedPromise.get_future().wait();

while (true)
Expand Down
16 changes: 13 additions & 3 deletions samples/mqtt/raw_pub_sub/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,12 @@ int main(int argc, char *argv[])
connection->OnConnectionInterrupted = std::move(onInterrupted);
connection->OnConnectionResumed = std::move(onResumed);

connection->SetOnMessageHandler([](Mqtt::MqttConnection &, const String &topic, const ByteBuf &payload) {
connection->SetOnMessageHandler([](Mqtt::MqttConnection &,
const String &topic,
const ByteBuf &payload,
bool /*dup*/,
Mqtt::QOS /*qos*/,
bool /*retain*/) {
fprintf(stdout, "Generic Publish received on topic %s, payload:\n", topic.c_str());
fwrite(payload.buffer, 1, payload.len, stdout);
fprintf(stdout, "\n");
Expand All @@ -352,7 +357,12 @@ int main(int argc, char *argv[])
/*
* This is invoked upon the receipt of a Publish on a subscribed topic.
*/
auto onPublish = [&](Mqtt::MqttConnection &, const String &topic, const ByteBuf &byteBuf) {
auto onMessage = [&](Mqtt::MqttConnection &,
const String &topic,
const ByteBuf &byteBuf,
bool /*dup*/,
Mqtt::QOS /*qos*/,
bool /*retain*/) {
fprintf(stdout, "Publish received on topic %s\n", topic.c_str());
fprintf(stdout, "\n Message:\n");
fwrite(byteBuf.buffer, 1, byteBuf.len, stdout);
Expand Down Expand Up @@ -385,7 +395,7 @@ int main(int argc, char *argv[])
subscribeFinishedPromise.set_value();
};

connection->Subscribe(topic.c_str(), AWS_MQTT_QOS_AT_LEAST_ONCE, onPublish, onSubAck);
connection->Subscribe(topic.c_str(), AWS_MQTT_QOS_AT_LEAST_ONCE, onMessage, onSubAck);
subscribeFinishedPromise.get_future().wait();

while (true)
Expand Down

0 comments on commit bb70677

Please sign in to comment.