rclrs 0.7.0

A ROS 2 client library for developing robotics applications in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
use super::empty_goal_status_array;
use crate::{
    log_warn,
    rcl_bindings::*,
    vendor::{action_msgs::srv::CancelGoal_Response, builtin_interfaces::msg::Time},
    CancelResponse, CancelResponseCode, DropGuard, GoalStatus, GoalStatusCode, GoalUuid,
    MultiCancelResponse, Node, NodeHandle, QoSProfile, RclPrimitive, RclPrimitiveHandle,
    RclPrimitiveKind, RclrsError, ReadyKind, TakeFailedAsNone, ToResult, Waitable,
    WaitableLifecycle, ENTITY_LIFECYCLE_MUTEX,
};
use rosidl_runtime_rs::{Action, Message, RmwFeedbackMessage, RmwGoalResponse, RmwResultResponse};
use std::{
    any::Any,
    borrow::{Borrow, Cow},
    collections::HashMap,
    ffi::CString,
    sync::{Arc, Mutex, MutexGuard, Weak},
};
use tokio::sync::{
    mpsc::{unbounded_channel, UnboundedSender},
    oneshot::{channel as oneshot_channel, Sender},
    watch::Sender as WatchSender,
};

mod cancellation_client;
pub use cancellation_client::*;

mod feedback_client;
pub use feedback_client::*;

mod goal_client;
pub use goal_client::*;

mod status_client;
pub use status_client::*;

mod result_client;
pub use result_client::*;

mod requested_goal_client;
pub use requested_goal_client::*;

/// `ActionClientOptions` are used by [`Node::create_action_client`][1] to initialize an
/// [`ActionClient`].
///
/// [1]: crate::Node::create_action_client
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct ActionClientOptions<'a> {
    /// The name of the action that this client will send requests to
    pub action_name: &'a str,
    /// The quality of service profile for the goal service
    pub goal_service_qos: QoSProfile,
    /// The quality of service profile for the result service
    pub result_service_qos: QoSProfile,
    /// The quality of service profile for the cancel service
    pub cancel_service_qos: QoSProfile,
    /// The quality of service profile for the feedback topic
    pub feedback_topic_qos: QoSProfile,
    /// The quality of service profile for the status topic
    pub status_topic_qos: QoSProfile,
}

impl<'a> ActionClientOptions<'a> {
    /// Initialize a new [`ActionClientOptions`] with default settings.
    pub fn new(action_name: &'a str) -> Self {
        Self {
            action_name,
            goal_service_qos: QoSProfile::services_default(),
            result_service_qos: QoSProfile::services_default(),
            cancel_service_qos: QoSProfile::services_default(),
            feedback_topic_qos: QoSProfile::topics_default(),
            status_topic_qos: QoSProfile::action_status_default(),
        }
    }
}

/// Trait to implicitly convert a compatible object into [`ActionClientOptions`].
pub trait IntoActionClientOptions<'a>: Sized {
    /// Change this into an [`ActionClientOptions`].
    fn into_action_client_options(self) -> ActionClientOptions<'a>;

    /// Set the quality of service profile for the goal service
    fn goal_service_qos(self, profile: QoSProfile) -> ActionClientOptions<'a> {
        let mut options = self.into_action_client_options();
        options.goal_service_qos = profile;
        options
    }

    /// Set the quality of service profile for the result service
    fn result_service_qos(self, profile: QoSProfile) -> ActionClientOptions<'a> {
        let mut options = self.into_action_client_options();
        options.result_service_qos = profile;
        options
    }

    /// Set the quality of service profile for the cancel service
    fn cancel_service_qos(self, profile: QoSProfile) -> ActionClientOptions<'a> {
        let mut options = self.into_action_client_options();
        options.cancel_service_qos = profile;
        options
    }

    /// Set the quality of service profile for the feedback topic
    fn feedback_topic_qos(self, profile: QoSProfile) -> ActionClientOptions<'a> {
        let mut options = self.into_action_client_options();
        options.feedback_topic_qos = profile;
        options
    }

    /// Set the quality of service profile for the status topic
    fn status_topic_qos(self, profile: QoSProfile) -> ActionClientOptions<'a> {
        let mut options = self.into_action_client_options();
        options.status_topic_qos = profile;
        options
    }
}

