@@ -289,51 +289,29 @@ impl RouteManager {
289
289
pub async fn stop ( & mut self ) {
290
290
if let Some ( tx) = self . manage_tx . take ( ) {
291
291
let ( wait_tx, wait_rx) = oneshot:: channel ( ) ;
292
-
293
- if tx
294
- . unbounded_send ( RouteManagerCommand :: Shutdown ( wait_tx) )
295
- . is_err ( )
296
- {
297
- log:: error!( "RouteManager already down!" ) ;
298
- return ;
299
- }
300
-
301
- if wait_rx. await . is_err ( ) {
302
- log:: error!( "{}" , Error :: ManagerChannelDown ) ;
303
- }
292
+ let _ = tx. unbounded_send ( RouteManagerCommand :: Shutdown ( wait_tx) ) ;
293
+ let _ = wait_rx. await ;
304
294
}
305
295
}
306
296
307
297
/// Applies the given routes until [`RouteManager::stop`] is called.
308
298
pub async fn add_routes ( & mut self , routes : HashSet < RequiredRoute > ) -> Result < ( ) , Error > {
309
- if let Some ( tx) = & self . manage_tx {
310
- let ( result_tx, result_rx) = oneshot:: channel ( ) ;
311
- if tx
312
- . unbounded_send ( RouteManagerCommand :: AddRoutes ( routes, result_tx) )
313
- . is_err ( )
314
- {
315
- return Err ( Error :: RouteManagerDown ) ;
316
- }
317
-
318
- result_rx
319
- . await
320
- . map_err ( |_| Error :: ManagerChannelDown ) ?
321
- . map_err ( Error :: PlatformError )
322
- } else {
323
- Err ( Error :: RouteManagerDown )
324
- }
299
+ let tx = self . get_command_tx ( ) ?;
300
+ let ( result_tx, result_rx) = oneshot:: channel ( ) ;
301
+ tx. unbounded_send ( RouteManagerCommand :: AddRoutes ( routes, result_tx) )
302
+ . map_err ( |_| Error :: RouteManagerDown ) ?;
303
+
304
+ result_rx
305
+ . await
306
+ . map_err ( |_| Error :: ManagerChannelDown ) ?
307
+ . map_err ( Error :: PlatformError )
325
308
}
326
309
327
310
/// Removes all routes previously applied in [`RouteManager::add_routes`].
328
311
pub fn clear_routes ( & mut self ) -> Result < ( ) , Error > {
329
- if let Some ( tx) = & self . manage_tx {
330
- if tx. unbounded_send ( RouteManagerCommand :: ClearRoutes ) . is_err ( ) {
331
- return Err ( Error :: RouteManagerDown ) ;
332
- }
333
- Ok ( ( ) )
334
- } else {
335
- Err ( Error :: RouteManagerDown )
336
- }
312
+ let tx = self . get_command_tx ( ) ?;
313
+ tx. unbounded_send ( RouteManagerCommand :: ClearRoutes )
314
+ . map_err ( |_| Error :: RouteManagerDown )
337
315
}
338
316
339
317
/// Ensure that packets are routed using the correct tables.
@@ -350,11 +328,12 @@ impl RouteManager {
350
328
351
329
/// Retrieve a sender directly to the command channel.
352
330
pub fn handle ( & self ) -> Result < RouteManagerHandle , Error > {
353
- if let Some ( tx) = & self . manage_tx {
354
- Ok ( RouteManagerHandle { tx : tx. clone ( ) } )
355
- } else {
356
- Err ( Error :: RouteManagerDown )
357
- }
331
+ let tx = self . get_command_tx ( ) ?;
332
+ Ok ( RouteManagerHandle { tx : tx. clone ( ) } )
333
+ }
334
+
335
+ fn get_command_tx ( & self ) -> Result < & Arc < UnboundedSender < RouteManagerCommand > > , Error > {
336
+ self . manage_tx . as_ref ( ) . ok_or ( Error :: RouteManagerDown )
358
337
}
359
338
}
360
339
0 commit comments