Skip to content

Commit ec5117d

Browse files
committed
Store action clients and servers in the Node
1 parent 997a27e commit ec5117d

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
@@ -13,7 +13,7 @@ use rosidl_runtime_rs::Message;
1313

1414
pub use self::{builder::*, graph::*};
1515
use crate::{
16-
rcl_bindings::*, ActionClient, ActionServer, CancelResponse, Client, ClientBase, Clock,
16+
rcl_bindings::*, ActionClient, ActionClientBase, ActionServer, ActionServerBase, CancelResponse, Client, ClientBase, Clock,
1717
Context, ContextHandle, GoalResponse, GoalUuid, GuardCondition, ParameterBuilder,
1818
ParameterInterface, ParameterVariant, Parameters, Publisher, QoSProfile, RclrsError,
1919
ServerGoalHandle, Service, ServiceBase, Subscription, SubscriptionBase, SubscriptionCallback,
@@ -64,6 +64,8 @@ pub struct Node {
6464
pub(crate) guard_conditions_mtx: Mutex<Vec<Weak<GuardCondition>>>,
6565
pub(crate) services_mtx: Mutex<Vec<Weak<dyn ServiceBase>>>,
6666
pub(crate) subscriptions_mtx: Mutex<Vec<Weak<dyn SubscriptionBase>>>,
67+
pub(crate) action_servers_mtx: Mutex<Vec<Weak<dyn ActionServerBase>>>,
68+
pub(crate) action_clients_mtx: Mutex<Vec<Weak<dyn ActionClientBase>>>,
6769
time_source: TimeSource,
6870
parameter: ParameterInterface,
6971
pub(crate) handle: Arc<NodeHandle>,
@@ -207,7 +209,7 @@ impl Node {
207209
T: rosidl_runtime_rs::Service,
208210
{
209211
let client = Arc::new(Client::<T>::new(Arc::clone(&self.handle), topic)?);
210-
{ self.clients_mtx.lock().unwrap() }.push(Arc::downgrade(&client) as Weak<dyn ClientBase>);
212+
self.clients_mtx.lock().unwrap().push(Arc::downgrade(&client) as Weak<dyn ClientBase>);
211213
Ok(client)
212214
}
213215

@@ -220,8 +222,8 @@ impl Node {
220222
T: rosidl_runtime_rs::Action,
221223
{
222224
let action_client = Arc::new(ActionClient::<T>::new(Arc::clone(&self.handle), topic)?);
223-
// self.action_clients
224-
// .push(Arc::downgrade(&action_client) as Weak<dyn ActionClientBase>);
225+
self.action_clients_mtx.lock().unwrap()
226+
.push(Arc::downgrade(&action_client) as Weak<dyn ActionClientBase>);
225227
Ok(action_client)
226228
}
227229

@@ -250,8 +252,8 @@ impl Node {
250252
handle_cancel,
251253
handle_accepted,
252254
)?);
253-
// self.action_servers
254-
// .push(Arc::downgrade(&action_server) as Weak<dyn ActionClientBase>);
255+
self.action_servers_mtx.lock().unwrap()
256+
.push(Arc::downgrade(&action_server) as Weak<dyn ActionServerBase>);
255257
Ok(action_server)
256258
}
257259

@@ -269,7 +271,7 @@ impl Node {
269271
Arc::clone(&self.handle.context_handle),
270272
None,
271273
));
272-
{ self.guard_conditions_mtx.lock().unwrap() }
274+
self.guard_conditions_mtx.lock().unwrap()
273275
.push(Arc::downgrade(&guard_condition) as Weak<GuardCondition>);
274276
guard_condition
275277
}
@@ -291,7 +293,7 @@ impl Node {
291293
Arc::clone(&self.handle.context_handle),
292294
Some(Box::new(callback) as Box<dyn Fn() + Send + Sync>),
293295
));
294-
{ self.guard_conditions_mtx.lock().unwrap() }
296+
self.guard_conditions_mtx.lock().unwrap()
295297
.push(Arc::downgrade(&guard_condition) as Weak<GuardCondition>);
296298
guard_condition
297299
}
@@ -330,7 +332,7 @@ impl Node {
330332
topic,
331333
callback,
332334
)?);
333-
{ self.services_mtx.lock().unwrap() }
335+
self.services_mtx.lock().unwrap()
334336
.push(Arc::downgrade(&service) as Weak<dyn ServiceBase>);
335337
Ok(service)
336338
}
@@ -354,36 +356,36 @@ impl Node {
354356
qos,
355357
callback,
356358
)?);
357-
{ self.subscriptions_mtx.lock() }
359+
self.subscriptions_mtx.lock()
358360
.unwrap()
359361
.push(Arc::downgrade(&subscription) as Weak<dyn SubscriptionBase>);
360362
Ok(subscription)
361363
}
362364

363365
/// Returns the subscriptions that have not been dropped yet.
364366
pub(crate) fn live_subscriptions(&self) -> Vec<Arc<dyn SubscriptionBase>> {
365-
{ self.subscriptions_mtx.lock().unwrap() }
367+
self.subscriptions_mtx.lock().unwrap()
366368
.iter()
367369
.filter_map(Weak::upgrade)
368370
.collect()
369371
}
370372

371373
pub(crate) fn live_clients(&self) -> Vec<Arc<dyn ClientBase>> {
372-
{ self.clients_mtx.lock().unwrap() }
374+
self.clients_mtx.lock().unwrap()
373375
.iter()
374376
.filter_map(Weak::upgrade)
375377
.collect()
376378
}
377379

378380
pub(crate) fn live_guard_conditions(&self) -> Vec<Arc<GuardCondition>> {
379-
{ self.guard_conditions_mtx.lock().unwrap() }
381+
self.guard_conditions_mtx.lock().unwrap()
380382
.iter()
381383
.filter_map(Weak::upgrade)
382384
.collect()
383385
}
384386

385387
pub(crate) fn live_services(&self) -> Vec<Arc<dyn ServiceBase>> {
386-
{ self.services_mtx.lock().unwrap() }
388+
self.services_mtx.lock().unwrap()
387389
.iter()
388390
.filter_map(Weak::upgrade)
389391
.collect()

rclrs/src/node/builder.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,8 @@ impl NodeBuilder {
314314
guard_conditions_mtx: Mutex::new(vec![]),
315315
services_mtx: Mutex::new(vec![]),
316316
subscriptions_mtx: Mutex::new(vec![]),
317+
action_servers_mtx: Mutex::new(vec![]),
318+
action_clients_mtx: Mutex::new(vec![]),
317319
time_source: TimeSource::builder(self.clock_type)
318320
.clock_qos(self.clock_qos)
319321
.build(),

0 commit comments

Comments
 (0)