impl<'a, T: Borrow<str> + ?Sized + 'a> IntoActionClientOptions<'a> for &'a T {
    fn into_action_client_options(self) -> ActionClientOptions<'a> {
        ActionClientOptions::new(self.borrow())
    }
}

impl<'a> IntoActionClientOptions<'a> for ActionClientOptions<'a> {
    fn into_action_client_options(self) -> ActionClientOptions<'a> {
        self
    }
}

impl<'a> From<&'_ ActionClientOptions<'a>> for rcl_action_client_options_t {
    fn from(value: &'_ ActionClientOptions<'a>) -> Self {
        rcl_action_client_options_s {
            goal_service_qos: value.goal_service_qos.into(),
            result_service_qos: value.result_service_qos.into(),
            cancel_service_qos: value.cancel_service_qos.into(),
            feedback_topic_qos: value.feedback_topic_qos.into(),
            status_topic_qos: value.status_topic_qos.into(),
            // SAFETY: No preconditions for this function
            allocator: unsafe { rcutils_get_default_allocator() },
        }
    }
}

/// Main class responsible for sending goals to a ROS action server.
///
/// Create a client using [`Node::create_action_client`][1].
///
/// Receiving feedback and results requires the node's executor to [spin][2].
///
/// [1]: crate::NodeState::create_action_client
/// [2]: crate::spin
pub type ActionClient<A> = Arc<ActionClientState<A>>;

/// The inner state of an [`ActionClient`].
///
/// This is public so that you can choose to create a [`Weak`][1] reference to it
/// if you want to be able to refer to an [`ActionClient`] in a non-owning way. It is
/// generally recommended to manage the `ActionClientState` inside of an [`Arc`],
/// and [`ActionClient`] is provided as a convenience alias for that.
///
/// The public API of the [`ActionClient`] type is implemented via `ActionClientState`.
///
/// [1]: std::sync::Weak
pub struct ActionClientState<A: Action> {
    board: Arc<ActionClientGoalBoard<A>>,
    #[allow(unused)]
    lifecycle: WaitableLifecycle,
}

impl<A: Action> ActionClientState<A> {
    /// Request the action server to execute a goal. You will receive a
    /// [`RequestedGoalClient`] which you can use to wait for the reply from the
    /// action server that indicates whether the goal was accepted.
    ///
    /// If the goal is accepted, the [`RequestedGoalClient`] will yield a
    /// [`GoalClient`]. If the goal was rejected by the action server then it will
    /// yield a [`None`]. The easiest way to get the response is to use `.await`
    /// in an async function.
    ///
    /// In the unlikely event of an error at the rcl layer, this will panic.
    /// Use [`Self::try_request_goal`] to handle errors without panicking.
    pub fn request_goal(self: &Arc<Self>, goal: A::Goal) -> RequestedGoalClient<A> {
        self.try_request_goal(goal).unwrap()
    }

    /// A version of [`Self::request_goal`] which allows you to handle any errors
    /// that may happen at the rcl layer.
    pub fn try_request_goal(
        self: &Arc<Self>,
        goal: A::Goal,
    ) -> Result<RequestedGoalClient<A>, RclrsError> {
        self.board.request_goal(self, goal)
    }

    /// Get a client to receive feedback for a specific goal.
    pub fn receive_feedback(self: &Arc<Self>, goal_id: GoalUuid) -> FeedbackClient<A> {
        self.board.create_feedback_client(self, goal_id)
    }

    /// Get a client to receive all status updates for a specific goal. If you
    /// only care about the latest status update, consider using [`Self::watch_status`]
    /// instead.
    pub fn receive_status(self: &Arc<Self>, goal_id: GoalUuid) -> StatusClient<A> {
        self.board.create_status_client(self, goal_id)
    }

    /// Get a client to watch the status of a specific goal.
    pub fn watch_status(self: &Arc<Self>, goal_id: GoalUuid) -> StatusWatcher<A> {
        self.board.create_status_watcher(self, goal_id)
    }

    /// Ask to receive the result of a goal. You can `.await` the [`ResultClient`]
    /// to get the result asynchronously.
    ///
    /// In the unlikely event of an error at the rcl layer, this will panic.
    /// Use [`Self::try_receive_result`] to catch errors without panicking.
    pub fn receive_result(self: &Arc<Self>, goal_id: GoalUuid) -> ResultClient<A> {
        self.try_receive_result(goal_id).unwrap()
    }

