From 9ca5e9d1425d63c00038ca8a723107e93dbc5c69 Mon Sep 17 00:00:00 2001 From: Patrick Roncagliolo Date: Wed, 22 Mar 2023 21:57:49 +0100 Subject: [PATCH 1/4] Allow usage of lambdas as transport subscription callbacks --- gazebo/transport/Node.hh | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/gazebo/transport/Node.hh b/gazebo/transport/Node.hh index e48836792f..cf2042614f 100644 --- a/gazebo/transport/Node.hh +++ b/gazebo/transport/Node.hh @@ -288,6 +288,36 @@ namespace gazebo return result; } + + /// \brief Subscribe to a topic using a bost::function as the callback, + /// allowing to pass lambdas. + /// \param[in] _topic The topic to subscribe to + /// \param[in] _cb Function to be called on receipt of new message + /// \param[in] _latching If true, latch latest incoming message; + /// otherwise don't latch + /// \return Pointer to new Subscriber object + public: template + SubscriberPtr Subscribe(const std::string &_topic, + const boost::function &)> &_cb, + bool _latching = false) + { + SubscribeOptions ops; + std::string decodedTopic = this->DecodeTopicName(_topic); + ops.template Init(decodedTopic, shared_from_this(), _latching); + + { + boost::recursive_mutex::scoped_lock lock(this->incomingMutex); + this->callbacks[decodedTopic].push_back( + CallbackHelperPtr(new CallbackHelperT(_cb, _latching))); + } + + SubscriberPtr result = + transport::TopicManager::Instance()->Subscribe(ops); + + result->SetCallbackId(this->callbacks[decodedTopic].back()->GetId()); + + return result; + } /// \brief Subscribe to a topic using a bare function as the callback /// \param[in] _topic The topic to subscribe to From d40b24723f9e7a86179949f02593944dd3d2d9e9 Mon Sep 17 00:00:00 2001 From: Patrick Roncagliolo Date: Tue, 6 Jun 2023 10:26:09 +0200 Subject: [PATCH 2/4] Add lambda transport cb test --- test/integration/transport.cc | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/test/integration/transport.cc b/test/integration/transport.cc index 494e4c40d9..e38d59223e 100644 --- a/test/integration/transport.cc +++ b/test/integration/transport.cc @@ -153,6 +153,38 @@ TEST_F(TransportTest, PubSub) subs.clear(); } +// Standard pub/sub using lambdas +TEST_F(TransportTest, PubSubLambda) +{ + Load("worlds/empty.world"); + + transport::NodePtr node = transport::NodePtr(new transport::Node()); + node->Init(); + transport::PublisherPtr scenePub = node->Advertise("~/scene"); + transport::SubscriberPtr sceneSub = node->Subscribe("~/scene", + [](ConstScenePtr &/*_msg*/) -> void{g_sceneMsg=true;}); + + msgs::Scene msg; + msgs::Init(msg, "test"); + msg.set_name("default"); + + scenePub->Publish(msg); + + std::vector pubs; + std::vector subs; + + for (unsigned int i = 0; i < 10; ++i) + { + pubs.push_back(node->Advertise("~/scene")); + subs.push_back(node->Subscribe("~/scene", + [](ConstScenePtr &/*_msg*/) -> void{g_sceneMsg=true;})); + scenePub->Publish(msg); + } + + pubs.clear(); + subs.clear(); +} + ///////////////////////////////////////////////// TEST_F(TransportTest, DirectPublish) { From f2613e3f066a74863e2703560e7ca0486c754ab7 Mon Sep 17 00:00:00 2001 From: Patrick Roncagliolo Date: Thu, 15 Jun 2023 19:02:27 +0200 Subject: [PATCH 3/4] Test --- test/integration/transport.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/integration/transport.cc b/test/integration/transport.cc index e38d59223e..bedaca2c13 100644 --- a/test/integration/transport.cc +++ b/test/integration/transport.cc @@ -176,8 +176,8 @@ TEST_F(TransportTest, PubSubLambda) for (unsigned int i = 0; i < 10; ++i) { pubs.push_back(node->Advertise("~/scene")); - subs.push_back(node->Subscribe("~/scene", - [](ConstScenePtr &/*_msg*/) -> void{g_sceneMsg=true;})); + subs.push_back(node->Subscribe("~/scene", + [](ConstScenePtr &/*_msg*/) -> void {g_sceneMsg=true;})); scenePub->Publish(msg); } From 214e0c5fb2d06144bb2c3a4f9a867df62b5837c7 Mon Sep 17 00:00:00 2001 From: roncapat Date: Mon, 19 Jun 2023 23:06:01 +0200 Subject: [PATCH 4/4] Tentative fix, double test --- gazebo/transport/Node.hh | 2 +- test/integration/transport.cc | 39 ++++++++++++++++------------------- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/gazebo/transport/Node.hh b/gazebo/transport/Node.hh index cf2042614f..7883afdb1c 100644 --- a/gazebo/transport/Node.hh +++ b/gazebo/transport/Node.hh @@ -288,7 +288,7 @@ namespace gazebo return result; } - + /// \brief Subscribe to a topic using a bost::function as the callback, /// allowing to pass lambdas. /// \param[in] _topic The topic to subscribe to diff --git a/test/integration/transport.cc b/test/integration/transport.cc index bedaca2c13..ee6230bbc1 100644 --- a/test/integration/transport.cc +++ b/test/integration/transport.cc @@ -154,35 +154,32 @@ TEST_F(TransportTest, PubSub) } // Standard pub/sub using lambdas -TEST_F(TransportTest, PubSubLambda) +TEST_F(TransportTest, PubSubNoncapturingLambda) { Load("worlds/empty.world"); transport::NodePtr node = transport::NodePtr(new transport::Node()); node->Init(); transport::PublisherPtr scenePub = node->Advertise("~/scene"); - transport::SubscriberPtr sceneSub = node->Subscribe("~/scene", - [](ConstScenePtr &/*_msg*/) -> void{g_sceneMsg=true;}); - - msgs::Scene msg; - msgs::Init(msg, "test"); - msg.set_name("default"); - - scenePub->Publish(msg); - - std::vector pubs; - std::vector subs; + transport::SubscriberPtr sceneSub = node->Subscribe("~/scene", + +[](ConstScenePtr & _msg) -> void { + g_sceneMsg=true; + } + ); +} - for (unsigned int i = 0; i < 10; ++i) - { - pubs.push_back(node->Advertise("~/scene")); - subs.push_back(node->Subscribe("~/scene", - [](ConstScenePtr &/*_msg*/) -> void {g_sceneMsg=true;})); - scenePub->Publish(msg); - } +TEST_F(TransportTest, PubSubCapturingLambda) +{ + Load("worlds/empty.world"); - pubs.clear(); - subs.clear(); + transport::NodePtr node = transport::NodePtr(new transport::Node()); + node->Init(); + transport::PublisherPtr scenePub = node->Advertise("~/scene"); + transport::SubscriberPtr sceneSub = node->Subscribe("~/scene", + [this](ConstScenePtr & _msg) -> void { + g_sceneMsg=true; + } + ); } /////////////////////////////////////////////////