Skip to content

Commit

Permalink
Use snippet to include two files.
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Agüero <[email protected]>
  • Loading branch information
caguero committed Sep 27, 2023
1 parent 741505a commit e1175c9
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 88 deletions.
2 changes: 2 additions & 0 deletions example/publisher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*
*/

//! [complete]
#include <atomic>
#include <chrono>
#include <csignal>
Expand Down Expand Up @@ -71,3 +72,4 @@ int main(int argc, char **argv)

return 0;
}
//! [complete]
2 changes: 2 additions & 0 deletions example/subscriber.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*
*/

//! [complete]
#include <iostream>
#include <string>
#include <gz/msgs.hh>
Expand Down Expand Up @@ -45,3 +46,4 @@ int main(int argc, char **argv)

return 0;
}
//! [complete]
90 changes: 2 additions & 88 deletions tutorials/04_messages.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <atomic>
#include <chrono>
#include <csignal>
#include <iostream>
#include <string>
#include <thread>
#include <gz/msgs.hh>
#include <gz/transport.hh>
/// brief Flag used to break the publisher loop and terminate the program.
static std::atomic<bool> 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<gz::msgs::StringMsg>(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

Expand Down Expand Up @@ -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 <iostream>
#include <string>
#include <gz/msgs.hh>
#include <gz/transport.hh>
//////////////////////////////////////////////////
/// 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

Expand Down

0 comments on commit e1175c9

Please sign in to comment.