    /// Try to receive the result of a goal. If an rcl error happens, you can
    /// handle it.
    pub fn try_receive_result(
        self: &Arc<Self>,
        goal_id: GoalUuid,
    ) -> Result<ResultClient<A>, RclrsError> {
        self.board.request_result(Arc::clone(self), goal_id)
    }

    /// Ask the action server to cancel a single goal.
    ///
    /// In the unlikely event of an error at the rcl layer, this will panic.
    /// Use [`Self::try_cancel_goal`] to handle errors without panicking.
    pub fn cancel_goal(self: &Arc<Self>, goal_id: GoalUuid) -> CancelResponseClient<A> {
        self.try_cancel_goal(goal_id).unwrap()
    }

    /// Try to ask the action server to cancel a single goal. If an rcl error
    /// happens, you can handle it.
    pub fn try_cancel_goal(
        self: &Arc<Self>,
        goal_id: GoalUuid,
    ) -> Result<CancelResponseClient<A>, RclrsError> {
        self.board.request_single_cancel(self, goal_id)
    }

    /// Ask the action server to cancel all of its goals.
    ///
    /// In the unlikely event of an error at the rcl layer, this will panic.
    /// Use [`Self::try_cancel_all_goals`] to catch errors without panicking.
    pub fn cancel_all_goals(self: &Arc<Self>) -> MultiCancelResponseClient<A> {
        self.try_cancel_all_goals().unwrap()
    }

    /// Try to ask the action server to cancel all of its goals. If an rcl error
    /// happens, you can handle it.
    pub fn try_cancel_all_goals(
        self: &Arc<Self>,
    ) -> Result<MultiCancelResponseClient<A>, RclrsError> {
        self.board.request_multi_cancel(self, None)
    }

    /// Ask the action server to cancel all of its goals that started before a
    /// specified time stamp.
    ///
    /// <div class="warning">Make sure the time stamp is based on the time according to the action server.</div>
    ///
    /// In the unlikely event of an error at the rcl layer, this will panic.
    /// Use [`Self::try_cancel_goals_prior_to`] to catch errors without panicking.
    pub fn cancel_goals_prior_to(self: &Arc<Self>, time: Time) -> MultiCancelResponseClient<A> {
        self.try_cancel_goals_prior_to(time).unwrap()
    }

    /// Try to ask the action server to cancel all of its goals that started before
    /// a specified time stamp. If an rcl error happens, you can handle it.
    ///
    /// <div class="warning">Make sure the time stamp is based on the time according to the action server.</div>
    pub fn try_cancel_goals_prior_to(
        self: &Arc<Self>,
        time: Time,
    ) -> Result<MultiCancelResponseClient<A>, RclrsError> {
        self.board.request_multi_cancel(self, Some(time))
    }

    /// Creates a new action client.
    pub(crate) fn create<'a>(
        node: &Node,
        options: impl IntoActionClientOptions<'a>,
    ) -> Result<Arc<Self>, RclrsError> {
        let options = options.into_action_client_options();
        // SAFETY: Getting a zero-initialized value is always safe.
        let mut rcl_action_client = unsafe { rcl_action_get_zero_initialized_client() };
        let type_support = A::get_type_support() as *const rosidl_action_type_support_t;
        let action_name_c_string =
            CString::new(options.action_name).map_err(|err| RclrsError::StringContainsNul {
                err,
                s: options.action_name.into(),
            })?;

        // SAFETY: No preconditions for this function.
        let action_client_options = (&options).into();

        {
            let mut rcl_node = node.handle().rcl_node.lock().unwrap();
            let _lifecycle_lock = ENTITY_LIFECYCLE_MUTEX.lock().unwrap();

            // SAFETY:
            // * The rcl_action_client was zero-initialized as expected by this function.
            // * The rcl_node is kept alive by the NodeHandle because it is a dependency of the action client.
            // * The action name and the options are copied by this function, so they can be dropped
            //   afterwards.
            // * The entity lifecycle mutex is locked to protect against the risk of global
            //   variables in the rmw implementation being unsafely modified during initialization.
            unsafe {
                rcl_action_client_init(
                    &mut rcl_action_client,
                    &mut *rcl_node,
                    type_support,
                    action_name_c_string.as_ptr(),
                    &action_client_options,
                )
                .ok()?;
            }
        }

        let handle = Arc::new(ActionClientHandle {
            rcl_action_client: Mutex::new(rcl_action_client),
            node_handle: Arc::clone(&node.handle()),
        });

        let board = Arc::new(ActionClientGoalBoard {
            handle,
            node: Arc::clone(node),
            pending_goal_clients: Default::default(),
            feedback_senders: Default::default(),
            status_senders: Default::default(),
            status_posters: Default::default(),
            cancel_response_senders: Default::default(),
            result_senders: Default::default(),
            client: Default::default(),
        });

        let async_commands = node.commands().async_worker_commands();
        let (waitable, lifecycle) = Waitable::new(
            Box::new(ActionClientExecutable {
                board: Arc::clone(&board),
            }),
            Some(Arc::clone(async_commands.get_guard_condition())),
        );
        async_commands.add_to_wait_set(waitable);

        let client = Arc::new(Self { board, lifecycle });
        *client.board.client.lock().unwrap() = Arc::downgrade(&client);
        Ok(client)
    }
}

