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

@ -7,27 +7,48 @@
//[hello_world_over_tcp
#include <iostream>
#include <string>
#include <boost/asio/io_context.hpp>
#include <boost/asio/detached.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/types.hpp>
struct config {
std::string brokers = "broker.hivemq.com";
uint16_t port = 1883; // 1883 is the default TCP MQTT port.
std::string client_id = "async_mqtt5_tester";
};
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];
}
int main() {
//[init_tcp_client
// Initialize the execution context required to run I/O operations.
boost::asio::io_context ioc;
// Construct the Client with ``__TCP_SOCKET__`` as the underlying stream.
async_mqtt5::mqtt_client<boost::asio::ip::tcp::socket> client(ioc);
//[init_tcp_client_with_logger
// Construct the Client with ``__TCP_SOCKET__`` as the underlying stream and enabled logging.
// Since we are not establishing a secure connection, set the TlsContext template parameter to std::monostate.
async_mqtt5::mqtt_client<
boost::asio::ip::tcp::socket, std::monostate /* TlsContext */, async_mqtt5::logger
> client(ioc, {} /* tls_context */, async_mqtt5::logger(async_mqtt5::log_level::info));
//]
// If you want to use the Client without logging, initialise it with the following line instead.
//async_mqtt5::mqtt_client<boost::asio::ip::tcp::socket> client(ioc);
//[configure_tcp_client
// 1883 is the default TCP MQTT port.
client.brokers("broker.hivemq.com", 1883)
.credentials("async_mqtt5_tester")
.async_run(boost::asio::detached);
client.brokers(cfg.brokers, cfg.port) // Set the Broker to connect to.
.credentials(cfg.client_id) // Set the Client Identifier. (optional)
.async_run(boost::asio::detached); // Start the Client.
//]
//[publish_hello_world