@@ -5,14 +5,12 @@ use std::{
5
5
6
6
use rosidl_runtime_rs:: { Message , RmwMessage } ;
7
7
8
- use futures:: channel:: mpsc:: { unbounded, UnboundedSender , TrySendError } ;
9
-
10
8
use crate :: {
11
9
error:: ToResult ,
12
10
qos:: QoSProfile ,
13
11
rcl_bindings:: * ,
14
12
ExecutorCommands , NodeHandle , RclrsError , Waitable , Executable , ExecutableHandle ,
15
- ExecutableKind , GuardCondition , WaitableLifecycle , ENTITY_LIFECYCLE_MUTEX ,
13
+ ExecutableKind , WaitableLifecycle , ENTITY_LIFECYCLE_MUTEX ,
16
14
} ;
17
15
18
16
mod any_subscription_callback;
@@ -30,9 +28,6 @@ pub use message_info::*;
30
28
mod readonly_loaned_message;
31
29
pub use readonly_loaned_message:: * ;
32
30
33
- mod subscription_task;
34
- use subscription_task:: * ;
35
-
36
31
/// Struct for receiving messages of type `T`.
37
32
///
38
33
/// The only way to instantiate a subscription is via [`Node::create_subscription()`][2]
59
54
///
60
55
/// Holding onto this sender will keep the subscription task alive. Once
61
56
/// this sender is dropped, the subscription task will end itself.
62
- action : UnboundedSender < SubscriptionAction < T > > ,
57
+ callback : Arc < Mutex < AnySubscriptionCallback < T > > > ,
63
58
/// Holding onto this keeps the waiter for this subscription alive in the
64
59
/// wait set of the executor.
65
60
lifecycle : WaitableLifecycle ,
91
86
pub fn set_callback < Args > (
92
87
& self ,
93
88
callback : impl SubscriptionCallback < T , Args > ,
94
- ) -> Result < ( ) , TrySendError < SubscriptionAction < T > > > {
89
+ ) {
95
90
let callback = callback. into_subscription_callback ( ) ;
96
- self . action . unbounded_send ( SubscriptionAction :: SetCallback ( callback ) )
91
+ * self . callback . lock ( ) . unwrap ( ) = callback ;
97
92
}
98
93
99
94
/// Set the callback of this subscription, replacing the callback that was
103
98
pub fn set_async_callback < Args > (
104
99
& self ,
105
100
callback : impl SubscriptionAsyncCallback < T , Args > ,
106
- ) -> Result < ( ) , TrySendError < SubscriptionAction < T > > > {
101
+ ) {
107
102
let callback = callback. into_subscription_async_callback ( ) ;
108
- self . action . unbounded_send ( SubscriptionAction :: SetCallback ( callback ) )
103
+ * self . callback . lock ( ) . unwrap ( ) = callback ;
109
104
}
110
105
111
106
/// Used by [`Node`][crate::Node] to create a new subscription.
@@ -116,40 +111,8 @@ where
116
111
node_handle : & Arc < NodeHandle > ,
117
112
commands : & Arc < ExecutorCommands > ,
118
113
) -> Result < Arc < Self > , RclrsError > {
119
- let ( sender, receiver) = unbounded ( ) ;
120
- let ( subscription, waiter) = Self :: new (
121
- topic,
122
- qos,
123
- sender,
124
- Arc :: clone ( & node_handle) ,
125
- Arc :: clone ( commands. get_guard_condition ( ) ) ,
126
- ) ?;
114
+ let callback = Arc :: new ( Mutex :: new ( callback) ) ;
127
115
128
- commands. run ( subscription_task (
129
- callback,
130
- receiver,
131
- Arc :: clone ( & subscription. handle ) ,
132
- Arc :: clone ( & commands) ,
133
- ) ) ;
134
-
135
- commands. add_to_wait_set ( waiter) ;
136
-
137
- Ok ( subscription)
138
- }
139
-
140
- /// Instantiate the Subscription.
141
- fn new (
142
- topic : & str ,
143
- qos : QoSProfile ,
144
- action : UnboundedSender < SubscriptionAction < T > > ,
145
- node_handle : Arc < NodeHandle > ,
146
- guard_condition : Arc < GuardCondition > ,
147
- ) -> Result < ( Arc < Self > , Waitable ) , RclrsError >
148
- // This uses pub(crate) visibility to avoid instantiating this struct outside
149
- // [`Node::create_subscription`], see the struct's documentation for the rationale
150
- where
151
- T : Message ,
152
- {
153
116
// SAFETY: Getting a zero-initialized value is always safe.
154
117
let mut rcl_subscription = unsafe { rcl_get_zero_initialized_subscription ( ) } ;
155
118
let type_support =
@@ -186,35 +149,35 @@ where
186
149
187
150
let handle = Arc :: new ( SubscriptionHandle {
188
151
rcl_subscription : Mutex :: new ( rcl_subscription) ,
189
- node_handle,
152
+ node_handle : Arc :: clone ( node_handle ) ,
190
153
} ) ;
191
154
192
- let ( waiter , lifecycle) = Waitable :: new (
155
+ let ( waitable , lifecycle) = Waitable :: new (
193
156
Box :: new ( SubscriptionExecutable {
194
157
handle : Arc :: clone ( & handle) ,
195
- action : action. clone ( ) ,
158
+ callback : Arc :: clone ( & callback) ,
159
+ commands : Arc :: clone ( commands) ,
196
160
} ) ,
197
- Some ( guard_condition ) ,
161
+ Some ( Arc :: clone ( commands . get_guard_condition ( ) ) ) ,
198
162
) ;
163
+ commands. add_to_wait_set ( waitable) ;
199
164
200
- let subscription = Arc :: new ( Self { handle, action, lifecycle } ) ;
201
-
202
- Ok ( ( subscription, waiter) )
165
+ Ok ( Arc :: new ( Self { handle, callback, lifecycle } ) )
203
166
}
204
167
}
205
168
206
169
struct SubscriptionExecutable < T : Message > {
207
170
handle : Arc < SubscriptionHandle > ,
208
- action : UnboundedSender < SubscriptionAction < T > > ,
171
+ callback : Arc < Mutex < AnySubscriptionCallback < T > > > ,
172
+ commands : Arc < ExecutorCommands > ,
209
173
}
210
174
211
175
impl < T > Executable for SubscriptionExecutable < T >
212
176
where
213
177
T : Message ,
214
178
{
215
179
fn execute ( & mut self ) -> Result < ( ) , RclrsError > {
216
- self . action . unbounded_send ( SubscriptionAction :: Execute ) . ok ( ) ;
217
- Ok ( ( ) )
180
+ self . callback . lock ( ) . unwrap ( ) . execute ( & self . handle , & self . commands )
218
181
}
219
182
220
183
fn kind ( & self ) -> crate :: ExecutableKind {
0 commit comments