@@ -30,7 +30,6 @@ class ActionServer : public rclcpp_action::Server<ActionT> {
3030public:
3131 ActionServer (rclcpp::Node *node, std::string action_name,
3232 UserExecuteCallback execute_callback,
33- UserAcceptedCallback accepted_callback,
3433 UserCancelCallback cancel_callback)
3534
3635 : rclcpp_action::Server<ActionT>(
@@ -39,66 +38,64 @@ class ActionServer : public rclcpp_action::Server<ActionT> {
3938 rcl_action_server_get_default_options(),
4039 std::bind(&ActionServer::handle_goal, this , _1, _2),
4140 std::bind(&ActionServer::handle_cancel, this , _1),
42- accepted_callback ) {
41+ std::bind(&ActionServer::handle_accepted, this , _1) ) {
4342
4443 this ->execute_callback = execute_callback;
4544 this ->cancel_callback = cancel_callback;
46- this ->server_canceled = false ;
4745 this ->goal_handle = nullptr ;
4846 }
4947
5048 ActionServer (rclcpp::Node *node, std::string action_name,
51- UserExecuteCallback execute_callback,
52- UserAcceptedCallback accepted_callback)
49+ UserExecuteCallback execute_callback)
5350
5451 : rclcpp_action::Server<ActionT>(
5552 node->get_node_base_interface (), node->get_node_clock_interface(),
5653 node->get_node_logging_interface(), action_name,
5754 rcl_action_server_get_default_options(),
5855 std::bind(&ActionServer::handle_goal, this , _1, _2),
5956 std::bind(&ActionServer::handle_cancel, this , _1),
60- accepted_callback ) {
57+ std::bind(&ActionServer::handle_accepted, this , _1) ) {
6158
6259 this ->execute_callback = execute_callback;
6360 this ->cancel_callback = nullptr ;
64- this ->server_canceled = false ;
6561 this ->goal_handle = nullptr ;
6662 }
6763
6864 bool is_working () { return this ->goal_handle != nullptr ; }
6965
70- bool is_canceled () { return this ->server_canceled ; }
71-
72- void wait_for_canceling () {
73- if (this ->server_canceled && this ->goal_handle != nullptr ) {
74- while (!this ->goal_handle ->is_canceling ()) {
75- std::this_thread::sleep_for (std::chrono::seconds (1 ));
76- }
77- }
78- }
79-
8066protected:
8167 std::shared_ptr<GoalHandle> goal_handle;
8268
8369 void execute_goal_handle (const std::shared_ptr<GoalHandle> goal_handle) {
84-
8570 std::thread{std::bind (&ActionServer::handle_execute, this , _1), goal_handle}
8671 .detach ();
8772 }
8873
8974private:
9075 UserExecuteCallback execute_callback;
9176 UserCancelCallback cancel_callback;
92- bool server_canceled ;
77+ std::mutex handle_accepted_mtx ;
9378
9479 rclcpp_action::GoalResponse handle_goal (const rclcpp_action::GoalUUID &uuid,
9580 std::shared_ptr<const Goal> goal) {
96-
9781 (void )uuid;
9882 (void )goal;
9983 return rclcpp_action::GoalResponse::ACCEPT_AND_EXECUTE;
10084 }
10185
86+ void handle_accepted (const std::shared_ptr<GoalHandle> goal_handle) {
87+
88+ const std::lock_guard<std::mutex> lock (this ->handle_accepted_mtx );
89+
90+ if (this ->goal_handle != nullptr && this ->goal_handle ->is_active ()) {
91+ this ->goal_handle ->abort (std::make_shared<typename ActionT::Result>());
92+ }
93+
94+ this ->goal_handle = goal_handle;
95+
96+ this ->execute_goal_handle (goal_handle);
97+ }
98+
10299 rclcpp_action::CancelResponse
103100 handle_cancel (const std::shared_ptr<GoalHandle> goal_handle) {
104101 (void )goal_handle;
@@ -108,26 +105,23 @@ class ActionServer : public rclcpp_action::Server<ActionT> {
108105 void handle_execute (const std::shared_ptr<GoalHandle> goal_handle) {
109106
110107 this ->goal_handle = goal_handle;
111- this ->server_canceled = false ;
112108
113- using namespace std ::placeholders;
114- std::thread{std::bind (&ActionServer::execute, this , _1), goal_handle}
115- .detach ();
109+ std::thread (std::bind (&ActionServer::execute, this )).detach ();
116110
117111 while (this ->goal_handle != nullptr ) {
118112 if (this ->goal_handle ->is_canceling ()) {
119113
120114 if (this ->cancel_callback != nullptr ) {
121115 this ->cancel_callback ();
122116 }
123- this ->server_canceled = true ;
124117 break ;
125118 }
119+ std::this_thread::sleep_for (std::chrono::seconds (1 ));
126120 }
127121 }
128122
129- void execute (const std::shared_ptr<GoalHandle> goal_handle ) {
130- this ->execute_callback (goal_handle);
123+ void execute () {
124+ this ->execute_callback (this -> goal_handle );
131125 this ->goal_handle = nullptr ;
132126 }
133127};
0 commit comments