moq_transport/session/
subscribed.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
use std::ops;

use futures::stream::FuturesUnordered;
use futures::StreamExt;

use crate::coding::Encode;
use crate::serve::{ServeError, TrackReaderMode};
use crate::watch::State;
use crate::{data, message, serve};

use super::{Publisher, SessionError, SubscribeInfo, Writer};

#[derive(Debug)]
struct SubscribedState {
	max: Option<(u64, u64)>,
	closed: Result<(), ServeError>,
}

impl SubscribedState {
	fn update_max(&mut self, group_id: u64, object_id: u64) -> Result<(), ServeError> {
		if let Some((max_group, max_object)) = self.max {
			if group_id >= max_group && object_id >= max_object {
				self.max = Some((group_id, object_id));
			}
		}

		Ok(())
	}
}

impl Default for SubscribedState {
	fn default() -> Self {
		Self {
			max: None,
			closed: Ok(()),
		}
	}
}

pub struct Subscribed {
	publisher: Publisher,
	state: State<SubscribedState>,
	msg: message::Subscribe,
	ok: bool,

	pub info: SubscribeInfo,
}

impl Subscribed {
	pub(super) fn new(publisher: Publisher, msg: message::Subscribe) -> (Self, SubscribedRecv) {
		let (send, recv) = State::default().split();
		let info = SubscribeInfo {
			namespace: msg.track_namespace.clone(),
			name: msg.track_name.clone(),
		};

		let send = Self {
			publisher,
			state: send,
			msg,
			info,
			ok: false,
		};

		// Prevents updates after being closed
		let recv = SubscribedRecv { state: recv };

		(send, recv)
	}

	pub async fn serve(mut self, track: serve::TrackReader) -> Result<(), SessionError> {
		let res = self.serve_inner(track).await;
		if let Err(err) = &res {
			self.close(err.clone().into())?;
		}

		res
	}

	async fn serve_inner(&mut self, track: serve::TrackReader) -> Result<(), SessionError> {
		let latest = track.latest();
		self.state.lock_mut().ok_or(ServeError::Cancel)?.max = latest;

		self.publisher.send_message(message::SubscribeOk {
			id: self.msg.id,
			expires: None,
			group_order: message::GroupOrder::Descending, // TODO: resolve correct value from publisher / subscriber prefs
			latest,
		});

		self.ok = true; // So we sent SubscribeDone on drop

		match track.mode().await? {
			// TODO cancel track/datagrams on closed
			TrackReaderMode::Stream(stream) => self.serve_track(stream).await,
			TrackReaderMode::Groups(groups) => self.serve_groups(groups).await,
			TrackReaderMode::Objects(objects) => self.serve_objects(objects).await,
			TrackReaderMode::Datagrams(datagrams) => self.serve_datagrams(datagrams).await,
		}
	}

