From 860f15ffbec7972077d8b5969595abab8ff58249 Mon Sep 17 00:00:00 2001 From: Nathan Hughes Date: Wed, 14 Oct 2026 14:20:43 +0000 Subject: [PATCH] make service anonymous --- ianvs/include/ianvs/spin_functions.h | 12 ++++++---- ianvs/src/spin_functions.cpp | 33 +++++++++++++++++----------- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/ianvs/include/ianvs/spin_functions.h b/ianvs/include/ianvs/spin_functions.h index 49e392f..6612c7a 100644 --- a/ianvs/include/ianvs/spin_functions.h +++ b/ianvs/include/ianvs/spin_functions.h @@ -33,18 +33,22 @@ * purposes notwithstanding any copyright notation herein. * -------------------------------------------------------------------------- */ #pragma once -#include #include "ianvs/node_handle.h" namespace ianvs { -void spinWhileClockPresent(NodeHandle nh, rclcpp::Executor* executor = nullptr); +void spinWhileClockPresent(NodeHandle nh, + rclcpp::Executor* executor = nullptr, + const std::string& service_name = "~/shutdown"); -void spinUntilExitRequested(NodeHandle nh, rclcpp::Executor* executor = nullptr); +void spinUntilExitRequested(NodeHandle nh, + rclcpp::Executor* executor = nullptr, + const std::string& service_name = "~/shutdown"); void spinAndWait(NodeHandle nh, bool exit_after_clock = false, - rclcpp::Executor* executor = nullptr); + rclcpp::Executor* executor = nullptr, + const std::string& service_name = "~/shutdown"); } // namespace ianvs diff --git a/ianvs/src/spin_functions.cpp b/ianvs/src/spin_functions.cpp index 74daa6d..cdbfbb3 100644 --- a/ianvs/src/spin_functions.cpp +++ b/ianvs/src/spin_functions.cpp @@ -36,10 +36,12 @@ #include #include +#include namespace ianvs { using BaseInterface = std::shared_ptr; +using std_srvs::srv::Empty; namespace { @@ -54,18 +56,16 @@ inline void spin_some(const BaseInterface& node, rclcpp::Executor* exec) { } // namespace struct ShutdownMonitor { - explicit ShutdownMonitor(NodeHandle nh) + ShutdownMonitor(NodeHandle nh, const std::string& name) : should_exit(false), - service(nh.create_service( - "shutdown", &ShutdownMonitor::callback, this)) {} + service(nh.create_service(name, &ShutdownMonitor::callback, this)) {} - void callback(const std_srvs::srv::Empty::Request::SharedPtr&, - std_srvs::srv::Empty::Response::SharedPtr) { + void callback(const Empty::Request::SharedPtr&, Empty::Response::SharedPtr) { should_exit = true; } bool should_exit; - rclcpp::Service::SharedPtr service; + rclcpp::Service::SharedPtr service; }; bool haveClock(NodeHandle nh) { @@ -73,8 +73,10 @@ bool haveClock(NodeHandle nh) { return graph->count_publishers("/clock") > 0; } -void spinWhileClockPresent(NodeHandle nh, rclcpp::Executor* executor) { - ShutdownMonitor functor(nh); +void spinWhileClockPresent(NodeHandle nh, + rclcpp::Executor* executor, + const std::string& service_name) { + ShutdownMonitor functor(nh, service_name); rclcpp::WallRate r(50); auto base = nh.node().get(); @@ -98,8 +100,10 @@ void spinWhileClockPresent(NodeHandle nh, rclcpp::Executor* executor) { RCLCPP_INFO(nh.logger(), "Exiting!"); } -void spinUntilExitRequested(NodeHandle nh, rclcpp::Executor* executor) { - ShutdownMonitor functor(nh); +void spinUntilExitRequested(NodeHandle nh, + rclcpp::Executor* executor, + const std::string& service_name) { + ShutdownMonitor functor(nh, service_name); rclcpp::WallRate r(50); auto base = nh.node().get(); @@ -117,11 +121,14 @@ void spinUntilExitRequested(NodeHandle nh, rclcpp::Executor* executor) { RCLCPP_INFO(nh.logger(), "Exiting!"); } -void spinAndWait(NodeHandle nh, bool exit_after_clock, rclcpp::Executor* executor) { +void spinAndWait(NodeHandle nh, + bool exit_after_clock, + rclcpp::Executor* executor, + const std::string& service_name) { if (exit_after_clock) { - spinWhileClockPresent(nh, executor); + spinWhileClockPresent(nh, executor, service_name); } else { - spinUntilExitRequested(nh, executor); + spinUntilExitRequested(nh, executor, service_name); } }