|
| 1 | +/** |
| 2 | + * Copyright 2022- IBM Inc. All rights reserved |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +#include "mqtt/async_client.h" |
| 7 | + |
| 8 | + |
| 9 | +// https://github.com/eclipse/paho.mqtt.cpp/issues/141 |
| 10 | +mqtt::async_client_ptr createClient(std::string serverAddress, |
| 11 | + std::string clientID){ |
| 12 | + // Use MQTT v5 to enable no_local: |
| 13 | + // This flag avoids receiving messages from the same host by telling |
| 14 | + // the broker not to send messages received with a client ID to a |
| 15 | + // subscriber with the same client ID |
| 16 | + |
| 17 | + mqtt::create_options createOpts = mqtt::create_options(); |
| 18 | + createOpts.set_mqtt_verison(5); |
| 19 | + std::string persistDir= "None"; |
| 20 | + auto client_ptr = std::make_shared<mqtt::async_client>(serverAddress, |
| 21 | + clientID, |
| 22 | + createOpts, |
| 23 | + persistDir); |
| 24 | + std::cout << "Created MQTT client to " + serverAddress + " and ID " + clientID << std::endl; |
| 25 | + return client_ptr; |
| 26 | +} |
| 27 | + |
| 28 | +mqtt::async_client_ptr connectClient(std::shared_ptr<mqtt::async_client> client_ptr, |
| 29 | + std::string node){ |
| 30 | + auto connOpts = std::make_shared<mqtt::connect_options>(); |
| 31 | + |
| 32 | + if (node == "server"){ |
| 33 | + connOpts->set_keep_alive_interval(20); |
| 34 | + connOpts->set_clean_session(false); |
| 35 | + connOpts->set_automatic_reconnect(true); |
| 36 | + connOpts->set_mqtt_version(5); |
| 37 | + } |
| 38 | + if (node == "client"){ |
| 39 | + connOpts->set_clean_session(false); |
| 40 | + connOpts->set_mqtt_version(5); |
| 41 | + } |
| 42 | + client_ptr->connect(*connOpts)->wait(); |
| 43 | + std::cout << "Connected MQTT client" << std::endl; |
| 44 | + return client_ptr; |
| 45 | +} |
| 46 | + |
| 47 | +void publishData(mqtt::async_client_ptr client_ptr, |
| 48 | + std::string topic, |
| 49 | + int QoS, |
| 50 | + std::string data){ |
| 51 | + mqtt::topic top(*client_ptr, topic, QoS, true); |
| 52 | + mqtt::message_ptr message = mqtt::make_message(topic, data); |
| 53 | + message->set_qos(QoS); |
| 54 | + message->set_payload("A single message"); |
| 55 | + |
| 56 | + client_ptr->publish(message); |
| 57 | + std::cout << "Published data to " + topic << std::endl; |
| 58 | +} |
| 59 | + |
| 60 | +mqtt::async_client_ptr subscribe(mqtt::async_client_ptr client_ptr, |
| 61 | + std::string topic, |
| 62 | + int QoS){ |
| 63 | + mqtt::subscribe_options subOpts; |
| 64 | + subOpts.set_no_local(true); // Only works with MQTT v5 |
| 65 | + |
| 66 | + client_ptr->subscribe(topic, QoS, subOpts)->wait(); |
| 67 | + std::cout << "Subscribed to " + topic << std::endl; |
| 68 | + return client_ptr; |
| 69 | +} |
| 70 | + |
| 71 | +void unsubscribe(mqtt::async_client_ptr client_ptr, |
| 72 | + std::string topic){ |
| 73 | + client_ptr->unsubscribe(topic)->wait(); |
| 74 | + client_ptr->stop_consuming(); |
| 75 | + std::cout << "Unsubscribed from " + topic << std::endl; |
| 76 | +} |
| 77 | + |
| 78 | +std::tuple<std::string, std::string> consumeMessage(mqtt::async_client_ptr client_ptr){ |
| 79 | + client_ptr->start_consuming(); |
| 80 | + auto msg = client_ptr->consume_message(); |
| 81 | + // msg->get_payload() |
| 82 | + return std::make_tuple(msg->get_topic(), msg->to_string()); |
| 83 | +} |
| 84 | + |
| 85 | +void disconnectClient(mqtt::async_client_ptr client_ptr){ |
| 86 | + client_ptr->disconnect()->wait(); |
| 87 | + std::cout << "Disconnected client" << std::endl; |
| 88 | +} |
| 89 | + |
| 90 | + |
| 91 | +// Example publisher |
| 92 | +// --------------------------------------------------------------- |
| 93 | +// #include "PubSub.h" |
| 94 | +// |
| 95 | +// int main(int argc, char **argv) { |
| 96 | +// mqtt::async_client_ptr client_ptr = createClient("tcp://localhost:1883", |
| 97 | +// "mds_server"); |
| 98 | +// std::string node_type = "server"; |
| 99 | +// mqtt::async_client_ptr connected_client_ptr = connectClient(client_ptr, |
| 100 | +// node_type); |
| 101 | +// publishData(connected_client_ptr, "home/file1", 1, "Hello World"); |
| 102 | +// disconnectClient(connected_client_ptr); |
| 103 | +// return 0; |
| 104 | +// } |
| 105 | + |
| 106 | +// Example subscriber |
| 107 | +// --------------------------------------------------------------- |
| 108 | +// #include "PubSub.h" |
| 109 | +// |
| 110 | +// int main(void) |
| 111 | +// { |
| 112 | +// mqtt::async_client_ptr client_ptr = createClient("tcp://localhost:1883", |
| 113 | +// "mds_client"); |
| 114 | +// std::string node_type = "client"; |
| 115 | +// mqtt::async_client_ptr connected_client_ptr = connectClient(client_ptr, |
| 116 | +// node_type); |
| 117 | +// mqtt::async_client_ptr subscribed_client_ptr = subscribe(connected_client_ptr, |
| 118 | +// "home/file1", 1); |
| 119 | +// while (true) { |
| 120 | +// auto msg = consumeMessage(subscribed_client_ptr); |
| 121 | +// std::string topic = std::get<0>(msg); |
| 122 | +// std::string payload = std::get<1>(msg); |
| 123 | +// |
| 124 | +// if (payload.empty()){ |
| 125 | +// break; |
| 126 | +// } |
| 127 | +// std::cout << topic + " " + payload << std::endl; |
| 128 | +// } |
| 129 | +// return 0; |
| 130 | +// } |
0 commit comments