@@ -23,15 +23,16 @@ use crate::{
23
23
muxing:: StreamMuxer ,
24
24
nodes:: {
25
25
node:: Substream ,
26
- handled_node_tasks:: { HandledNodesEvent , HandledNodesTasks , TaskClosedEvent } ,
27
- handled_node_tasks:: { IntoNodeHandler , Task as HandledNodesTask , TaskId , ClosedTask } ,
28
- handled_node:: { HandledNodeError , NodeHandler }
26
+ handled_node:: { HandledNodeError , IntoNodeHandler , NodeHandler } ,
27
+ tasks:: { self , ClosedTask , TaskEntry , TaskId }
29
28
}
30
29
} ;
31
30
use fnv:: FnvHashMap ;
32
31
use futures:: prelude:: * ;
33
32
use std:: { error, fmt, hash:: Hash , mem} ;
34
33
34
+ pub use crate :: nodes:: tasks:: StartTakeOver ;
35
+
35
36
mod tests;
36
37
37
38
/// Implementation of `Stream` that handles a collection of nodes.
@@ -40,7 +41,7 @@ pub struct CollectionStream<TInEvent, TOutEvent, THandler, TReachErr, THandlerEr
40
41
///
41
42
/// The user data contains the state of the task. If `Connected`, then a corresponding entry
42
43
/// must be present in `nodes`.
43
- inner : HandledNodesTasks < TInEvent , TOutEvent , THandler , TReachErr , THandlerErr , TaskState < TConnInfo , TUserData > , TConnInfo > ,
44
+ inner : tasks :: Manager < TInEvent , TOutEvent , THandler , TReachErr , THandlerErr , TaskState < TConnInfo , TUserData > , TConnInfo > ,
44
45
45
46
/// List of nodes, with the task id that handles this node. The corresponding entry in `tasks`
46
47
/// must always be in the `Connected` state.
@@ -310,7 +311,7 @@ where
310
311
#[ inline]
311
312
pub fn new ( ) -> Self {
312
313
CollectionStream {
313
- inner : HandledNodesTasks :: new ( ) ,
314
+ inner : tasks :: Manager :: new ( ) ,
314
315
nodes : Default :: default ( ) ,
315
316
}
316
317
}
@@ -357,12 +358,17 @@ where
357
358
}
358
359
359
360
/// Sends an event to all nodes.
360
- #[ inline]
361
- pub fn broadcast_event ( & mut self , event : & TInEvent )
362
- where TInEvent : Clone ,
361
+ #[ must_use]
362
+ pub fn start_broadcast ( & mut self , event : & TInEvent ) -> AsyncSink < ( ) >
363
+ where
364
+ TInEvent : Clone
363
365
{
364
- // TODO: remove the ones we're not connected to?
365
- self . inner . broadcast_event ( event)
366
+ self . inner . start_broadcast ( event)
367
+ }
368
+
369
+ #[ must_use]
370
+ pub fn complete_broadcast ( & mut self ) -> Async < ( ) > {
371
+ self . inner . complete_broadcast ( )
366
372
}
367
373
368
374
/// Adds an existing connection to a node to the collection.
@@ -383,8 +389,8 @@ where
383
389
TConnInfo : Clone + Send + ' static ,
384
390
TPeerId : Clone ,
385
391
{
386
- // Calling `HandledNodesTasks ::add_connection` is the same as calling
387
- // `HandledNodesTasks ::add_reach_attempt`, except that we don't get any `NodeReached` event.
392
+ // Calling `tasks::Manager ::add_connection` is the same as calling
393
+ // `tasks::Manager ::add_reach_attempt`, except that we don't get any `NodeReached` event.
388
394
// We therefore implement this method the same way as calling `add_reach_attempt` followed
389
395
// with simulating a received `NodeReached` event and accepting it.
390
396
@@ -451,29 +457,28 @@ where
451
457
} ;
452
458
453
459
match item {
454
- HandledNodesEvent :: TaskClosed { task, result, handler } => {
460
+ tasks :: Event :: TaskClosed { task, result, handler } => {
455
461
let id = task. id ( ) ;
456
462
let user_data = task. into_user_data ( ) ;
457
463
458
464
match ( user_data, result, handler) {
459
- ( TaskState :: Pending , TaskClosedEvent :: Reach ( err) , Some ( handler) ) => {
465
+ ( TaskState :: Pending , tasks :: Error :: Reach ( err) , Some ( handler) ) => {
460
466
Async :: Ready ( CollectionEvent :: ReachError {
461
467
id : ReachAttemptId ( id) ,
462
468
error : err,
463
469
handler,
464
470
} )
465
471
} ,
466
- ( TaskState :: Pending , TaskClosedEvent :: Node ( _) , _) => {
472
+ ( TaskState :: Pending , tasks :: Error :: Node ( _) , _) => {
467
473
panic ! ( "We switch the task state to Connected once we're connected, and \
468
- a TaskClosedEvent::Node can only happen after we're \
469
- connected; QED") ;
474
+ a tasks::Error::Node can only happen after we're connected; QED") ;
470
475
} ,
471
- ( TaskState :: Pending , TaskClosedEvent :: Reach ( _) , None ) => {
472
- // TODO: this could be improved in the API of HandledNodesTasks
473
- panic ! ( "The HandledNodesTasks is guaranteed to always return the handler \
474
- when producing a TaskClosedEvent ::Reach error") ;
476
+ ( TaskState :: Pending , tasks :: Error :: Reach ( _) , None ) => {
477
+ // TODO: this could be improved in the API of tasks::Manager
478
+ panic ! ( "The tasks::Manager is guaranteed to always return the handler \
479
+ when producing a tasks::Error ::Reach error") ;
475
480
} ,
476
- ( TaskState :: Connected ( conn_info, user_data) , TaskClosedEvent :: Node ( err) , _handler) => {
481
+ ( TaskState :: Connected ( conn_info, user_data) , tasks :: Error :: Node ( err) , _handler) => {
477
482
debug_assert ! ( _handler. is_none( ) ) ;
478
483
let _node_task_id = self . nodes . remove ( conn_info. peer_id ( ) ) ;
479
484
debug_assert_eq ! ( _node_task_id, Some ( id) ) ;
@@ -483,13 +488,13 @@ where
483
488
user_data,
484
489
} )
485
490
} ,
486
- ( TaskState :: Connected ( _, _) , TaskClosedEvent :: Reach ( _) , _) => {
487
- panic ! ( "A TaskClosedEvent ::Reach can only happen before we are connected \
491
+ ( TaskState :: Connected ( _, _) , tasks :: Error :: Reach ( _) , _) => {
492
+ panic ! ( "A tasks::Error ::Reach can only happen before we are connected \
488
493
to a node; therefore the TaskState won't be Connected; QED") ;
489
494
} ,
490
495
}
491
496
} ,
492
- HandledNodesEvent :: NodeReached { task, conn_info } => {
497
+ tasks :: Event :: NodeReached { task, conn_info } => {
493
498
let id = task. id ( ) ;
494
499
drop ( task) ;
495
500
Async :: Ready ( CollectionEvent :: NodeReached ( CollectionReachEvent {
@@ -498,7 +503,7 @@ where
498
503
conn_info : Some ( conn_info) ,
499
504
} ) )
500
505
} ,
501
- HandledNodesEvent :: NodeEvent { task, event } => {
506
+ tasks :: Event :: NodeEvent { task, event } => {
502
507
let conn_info = match task. user_data ( ) {
503
508
TaskState :: Connected ( conn_info, _) => conn_info. clone ( ) ,
504
509
_ => panic ! ( "we can only receive NodeEvent events from a task after we \
@@ -566,7 +571,7 @@ where
566
571
567
572
/// Access to a peer in the collection.
568
573
pub struct PeerMut < ' a , TInEvent , TUserData , TConnInfo = PeerId , TPeerId = PeerId > {
569
- inner : HandledNodesTask < ' a , TInEvent , TaskState < TConnInfo , TUserData > > ,
574
+ inner : TaskEntry < ' a , TInEvent , TaskState < TConnInfo , TUserData > > ,
570
575
nodes : & ' a mut FnvHashMap < TPeerId , TaskId > ,
571
576
}
572
577
@@ -612,9 +617,13 @@ where
612
617
}
613
618
614
619
/// Sends an event to the given node.
615
- #[ inline]
616
- pub fn send_event ( & mut self , event : TInEvent ) {
617
- self . inner . send_event ( event)
620
+ pub fn start_send_event ( & mut self , event : TInEvent ) -> StartSend < TInEvent , ( ) > {
621
+ self . inner . start_send_event ( event)
622
+ }
623
+
624
+ /// Complete sending an event message initiated by `start_send_event`.
625
+ pub fn complete_send_event ( & mut self ) -> Poll < ( ) , ( ) > {
626
+ self . inner . complete_send_event ( )
618
627
}
619
628
620
629
/// Closes the connections to this node. Returns the user data.
@@ -639,8 +648,23 @@ where
639
648
/// The reach attempt will only be effectively cancelled once the peer (the object you're
640
649
/// manipulating) has received some network activity. However no event will be ever be
641
650
/// generated from this reach attempt, and this takes effect immediately.
642
- pub fn take_over ( & mut self , id : InterruptedReachAttempt < TInEvent , TConnInfo , TUserData > ) {
643
- let _state = self . inner . take_over ( id. inner ) ;
644
- debug_assert ! ( if let TaskState :: Pending = _state { true } else { false } ) ;
651
+ #[ must_use]
652
+ pub fn start_take_over ( & mut self , id : InterruptedReachAttempt < TInEvent , TConnInfo , TUserData > )
653
+ -> StartTakeOver < ( ) , InterruptedReachAttempt < TInEvent , TConnInfo , TUserData > >
654
+ {
655
+ match self . inner . start_take_over ( id. inner ) {
656
+ StartTakeOver :: Ready ( _state) => {
657
+ debug_assert ! ( if let TaskState :: Pending = _state { true } else { false } ) ;
658
+ StartTakeOver :: Ready ( ( ) )
659
+ }
660
+ StartTakeOver :: NotReady ( inner) =>
661
+ StartTakeOver :: NotReady ( InterruptedReachAttempt { inner } ) ,
662
+ StartTakeOver :: Gone => StartTakeOver :: Gone
663
+ }
664
+ }
665
+
666
+ /// Complete a take over initiated by `start_take_over`.
667
+ pub fn complete_take_over ( & mut self ) -> Poll < ( ) , ( ) > {
668
+ self . inner . complete_take_over ( )
645
669
}
646
670
}
0 commit comments