struct ActionClientGoalBoard<A: Action> {
    pending_goal_clients: Mutex<HashMap<i64, PendingGoalClient<A>>>,
    feedback_senders: Mutex<HashMap<GoalUuid, Vec<UnboundedSender<A::Feedback>>>>,
    status_senders: Mutex<HashMap<GoalUuid, Vec<UnboundedSender<GoalStatus>>>>,
    status_posters: Mutex<HashMap<GoalUuid, WatchSender<GoalStatus>>>,
    cancel_response_senders: Mutex<HashMap<i64, CancelResponseSender>>,
    result_senders: Mutex<HashMap<i64, Sender<(GoalStatusCode, A::Result)>>>,
    handle: Arc<ActionClientHandle>,
    client: Mutex<Weak<ActionClientState<A>>>,
    /// Ensure the parent node remains alive as long as the subscription is held.
    /// This implementation will change in the future.
    #[allow(unused)]
    node: Node,
}

enum CancelResponseSender {
    /// Used when only a single goal is being cancelled
    SingleGoalCancel(Sender<CancelResponse>),
    /// Used when multiple goals are being cancelled
    MultiGoalCancel(Sender<MultiCancelResponse>),
}

impl<A: Action> ActionClientGoalBoard<A> {
    fn execute_feedback(&self) -> Result<(), RclrsError> {
        let Some(feedback_rmw) = self.handle.take_feedback::<A>().take_failed_as_none()? else {
            return Ok(());
        };

        let (goal_uuid, feedback) = A::split_feedback_message(feedback_rmw);
        let feedback: A::Feedback = Message::from_rmw_message(feedback);
        if let Some(senders) = self
            .feedback_senders
            .lock()
            .unwrap()
            .get(&GoalUuid(goal_uuid))
        {
            // Avoid making any unnecessary clones
            for sender in senders.iter().take(senders.len() - 1) {
                let _ = sender.send(feedback.clone());
            }

            if let Some(last_sender) = senders.last() {
                let _ = last_sender.send(feedback);
            }
        }

        Ok(())
    }

    fn execute_status(&self) -> Result<(), RclrsError> {
        let Some(goal_statuses) = self.handle.take_status().take_failed_as_none()? else {
            return Ok(());
        };

        let all_status_senders = self.status_senders.lock().unwrap();
        let all_status_watchers = self.status_posters.lock().unwrap();
        for index in 0..goal_statuses.msg.status_list.size {
            let rcl_status = unsafe { &*goal_statuses.msg.status_list.data.add(index) };

            let stamp = &rcl_status.goal_info.stamp;
            let goal_id = GoalUuid(rcl_status.goal_info.goal_id.uuid);
            let status = GoalStatus {
                goal_id,
                code: rcl_status.status.into(),
                stamp: Time {
                    sec: stamp.sec,
                    nanosec: stamp.nanosec,
                },
            };

            if let Some(senders) = all_status_senders.get(&goal_id) {
                for sender in senders {
                    let _ = sender.send(status.clone());
                }
            }

            if let Some(watcher) = all_status_watchers.get(&goal_id) {
                watcher.send_modify(|watched_status| *watched_status = status);
            }
        }

        Ok(())
    }

