forked from boostorg/mqtt5
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:
@@ -7,89 +7,98 @@
|
||||
|
||||
//[hello_world_in_multithreaded_env
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include <boost/asio/bind_executor.hpp>
|
||||
#include <boost/asio/detached.hpp>
|
||||
#include <boost/asio/io_context.hpp>
|
||||
#include <boost/asio/post.hpp>
|
||||
#include <boost/asio/dispatch.hpp>
|
||||
#include <boost/asio/thread_pool.hpp>
|
||||
#include <boost/asio/strand.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>
|
||||
|
||||
int main() {
|
||||
// Create a multithreaded environment where 4 threads
|
||||
// will be calling ioc.run().
|
||||
struct config {
|
||||
std::string brokers = "broker.hivemq.com";
|
||||
uint16_t port = 1883;
|
||||
std::string client_id = "async_mqtt5_tester";
|
||||
};
|
||||
|
||||
// Number of threads that will call io_context::run().
|
||||
int thread_num = 4;
|
||||
boost::asio::io_context ioc(4);
|
||||
int main(int argc, char** argv) {
|
||||
config cfg;
|
||||
|
||||
// Create the remaining threads (aside of this one).
|
||||
std::vector<std::thread> threads;
|
||||
threads.reserve(thread_num - 1);
|
||||
if (argc == 4) {
|
||||
cfg.brokers = argv[1];
|
||||
cfg.port = uint16_t(std::stoi(argv[2]));
|
||||
cfg.client_id = argv[3];
|
||||
}
|
||||
|
||||
// Create a thread pool with 4 threads.
|
||||
boost::asio::thread_pool tp(4);
|
||||
|
||||
// Create an explicit strand from io_context's executor.
|
||||
// Create an explicit strand from thread_pool's executor.
|
||||
// The strand guarantees a serialised handler execution regardless of the
|
||||
// number of threads running in the io_context.
|
||||
boost::asio::strand strand = boost::asio::make_strand(ioc.get_executor());
|
||||
// number of threads running in the thread_pool.
|
||||
boost::asio::strand strand = boost::asio::make_strand(tp.get_executor());
|
||||
|
||||
// Create the Client with the explicit strand as the default associated executor.
|
||||
async_mqtt5::mqtt_client<boost::asio::ip::tcp::socket> client(strand);
|
||||
async_mqtt5::mqtt_client<
|
||||
boost::asio::ip::tcp::socket, std::monostate /* TlsContext */, async_mqtt5::logger
|
||||
> client(strand, {} /* tls_context */, async_mqtt5::logger(async_mqtt5::log_level::info));
|
||||
|
||||
// Configure the client.
|
||||
client.brokers("<your-mqtt-broker>", 1883);
|
||||
client.brokers(cfg.brokers, cfg.port) // Broker that we want to connect to.
|
||||
.credentials(cfg.client_id); // Set the Client Identifier. (optional)
|
||||
|
||||
// Start the Client.
|
||||
// The async_run function call must be posted/dispatched to the strand.
|
||||
boost::asio::post(
|
||||
strand,
|
||||
[&client, &strand] {
|
||||
// Considering that the default associated executor of all completion handlers is the strand,
|
||||
// it is not necessary to explicitly bind it to async_run or other async_xxx's handlers.
|
||||
client.async_run(boost::asio::detached);
|
||||
}
|
||||
boost::asio::dispatch(
|
||||
boost::asio::bind_executor(
|
||||
strand,
|
||||
[&client, &strand, &cfg] {
|
||||
// Considering that the default associated executor of all completion handlers is the strand,
|
||||
// it is not necessary to explicitly bind it to async_run or other async_xxx's handlers.
|
||||
client.async_run(boost::asio::detached); // Start the Client.
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
// The async_publish function call must be posted/dispatched to the strand.
|
||||
// The associated executor of async_publish's completion handler must be the same strand.
|
||||
boost::asio::post(
|
||||
strand,
|
||||
[&client, &strand] {
|
||||
assert(strand.running_in_this_thread());
|
||||
boost::asio::dispatch(
|
||||
boost::asio::bind_executor(
|
||||
strand,
|
||||
[&client, &strand, &cfg] {
|
||||
assert(strand.running_in_this_thread());
|
||||
|
||||
client.async_publish<async_mqtt5::qos_e::at_least_once>(
|
||||
"<your-mqtt-topic>", "Hello world!", async_mqtt5::retain_e::no,
|
||||
async_mqtt5::publish_props {},
|
||||
// You may bind the strand to this handler, but it is not necessary
|
||||
// as the strand is already the default associated handler.
|
||||
// However, you must not bind it to any other executor!
|
||||
[&client, &strand](
|
||||
async_mqtt5::error_code ec, async_mqtt5::reason_code rc,
|
||||
async_mqtt5::puback_props props
|
||||
) {
|
||||
assert(strand.running_in_this_thread());
|
||||
client.async_publish<async_mqtt5::qos_e::at_least_once>(
|
||||
"async-mqtt5/test", "Hello World!", async_mqtt5::retain_e::no,
|
||||
async_mqtt5::publish_props {},
|
||||
// You may bind the strand to this handler, but it is not necessary
|
||||
// as the strand is already the default associated handler.
|
||||
[&client, &strand](
|
||||
async_mqtt5::error_code ec, async_mqtt5::reason_code rc,
|
||||
async_mqtt5::puback_props props
|
||||
) {
|
||||
assert(strand.running_in_this_thread());
|
||||
|
||||
std::cout << ec.message() << std::endl;
|
||||
std::cout << rc.message() << std::endl;
|
||||
std::cout << ec.message() << std::endl;
|
||||
std::cout << rc.message() << std::endl;
|
||||
|
||||
// Stop the Client. This will cause ioc.run() to return.
|
||||
client.cancel();
|
||||
}
|
||||
);
|
||||
}
|
||||
// Stop the Client.
|
||||
client.cancel();
|
||||
}
|
||||
);
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
// Call ioc.run() on the other threads.
|
||||
for (int i = 0; i < thread_num - 1; ++i)
|
||||
threads.emplace_back([&ioc] { ioc.run(); });
|
||||
|
||||
// Call ioc.run() on this thread.
|
||||
ioc.run();
|
||||
|
||||
for (auto& t : threads)
|
||||
if (t.joinable()) t.join();
|
||||
tp.join();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user