	pub fn close(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 async fn closed(&self) -> Result<(), ServeError> {
		loop {
			{
				let state = self.state.lock();
				state.closed.clone()?;

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

impl ops::Deref for Subscribed {
	type Target = SubscribeInfo;

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

impl Drop for Subscribed {
	fn drop(&mut self) {
		let state = self.state.lock();
		let err = state.closed.as_ref().err().cloned().unwrap_or(ServeError::Done);
		let max = state.max;
		drop(state); // Important to avoid a deadlock

		if self.ok {
			self.publisher.send_message(message::SubscribeDone {
				id: self.msg.id,
				last: max,
				code: err.code(),
				reason: err.to_string(),
			});
		} else {
			self.publisher.send_message(message::SubscribeError {
				id: self.msg.id,
				alias: 0,
				code: err.code(),
				reason: err.to_string(),
			});
		};
	}
}

impl Subscribed {
	async fn serve_track(&mut self, mut track: serve::StreamReader) -> Result<(), SessionError> {
		let mut stream = self.publisher.open_uni().await?;

		// TODO figure out u32 vs u64 priority
		stream.set_priority(track.priority as i32);

		let mut writer = Writer::new(stream);

		let header: data::Header = data::TrackHeader {
			subscribe_id: self.msg.id,
			track_alias: self.msg.track_alias,
			publisher_priority: track.priority,
		}
		.into();

		writer.encode(&header).await?;

		log::trace!("sent track header: {:?}", header);

		while let Some(mut group) = track.next().await? {
			while let Some(mut object) = group.next().await? {
				let header = data::TrackObject {
					group_id: object.group_id,
					object_id: object.object_id,
					size: object.size,
					status: object.status,
				};

				self.state
					.lock_mut()
					.ok_or(ServeError::Done)?
					.update_max(object.group_id, object.object_id)?;

				writer.encode(&header).await?;

				log::trace!("sent track object: {:?}", header);

				while let Some(chunk) = object.read().await? {
					writer.write(&chunk).await?;
					log::trace!("sent track payload: {:?}", chunk.len());
				}

				log::trace!("sent track done");
			}
		}

		Ok(())
	}

	async fn serve_groups(&mut self, mut groups: serve::GroupsReader) -> Result<(), SessionError> {
		let mut tasks = FuturesUnordered::new();
		let mut done: Option<Result<(), ServeError>> = None;

		loop {
			tokio::select! {
				res = groups.next(), if done.is_none() => match res {
					Ok(Some(group)) => {
						let header = data::GroupHeader {
							subscribe_id: self.msg.id,
							track_alias: self.msg.track_alias,
							group_id: group.group_id,
							publisher_priority: group.priority,
						};

						let publisher = self.publisher.clone();
						let state = self.state.clone();
						let info = group.info.clone();

						tasks.push(async move {
							if let Err(err) = Self::serve_group(header, group, publisher, state).await {
								log::warn!("failed to serve group: {:?}, error: {}", info, err);
							}
						});
					},
					Ok(None) => done = Some(Ok(())),
					Err(err) => done = Some(Err(err)),
				},
				res = self.closed(), if done.is_none() => done = Some(res),
				_ = tasks.next(), if !tasks.is_empty() => {},
				else => return Ok(done.unwrap()?),
			}
		}
	}

	async fn serve_group(
		header: data::GroupHeader,
		mut group: serve::GroupReader,
		mut publisher: Publisher,
		state: State<SubscribedState>,
	) -> Result<(), SessionError> {
		let mut stream = publisher.open_uni().await?;

		// TODO figure out u32 vs u64 priority
		stream.set_priority(group.priority as i32);

		let mut writer = Writer::new(stream);

		let header: data::Header = header.into();
		writer.encode(&header).await?;

		log::trace!("sent group: {:?}", header);

		while let Some(mut object) = group.next().await? {
			let header = data::GroupObject {
				object_id: object.object_id,
				size: object.size,
				status: object.status,
			};

			writer.encode(&header).await?;

			state
				.lock_mut()
				.ok_or(ServeError::Done)?
				.update_max(group.group_id, object.object_id)?;

			log::trace!("sent group object: {:?}", header);

			while let Some(chunk) = object.read().await? {
				writer.write(&chunk).await?;
				log::trace!("sent group payload: {:?}", chunk.len());
			}

			log::trace!("sent group done");
		}

		Ok(())
	}

	pub async fn serve_objects(&mut self, mut objects: serve::ObjectsReader) -> Result<(), SessionError> {
		let mut tasks = FuturesUnordered::new();
		let mut done = None;

		loop {
			tokio::select! {
				res = objects.next(), if done.is_none() => match res {
					Ok(Some(object)) => {
						let header = data::ObjectHeader {
							subscribe_id: self.msg.id,
							track_alias: self.msg.track_alias,
							group_id: object.group_id,
							object_id: object.object_id,
							publisher_priority: object.priority,
							object_status: object.status,

						};

						let publisher = self.publisher.clone();
						let state = self.state.clone();
						let info = object.info.clone();

						tasks.push(async move {
							if let Err(err) = Self::serve_object(header, object, publisher, state).await {
								log::warn!("failed to serve object: {:?}, error: {}", info, err);
							};
						});
					},
					Ok(None) => done = Some(Ok(())),
					Err(err) => done = Some(Err(err)),
				},
				_ = tasks.next(), if !tasks.is_empty() => {},
				res = self.closed(), if done.is_none() => done = Some(res),
				else => return Ok(done.unwrap()?),
			}
		}
	}

	async fn serve_object(
		header: data::ObjectHeader,
		mut object: serve::ObjectReader,
		mut publisher: Publisher,
		state: State<SubscribedState>,
	) -> Result<(), SessionError> {
		state
			.lock_mut()
			.ok_or(ServeError::Done)?
			.update_max(object.group_id, object.object_id)?;

		let mut stream = publisher.open_uni().await?;

		// TODO figure out u32 vs u64 priority
		stream.set_priority(object.priority as i32);

		let mut writer = Writer::new(stream);

		let header: data::Header = header.into();
		writer.encode(&header).await?;

		log::trace!("sent object: {:?}", header);

		while let Some(chunk) = object.read().await? {
			writer.write(&chunk).await?;
			log::trace!("sent object payload: {:?}", chunk.len());
		}

		log::trace!("sent object done");

		Ok(())
	}

	async fn serve_datagrams(&mut self, mut datagrams: serve::DatagramsReader) -> Result<(), SessionError> {
		while let Some(datagram) = datagrams.read().await? {
			let datagram = data::Datagram {
				subscribe_id: self.msg.id,
				track_alias: self.msg.track_alias,
				group_id: datagram.group_id,
				object_id: datagram.object_id,
				publisher_priority: datagram.priority,
				object_status: datagram.status,
				payload: datagram.payload,
			};

			let mut buffer = bytes::BytesMut::with_capacity(datagram.payload.len() + 100);
			datagram.encode(&mut buffer)?;

			self.publisher.send_datagram(buffer.into()).await?;
			log::trace!("sent datagram: {:?}", datagram);

			self.state
				.lock_mut()
				.ok_or(ServeError::Done)?
				.update_max(datagram.group_id, datagram.object_id)?;
		}

		Ok(())
	}
}

pub(super) struct SubscribedRecv {
	state: State<SubscribedState>,
}

impl SubscribedRecv {
	pub fn recv_unsubscribe(&mut self) -> Result<(), ServeError> {
		let state = self.state.lock();
		state.closed.clone()?;

		if let Some(mut state) = state.into_mut() {
			state.closed = Err(ServeError::Cancel);
		}

		Ok(())
	}
}