    fn execute_goal_response(&self) -> Result<(), RclrsError> {
        let Some(client) = self.client.lock().unwrap().upgrade() else {
            // The action client has already expired which means the user is not
            // waiting for this response any longer. We can just discard it.
            return Ok(());
        };

        let Some((response_rmw, header)) = self
            .handle
            .take_goal_response::<A>()
            .take_failed_as_none()?
        else {
            return Ok(());
        };

        let accepted = A::get_goal_response_accepted(&response_rmw);
        let stamp = A::get_goal_response_stamp(&response_rmw);

        let Some(pending) = self
            .pending_goal_clients
            .lock()
            .unwrap()
            .remove(&header.sequence_number)
        else {
            // This suggests that the RequestedGoalClient was dropped before the result arrived.
            return Ok(());
        };

        if accepted {
            let _ = pending.sender.send(Some(GoalClient {
                feedback: pending.feedback,
                status: pending.status,
                result: self.request_result(Arc::clone(&client), pending.goal_id)?,
                cancellation: CancellationClient::new(client, pending.goal_id),
                stamp: Time {
                    sec: stamp.0,
                    nanosec: stamp.1,
                },
            }));
        } else {
            let _ = pending.sender.send(None);
        }

        Ok(())
    }

    fn execute_cancel_response(&self) -> Result<(), RclrsError> {
        let Some((result, header)) = self.handle.take_cancel_response().take_failed_as_none()?
        else {
            return Ok(());
        };

        let seq = header.sequence_number;
        let Some(sender) = self.cancel_response_senders.lock().unwrap().remove(&seq) else {
            // If the sender doesn't exist, most likely it means the CancelResponseClient
            // has been dropped.
            return Ok(());
        };

        let code: CancelResponseCode = result.return_code.into();
        match sender {
            CancelResponseSender::SingleGoalCancel(sender) => {
                if result.goals_canceling.len() > 1 {
                    log_warn!(
                        "action_client.execute_cancel_response",
                        "Multiple goals were cancelled when only one was expected. \
                        This may indicate a client library implementation bug.",
                    );
                }

                // Use the first goal's info to get the time stamp. We should
                // only be expecting one goal to be cancelled anyway.
                let stamp = result.goals_canceling.first().map(|g| g.stamp.clone());
                let _ = sender.send(CancelResponse { code, stamp });
            }
            CancelResponseSender::MultiGoalCancel(sender) => {
                let mut stamps = HashMap::default();
                for info in result.goals_canceling {
                    stamps.insert(GoalUuid(info.goal_id.uuid), info.stamp);
                }
                let _ = sender.send(MultiCancelResponse { code, stamps });
            }
        }

        Ok(())
    }

    fn execute_result_response(&self) -> Result<(), RclrsError> {
        let Some((result_rmw, header)) = self
            .handle
            .take_result_response::<A>()
            .take_failed_as_none()?
        else {
            return Ok(());
        };

        let (status, result) = A::split_result_response(result_rmw);
        let status_code = status.into();

        let seq = header.sequence_number;
        let Some(sender) = self.result_senders.lock().unwrap().remove(&seq) else {
            // If the sender doesn't exist, most likely it means the ResultClient
            // has been dropped.
            return Ok(());
        };

        let result = Message::from_rmw_message(result);
        let _ = sender.send((status_code, result));
        Ok(())
    }

    fn request_goal(
        &self,
        client: &ActionClient<A>,
        goal: A::Goal,
    ) -> Result<RequestedGoalClient<A>, RclrsError> {
        let goal_id: GoalUuid = uuid::Uuid::new_v4().as_bytes().into();
        let goal_rmw = <A::Goal as Message>::into_rmw_message(Cow::Owned(goal)).into_owned();
        let request = A::create_goal_request(&*goal_id, goal_rmw);

        let mut seq: i64 = 0;
        unsafe {
            let handle = self.handle.lock();
            rcl_action_send_goal_request(&*handle, &request as *const _ as *const _, &mut seq)
        }
        .ok()?;

        let (sender, receiver) = oneshot_channel();
        let feedback = self.create_feedback_client(client, goal_id);
        let status = self.create_status_watcher(client, goal_id);

        self.pending_goal_clients.lock().unwrap().insert(
            seq,
            PendingGoalClient {
                goal_id,
                feedback,
                status,
                sender,
            },
        );

        Ok(RequestedGoalClient::new(
            goal_id,
            receiver,
            GoalClientLifecycle {
                kind: GoalClientKind::Request(seq),
                client: Arc::clone(client),
            },
        ))
    }

