Skip to content

Commit f197f24

Browse files
committed
Store action clients and servers in the Node
1 parent ae62fb1 commit f197f24

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

rclrs/src/node.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use std::{
1919
use rosidl_runtime_rs::Message;
2020

2121
use crate::{
22-
rcl_bindings::*, ActionClient, ActionServer, CancelResponse, Client, ClientBase, ClientOptions,
22+
rcl_bindings::*, ActionClient, ActionClientBase, ActionServer, ActionServerBase, CancelResponse, Client, ClientBase, ClientOptions,
2323
ClientState, Clock, ContextHandle, GoalResponse, GoalUuid, GuardCondition, LogParams, Logger,
2424
ParameterBuilder, ParameterInterface, ParameterVariant, Parameters, Publisher,
2525
PublisherOptions, PublisherState, RclrsError, ServerGoalHandle, Service, ServiceBase,
@@ -88,6 +88,8 @@ pub struct NodeState {
8888
pub(crate) guard_conditions_mtx: Mutex<Vec<Weak<GuardCondition>>>,
8989
pub(crate) services_mtx: Mutex<Vec<Weak<dyn ServiceBase>>>,
9090
pub(crate) subscriptions_mtx: Mutex<Vec<Weak<dyn SubscriptionBase>>>,
91+
pub(crate) action_servers_mtx: Mutex<Vec<Weak<dyn ActionServerBase>>>,
92+
pub(crate) action_clients_mtx: Mutex<Vec<Weak<dyn ActionClientBase>>>,
9193
time_source: TimeSource,
9294
parameter: ParameterInterface,
9395
logger: Logger,
@@ -282,7 +284,7 @@ impl NodeState {
282284
T: rosidl_runtime_rs::Service,
283285
{
284286
let client = Arc::new(ClientState::<T>::new(self, options)?);
285-
{ self.clients_mtx.lock().unwrap() }.push(Arc::downgrade(&client) as Weak<dyn ClientBase>);
287+
self.clients_mtx.lock().unwrap().push(Arc::downgrade(&client) as Weak<dyn ClientBase>);
286288
Ok(client)
287289
}
288290

@@ -298,8 +300,8 @@ impl NodeState {
298300
T: rosidl_runtime_rs::Action,
299301
{
300302
let action_client = Arc::new(ActionClient::<T>::new(self, topic)?);
301-
// self.action_clients
302-
// .push(Arc::downgrade(&action_client) as Weak<dyn ActionClientBase>);
303+
self.action_clients_mtx.lock().unwrap()
304+
.push(Arc::downgrade(&action_client) as Weak<dyn ActionClientBase>);
303305
Ok(action_client)
304306
}
305307

@@ -328,8 +330,8 @@ impl NodeState {
328330
handle_cancel,
329331
handle_accepted,
330332
)?);
331-
// self.action_servers
332-
// .push(Arc::downgrade(&action_server) as Weak<dyn ActionClientBase>);
333+
self.action_servers_mtx.lock().unwrap()
334+
.push(Arc::downgrade(&action_server) as Weak<dyn ActionServerBase>);
333335
Ok(action_server)
334336
}
335337

@@ -346,7 +348,7 @@ impl NodeState {
346348
Arc::clone(&self.handle.context_handle),
347349
None,
348350
));
349-
{ self.guard_conditions_mtx.lock().unwrap() }
351+
self.guard_conditions_mtx.lock().unwrap()
350352
.push(Arc::downgrade(&guard_condition) as Weak<GuardCondition>);
351353
guard_condition
352354
}
@@ -367,7 +369,7 @@ impl NodeState {
367369
Arc::clone(&self.handle.context_handle),
368370
Some(Box::new(callback) as Box<dyn Fn() + Send + Sync>),
369371
));
370-
{ self.guard_conditions_mtx.lock().unwrap() }
372+
self.guard_conditions_mtx.lock().unwrap()
371373
.push(Arc::downgrade(&guard_condition) as Weak<GuardCondition>);
372374
guard_condition
373375
}
@@ -467,7 +469,7 @@ impl NodeState {
467469
F: Fn(&rmw_request_id_t, T::Request) -> T::Response + 'static + Send,
468470
{
469471
let service = Arc::new(ServiceState::<T>::new(self, options, callback)?);
470-
{ self.services_mtx.lock().unwrap() }
472+
self.services_mtx.lock().unwrap()
471473
.push(Arc::downgrade(&service) as Weak<dyn ServiceBase>);
472474
Ok(service)
473475
}
@@ -522,36 +524,36 @@ impl NodeState {
522524
T: Message,
523525
{
524526
let subscription = Arc::new(SubscriptionState::<T>::new(self, options, callback)?);
525-
{ self.subscriptions_mtx.lock() }
527+
self.subscriptions_mtx.lock()
526528
.unwrap()
527529
.push(Arc::downgrade(&subscription) as Weak<dyn SubscriptionBase>);
528530
Ok(subscription)
529531
}
530532

531533
/// Returns the subscriptions that have not been dropped yet.
532534
pub(crate) fn live_subscriptions(&self) -> Vec<Arc<dyn SubscriptionBase>> {
533-
{ self.subscriptions_mtx.lock().unwrap() }
535+
self.subscriptions_mtx.lock().unwrap()
534536
.iter()
535537
.filter_map(Weak::upgrade)
536538
.collect()
537539
}
538540

539541
pub(crate) fn live_clients(&self) -> Vec<Arc<dyn ClientBase>> {
540-
{ self.clients_mtx.lock().unwrap() }
542+
self.clients_mtx.lock().unwrap()
541543
.iter()
542544
.filter_map(Weak::upgrade)
543545
.collect()
544546
}
545547

546548
pub(crate) fn live_guard_conditions(&self) -> Vec<Arc<GuardCondition>> {
547-
{ self.guard_conditions_mtx.lock().unwrap() }
549+
self.guard_conditions_mtx.lock().unwrap()
548550
.iter()
549551
.filter_map(Weak::upgrade)
550552
.collect()
551553
}
552554

553555
pub(crate) fn live_services(&self) -> Vec<Arc<dyn ServiceBase>> {
554-
{ self.services_mtx.lock().unwrap() }
556+
self.services_mtx.lock().unwrap()
555557
.iter()
556558
.filter_map(Weak::upgrade)
557559
.collect()

rclrs/src/node/node_options.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,8 @@ impl<'a> NodeOptions<'a> {
361361
guard_conditions_mtx: Mutex::default(),
362362
services_mtx: Mutex::default(),
363363
subscriptions_mtx: Mutex::default(),
364+
action_clients_mtx: Mutex::default(),
365+
action_servers_mtx: Mutex::default(),
364366
time_source: TimeSource::builder(self.clock_type)
365367
.clock_qos(self.clock_qos)
366368
.build(),

0 commit comments

Comments
 (0)