Skip to content

Commit 997a27e

Browse files
committed
Take goal, cancel, and accepted callbacks in ActionServer
I'm not sure that this is actually the signature that we'll want, but we'll start from there.
1 parent daed396 commit 997a27e

File tree

3 files changed

+34
-21
lines changed

3 files changed

+34
-21
lines changed

rclrs/src/action.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
mod client;
2-
mod server;
2+
pub(crate) mod server;
33
mod server_goal_handle;
44

55
use crate::rcl_bindings::RCL_ACTION_UUID_SIZE;

rclrs/src/action/server.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use crate::{
2-
error::ToResult, rcl_bindings::*, Clock, NodeHandle, RclrsError, ENTITY_LIFECYCLE_MUTEX,
2+
action::{GoalResponse, GoalUuid, CancelResponse, ServerGoalHandle}, error::ToResult, rcl_bindings::*, Clock, NodeHandle, RclrsError, ENTITY_LIFECYCLE_MUTEX,
33
};
44
use std::{
55
ffi::CString,
6-
marker::PhantomData,
76
sync::{atomic::AtomicBool, Arc, Mutex, MutexGuard},
87
};
98

@@ -51,15 +50,18 @@ pub trait ActionServerBase: Send + Sync {
5150
// fn execute(&self) -> Result<(), RclrsError>;
5251
}
5352

54-
pub struct ActionServer<T>
53+
pub type GoalCallback<ActionT> = dyn Fn(GoalUuid, <ActionT as rosidl_runtime_rs::Action>::Goal) -> GoalResponse + 'static + Send + Sync;
54+
pub type CancelCallback<ActionT> = dyn Fn(ServerGoalHandle<ActionT>) -> CancelResponse + 'static + Send + Sync;
55+
pub type AcceptedCallback<ActionT> = dyn Fn(ServerGoalHandle<ActionT>) + 'static + Send + Sync;
56+
57+
pub struct ActionServer<ActionT>
5558
where
56-
T: rosidl_runtime_rs::Action,
59+
ActionT: rosidl_runtime_rs::Action,
5760
{
58-
_marker: PhantomData<fn() -> T>,
5961
pub(crate) handle: Arc<ActionServerHandle>,
60-
// goal_callback: (),
61-
// cancel_callback: (),
62-
// accepted_callback: (),
62+
goal_callback: Box<GoalCallback<ActionT>>,
63+
cancel_callback: Box<CancelCallback<ActionT>>,
64+
accepted_callback: Box<AcceptedCallback<ActionT>>,
6365
}
6466

6567
impl<T> ActionServer<T>
@@ -71,6 +73,9 @@ where
7173
node_handle: Arc<NodeHandle>,
7274
clock: Clock,
7375
topic: &str,
76+
goal_callback: impl Fn(GoalUuid, T::Goal) -> GoalResponse + 'static + Send + Sync,
77+
cancel_callback: impl Fn(ServerGoalHandle<T>) -> CancelResponse + 'static + Send + Sync,
78+
accepted_callback: impl Fn(ServerGoalHandle<T>) + 'static + Send + Sync,
7479
) -> Result<Self, RclrsError>
7580
where
7681
T: rosidl_runtime_rs::Action,
@@ -119,8 +124,10 @@ where
119124
});
120125

121126
Ok(Self {
122-
_marker: Default::default(),
123127
handle,
128+
goal_callback: Box::new(goal_callback),
129+
cancel_callback: Box::new(cancel_callback),
130+
accepted_callback: Box::new(accepted_callback),
124131
})
125132
}
126133
}

rclrs/src/node.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -220,32 +220,38 @@ impl Node {
220220
T: rosidl_runtime_rs::Action,
221221
{
222222
let action_client = Arc::new(ActionClient::<T>::new(Arc::clone(&self.handle), topic)?);
223-
// self.clients
224-
// .push(Arc::downgrade(&client) as Weak<dyn ClientBase>);
223+
// self.action_clients
224+
// .push(Arc::downgrade(&action_client) as Weak<dyn ActionClientBase>);
225225
Ok(action_client)
226226
}
227227

228228
/// Creates an [`ActionServer`][1].
229229
///
230230
/// [1]: crate::ActionServer
231231
// TODO: make action server's lifetime depend on node's lifetime
232-
pub fn create_action_server<T>(
232+
pub fn create_action_server<ActionT, GoalCallback, CancelCallback, AcceptedCallback>(
233233
&mut self,
234234
topic: &str,
235-
handle_goal: fn(&crate::action::GoalUuid, Arc<T::Goal>) -> GoalResponse,
236-
handle_cancel: fn(Arc<ServerGoalHandle<T>>) -> CancelResponse,
237-
handle_accepted: fn(Arc<ServerGoalHandle<T>>),
238-
) -> Result<Arc<ActionServer<T>>, RclrsError>
235+
handle_goal: GoalCallback,
236+
handle_cancel: CancelCallback,
237+
handle_accepted: AcceptedCallback,
238+
) -> Result<Arc<ActionServer<ActionT>>, RclrsError>
239239
where
240-
T: rosidl_runtime_rs::Action,
240+
ActionT: rosidl_runtime_rs::Action,
241+
GoalCallback: Fn(GoalUuid, <ActionT as rosidl_runtime_rs::Action>::Goal) -> GoalResponse + 'static + Send + Sync,
242+
CancelCallback: Fn(ServerGoalHandle<ActionT>) -> CancelResponse + 'static + Send + Sync,
243+
AcceptedCallback: Fn(ServerGoalHandle<ActionT>) + 'static + Send + Sync,
241244
{
242-
let action_server = Arc::new(ActionServer::<T>::new(
245+
let action_server = Arc::new(ActionServer::<ActionT>::new(
243246
Arc::clone(&self.handle),
244247
self.get_clock(),
245248
topic,
249+
handle_goal,
250+
handle_cancel,
251+
handle_accepted,
246252
)?);
247-
// self.servers
248-
// .push(Arc::downgrade(&server) as Weak<dyn ClientBase>);
253+
// self.action_servers
254+
// .push(Arc::downgrade(&action_server) as Weak<dyn ActionClientBase>);
249255
Ok(action_server)
250256
}
251257

0 commit comments

Comments
 (0)