    fn create_feedback_client(
        &self,
        client: &ActionClient<A>,
        goal_id: GoalUuid,
    ) -> FeedbackClient<A> {
        let (sender, receiver) = unbounded_channel();
        self.feedback_senders
            .lock()
            .unwrap()
            .entry(goal_id)
            .or_default()
            .push(sender);
        FeedbackClient::new(
            receiver,
            goal_id,
            GoalClientLifecycle {
                kind: GoalClientKind::Feedback(goal_id),
                client: Arc::clone(client),
            },
        )
    }

    fn create_status_client(&self, client: &ActionClient<A>, goal_id: GoalUuid) -> StatusClient<A> {
        let (sender, receiver) = unbounded_channel();
        self.status_senders
            .lock()
            .unwrap()
            .entry(goal_id)
            .or_default()
            .push(sender);
        StatusClient::new(
            receiver,
            goal_id,
            GoalClientLifecycle {
                kind: GoalClientKind::Status(goal_id),
                client: Arc::clone(client),
            },
        )
    }

    fn create_status_watcher(
        &self,
        client: &ActionClient<A>,
        goal_id: GoalUuid,
    ) -> StatusWatcher<A> {
        let watcher = self
            .status_posters
            .lock()
            .unwrap()
            .entry(goal_id)
            .or_insert_with(|| {
                WatchSender::new(GoalStatus {
                    code: GoalStatusCode::Unknown,
                    goal_id,
                    stamp: Time { sec: 0, nanosec: 0 },
                })
            })
            .subscribe();

        StatusWatcher::new(
            watcher,
            Arc::new(GoalClientLifecycle {
                kind: GoalClientKind::Status(goal_id),
                client: Arc::clone(client),
            }),
        )
    }

    fn request_single_cancel(
        &self,
        client: &ActionClient<A>,
        goal_id: GoalUuid,
    ) -> Result<CancelResponseClient<A>, RclrsError> {
        let seq = self
            .handle
            .send_cancel_goal(goal_id, Time { sec: 0, nanosec: 0 })?;
        let (sender, receiver) = oneshot_channel();
        self.cancel_response_senders
            .lock()
            .unwrap()
            .insert(seq, CancelResponseSender::SingleGoalCancel(sender));
        Ok(CancelResponseClient::new(
            receiver,
            GoalClientLifecycle {
                kind: GoalClientKind::CancelResponse(seq),
                client: Arc::clone(client),
            },
        ))
    }

    fn request_multi_cancel(
        &self,
        client: &ActionClient<A>,
        stamp: Option<Time>,
    ) -> Result<MultiCancelResponseClient<A>, RclrsError> {
        let goal_id = GoalUuid::zero();
        let stamp = stamp.unwrap_or(Time { sec: 0, nanosec: 0 });
        let seq = self.handle.send_cancel_goal(goal_id, stamp)?;
        let (sender, receiver) = oneshot_channel();
        self.cancel_response_senders
            .lock()
            .unwrap()
            .insert(seq, CancelResponseSender::MultiGoalCancel(sender));
        Ok(MultiCancelResponseClient::new(
            receiver,
            GoalClientLifecycle {
                kind: GoalClientKind::CancelResponse(seq),
                client: Arc::clone(client),
            },
        ))
    }

    fn request_result(
        &self,
        client: ActionClient<A>,
        goal_id: GoalUuid,
    ) -> Result<ResultClient<A>, RclrsError> {
        let request_rmw = A::create_result_request(&*goal_id);
        let mut seq: i64 = 0;
        unsafe {
            let handle = self.handle.lock();
            rcl_action_send_result_request(&*handle, &request_rmw as *const _ as *const _, &mut seq)
        }
        .ok()?;

        let (sender, receiver) = oneshot_channel();
        self.result_senders.lock().unwrap().insert(seq, sender);
        let lifecycle = GoalClientLifecycle {
            client,
            kind: GoalClientKind::Result(seq),
        };
        Ok(ResultClient::new(receiver, lifecycle))
    }
}

/// This struct represents a goal client that has not been accepted yet. We
/// create the feedback and status channels before sending the request just in
/// case the action server sends updates for the goal before we process that the
/// goal was accepted. This ensures that no information can be lost by accident.
struct PendingGoalClient<A: Action> {
    goal_id: GoalUuid,
    feedback: FeedbackClient<A>,
    status: StatusWatcher<A>,
    sender: Sender<Option<GoalClient<A>>>,
}

