Examples rework

Summary:
related to T15261, T15262
- all examples are added to CI checks
	- as a result, CMakeLists.txt in example folder is reworked to build all examples instead of one
	- also removed chapter Building with CMake temporarily (it will be back)
- all examples accept runtime arguments (brokers, port, clientid) (default: hivemq public broker)
- implemented example suggestions by R.Perez, most notable:
	- all examples include mqtt headers they use (no more unified headers)
	- multithreaded examples use thread_pool instead of asio::io_context
- all examples use logger with log_level::info by default

Reviewers: ivica

Reviewed By: ivica

Subscribers: iljazovic, miljen

Differential Revision: https://repo.mireo.local/D32602
This commit is contained in:
Korina Šimičević
2024-12-03 15:26:05 +01:00
parent d19f466e3e
commit 9a6788c913
20 changed files with 556 additions and 295 deletions

View File

@ -5,13 +5,18 @@
// (See accompanying file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include <boost/asio/use_awaitable.hpp>
#ifdef BOOST_ASIO_HAS_CO_AWAIT
//[timeout_with_awaitable_operators
#include <chrono>
#include <iostream>
#include <string>
#include <boost/asio/as_tuple.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/co_spawn.hpp>
#include <boost/asio/deferred.hpp>
#include <boost/asio/detached.hpp>
#include <boost/asio/steady_timer.hpp>
#include <boost/asio/use_awaitable.hpp>
@ -19,42 +24,72 @@
#include <boost/asio/experimental/parallel_group.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <async_mqtt5.hpp>
#include <async_mqtt5/logger.hpp>
#include <async_mqtt5/mqtt_client.hpp>
#include <async_mqtt5/reason_codes.hpp>
#include <async_mqtt5/types.hpp>
#ifdef BOOST_ASIO_HAS_CO_AWAIT
struct config {
std::string brokers = "broker.hivemq.com";
uint16_t port = 1883;
std::string client_id = "async_mqtt5_tester";
};
// Modified completion token that will prevent co_await from throwing exceptions.
constexpr auto use_nothrow_awaitable = boost::asio::as_tuple(boost::asio::use_awaitable);
constexpr auto use_nothrow_awaitable = boost::asio::as_tuple(boost::asio::deferred);
using client_type = async_mqtt5::mqtt_client<boost::asio::ip::tcp::socket>;
// client_type with logging enabled
using client_type = async_mqtt5::mqtt_client<
boost::asio::ip::tcp::socket, std::monostate /* TlsContext */, async_mqtt5::logger
>;
boost::asio::awaitable<void> send_over_mqtt(client_type& client, const std::string& message) {
client.brokers("<your-mqtt-broker>", 1883)
.async_run(boost::asio::detached);
// client_type without logging
//using client_type = async_mqtt5::mqtt_client<boost::asio::ip::tcp::socket>;
boost::asio::awaitable<void> send_over_mqtt(
const config& cfg, client_type& client
) {
client.brokers(cfg.brokers, cfg.port) // Broker that we want to connect to.
.credentials(cfg.client_id) // Set the Client Identifier. (optional)
.async_run(boost::asio::detached); // Start the Client.
auto&& [pec, prc, puback_props] = co_await client.async_publish<async_mqtt5::qos_e::at_least_once>(
"<your-mqtt-topic>", message,
"async-mqtt5/test", "Hello World!",
async_mqtt5::retain_e::no, async_mqtt5::publish_props {},
use_nothrow_awaitable
);
co_await client.async_disconnect(use_nothrow_awaitable);
co_return;
}
int main() {
int main(int argc, char** argv) {
config cfg;
if (argc == 4) {
cfg.brokers = argv[1];
cfg.port = uint16_t(std::stoi(argv[2]));
cfg.client_id = argv[3];
}
boost::asio::io_context ioc;
co_spawn(ioc, [&ioc]() -> boost::asio::awaitable<void> {
// Construct the Client.
async_mqtt5::mqtt_client<boost::asio::ip::tcp::socket> client(ioc);
co_spawn(
ioc,
[&ioc, &cfg]() -> boost::asio::awaitable<void> {
// Initialise the Client to connect to the Broker over TCP.
client_type client(ioc);
// You can also initialise the Client and its logger with a specific log_level (default log_level::info).
//client_type client(ioc, {} /* tls_context */, async_mqtt5::logger(async_mqtt5::log_level::debug));
// Construct the timer.
boost::asio::steady_timer timer(ioc, std::chrono::seconds(5));
using namespace boost::asio::experimental::awaitable_operators;
auto res = co_await (
send_over_mqtt(client, "Hello world!") ||
timer.async_wait(use_nothrow_awaitable)
send_over_mqtt(cfg, client) ||
timer.async_wait(boost::asio::as_tuple(boost::asio::use_awaitable))
);
// The timer expired first. The client is cancelled.
@ -64,11 +99,24 @@ int main() {
else
std::cout << "Send over MQTT completed!" << std::endl;
}, boost::asio::detached);
},
[](std::exception_ptr e) {
if (e)
std::rethrow_exception(e);
}
);
ioc.run();
}
#endif
//]
#else
#include <iostream>
int main() {
std::cout << "This example requires C++20 standard to compile and run" << std::endl;
}
#endif