moq_transport/session/
announce.rs

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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
use std::{collections::VecDeque, ops};

use crate::watch::State;
use crate::{message, serve::ServeError};

use super::{Publisher, Subscribed, TrackStatusRequested};

#[derive(Debug, Clone)]
pub struct AnnounceInfo {
	pub namespace: String,
}

struct AnnounceState {
	subscribers: VecDeque<Subscribed>,
	track_statuses_requested: VecDeque<TrackStatusRequested>,
	ok: bool,
	closed: Result<(), ServeError>,
}

impl Default for AnnounceState {
	fn default() -> Self {
		Self {
			subscribers: Default::default(),
			track_statuses_requested: Default::default(),
			ok: false,
			closed: Ok(()),
		}
	}
}

impl Drop for AnnounceState {
	fn drop(&mut self) {
		for subscriber in self.subscribers.drain(..) {
			subscriber.close(ServeError::NotFound).ok();
		}
	}
}

#[must_use = "unannounce on drop"]
pub struct Announce {
	publisher: Publisher,
	state: State<AnnounceState>,

	pub info: AnnounceInfo,
}

impl Announce {
	pub(super) fn new(mut publisher: Publisher, namespace: String) -> (Announce, AnnounceRecv) {
		let info = AnnounceInfo {
			namespace: namespace.clone(),
		};

		publisher.send_message(message::Announce {
			namespace,
			params: Default::default(),
		});

		let (send, recv) = State::default().split();

		let send = Self {
			publisher,
			info,
			state: send,
		};
		let recv = AnnounceRecv { state: recv };

		(send, recv)
	}

	// Run until we get an error
	pub async fn closed(&self) -> Result<(), ServeError> {
		loop {
			{
				let state = self.state.lock();
				state.closed.clone()?;

				match state.modified() {
					Some(notified) => notified,
					None => return Ok(()),
				}
			}
			.await;
		}
	}

	pub async fn subscribed(&self) -> Result<Option<Subscribed>, ServeError> {
		loop {
			{
				let state = self.state.lock();
				if !state.subscribers.is_empty() {
					return Ok(state.into_mut().and_then(|mut state| state.subscribers.pop_front()));
				}

				state.closed.clone()?;
				match state.modified() {
					Some(notified) => notified,
					None => return Ok(None),
				}
			}
			.await;
		}
	}

	pub async fn track_status_requested(&self) -> Result<Option<TrackStatusRequested>, ServeError> {
		loop {
			{
				let state = self.state.lock();
				if !state.track_statuses_requested.is_empty() {
					return Ok(state
						.into_mut()
						.and_then(|mut state| state.track_statuses_requested.pop_front()));
				}

				state.closed.clone()?;
				match state.modified() {
					Some(notified) => notified,
					None => return Ok(None),
				}
			}
			.await;
		}
	}

	// Wait until an OK is received
	pub async fn ok(&self) -> Result<(), ServeError> {
		loop {
			{
				let state = self.state.lock();
				if state.ok {
					return Ok(());
				}
				state.closed.clone()?;

				match state.modified() {
					Some(notified) => notified,
					None => return Ok(()),
				}
			}
			.await;
		}
	}
}

impl Drop for Announce {
	fn drop(&mut self) {
		if self.state.lock().closed.is_err() {
			return;
		}

		self.publisher.send_message(message::Unannounce {
			namespace: self.namespace.to_string(),
		});
	}
}

impl ops::Deref for Announce {
	type Target = AnnounceInfo;

	fn deref(&self) -> &Self::Target {
		&self.info
	}
}

pub(super) struct AnnounceRecv {
	state: State<AnnounceState>,
}

impl AnnounceRecv {
	pub fn recv_ok(&mut self) -> Result<(), ServeError> {
		if let Some(mut state) = self.state.lock_mut() {
			if state.ok {
				return Err(ServeError::Duplicate);
			}

			state.ok = true;
		}

		Ok(())
	}

	pub fn recv_error(self, err: ServeError) -> Result<(), ServeError> {
		let state = self.state.lock();
		state.closed.clone()?;

		let mut state = state.into_mut().ok_or(ServeError::Done)?;
		state.closed = Err(err);

		Ok(())
	}

	pub fn recv_subscribe(&mut self, subscriber: Subscribed) -> Result<(), ServeError> {
		let mut state = self.state.lock_mut().ok_or(ServeError::Done)?;
		state.subscribers.push_back(subscriber);

		Ok(())
	}

	pub fn recv_track_status_requested(
		&mut self,
		track_status_requested: TrackStatusRequested,
	) -> Result<(), ServeError> {
		let mut state = self.state.lock_mut().ok_or(ServeError::Done)?;
		state.track_statuses_requested.push_back(track_status_requested);
		Ok(())
	}
}