1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//! What a running node reports (W5): an [`Event`] stream and a [`Status`] watch, for apps
//! that show connectivity, and the lifecycle calls a mobile or desktop app makes
//! ([`Node::pause`], [`Node::resume`], [`Node::network_changed`]).
//!
//! [`Node::pause`]: crate::Node::pause
//! [`Node::resume`]: crate::Node::resume
//! [`Node::network_changed`]: crate::Node::network_changed
use crate::node::Status;
use crate::stream::Path;
use futures::StreamExt;
use futures::channel::mpsc;
use libp2p::{Multiaddr, PeerId};
use std::pin::Pin;
use std::sync::{Arc, Mutex};
use std::task::{Context, Poll};
use std::time::{Duration, Instant};
/// Something that changed in the node.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Event {
/// Listening on an address (a circuit through a relay included).
Listening(Multiaddr),
/// An address stopped being listened on.
ListenClosed(Multiaddr),
/// Others confirmed they reach us here (`true`), or no longer do (`false`).
External {
addr: Multiaddr,
confirmed: bool,
},
/// A connection to `peer` came up.
Connected {
peer: PeerId,
path: Path,
},
/// A connection to `peer` went away; `remaining` are still open.
Disconnected {
peer: PeerId,
path: Path,
remaining: u32,
},
/// A relay holds a reservation for us.
Reserved {
relay: PeerId,
},
/// A relay's reservation is gone (the node asks again while it isn't paused).
ReservationLost {
relay: PeerId,
},
/// AutoNAT's verdict: `public`, `private` or `unknown`.
Nat(String),
Paused,
Resumed,
NetworkChanged,
}
/// Where the loop sends events: every live [`Events`] gets each one.
#[derive(Clone, Default)]
pub(crate) struct Hub {
subs: Arc<Mutex<Vec<mpsc::UnboundedSender<Event>>>>,
}
impl Hub {
pub(crate) fn send(&self, e: Event) {
if let Ok(mut s) = self.subs.lock() {
s.retain(|t| t.unbounded_send(e.clone()).is_ok());
}
}
pub(crate) fn subscribe(&self) -> Events {
let (tx, rx) = mpsc::unbounded();
if let Ok(mut s) = self.subs.lock() {
s.push(tx);
}
Events { rx }
}
}
/// The node's events from when this was made on, as a `futures` stream or by blocking.
pub struct Events {
rx: mpsc::UnboundedReceiver<Event>,
}
impl Events {
/// The next event, waiting at most `d` (`None`: none in time, or the node stopped).
pub fn next_timeout(&mut self, d: Duration) -> Option<Event> {
let end = Instant::now() + d;
loop {
match self.rx.try_recv() {
Ok(e) => return Some(e),
Err(mpsc::TryRecvError::Closed) => return None,
Err(mpsc::TryRecvError::Empty) if Instant::now() >= end => return None,
Err(mpsc::TryRecvError::Empty) => std::thread::sleep(Duration::from_millis(5)),
}
}
}
/// Wait (at most `d`) for an event `f` picks.
pub fn wait_for(&mut self, d: Duration, mut f: impl FnMut(&Event) -> bool) -> Option<Event> {
let end = Instant::now() + d;
while let Some(e) = self.next_timeout(end.saturating_duration_since(Instant::now())) {
if f(&e) {
return Some(e);
}
}
None
}
}
impl futures::Stream for Events {
type Item = Event;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Event>> {
self.rx.poll_next_unpin(cx)
}
}
/// The latest [`Status`], with a way to wait for the next change.
#[derive(Clone)]
pub struct StatusWatch {
pub(crate) rx: tokio::sync::watch::Receiver<Status>,
}
impl StatusWatch {
pub fn get(&self) -> Status {
self.rx.borrow().clone()
}
/// Wait for the status to change (any executor); `None` once the node has stopped.
pub async fn changed(&mut self) -> Option<Status> {
self.rx.changed().await.ok()?;
Some(self.rx.borrow_and_update().clone())
}
/// Block until the status changes, at most `d`.
pub fn wait_change(&mut self, d: Duration) -> Option<Status> {
let end = Instant::now() + d;
loop {
if self.rx.has_changed().ok()? {
return Some(self.rx.borrow_and_update().clone());
}
if Instant::now() >= end {
return None;
}
std::thread::sleep(Duration::from_millis(5));
}
}
}