/// Once all of the constituent clients for the goal are dropped, this will
/// remove the [`GoalClientSender`][super::GoalClientSender] from the goal
/// board.
///
/// This also ensures that the action client remains alive for as long as any
/// of its constituent clients are using it.
struct GoalClientLifecycle<A: Action> {
    kind: GoalClientKind,
    client: ActionClient<A>,
}

enum GoalClientKind {
    Request(i64),
    Feedback(GoalUuid),
    Status(GoalUuid),
    Result(i64),
    CancelResponse(i64),
}

impl<A: Action> Drop for GoalClientLifecycle<A> {
    fn drop(&mut self) {
        match self.kind {
            GoalClientKind::Request(seq) => {
                let mut all_pending_goal_clients =
                    self.client.board.pending_goal_clients.lock().unwrap();
                all_pending_goal_clients.remove(&seq);
            }
            GoalClientKind::Feedback(goal_uuid) => {
                let mut all_feedback_senders = self.client.board.feedback_senders.lock().unwrap();

                let mut is_empty = false;
                if let Some(senders) = all_feedback_senders.get_mut(&goal_uuid) {
                    senders.retain(|sender| !sender.is_closed());
                    is_empty = senders.is_empty();
                }
                if is_empty {
                    all_feedback_senders.remove(&goal_uuid);
                }
            }
            GoalClientKind::Status(goal_uuid) => {
                {
                    let mut all_status_senders = self.client.board.status_senders.lock().unwrap();
                    let mut is_empty = false;
                    if let Some(senders) = all_status_senders.get_mut(&goal_uuid) {
                        senders.retain(|sender| !sender.is_closed());
                        is_empty = senders.is_empty();
                    }
                    if is_empty {
                        all_status_senders.remove(&goal_uuid);
                    }
                }

                {
                    let mut all_status_posters = self.client.board.status_posters.lock().unwrap();
                    let remove = all_status_posters
                        .get(&goal_uuid)
                        .is_some_and(|sender| sender.is_closed());
                    if remove {
                        all_status_posters.remove(&goal_uuid);
                    }
                }
            }
            GoalClientKind::CancelResponse(seq) => {
                let mut all_cancel_response_senders =
                    self.client.board.cancel_response_senders.lock().unwrap();
                all_cancel_response_senders.remove(&seq);
            }
            GoalClientKind::Result(seq) => {
                let mut all_result_senders = self.client.board.result_senders.lock().unwrap();
                all_result_senders.remove(&seq);
            }
        }
    }
}

struct ActionClientExecutable<A: Action> {
    board: Arc<ActionClientGoalBoard<A>>,
}

impl<A: Action> RclPrimitive for ActionClientExecutable<A> {
    unsafe fn execute(&mut self, ready: ReadyKind, _: &mut dyn Any) -> Result<(), RclrsError> {
        let ready = ready.for_action_client()?;

        if ready.goal_response {
            self.board.execute_goal_response()?;
        }

        if ready.feedback {
            self.board.execute_feedback()?;
        }

        if ready.status {
            self.board.execute_status()?;
        }

        if ready.cancel_response {
            self.board.execute_cancel_response()?;
        }

        if ready.result_response {
            self.board.execute_result_response()?;
        }

        Ok(())
    }

    fn kind(&self) -> RclPrimitiveKind {
        RclPrimitiveKind::ActionClient
    }

    fn handle(&self) -> crate::RclPrimitiveHandle<'_> {
        RclPrimitiveHandle::ActionClient(self.board.handle.lock())
    }
}

/// Manage the lifecycle of an `rcl_action_client_t`, including managing its dependencies
/// on `rcl_node_t` and `rcl_context_t` by ensuring that these dependencies are
/// [dropped after][1] the `rcl_action_client_t`.
///
/// [1]: <https://doc.rust-lang.org/reference/destructors.html>
pub struct ActionClientHandle {
    rcl_action_client: Mutex<rcl_action_client_t>,
    node_handle: Arc<NodeHandle>,
}

