Skip to content

Instantly share code, notes, and snippets.

@j-rivero
Created April 29, 2021 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save j-rivero/5095469d94434a7cc4e633b96b21c231 to your computer and use it in GitHub Desktop.
Save j-rivero/5095469d94434a7cc4e633b96b21c231 to your computer and use it in GitHub Desktop.
Gazebo test case issue 2875 - Modified version of https://github.com/osrf/gazebo/issues/2875#issuecomment-822834452
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <gazebo/gazebo.hh>
#include <gazebo/common/common.hh>
#include <gazebo/physics/physics.hh>
#include <gazebo/msgs/msgs.hh>
#include <gazebo/gazebo_client.hh>
volatile sig_atomic_t stop;
const std::string TOPIC = "/chatter";
void inthand(int signum) {
stop = 1;
}
void cb(ConstGzStringPtr &_msg)
{
// Dump the message contents to stdout.
std::cout << _msg->data() << std::endl;
}
// Main function for the child process, runs client
void client_main(int argc, char** argv)
{
gazebo::client::setup(argc, argv);
gazebo::transport::NodePtr node(new gazebo::transport::Node());
node->Init();
gazebo::transport::SubscriberPtr sub = node->Subscribe(TOPIC, cb);
while (!stop) {
gazebo::common::Time::MSleep(10);
}
gazebo::client::shutdown();
}
// Main function for the parent process, runs server
void server_main(int argc, char **argv)
{
gazebo::setupServer(argc, argv);
gazebo::physics::WorldPtr world = gazebo::loadWorld("worlds/empty.world");
gazebo::transport::NodePtr node(new gazebo::transport::Node());
node->Init();
gazebo::transport::PublisherPtr pub =
node->Advertise<gazebo::msgs::GzString>(TOPIC);
pub.reset();
while (!stop) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
gazebo::msgs::GzString msg;
msg.set_data("arst");
// j-rivero: uncommented to make the test to crash
pub->Publish(msg);
}
gazebo::shutdown();
}
/////////////////////////////////////////////////
int main(int argc, char **argv)
{
signal(SIGINT, inthand);
if (fork() == 0) {
client_main(argc, argv);
} else {
server_main(argc, argv);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment