diff --git a/example/publisher.cc b/example/publisher.cc index c4e6717ab..0073c45a1 100644 --- a/example/publisher.cc +++ b/example/publisher.cc @@ -15,6 +15,7 @@ * */ +//! [complete] #include #include #include @@ -71,3 +72,4 @@ int main(int argc, char **argv) return 0; } +//! [complete] diff --git a/example/subscriber.cc b/example/subscriber.cc index f4b02d9f9..41ac6baed 100644 --- a/example/subscriber.cc +++ b/example/subscriber.cc @@ -15,6 +15,7 @@ * */ +//! [complete] #include #include #include @@ -45,3 +46,4 @@ int main(int argc, char **argv) return 0; } +//! [complete] diff --git a/tutorials/04_messages.md b/tutorials/04_messages.md index 0f07e4c3c..98528d5fe 100644 --- a/tutorials/04_messages.md +++ b/tutorials/04_messages.md @@ -20,63 +20,7 @@ cd ~/gz_transport_tutorial Download the [publisher.cc](https://github.com/gazebosim/gz-transport/raw/gz-transport13/example/publisher.cc) file within the `gz_transport_tutorial` folder and open it with your favorite editor: -```{.cpp} -#include -#include -#include -#include -#include -#include -#include -#include - -/// brief Flag used to break the publisher loop and terminate the program. -static std::atomic g_terminatePub(false); - -////////////////////////////////////////////////// -/// brief Function callback executed when a SIGINT or SIGTERM signals are -/// captured. This is used to break the infinite loop that publishes messages -/// and exit the program smoothly. -void signal_handler(int _signal) -{ - if (_signal == SIGINT || _signal == SIGTERM) - g_terminatePub = true; -} - -////////////////////////////////////////////////// -int main(int argc, char **argv) -{ - // Install a signal handler for SIGINT and SIGTERM. - std::signal(SIGINT, signal_handler); - std::signal(SIGTERM, signal_handler); - - // Create a transport node and advertise a topic. - gz::transport::Node node; - std::string topic = "/foo"; - - auto pub = node.Advertise(topic); - if (!pub) - { - std::cerr << "Error advertising topic [" << topic << "]" << std::endl; - return -1; - } - - // Prepare the message. - gz::msgs::StringMsg msg; - msg.set_data("HELLO"); - - // Publish messages at 1Hz. - while (!g_terminatePub) - { - if (!pub.Publish(msg)) - break; - std::cout << "Publishing hello on topic [" << topic << "]" << std::endl; - std::this_thread::sleep_for(std::chrono::milliseconds(1000)); - } - - return 0; -} -``` +\snippet example/publisher.cc complete ### Walkthrough @@ -135,37 +79,7 @@ The method *Publish()* sends a message to all the subscribers. Download the [subscriber.cc](https://github.com/gazebosim/gz-transport/raw/gz-transport13/example/subscriber.cc) file into the `gz_transport_tutorial` folder and open it with your favorite editor: -```{.cpp} -#include -#include -#include -#include - -////////////////////////////////////////////////// -/// brief Function called each time a topic update is received. -void cb(const gz::msgs::StringMsg &_msg) -{ - std::cout << "Msg: " << _msg.data() << std::endl << std::endl; -} - -////////////////////////////////////////////////// -int main(int argc, char **argv) -{ - gz::transport::Node node; - std::string topic = "/foo"; - // Subscribe to a topic by registering a callback. - if (!node.Subscribe(topic, cb)) - { - std::cerr << "Error subscribing to topic [" << topic << "]" << std::endl; - return -1; - } - - // Zzzzzz. - gz::transport::waitForShutdown(); - - return 0; -} -``` +\snippet example/subscriber.cc complete ### Walkthrough