impl ActionClientHandle {
    fn lock(&self) -> MutexGuard<'_, rcl_action_client_t> {
        self.rcl_action_client.lock().unwrap()
    }

    fn take_goal_response<A: Action>(
        &self,
    ) -> Result<(RmwGoalResponse<A>, rmw_request_id_t), RclrsError> {
        let mut response_rmw = RmwGoalResponse::<A>::default();
        let mut response_header = rmw_request_id_t {
            writer_guid: [0; RCL_ACTION_UUID_SIZE],
            sequence_number: 0,
        };

        unsafe {
            let handle = self.lock();
            rcl_action_take_goal_response(
                &*handle,
                &mut response_header,
                &mut response_rmw as *mut _ as *mut _,
            )
        }
        .ok()?;

        Ok((response_rmw, response_header))
    }

    fn take_feedback<A: Action>(&self) -> Result<RmwFeedbackMessage<A>, RclrsError> {
        let mut feedback_rmw = RmwFeedbackMessage::<A>::default();
        unsafe {
            let handle = self.lock();
            rcl_action_take_feedback(&*handle, &mut feedback_rmw as *mut _ as *mut _)
        }
        .ok()?;

        Ok(feedback_rmw)
    }

    fn take_status(&self) -> Result<DropGuard<rcl_action_goal_status_array_t>, RclrsError> {
        let mut goal_statuses = empty_goal_status_array();
        unsafe {
            let handle = self.lock();
            rcl_action_take_status(&*handle, &mut *goal_statuses as *mut _ as *mut _)
        }
        .ok()?;

        Ok(goal_statuses)
    }

    fn take_result_response<A: Action>(
        &self,
    ) -> Result<(RmwResultResponse<A>, rmw_request_id_t), RclrsError> {
        let mut result_rmw = RmwResultResponse::<A>::default();
        let mut response_header = rmw_request_id_t {
            writer_guid: [0; RCL_ACTION_UUID_SIZE],
            sequence_number: 0,
        };

        unsafe {
            let handle = self.lock();
            rcl_action_take_result_response(
                &*handle,
                &mut response_header,
                &mut result_rmw as *mut _ as *mut _,
            )
        }
        .ok()?;

        Ok((result_rmw, response_header))
    }

    fn send_cancel_goal(&self, goal_id: GoalUuid, stamp: Time) -> Result<i64, RclrsError> {
        let cancel_request_rmw = action_msgs__srv__CancelGoal_Request {
            goal_info: action_msgs__msg__GoalInfo {
                goal_id: unique_identifier_msgs__msg__UUID { uuid: *goal_id },
                stamp: builtin_interfaces__msg__Time {
                    sec: stamp.sec,
                    nanosec: stamp.nanosec,
                },
            },
        };
        let mut seq: i64 = 0;

        unsafe {
            let handle = self.lock();
            rcl_action_send_cancel_request(
                &*handle,
                &cancel_request_rmw as *const _ as *const _,
                &mut seq,
            )
        }
        .ok()?;

        Ok(seq)
    }

    fn take_cancel_response(&self) -> Result<(CancelGoal_Response, rmw_request_id_t), RclrsError> {
        let mut result_rmw = <CancelGoal_Response as Message>::RmwMsg::default();
        let mut header = rmw_request_id_t {
            writer_guid: [0; RCL_ACTION_UUID_SIZE],
            sequence_number: 0,
        };

        unsafe {
            let handle = self.lock();
            rcl_action_take_cancel_response(
                &*handle,
                &mut header,
                &mut result_rmw as *mut _ as *mut _,
            )
        }
        .ok()?;

        let result = Message::from_rmw_message(result_rmw);
        Ok((result, header))
    }
}

impl Drop for ActionClientHandle {
    fn drop(&mut self) {
        let rcl_action_client = self.rcl_action_client.get_mut().unwrap();
        let mut rcl_node = self.node_handle.rcl_node.lock().unwrap();
        let _lifecycle_lock = ENTITY_LIFECYCLE_MUTEX.lock().unwrap();
        // SAFETY: The entity lifecycle mutex is locked to protect against the risk of
        // global variables in the rmw implementation being unsafely modified during cleanup.
        unsafe {
            rcl_action_client_fini(rcl_action_client, &mut *rcl_node);
        }
    }
}

// SAFETY: The functions accessing this type, including drop(), shouldn't care about the thread
// they are running in. Therefore, this type can be safely sent to another thread.
unsafe impl Send for rcl_action_client_t {}