From 755dc74662562acc3533301995632d484095f4ce Mon Sep 17 00:00:00 2001 From: tetta maeda Date: Sun, 6 Oct 2024 00:10:38 +0900 Subject: [PATCH 01/17] impl: new data model for track_namespace_manager --- .../subscription_models/subscription_nodes.rs | 454 +++++++ .../subscription_models/subscriptions.rs | 82 ++ .../src/modules/subscription_models/tracks.rs | 42 + .../track_namespace_manager_repository.rs | 108 +- .../src/modules/track_namespace_manager.rs | 1129 ++++++++--------- 5 files changed, 1208 insertions(+), 607 deletions(-) create mode 100644 moqt-core/src/modules/subscription_models/subscription_nodes.rs create mode 100644 moqt-core/src/modules/subscription_models/subscriptions.rs create mode 100644 moqt-core/src/modules/subscription_models/tracks.rs diff --git a/moqt-core/src/modules/subscription_models/subscription_nodes.rs b/moqt-core/src/modules/subscription_models/subscription_nodes.rs new file mode 100644 index 00000000..56d85e4a --- /dev/null +++ b/moqt-core/src/modules/subscription_models/subscription_nodes.rs @@ -0,0 +1,454 @@ +use crate::messages::control_messages::subscribe::{FilterType, GroupOrder}; +use crate::subscription_models::subscriptions::Subscription; +use crate::subscription_models::tracks::ForwardingPreference; +use anyhow::{bail, Result}; +use std::collections::HashMap; + +type SubscribeId = u64; +type TrackNamespace = Vec; +type TrackAlias = u64; + +pub struct Consumer { + max_subscriber_id: u64, + announced_namespaces: Vec, + subscribing_namespace_prefixes: Vec, + subscriptions: HashMap, +} + +impl Consumer { + pub fn new(max_subscriber_id: u64) -> Self { + Consumer { + max_subscriber_id, + announced_namespaces: Vec::new(), + subscribing_namespace_prefixes: Vec::new(), + subscriptions: HashMap::new(), + } + } +} + +pub struct Producer { + max_subscriber_id: u64, + announcing_namespaces: Vec, + subscribed_namespace_prefixes: Vec, + subscriptions: HashMap, +} + +impl Producer { + pub fn new(max_subscriber_id: u64) -> Self { + Producer { + max_subscriber_id, + announcing_namespaces: Vec::new(), + subscribed_namespace_prefixes: Vec::new(), + subscriptions: HashMap::new(), + } + } +} + +pub trait SubscriptionNodeRegistory { + #[allow(clippy::too_many_arguments)] + fn set_subscription( + &mut self, + subscribe_id: SubscribeId, + track_alias: u64, + track_namespace: TrackNamespace, + track_name: String, + subscriber_priority: u8, + group_order: GroupOrder, + filter_type: FilterType, + start_group: Option, + start_object: Option, + end_group: Option, + end_object: Option, + ) -> Result<()>; + fn get_subscription(&self, subscribe_id: SubscribeId) -> Result>; + fn get_subscription_by_full_track_name( + &self, + track_namespace: TrackNamespace, + track_name: String, + ) -> Result>; + fn get_subscribe_id( + &self, + track_namespace: TrackNamespace, + track_name: String, + ) -> Result>; + fn has_track(&self, track_namespace: TrackNamespace, track_name: String) -> bool; + fn activate_subscription(&mut self, subscribe_id: SubscribeId) -> Result<()>; + fn is_requesting(&self, subscribe_id: SubscribeId) -> bool; + fn delete_subscription(&mut self, subscribe_id: SubscribeId) -> Result<()>; + + fn is_within_max_subscribe_id(&self, subscribe_id: SubscribeId) -> bool; + fn is_subscribe_id_unique(&self, subscribe_id: SubscribeId) -> bool; + fn is_track_alias_unique(&self, track_alias: TrackAlias) -> bool; + fn find_unused_subscribe_id_and_track_alias(&self) -> Result<(SubscribeId, TrackAlias)>; + + fn set_namespace(&mut self, namespace: TrackNamespace) -> Result<()>; + fn get_namespaces(&self) -> Result<&Vec>; + fn has_namespace(&self, namespace: TrackNamespace) -> bool; + fn delete_namespace(&mut self, namespace: TrackNamespace) -> Result<()>; + + fn set_namespace_prefix(&mut self, namespace_prefix: TrackNamespace) -> Result<()>; + fn get_namespace_prefixes(&self) -> Result<&Vec>; + fn delete_namespace_prefix(&mut self, namespace_prefix: TrackNamespace) -> Result<()>; +} + +impl SubscriptionNodeRegistory for Consumer { + fn set_subscription( + &mut self, + subscribe_id: SubscribeId, + track_alias: u64, + track_namespace: Vec, + track_name: String, + subscriber_priority: u8, + group_order: GroupOrder, + filter_type: FilterType, + start_group: Option, + start_object: Option, + end_group: Option, + end_object: Option, + ) -> Result<()> { + // Subscriber cannot define forwarding preference until it receives object message. + let subscription = Subscription::new( + track_alias, + track_namespace, + track_name, + subscriber_priority, + group_order, + filter_type, + start_group, + start_object, + end_group, + end_object, + None, + ); + + self.subscriptions.insert(subscribe_id, subscription); + + Ok(()) + } + + fn get_subscription(&self, subscribe_id: SubscribeId) -> Result> { + Ok(self.subscriptions.get(&subscribe_id).cloned()) + } + + fn get_subscription_by_full_track_name( + &self, + track_namespace: TrackNamespace, + track_name: String, + ) -> Result> { + Ok(self + .subscriptions + .values() + .find(|subscription| { + subscription.get_track_namespace_and_name() + == (track_namespace.clone(), track_name.clone()) + }) + .cloned()) + } + + fn get_subscribe_id( + &self, + track_namespace: TrackNamespace, + track_name: String, + ) -> Result> { + Ok(self + .subscriptions + .iter() + .find(|(_, subscription)| { + subscription.get_track_namespace_and_name() + == (track_namespace.clone(), track_name.clone()) + }) + .map(|(subscribe_id, _)| *subscribe_id)) + } + + fn has_track(&self, track_namespace: TrackNamespace, track_name: String) -> bool { + self.subscriptions.values().any(|subscription| { + subscription.get_track_namespace_and_name() + == (track_namespace.clone(), track_name.clone()) + }) + } + + fn activate_subscription(&mut self, subscribe_id: SubscribeId) -> Result<()> { + if let Some(subscription) = self.subscriptions.get_mut(&subscribe_id) { + subscription.active(); + } + + Ok(()) + } + + fn is_requesting(&self, subscribe_id: SubscribeId) -> bool { + self.subscriptions + .get(&subscribe_id) + .map(|subscription| subscription.is_requesting()) + .unwrap_or(false) + } + + fn delete_subscription(&mut self, subscribe_id: SubscribeId) -> Result<()> { + self.subscriptions.remove(&subscribe_id); + Ok(()) + } + + fn is_within_max_subscribe_id(&self, subscribe_id: SubscribeId) -> bool { + subscribe_id <= self.max_subscriber_id + } + + fn is_subscribe_id_unique(&self, subscribe_id: SubscribeId) -> bool { + !self.subscriptions.contains_key(&subscribe_id) + } + + fn is_track_alias_unique(&self, track_alias: TrackAlias) -> bool { + !self + .subscriptions + .values() + .any(|subscription| subscription.get_track_alias() == track_alias) + } + fn find_unused_subscribe_id_and_track_alias(&self) -> Result<(SubscribeId, TrackAlias)> { + for subscribe_id in 1..=self.max_subscriber_id { + if !self.subscriptions.contains_key(&subscribe_id) { + for track_alias in 0.. { + if !self + .subscriptions + .values() + .any(|subscription| subscription.get_track_alias() == track_alias) + { + return Ok((subscribe_id, track_alias)); + } + } + } + } + + bail!("No available subscribe_id and track_alias."); + } + + fn set_namespace(&mut self, namespace: TrackNamespace) -> Result<()> { + if self.announced_namespaces.contains(&namespace) { + bail!("Namespace already exists."); + } + + self.announced_namespaces.push(namespace); + + Ok(()) + } + + fn get_namespaces(&self) -> Result<&Vec> { + Ok(&self.announced_namespaces) + } + + fn has_namespace(&self, namespace: TrackNamespace) -> bool { + self.announced_namespaces.contains(&namespace) + } + + fn delete_namespace(&mut self, namespace: TrackNamespace) -> Result<()> { + if let Some(index) = self + .announced_namespaces + .iter() + .position(|x| x == &namespace) + { + self.announced_namespaces.remove(index); + } + + Ok(()) + } + fn set_namespace_prefix(&mut self, namespace_prefix: TrackNamespace) -> Result<()> { + if self + .subscribing_namespace_prefixes + .contains(&namespace_prefix) + { + bail!("Namespace prefix already exists."); + } + + self.subscribing_namespace_prefixes.push(namespace_prefix); + + Ok(()) + } + + fn get_namespace_prefixes(&self) -> Result<&Vec> { + Ok(&self.subscribing_namespace_prefixes) + } + + fn delete_namespace_prefix(&mut self, namespace_prefix: TrackNamespace) -> Result<()> { + if let Some(index) = self + .subscribing_namespace_prefixes + .iter() + .position(|x| x == &namespace_prefix) + { + self.subscribing_namespace_prefixes.remove(index); + } + + Ok(()) + } +} + +impl SubscriptionNodeRegistory for Producer { + fn set_subscription( + &mut self, + subscribe_id: SubscribeId, + track_alias: u64, + track_namespace: Vec, + track_name: String, + subscriber_priority: u8, + group_order: GroupOrder, + filter_type: FilterType, + start_group: Option, + start_object: Option, + end_group: Option, + end_object: Option, + ) -> Result<()> { + // Publisher can define forwarding preference when it publishes track. + let subscription = Subscription::new( + track_alias, + track_namespace, + track_name, + subscriber_priority, + group_order, + filter_type, + start_group, + start_object, + end_group, + end_object, + Some(ForwardingPreference::Track), + ); + + self.subscriptions.insert(subscribe_id, subscription); + + Ok(()) + } + + fn get_subscription(&self, subscribe_id: SubscribeId) -> Result> { + Ok(self.subscriptions.get(&subscribe_id).cloned()) + } + + fn get_subscription_by_full_track_name( + &self, + track_namespace: TrackNamespace, + track_name: String, + ) -> Result> { + Ok(self + .subscriptions + .values() + .find(|subscription| { + subscription.get_track_namespace_and_name() + == (track_namespace.clone(), track_name.clone()) + }) + .cloned()) + } + + fn get_subscribe_id( + &self, + track_namespace: TrackNamespace, + track_name: String, + ) -> Result> { + Ok(self + .subscriptions + .iter() + .find(|(_, subscription)| { + subscription.get_track_namespace_and_name() + == (track_namespace.clone(), track_name.clone()) + }) + .map(|(subscribe_id, _)| *subscribe_id)) + } + + fn has_track(&self, track_namespace: TrackNamespace, track_name: String) -> bool { + self.subscriptions.values().any(|subscription| { + subscription.get_track_namespace_and_name() + == (track_namespace.clone(), track_name.clone()) + }) + } + + fn activate_subscription(&mut self, subscribe_id: SubscribeId) -> Result<()> { + if let Some(subscription) = self.subscriptions.get_mut(&subscribe_id) { + subscription.active(); + } + + Ok(()) + } + + fn is_requesting(&self, subscribe_id: SubscribeId) -> bool { + self.subscriptions + .get(&subscribe_id) + .map(|subscription| subscription.is_requesting()) + .unwrap_or(false) + } + + fn delete_subscription(&mut self, subscribe_id: SubscribeId) -> Result<()> { + self.subscriptions.remove(&subscribe_id); + Ok(()) + } + + fn is_within_max_subscribe_id(&self, subscribe_id: SubscribeId) -> bool { + subscribe_id <= self.max_subscriber_id + } + + fn is_subscribe_id_unique(&self, subscribe_id: SubscribeId) -> bool { + !self.subscriptions.contains_key(&subscribe_id) + } + + fn is_track_alias_unique(&self, track_alias: TrackAlias) -> bool { + !self + .subscriptions + .values() + .any(|subscription| subscription.get_track_alias() == track_alias) + } + + fn find_unused_subscribe_id_and_track_alias(&self) -> Result<(SubscribeId, TrackAlias)> { + unimplemented!() + } + + fn set_namespace(&mut self, namespace: TrackNamespace) -> Result<()> { + if self.announcing_namespaces.contains(&namespace) { + bail!("Namespace already exists."); + } + + self.announcing_namespaces.push(namespace); + + Ok(()) + } + + fn get_namespaces(&self) -> Result<&Vec> { + Ok(&self.announcing_namespaces) + } + + fn has_namespace(&self, namespace: TrackNamespace) -> bool { + self.announcing_namespaces.contains(&namespace) + } + + fn delete_namespace(&mut self, namespace: TrackNamespace) -> Result<()> { + if let Some(index) = self + .announcing_namespaces + .iter() + .position(|x| x == &namespace) + { + self.announcing_namespaces.remove(index); + } + + Ok(()) + } + + fn set_namespace_prefix(&mut self, namespace_prefix: TrackNamespace) -> Result<()> { + if self + .subscribed_namespace_prefixes + .contains(&namespace_prefix) + { + bail!("Namespace prefix already exists."); + } + + self.subscribed_namespace_prefixes.push(namespace_prefix); + + Ok(()) + } + + fn get_namespace_prefixes(&self) -> Result<&Vec> { + Ok(&self.subscribed_namespace_prefixes) + } + + fn delete_namespace_prefix(&mut self, namespace_prefix: TrackNamespace) -> Result<()> { + if let Some(index) = self + .subscribed_namespace_prefixes + .iter() + .position(|x| x == &namespace_prefix) + { + self.subscribed_namespace_prefixes.remove(index); + } + + Ok(()) + } +} diff --git a/moqt-core/src/modules/subscription_models/subscriptions.rs b/moqt-core/src/modules/subscription_models/subscriptions.rs new file mode 100644 index 00000000..452c0c90 --- /dev/null +++ b/moqt-core/src/modules/subscription_models/subscriptions.rs @@ -0,0 +1,82 @@ +use crate::messages::control_messages::subscribe::{FilterType, GroupOrder}; +use crate::subscription_models::tracks::ForwardingPreference; +use crate::subscription_models::tracks::Track; + +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +enum Status { + Requesting, + Active, +} + +#[derive(Debug, Clone)] +pub struct Subscription { + track: Track, + priority: u8, + group_order: GroupOrder, + filter_type: FilterType, + start_group: Option, + start_object: Option, + end_group: Option, + end_object: Option, + subscription_status: Status, +} + +impl Subscription { + #[allow(clippy::too_many_arguments)] + pub fn new( + track_alias: u64, + track_namespace: Vec, + track_name: String, + priority: u8, + group_order: GroupOrder, + filter_type: FilterType, + start_group: Option, + start_object: Option, + end_group: Option, + end_object: Option, + forwarding_preference: Option, + ) -> Self { + let track = Track::new( + track_alias, + track_namespace, + track_name, + forwarding_preference, + ); + + Self { + track, + priority, + group_order, + filter_type, + start_group, + start_object, + end_group, + end_object, + subscription_status: Status::Requesting, + } + } + + pub fn active(&mut self) { + self.subscription_status = Status::Active; + } + + pub fn is_active(&self) -> bool { + self.subscription_status == Status::Active + } + + pub fn is_requesting(&self) -> bool { + self.subscription_status == Status::Requesting + } + + pub fn set_forwarding_preference(&mut self, forwarding_preference: ForwardingPreference) { + self.track.set_forwarding_preference(forwarding_preference); + } + + pub fn get_track_namespace_and_name(&self) -> (Vec, String) { + self.track.get_track_namespace_and_name() + } + + pub fn get_track_alias(&self) -> u64 { + self.track.get_track_alias() + } +} diff --git a/moqt-core/src/modules/subscription_models/tracks.rs b/moqt-core/src/modules/subscription_models/tracks.rs new file mode 100644 index 00000000..2911d77c --- /dev/null +++ b/moqt-core/src/modules/subscription_models/tracks.rs @@ -0,0 +1,42 @@ +#[derive(Debug, Clone)] +pub enum ForwardingPreference { + Datagram, + Track, + Subgroup, +} + +#[derive(Debug, Clone)] +pub struct Track { + track_alias: u64, + track_namespace: Vec, + track_name: String, + forwarding_preference: Option, +} + +impl Track { + pub fn new( + track_alias: u64, + track_namespace: Vec, + track_name: String, + forwarding_preference: Option, + ) -> Self { + Self { + track_alias, + track_namespace, + track_name, + forwarding_preference, + } + } + + pub fn set_forwarding_preference(&mut self, forwarding_preference: ForwardingPreference) { + self.forwarding_preference = Some(forwarding_preference); + } + + pub fn get_track_namespace_and_name(&self) -> (Vec, String) { + (self.track_namespace.clone(), self.track_name.to_string()) + } + + pub fn get_track_alias(&self) -> u64 { + self.track_alias + } +} diff --git a/moqt-core/src/modules/track_namespace_manager_repository.rs b/moqt-core/src/modules/track_namespace_manager_repository.rs index ae905617..51fa81f9 100644 --- a/moqt-core/src/modules/track_namespace_manager_repository.rs +++ b/moqt-core/src/modules/track_namespace_manager_repository.rs @@ -1,47 +1,109 @@ +use crate::messages::control_messages::subscribe::{FilterType, GroupOrder}; +use crate::subscription_models::subscriptions::Subscription; use anyhow::Result; use async_trait::async_trait; #[async_trait] pub trait TrackNamespaceManagerRepository: Send + Sync { - async fn set_publisher(&self, track_namespace: Vec, session_id: usize) -> Result<()>; - async fn delete_publisher_by_namespace(&self, track_namespace: Vec) -> Result<()>; - async fn delete_publisher_by_session_id(&self, publisher_session_id: usize) -> Result<()>; - async fn has_track_namespace(&self, track_namespace: Vec) -> bool; - async fn has_track_name(&self, track_namespace: Vec, track_name: &str) -> bool; - async fn get_publisher_session_id_by_track_namespace( + async fn setup_publisher( &self, - track_namespace: Vec, - ) -> Option; - async fn set_subscriber( + max_subscribe_id: u64, + publisher_session_id: usize, + ) -> Result<()>; + async fn set_publisher_announced_namespace( &self, track_namespace: Vec, + publisher_session_id: usize, + ) -> Result<()>; + async fn setup_subscriber( + &self, + max_subscribe_id: u64, subscriber_session_id: usize, - track_name: &str, ) -> Result<()>; - async fn delete_subscriber( + async fn is_valid_subscriber_subscribe_id( + &self, + subscribe_id: u64, + subscriber_session_id: usize, + ) -> Result; + async fn is_valid_subscriber_track_alias( + &self, + track_alias: u64, + subscriber_session_id: usize, + ) -> Result; + async fn is_track_existing( &self, track_namespace: Vec, - track_name: &str, + track_name: String, + ) -> Result; + async fn get_publisher_subscription_by_full_track_name( + &self, + track_namespace: Vec, + track_name: String, + ) -> Result>; + async fn get_publisher_session_id(&self, track_namespace: Vec) + -> Result>; + async fn get_requesting_subscriber_session_ids_and_subscribe_ids( + &self, + published_subscribe_id: u64, + publisher_session_id: usize, + ) -> Result>>; + async fn get_publisher_subscribe_id( + &self, + track_namespace: Vec, + track_name: String, + publisher_session_id: usize, + ) -> Result>; + #[allow(clippy::too_many_arguments)] + async fn set_subscriber_subscription( + &self, subscriber_session_id: usize, + subscribe_id: u64, + track_alias: u64, + track_namespace: Vec, + track_name: String, + subscriber_priority: u8, + group_order: GroupOrder, + filter_type: FilterType, + start_group: Option, + start_object: Option, + end_group: Option, + end_object: Option, ) -> Result<()>; - async fn delete_subscribers_by_session_id(&self, subscriber_session_id: usize) -> Result<()>; - async fn set_track_id( + #[allow(clippy::too_many_arguments)] + async fn set_publisher_subscription( &self, + publisher_session_id: usize, track_namespace: Vec, - track_name: &str, - track_id: u64, + track_name: String, + subscriber_priority: u8, + group_order: GroupOrder, + filter_type: FilterType, + start_group: Option, + start_object: Option, + end_group: Option, + end_object: Option, + ) -> Result<(u64, u64)>; + async fn register_pubsup_relation( + &self, + publisher_session_id: usize, + published_subscribe_id: u64, + subscriber_session_id: usize, + subscribed_subscribe_id: u64, ) -> Result<()>; - async fn activate_subscriber( + async fn activate_subscriber_subscription( &self, - track_namespace: Vec, - track_name: &str, subscriber_session_id: usize, + subscribe_id: u64, ) -> Result<()>; - async fn get_subscriber_session_ids_by_track_namespace_and_track_name( + async fn activate_publisher_subscription( + &self, + publisher_session_id: usize, + subscribe_id: u64, + ) -> Result<()>; + async fn delete_publisher_announced_namespace( &self, track_namespace: Vec, - track_name: &str, - ) -> Option>; - async fn get_subscriber_session_ids_by_track_id(&self, track_id: u64) -> Option>; + publisher_session_id: usize, + ) -> Result<()>; async fn delete_client(&self, session_id: usize) -> Result<()>; } diff --git a/moqt-server/src/modules/track_namespace_manager.rs b/moqt-server/src/modules/track_namespace_manager.rs index 2bfaa550..7a162f94 100644 --- a/moqt-server/src/modules/track_namespace_manager.rs +++ b/moqt-server/src/modules/track_namespace_manager.rs @@ -1,528 +1,348 @@ -use std::collections::HashMap; - use anyhow::{bail, Result}; use async_trait::async_trait; +use moqt_core::messages::control_messages::subscribe::{self, FilterType, GroupOrder}; +use moqt_core::subscription_models::subscription_nodes::SubscriptionNodeRegistory; +use moqt_core::subscription_models::subscription_nodes::{Consumer, Producer}; +use moqt_core::subscription_models::subscriptions::Subscription; use moqt_core::TrackNamespaceManagerRepository; +use std::collections::HashMap; use tokio::sync::{mpsc, oneshot}; use TrackCommand::*; type SubscriberSessionId = usize; +type PublisherSessionId = usize; +type PublishedSubscribeId = u64; +type SubscribedSubscribeId = u64; type TrackName = String; type TrackNamespace = Vec; - -#[derive(Debug, PartialEq, Clone)] -pub enum SubscriberStatus { - Waiting, - Activate, -} - -#[derive(Debug)] -struct SubscriberObject { - state: SubscriberStatus, -} - -impl SubscriberObject { - fn new() -> Self { - Self { - state: SubscriberStatus::Waiting, - } - } - - fn set_state(&mut self, state: SubscriberStatus) { - self.state = state; - } - - fn is_active(&self) -> bool { - self.state == SubscriberStatus::Activate - } - - fn is_waiting(&self) -> bool { - self.state == SubscriberStatus::Waiting - } +struct PubSubRelation { + records: HashMap< + (PublisherSessionId, PublishedSubscribeId), + Vec<(SubscriberSessionId, SubscribedSubscribeId)>, + >, } -#[derive(Debug)] -struct TrackNameObject { - track_id: Option, - subscribers: HashMap, -} - -impl TrackNameObject { +impl PubSubRelation { fn new() -> Self { Self { - track_id: Option::None, - subscribers: HashMap::new(), + records: HashMap::new(), } } - fn set_track_id(&mut self, track_id: u64) { - self.track_id = Some(track_id); - } - - fn is_exist_subscriber(&self, subscriber_session_id: usize) -> bool { - self.subscribers.contains_key(&subscriber_session_id) - } - fn is_subscriber_empty(&self) -> bool { - self.subscribers.is_empty() - } - - fn set_subscriber(&mut self, subscriber_session_id: usize) { - self.subscribers - .insert(subscriber_session_id, SubscriberObject::new()); - } - - fn delete_subscriber(&mut self, subscriber_session_id: usize) { - self.subscribers.remove(&subscriber_session_id); - } -} - -#[derive(Debug)] -struct TrackNamespaceObject { - publisher_session_id: usize, - tracks: HashMap, -} - -impl TrackNamespaceObject { - fn new(publisher_session_id: usize) -> Self { - Self { - publisher_session_id, - tracks: HashMap::new(), - } - } - - fn is_exist_track_name(&self, track_name: String) -> bool { - self.tracks.contains_key(&track_name) - } - - fn set_track(&mut self, track_name: String) { - self.tracks.insert(track_name, TrackNameObject::new()); - } - - fn delete_track(&mut self, track_name: String) { - self.tracks.remove(&track_name); - } - - fn delete_subscriber_from_all_tracks(&mut self, subscriber_session_id: SubscriberSessionId) { - for track in self.tracks.values_mut() { - track.delete_subscriber(subscriber_session_id); - } - } -} - -#[derive(Debug)] -struct TrackNamespaces { - publishers: HashMap, -} - -impl TrackNamespaces { - fn new() -> Self { - Self { - publishers: HashMap::new(), - } - } - - fn is_exist_track_namespace(&self, track_namespace: Vec) -> bool { - self.publishers.contains_key(&track_namespace) - } - - fn is_exist_track_name(&mut self, track_namespace: Vec, track_name: String) -> bool { - if !self.is_exist_track_namespace(track_namespace.clone()) { - return false; - } - let track_namespace_object = self.publishers.get_mut(&track_namespace).unwrap(); - - track_namespace_object.is_exist_track_name(track_name) - } - - fn set_publisher( - &mut self, - track_namespace: Vec, - publisher_session_id: usize, - ) -> Result<()> { - if self.is_exist_track_namespace(track_namespace.clone()) { - bail!("already exist"); - } - - let publisher = TrackNamespaceObject::new(publisher_session_id); - self.publishers.insert(track_namespace, publisher); - Ok(()) - } - - fn delete_publisher_by_namespace(&mut self, track_namespace: Vec) -> Result<()> { - if !self.is_exist_track_namespace(track_namespace.clone()) { - bail!("not found"); - } - - self.publishers.remove(&track_namespace); - Ok(()) - } - - fn delete_publisher_by_session_id(&mut self, publisher_session_id: usize) -> Result<()> { - // Retain elements other than the specified publisher_session_id - // = Delete the element specified publisher_session_id - self.publishers - .retain(|_, namespace| namespace.publisher_session_id != publisher_session_id); - Ok(()) - } - - fn get_publisher_session_id_by_track_namespace( - &self, - track_namespace: Vec, - ) -> Option { - self.publishers - .get(&track_namespace) - .map(|p| p.publisher_session_id) - } - - fn set_subscriber( - &mut self, - track_namespace: Vec, - subscriber_session_id: usize, - track_name: String, - ) -> Result<()> { - if !self.is_exist_track_namespace(track_namespace.clone()) { - bail!("track_namespace not found"); - } - let track_namespace_object = self.publishers.get_mut(&track_namespace).unwrap(); - - if track_namespace_object.is_exist_track_name(track_name.clone()) { - let track_name_object = track_namespace_object.tracks.get_mut(&track_name).unwrap(); - if track_name_object.is_exist_subscriber(subscriber_session_id) { - bail!("already exist"); - } - track_name_object.set_subscriber(subscriber_session_id); - - Ok(()) - } else { - track_namespace_object.set_track(track_name.clone()); - let new_track_name_object = track_namespace_object.tracks.get_mut(&track_name).unwrap(); - new_track_name_object.set_subscriber(subscriber_session_id); - - Ok(()) - } - } - - fn delete_subscriber( - &mut self, - track_namespace: Vec, - track_name: String, - subscriber_session_id: usize, - ) -> Result<()> { - if !self.is_exist_track_namespace(track_namespace.clone()) { - bail!("track_namespace not found"); - } - let track_namespace_object = self.publishers.get_mut(&track_namespace).unwrap(); - - if !track_namespace_object.is_exist_track_name(track_name.clone()) { - bail!("track_name not found"); - } - let track_name_object = track_namespace_object.tracks.get_mut(&track_name).unwrap(); - - if !track_name_object.is_exist_subscriber(subscriber_session_id) { - bail!("subscriber not found"); - } - track_name_object.delete_subscriber(subscriber_session_id); - - // Delete track with no subscribers - if track_name_object.is_subscriber_empty() { - track_namespace_object.delete_track(track_name); - } - - Ok(()) - } - - fn delete_subscriber_from_all_publishers( + fn add_relation( &mut self, + publisher_session_id: PublisherSessionId, + published_subscribe_id: PublishedSubscribeId, subscriber_session_id: SubscriberSessionId, - ) -> Result<()> { - for track_namespace_object in self.publishers.values_mut() { - track_namespace_object.delete_subscriber_from_all_tracks(subscriber_session_id); - - // Delete track with no subscribers - let mut empty_tracks = Vec::new(); - for (track_name, track_name_object) in track_namespace_object.tracks.iter_mut() { - if track_name_object.is_subscriber_empty() { - empty_tracks.push(track_name.to_string()); - } + subscribed_subscribe_id: SubscribedSubscribeId, + ) { + let key = (publisher_session_id, published_subscribe_id); + let value = (subscriber_session_id, subscribed_subscribe_id); + + match self.records.get_mut(&key) { + // If the key exists, add the value to the existing vector + Some(subscribers) => { + subscribers.push(value); } - for track_name in empty_tracks { - track_namespace_object.delete_track(track_name); + // If the key does not exist, create a new vector and insert the value + None => { + self.records.insert(key, vec![value]); } } - - Ok(()) - } - - fn get_subscriber_session_ids_by_track_namespace_and_track_name( - &mut self, - track_namespace: Vec, - track_name: String, - ) -> Option> { - if !self.is_exist_track_namespace(track_namespace.clone()) { - return None; - } - let track_namespace_object = self.publishers.get_mut(&track_namespace).unwrap(); - if !track_namespace_object.is_exist_track_name(track_name.clone()) { - return None; - } - - let track_name_object = track_namespace_object.tracks.get_mut(&track_name).unwrap(); - - let waiting_subscribers = track_name_object - .subscribers - .iter() - .filter(|(_, status)| status.is_waiting()); - - let waiting_subscriber_session_ids: Vec = waiting_subscribers - .map(|(session_id, _)| *session_id) - .collect(); - - if waiting_subscriber_session_ids.is_empty() { - return None; - } - - Some(waiting_subscriber_session_ids) - } - - fn get_subscriber_session_ids_by_track_id(&self, track_id: u64) -> Option> { - let track = self - .publishers - .values() - .flat_map(|publisher| publisher.tracks.values()) - .find(|track| track.track_id == Some(track_id))?; - - let active_subscribers = track - .subscribers - .iter() - .filter(|(_, status)| status.is_active()); - - let active_subscriber_session_ids: Vec = active_subscribers - .map(|(session_id, _)| *session_id) - .collect(); - - if active_subscriber_session_ids.is_empty() { - return None; - } - Some(active_subscriber_session_ids) } - fn set_track_id( - &mut self, - track_namespace: Vec, - track_name: String, - track_id: u64, - ) -> Result<()> { - if !self.is_exist_track_namespace(track_namespace.clone()) { - bail!("track_namespace not found"); - } - - let track_namespace_object = self.publishers.get_mut(&track_namespace).unwrap(); - if !track_namespace_object.is_exist_track_name(track_name.clone()) { - bail!("track_name not found"); - } - - let track_name_object = track_namespace_object.tracks.get_mut(&track_name).unwrap(); - track_name_object.set_track_id(track_id); - - Ok(()) + fn get_subscribers( + &self, + publisher_session_id: PublisherSessionId, + published_subscribe_id: PublishedSubscribeId, + ) -> Option<&Vec<(SubscriberSessionId, SubscribedSubscribeId)>> { + let key = (publisher_session_id, published_subscribe_id); + self.records.get(&key) } - fn set_status( + fn delete_relarion( &mut self, - track_namespace: Vec, - track_name: String, - subscriber_session_id: usize, - status: SubscriberStatus, - ) -> Result<()> { - if !self.is_exist_track_namespace(track_namespace.clone()) { - bail!("track_namespace not found"); - } - let track_namespace_object = self.publishers.get_mut(&track_namespace).unwrap(); - if !track_namespace_object.is_exist_track_name(track_name.clone()) { - bail!("track_name not found"); + publisher_session_id: PublisherSessionId, + published_subscribe_id: PublishedSubscribeId, + subscriber_session_id: SubscriberSessionId, + subscribed_subscribe_id: SubscribedSubscribeId, + ) { + let key = (publisher_session_id, published_subscribe_id); + if let Some(subscribers) = self.records.get_mut(&key) { + subscribers.retain(|(session_id, subscribe_id)| { + *session_id != subscriber_session_id && *subscribe_id != subscribed_subscribe_id + }); } - - let track_name_object = track_namespace_object.tracks.get_mut(&track_name).unwrap(); - let subscriber = track_name_object - .subscribers - .get_mut(&subscriber_session_id) - .unwrap(); - subscriber.set_state(status.clone()); - - Ok(()) } } +// [Original Publisher: (Producer) ] -> [Relay: (Consumer) - - (Producer) ] -> [End Subscriber: (Consumer)] + // Called as a separate thread pub(crate) async fn track_namespace_manager(rx: &mut mpsc::Receiver) { tracing::trace!("track_namespace_manager start"); - // TrackNamespaces - // { - // "publishers": { - // "${track_namespace}": { - // "publisher_session_id": "usize", - // "tracks": { - // "${track_name}": { - // "track_id": "Option", - // "subscribers": { - // "${subscriber_session_id}": { - // "state": "SubscriberStatus" - // } - // } - // } - // } - // } - // } - // } - let mut namespaces: TrackNamespaces = TrackNamespaces::new(); + let mut consumers: HashMap = HashMap::new(); + let mut producers: HashMap = HashMap::new(); + let mut pubsub_relation = PubSubRelation::new(); while let Some(cmd) = rx.recv().await { tracing::debug!("command received: {:#?}", cmd); match cmd { - SetPublisher { - track_namespace, + SetupPublisher { + max_subscribe_id, publisher_session_id, resp, - } => match namespaces.set_publisher(track_namespace, publisher_session_id) { - Ok(_) => resp.send(true).unwrap(), - Err(err) => { - tracing::error!("set_publisher: err: {:?}", err.to_string()); - resp.send(false).unwrap(); - } - }, - DeletePublisherByNamespace { + } => { + consumers.insert(publisher_session_id, Consumer::new(max_subscribe_id)); + resp.send(true).unwrap(); + } + SetPublisherAnnouncedNamespace { track_namespace, + publisher_session_id, resp, - } => match namespaces.delete_publisher_by_namespace(track_namespace) { - Ok(_) => resp.send(true).unwrap(), - Err(err) => { - tracing::error!("delete_publisher_by_namespace: err: {:?}", err.to_string()); - resp.send(false).unwrap(); + } => { + let consumer = consumers.get_mut(&publisher_session_id).unwrap(); + match consumer.set_namespace(track_namespace) { + Ok(_) => { + resp.send(true).unwrap(); + } + Err(err) => { + tracing::error!("set_namespace: err: {:?}", err.to_string()); + resp.send(false).unwrap(); + } } - }, - DeletePublisherBySessionId { - publisher_session_id, + } + SetupSubscriber { + max_subscribe_id, + subscriber_session_id, + resp, + } => { + producers.insert(subscriber_session_id, Producer::new(max_subscribe_id)); + resp.send(true).unwrap(); + } + IsValidSubscriberSubscribeId { + subscribe_id, + subscriber_session_id, resp, - } => match namespaces.delete_publisher_by_session_id(publisher_session_id) { - Ok(_) => resp.send(true).unwrap(), - Err(err) => { - tracing::error!("delete_publisher_by_session_id: err: {:?}", err.to_string()); + } => { + let producer = producers.get(&subscriber_session_id).unwrap(); + + if producer.is_within_max_subscribe_id(subscribe_id) + && producer.is_subscribe_id_unique(subscribe_id) + { + resp.send(true).unwrap(); + } else { resp.send(false).unwrap(); } - }, - HasTrackNamespace { - track_namespace, + } + IsValidSubscriberTrackAlias { + track_alias, + subscriber_session_id, resp, } => { - let result = namespaces.is_exist_track_namespace(track_namespace); - resp.send(result).unwrap(); + let producer = producers.get(&subscriber_session_id).unwrap(); + let is_valid = producer.is_track_alias_unique(track_alias); + resp.send(is_valid).unwrap(); } - HasTrackName { + GetPublisherSessionId { track_namespace, - track_name, resp, } => { - let result = namespaces.is_exist_track_name(track_namespace, track_name); - resp.send(result).unwrap(); + // Find the publisher that has the track namespace from all consumers + let publisher_session_id = consumers + .iter() + .find(|(_, consumer)| consumer.has_namespace(track_namespace.clone())) + .map(|(session_id, _)| *session_id); + resp.send(publisher_session_id).unwrap(); } - GetPublisherSessionId { - track_namespace, + GetRequestingSubscriberSessionIdsAndSubscribeIds { + published_subscribe_id, + publisher_session_id, resp, } => { - let result = - namespaces.get_publisher_session_id_by_track_namespace(track_namespace); - resp.send(result).unwrap(); + let subscribers = + pubsub_relation.get_subscribers(publisher_session_id, published_subscribe_id); + + resp.send(subscribers.cloned()).unwrap(); } - SetSubscliber { + GetPublisherSubscribeId { track_namespace, - subscriber_session_id, track_name, + publisher_session_id, resp, } => { - match namespaces.set_subscriber(track_namespace, subscriber_session_id, track_name) - { - Ok(_) => resp.send(true).unwrap(), - Err(err) => { - tracing::error!("set_subscriber: err: {:?}", err.to_string()); - resp.send(false).unwrap(); - } + let consumer = consumers.get_mut(&publisher_session_id).unwrap(); + let result = consumer.get_subscribe_id(track_namespace, track_name); + + match result { + Ok(subscribe_id) => resp.send(subscribe_id).unwrap(), + Err(_) => resp.send(None).unwrap(), } } - DeleteSubscliber { + IsTrackExisting { track_namespace, track_name, - subscriber_session_id, resp, - } => match namespaces.delete_subscriber( + } => { + let consumer = consumers.iter().find(|(_, consumer)| { + consumer.has_track(track_namespace.clone(), track_name.clone()) + }); + let is_existing = consumer.is_some(); + resp.send(is_existing).unwrap(); + } + GetPublisherSubscription { track_namespace, track_name, - subscriber_session_id, - ) { - Ok(_) => resp.send(true).unwrap(), - Err(err) => { - tracing::error!("delete_subscriber: err: {:?}", err.to_string()); - resp.send(false).unwrap(); - } - }, - DeleteSubscliberBySessionId { - subscriber_session_id, resp, - } => match namespaces.delete_subscriber_from_all_publishers(subscriber_session_id) { - Ok(_) => resp.send(true).unwrap(), - Err(err) => { - tracing::error!( - "delete_subscriber_from_all_publishers: err: {:?}", - err.to_string() - ); - resp.send(false).unwrap(); + } => { + let consumer = consumers.iter().find(|(_, consumer)| { + consumer.has_track(track_namespace.clone(), track_name.clone()) + }); + let result = consumer + .map(|(_, consumer)| { + consumer.get_subscription_by_full_track_name(track_namespace, track_name) + }) + .unwrap(); + + match result { + Ok(subscription) => resp.send(subscription).unwrap(), + Err(_) => resp.send(None).unwrap(), } - }, - SetTrackId { + } + SetSubscriberSubscription { + subscriber_session_id, + subscribe_id, + track_alias, track_namespace, track_name, - track_id, + subscriber_priority, + group_order, + filter_type, + start_group, + start_object, + end_group, + end_object, resp, - } => match namespaces.set_track_id(track_namespace, track_name, track_id) { - Ok(_) => resp.send(true).unwrap(), - Err(err) => { - tracing::error!("set_track_id: err: {:?}", err.to_string()); - resp.send(false).unwrap(); + } => { + let producer = producers.get_mut(&subscriber_session_id).unwrap(); + match producer.set_subscription( + subscribe_id, + track_alias, + track_namespace, + track_name, + subscriber_priority, + group_order, + filter_type, + start_group, + start_object, + end_group, + end_object, + ) { + Ok(_) => resp.send(true).unwrap(), + Err(err) => { + tracing::error!("set_subscriber_subscription: err: {:?}", err.to_string()); + resp.send(false).unwrap(); + } } - }, - SetStatus { + } + SetPublisherSubscription { + publisher_session_id, track_namespace, track_name, + subscriber_priority, + group_order, + filter_type, + start_group, + start_object, + end_group, + end_object, + resp, + } => { + let consumer = consumers.get_mut(&publisher_session_id).unwrap(); + let (subscribe_id, track_alias) = + consumer.find_unused_subscribe_id_and_track_alias().unwrap(); + match consumer.set_subscription( + subscribe_id, + track_alias, + track_namespace, + track_name, + subscriber_priority, + group_order, + filter_type, + start_group, + start_object, + end_group, + end_object, + ) { + Ok(_) => resp.send((subscribe_id, track_alias)).unwrap(), + Err(err) => { + tracing::error!("set_publisher_subscription: err: {:?}", err.to_string()); + resp.send((0, 0)).unwrap(); + continue; + } + }; + } + RegisterPubSupRelation { + publisher_session_id, + published_subscribe_id, subscriber_session_id, - status, + subscribed_subscribe_id, resp, - } => match namespaces.set_status( - track_namespace, - track_name, + } => { + pubsub_relation.add_relation( + publisher_session_id, + published_subscribe_id, + subscriber_session_id, + subscribed_subscribe_id, + ); + + resp.send(true).unwrap(); + } + ActivateSubscriberSubscription { subscriber_session_id, - status, - ) { - Ok(_) => resp.send(true).unwrap(), - Err(err) => { - tracing::error!("set_status: err: {:?}", err.to_string()); - resp.send(false).unwrap(); - } - }, - GetSubscliberSessionIdsByNamespaceAndName { + subscribe_id, + resp, + } => { + let producer = producers.get_mut(&subscriber_session_id).unwrap(); + producer.activate_subscription(subscribe_id); + resp.send(true).unwrap(); + } + ActivatePublisherSubscription { + publisher_session_id, + subscribe_id, + resp, + } => { + let consumer = consumers.get_mut(&publisher_session_id).unwrap(); + consumer.activate_subscription(subscribe_id); + resp.send(true).unwrap(); + } + DeletePublisherAnnouncedNamespace { track_namespace, - track_name, + publisher_session_id, resp, } => { - let result = namespaces - .get_subscriber_session_ids_by_track_namespace_and_track_name( - track_namespace, - track_name, - ); - resp.send(result).unwrap(); + let consumer = consumers.get_mut(&publisher_session_id).unwrap(); + consumer.delete_namespace(track_namespace); + resp.send(true).unwrap(); } - GetSubscliberSessionIdsByTrackId { track_id, resp } => { - let result = namespaces.get_subscriber_session_ids_by_track_id(track_id); - resp.send(result).unwrap(); + DeleteClient { session_id, resp } => { + // Delete as a publisher + consumers.remove(&session_id); + pubsub_relation.records.retain(|key, _| key.0 != session_id); + + // Delete as a subscriber + producers.remove(&session_id); + pubsub_relation + .records + .iter_mut() + .for_each(|(_, subscribers)| { + subscribers.retain(|(subscriber_session_id, _)| { + *subscriber_session_id != session_id + }); + }); + + resp.send(true).unwrap(); } } } @@ -532,69 +352,109 @@ pub(crate) async fn track_namespace_manager(rx: &mut mpsc::Receiver, + SetupPublisher { + max_subscribe_id: u64, publisher_session_id: usize, resp: oneshot::Sender, }, - DeletePublisherByNamespace { + SetPublisherAnnouncedNamespace { track_namespace: Vec, + publisher_session_id: usize, resp: oneshot::Sender, }, - DeletePublisherBySessionId { - publisher_session_id: usize, + SetupSubscriber { + max_subscribe_id: u64, + subscriber_session_id: usize, resp: oneshot::Sender, }, - HasTrackNamespace { - track_namespace: Vec, + IsValidSubscriberSubscribeId { + subscribe_id: u64, + subscriber_session_id: usize, resp: oneshot::Sender, }, - HasTrackName { + IsValidSubscriberTrackAlias { + track_alias: u64, + subscriber_session_id: usize, + resp: oneshot::Sender, + }, + IsTrackExisting { track_namespace: Vec, track_name: String, resp: oneshot::Sender, }, + GetPublisherSubscription { + track_namespace: Vec, + track_name: String, + resp: oneshot::Sender>, + }, GetPublisherSessionId { track_namespace: Vec, resp: oneshot::Sender>, }, - SetSubscliber { + GetRequestingSubscriberSessionIdsAndSubscribeIds { + published_subscribe_id: u64, + publisher_session_id: usize, + resp: oneshot::Sender>>, + }, + GetPublisherSubscribeId { track_namespace: Vec, + track_name: String, + publisher_session_id: usize, + resp: oneshot::Sender>, + }, + SetSubscriberSubscription { subscriber_session_id: usize, + subscribe_id: u64, + track_alias: u64, + track_namespace: Vec, track_name: String, + subscriber_priority: u8, + group_order: GroupOrder, + filter_type: FilterType, + start_group: Option, + start_object: Option, + end_group: Option, + end_object: Option, resp: oneshot::Sender, }, - DeleteSubscliber { + SetPublisherSubscription { + publisher_session_id: usize, track_namespace: Vec, track_name: String, + subscriber_priority: u8, + group_order: GroupOrder, + filter_type: FilterType, + start_group: Option, + start_object: Option, + end_group: Option, + end_object: Option, + resp: oneshot::Sender<(u64, u64)>, + }, + RegisterPubSupRelation { + publisher_session_id: usize, + published_subscribe_id: u64, subscriber_session_id: usize, + subscribed_subscribe_id: u64, resp: oneshot::Sender, }, - DeleteSubscliberBySessionId { + ActivateSubscriberSubscription { subscriber_session_id: usize, + subscribe_id: u64, resp: oneshot::Sender, }, - SetTrackId { - track_namespace: Vec, - track_name: String, - track_id: u64, + ActivatePublisherSubscription { + publisher_session_id: usize, + subscribe_id: u64, resp: oneshot::Sender, }, - SetStatus { + DeletePublisherAnnouncedNamespace { track_namespace: Vec, - track_name: String, - subscriber_session_id: usize, - status: SubscriberStatus, + publisher_session_id: usize, resp: oneshot::Sender, }, - GetSubscliberSessionIdsByNamespaceAndName { - track_namespace: Vec, - track_name: String, - resp: oneshot::Sender>>, - }, - GetSubscliberSessionIdsByTrackId { - track_id: u64, - resp: oneshot::Sender>>, + DeleteClient { + session_id: usize, + resp: oneshot::Sender, }, } @@ -611,15 +471,15 @@ impl TrackNamespaceManager { #[async_trait] impl TrackNamespaceManagerRepository for TrackNamespaceManager { - async fn set_publisher( + async fn setup_publisher( &self, - track_namespace: Vec, + max_subscribe_id: u64, publisher_session_id: usize, ) -> Result<()> { let (resp_tx, resp_rx) = oneshot::channel::(); - let cmd = TrackCommand::SetPublisher { - track_namespace, + let cmd = TrackCommand::SetupPublisher { + max_subscribe_id, publisher_session_id, resp: resp_tx, }; @@ -632,12 +492,16 @@ impl TrackNamespaceManagerRepository for TrackNamespaceManager { false => bail!("already exist"), } } - - async fn delete_publisher_by_namespace(&self, track_namespace: Vec) -> Result<()> { + async fn set_publisher_announced_namespace( + &self, + track_namespace: Vec, + publisher_session_id: usize, + ) -> Result<()> { let (resp_tx, resp_rx) = oneshot::channel::(); - let cmd = TrackCommand::DeletePublisherByNamespace { + let cmd = TrackCommand::SetPublisherAnnouncedNamespace { track_namespace, + publisher_session_id, resp: resp_tx, }; self.tx.send(cmd).await.unwrap(); @@ -646,15 +510,19 @@ impl TrackNamespaceManagerRepository for TrackNamespaceManager { match result { true => Ok(()), - false => bail!("not found"), + false => bail!("already exist"), } } - - async fn delete_publisher_by_session_id(&self, publisher_session_id: usize) -> Result<()> { + async fn setup_subscriber( + &self, + max_subscribe_id: u64, + subscriber_session_id: usize, + ) -> Result<()> { let (resp_tx, resp_rx) = oneshot::channel::(); - let cmd = TrackCommand::DeletePublisherBySessionId { - publisher_session_id, + let cmd = TrackCommand::SetupSubscriber { + max_subscribe_id, + subscriber_session_id, resp: resp_tx, }; self.tx.send(cmd).await.unwrap(); @@ -663,41 +531,84 @@ impl TrackNamespaceManagerRepository for TrackNamespaceManager { match result { true => Ok(()), - false => bail!("unknown error"), + false => bail!("already exist"), } } - - async fn has_track_namespace(&self, track_namespace: Vec) -> bool { + async fn is_valid_subscriber_subscribe_id( + &self, + subscribe_id: u64, + subscriber_session_id: usize, + ) -> Result { let (resp_tx, resp_rx) = oneshot::channel::(); - - let cmd = TrackCommand::HasTrackNamespace { - track_namespace, + let cmd = TrackCommand::IsValidSubscriberSubscribeId { + subscribe_id, + subscriber_session_id, resp: resp_tx, }; self.tx.send(cmd).await.unwrap(); let result = resp_rx.await.unwrap(); - return result; - } - async fn has_track_name(&self, track_namespace: Vec, track_name: &str) -> bool { + return Ok(result); + } + async fn is_valid_subscriber_track_alias( + &self, + track_alias: u64, + subscriber_session_id: usize, + ) -> Result { let (resp_tx, resp_rx) = oneshot::channel::(); + let cmd = TrackCommand::IsValidSubscriberTrackAlias { + track_alias, + subscriber_session_id, + resp: resp_tx, + }; + self.tx.send(cmd).await.unwrap(); + + let result = resp_rx.await.unwrap(); - let cmd = TrackCommand::HasTrackName { + return Ok(result); + } + async fn is_track_existing( + &self, + track_namespace: Vec, + track_name: String, + ) -> Result { + let (resp_tx, resp_rx) = oneshot::channel::(); + let cmd = TrackCommand::IsTrackExisting { track_namespace, - track_name: track_name.to_string(), + track_name, resp: resp_tx, }; self.tx.send(cmd).await.unwrap(); let result = resp_rx.await.unwrap(); - return result; + + return Ok(result); } + async fn get_publisher_subscription_by_full_track_name( + &self, + track_namespace: Vec, + track_name: String, + ) -> Result> { + let (resp_tx, resp_rx) = oneshot::channel::>(); + let cmd = TrackCommand::GetPublisherSubscription { + track_namespace, + track_name, + resp: resp_tx, + }; + self.tx.send(cmd).await.unwrap(); + + let result = resp_rx.await; - async fn get_publisher_session_id_by_track_namespace( + match result { + Ok(subscription) => Ok(subscription), + Err(_) => bail!("not found"), + } + } + async fn get_publisher_session_id( &self, track_namespace: Vec, - ) -> Option { + ) -> Result> { let (resp_tx, resp_rx) = oneshot::channel::>(); let cmd = TrackCommand::GetPublisherSessionId { track_namespace, @@ -707,21 +618,73 @@ impl TrackNamespaceManagerRepository for TrackNamespaceManager { let session_id = resp_rx.await.unwrap(); - return session_id; + return Ok(session_id); } + async fn get_requesting_subscriber_session_ids_and_subscribe_ids( + &self, + published_subscribe_id: u64, + publisher_session_id: usize, + ) -> Result>> { + let (resp_tx, resp_rx) = oneshot::channel::>>(); + let cmd = TrackCommand::GetRequestingSubscriberSessionIdsAndSubscribeIds { + published_subscribe_id, + publisher_session_id, + resp: resp_tx, + }; + self.tx.send(cmd).await.unwrap(); + + let result = resp_rx.await.unwrap(); - async fn set_subscriber( + return Ok(result); + } + async fn get_publisher_subscribe_id( &self, track_namespace: Vec, + track_name: String, + publisher_session_id: usize, + ) -> Result> { + let (resp_tx, resp_rx) = oneshot::channel::>(); + let cmd = TrackCommand::GetPublisherSubscribeId { + track_namespace, + track_name, + publisher_session_id, + resp: resp_tx, + }; + self.tx.send(cmd).await.unwrap(); + + let subscribe_id = resp_rx.await.unwrap(); + + return Ok(subscribe_id); + } + async fn set_subscriber_subscription( + &self, subscriber_session_id: usize, - track_name: &str, + subscribe_id: u64, + track_alias: u64, + track_namespace: Vec, + track_name: String, + subscriber_priority: u8, + group_order: GroupOrder, + filter_type: FilterType, + start_group: Option, + start_object: Option, + end_group: Option, + end_object: Option, ) -> Result<()> { let (resp_tx, resp_rx) = oneshot::channel::(); - - let cmd = TrackCommand::SetSubscliber { - track_namespace, + let cmd = TrackCommand::SetSubscriberSubscription { subscriber_session_id, - track_name: track_name.to_string(), + subscribe_id, + track_alias, + track_namespace, + track_name, + subscriber_priority, + group_order, + filter_type, + start_group, + start_object, + end_group, + end_object, resp: resp_tx, }; self.tx.send(cmd).await.unwrap(); @@ -733,136 +696,134 @@ impl TrackNamespaceManagerRepository for TrackNamespaceManager { false => bail!("already exist"), } } - - async fn delete_subscriber( + #[allow(clippy::too_many_arguments)] + async fn set_publisher_subscription( &self, + publisher_session_id: usize, track_namespace: Vec, - track_name: &str, + track_name: String, + subscriber_priority: u8, + group_order: GroupOrder, + filter_type: FilterType, + start_group: Option, + start_object: Option, + end_group: Option, + end_object: Option, + ) -> Result<(u64, u64)> { + let (resp_tx, resp_rx) = oneshot::channel::<(u64, u64)>(); + let cmd = TrackCommand::SetPublisherSubscription { + publisher_session_id, + track_namespace, + track_name, + subscriber_priority, + group_order, + filter_type, + start_group, + start_object, + end_group, + end_object, + resp: resp_tx, + }; + self.tx.send(cmd).await.unwrap(); + + let (subscribe_id, track_alias) = resp_rx.await.unwrap(); + + Ok((subscribe_id, track_alias)) + } + async fn register_pubsup_relation( + &self, + publisher_session_id: usize, + published_subscribe_id: u64, subscriber_session_id: usize, + subscribed_subscribe_id: u64, ) -> Result<()> { let (resp_tx, resp_rx) = oneshot::channel::(); - - let cmd = TrackCommand::DeleteSubscliber { - track_namespace, - track_name: track_name.to_string(), + let cmd = RegisterPubSupRelation { + publisher_session_id, + published_subscribe_id, subscriber_session_id, + subscribed_subscribe_id, resp: resp_tx, }; self.tx.send(cmd).await.unwrap(); - let result = resp_rx.await.unwrap(); match result { true => Ok(()), - false => bail!("not found"), + false => bail!("failure"), } } - - async fn delete_subscribers_by_session_id(&self, subscriber_session_id: usize) -> Result<()> { + async fn activate_subscriber_subscription( + &self, + subscriber_session_id: usize, + subscribe_id: u64, + ) -> Result<()> { let (resp_tx, resp_rx) = oneshot::channel::(); - - let cmd = TrackCommand::DeleteSubscliberBySessionId { + let cmd = ActivateSubscriberSubscription { subscriber_session_id, + subscribe_id, resp: resp_tx, }; self.tx.send(cmd).await.unwrap(); - let result = resp_rx.await.unwrap(); match result { true => Ok(()), - false => bail!("unknown error"), + false => bail!("failure"), } } - - async fn set_track_id( + async fn activate_publisher_subscription( &self, - track_namespace: Vec, - track_name: &str, - track_id: u64, + publisher_session_id: usize, + subscribe_id: u64, ) -> Result<()> { let (resp_tx, resp_rx) = oneshot::channel::(); - - let cmd = TrackCommand::SetTrackId { - track_namespace, - track_name: track_name.to_string(), - track_id, + let cmd = ActivatePublisherSubscription { + publisher_session_id, + subscribe_id, resp: resp_tx, }; self.tx.send(cmd).await.unwrap(); - let result = resp_rx.await.unwrap(); match result { true => Ok(()), - false => bail!("not found"), + false => bail!("failure"), } } - - async fn get_subscriber_session_ids_by_track_namespace_and_track_name( + async fn delete_publisher_announced_namespace( &self, track_namespace: Vec, - track_name: &str, - ) -> Option> { - let (resp_tx, resp_rx) = oneshot::channel::>>(); - - let cmd = TrackCommand::GetSubscliberSessionIdsByNamespaceAndName { + publisher_session_id: usize, + ) -> Result<()> { + let (resp_tx, resp_rx) = oneshot::channel::(); + let cmd = DeletePublisherAnnouncedNamespace { track_namespace, - track_name: track_name.to_string(), - resp: resp_tx, - }; - self.tx.send(cmd).await.unwrap(); - - let session_ids = resp_rx.await.unwrap(); - - return session_ids; - } - - async fn get_subscriber_session_ids_by_track_id(&self, track_id: u64) -> Option> { - let (resp_tx, resp_rx) = oneshot::channel::>>(); - - let cmd = TrackCommand::GetSubscliberSessionIdsByTrackId { - track_id, + publisher_session_id, resp: resp_tx, }; self.tx.send(cmd).await.unwrap(); + let result = resp_rx.await.unwrap(); - let session_ids = resp_rx.await.unwrap(); - - return session_ids; + match result { + true => Ok(()), + false => bail!("failure"), + } } - - async fn activate_subscriber( - &self, - track_namespace: Vec, - track_name: &str, - subscriber_session_id: usize, - ) -> Result<()> { + async fn delete_client(&self, session_id: usize) -> Result<()> { let (resp_tx, resp_rx) = oneshot::channel::(); - - let cmd = TrackCommand::SetStatus { - track_namespace, - track_name: track_name.to_string(), - subscriber_session_id, - status: SubscriberStatus::Activate, + let cmd = DeleteClient { + session_id, resp: resp_tx, }; self.tx.send(cmd).await.unwrap(); - let result = resp_rx.await.unwrap(); match result { true => Ok(()), - false => bail!("not found"), + false => bail!("failure"), } } - - async fn delete_client(&self, session_id: usize) -> Result<()> { - self.delete_publisher_by_session_id(session_id).await?; - self.delete_subscribers_by_session_id(session_id).await?; - - Ok(()) - } } #[cfg(test)] @@ -908,7 +869,7 @@ mod success { .await; let result = track_namespace_manager - .get_publisher_session_id_by_track_namespace(track_namespace.clone()) + .get_publisher_session_id(track_namespace.clone()) .await; assert!(result.is_none()); @@ -936,7 +897,7 @@ mod success { } #[tokio::test] - async fn get_publisher_session_id_by_track_namespace() { + async fn get_publisher_session_id() { let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); let publisher_session_id = 1; @@ -950,7 +911,7 @@ mod success { .await; let result = track_namespace_manager - .get_publisher_session_id_by_track_namespace(track_namespace.clone()) + .get_publisher_session_id(track_namespace.clone()) .await; assert_eq!(result, Some(publisher_session_id)); @@ -1231,11 +1192,11 @@ mod success { // Test for subscriber // Remain: pub 1 let delete_publisher_result_1 = track_namespace_manager - .get_publisher_session_id_by_track_namespace(track_namespaces[0].clone()) + .get_publisher_session_id(track_namespaces[0].clone()) .await .unwrap(); let delete_publisher_result_2 = track_namespace_manager - .get_publisher_session_id_by_track_namespace(track_namespaces[1].clone()) + .get_publisher_session_id(track_namespaces[1].clone()) .await; assert_eq!(delete_publisher_result_1, publisher_session_ids[0]); @@ -1348,7 +1309,7 @@ mod failure { } #[tokio::test] - async fn get_publisher_session_id_by_track_namespace_not_found() { + async fn get_publisher_session_id_not_found() { let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); // Start track management thread @@ -1358,7 +1319,7 @@ mod failure { let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); let result = track_namespace_manager - .get_publisher_session_id_by_track_namespace(track_namespace) + .get_publisher_session_id(track_namespace) .await; assert_eq!(result, None); From 7f1f337c0c24eb5ba6142a1b44c307c5c2061e5b Mon Sep 17 00:00:00 2001 From: tetta maeda Date: Sun, 6 Oct 2024 00:12:01 +0900 Subject: [PATCH 02/17] impl: update behavior arround track_namespace_manager --- moqt-core/src/modules.rs | 1 + .../messages/control_messages/subscribe.rs | 41 +++ moqt-core/src/modules/subscription_models.rs | 3 + .../src/modules/control_message_handler.rs | 13 +- .../src/modules/handlers/announce_handler.rs | 7 +- .../modules/handlers/server_setup_handler.rs | 127 +++++++-- .../src/modules/handlers/subscribe_handler.rs | 257 ++++++++++++++++-- .../modules/handlers/subscribe_ok_handler.rs | 53 ++-- .../modules/handlers/unannounce_handler.rs | 5 +- .../server_processes/client_setup_message.rs | 10 +- .../server_processes/subscribe_message.rs | 27 +- .../server_processes/subscribe_ok_message.rs | 4 +- 12 files changed, 469 insertions(+), 79 deletions(-) create mode 100644 moqt-core/src/modules/subscription_models.rs diff --git a/moqt-core/src/modules.rs b/moqt-core/src/modules.rs index f32914f0..1a5fec31 100644 --- a/moqt-core/src/modules.rs +++ b/moqt-core/src/modules.rs @@ -3,6 +3,7 @@ pub mod control_message_type; pub mod messages; pub mod moqt_client; pub mod send_stream_dispatcher_repository; +pub mod subscription_models; pub mod track_namespace_manager_repository; pub mod variable_bytes; pub mod variable_integer; diff --git a/moqt-core/src/modules/messages/control_messages/subscribe.rs b/moqt-core/src/modules/messages/control_messages/subscribe.rs index 0002877e..09379cfb 100644 --- a/moqt-core/src/modules/messages/control_messages/subscribe.rs +++ b/moqt-core/src/modules/messages/control_messages/subscribe.rs @@ -109,12 +109,53 @@ impl Subscribe { }) } + pub fn subscribe_id(&self) -> u64 { + self.subscribe_id + } + + pub fn track_alias(&self) -> u64 { + self.track_alias + } + pub fn track_namespace(&self) -> &Vec { &self.track_namespace } pub fn track_name(&self) -> &str { &self.track_name } + + pub fn subscriber_priority(&self) -> u8 { + self.subscriber_priority + } + + pub fn group_order(&self) -> GroupOrder { + self.group_order + } + + pub fn filter_type(&self) -> FilterType { + self.filter_type + } + + pub fn start_group(&self) -> Option { + self.start_group + } + + pub fn start_object(&self) -> Option { + self.start_object + } + + pub fn end_group(&self) -> Option { + self.end_group + } + + pub fn end_object(&self) -> Option { + self.end_object + } + + pub fn replace_subscribe_id_and_track_alias(&mut self, subscribe_id: u64, track_alias: u64) { + self.subscribe_id = subscribe_id; + self.track_alias = track_alias; + } } impl MOQTPayload for Subscribe { diff --git a/moqt-core/src/modules/subscription_models.rs b/moqt-core/src/modules/subscription_models.rs new file mode 100644 index 00000000..8026549f --- /dev/null +++ b/moqt-core/src/modules/subscription_models.rs @@ -0,0 +1,3 @@ +pub mod subscription_nodes; +pub mod subscriptions; +pub mod tracks; diff --git a/moqt-server/src/modules/control_message_handler.rs b/moqt-server/src/modules/control_message_handler.rs index d155203f..6ca3647e 100644 --- a/moqt-server/src/modules/control_message_handler.rs +++ b/moqt-server/src/modules/control_message_handler.rs @@ -99,6 +99,7 @@ pub async fn control_message_handler( client, underlay_type, &mut write_buf, + track_namespace_manager_repository, ) { Ok(_) => ControlMessageType::ServerSetup, Err(err) => { @@ -124,13 +125,20 @@ pub async fn control_message_handler( match process_subscribe_message( &mut payload_buf, client, + &mut write_buf, track_namespace_manager_repository, send_stream_dispatcher_repository, ) .await { - Ok(_) => { - return MessageProcessResult::SuccessWithoutResponse; + Ok(result) => { + match result { + Some(_) => (), + None => { + return MessageProcessResult::SuccessWithoutResponse; + } + } + ControlMessageType::SubscribeOk } Err(err) => { return MessageProcessResult::Failure( @@ -155,6 +163,7 @@ pub async fn control_message_handler( &mut payload_buf, track_namespace_manager_repository, send_stream_dispatcher_repository, + client, ) .await { diff --git a/moqt-server/src/modules/handlers/announce_handler.rs b/moqt-server/src/modules/handlers/announce_handler.rs index a1c52e41..74d5d56c 100644 --- a/moqt-server/src/modules/handlers/announce_handler.rs +++ b/moqt-server/src/modules/handlers/announce_handler.rs @@ -24,7 +24,7 @@ pub(crate) async fn announce_handler( // Record the announced Track Namespace let set_result = track_namespace_manager_repository - .set_publisher(announce_message.track_namespace().clone(), client.id) + .set_publisher_announced_namespace(announce_message.track_namespace().clone(), client.id) .await; match set_result { @@ -149,7 +149,10 @@ mod failure { // Set the duplicated publisher in advance let _ = track_namespace_manager - .set_publisher(announce_message.track_namespace().clone(), client.id) + .set_publisher_announced_namespace( + announce_message.track_namespace().clone(), + client.id, + ) .await; // Execute announce_handler and get result diff --git a/moqt-server/src/modules/handlers/server_setup_handler.rs b/moqt-server/src/modules/handlers/server_setup_handler.rs index 48ff7f1d..d1cebe0a 100644 --- a/moqt-server/src/modules/handlers/server_setup_handler.rs +++ b/moqt-server/src/modules/handlers/server_setup_handler.rs @@ -10,13 +10,14 @@ use moqt_core::{ setup_parameters::{Role, RoleCase}, }, moqt_client::MOQTClientStatus, - MOQTClient, + MOQTClient, TrackNamespaceManagerRepository, }; pub(crate) fn setup_handler( client_setup_message: ClientSetup, underlay_type: UnderlayType, client: &mut MOQTClient, + track_namespace_manager_repository: &mut dyn TrackNamespaceManagerRepository, ) -> Result { tracing::trace!("setup_handler start."); @@ -35,18 +36,20 @@ pub(crate) fn setup_handler( bail!("Supported version is not included"); } + let mut max_subscribe_id: u64 = 0; + for setup_parameter in &client_setup_message.setup_parameters { match setup_parameter { - SetupParameter::Role(role) => { - client.set_role(role.value)?; + SetupParameter::Role(param) => { + client.set_role(param.value)?; } SetupParameter::Path(_) => { if underlay_type == UnderlayType::WebTransport { bail!("PATH parameter is not allowed on WebTransport."); } } - SetupParameter::MaxSubscribeID(_) => { - // TODO: handle MaxSubscribeID + SetupParameter::MaxSubscribeID(param) => { + max_subscribe_id = param.value; } SetupParameter::Unknown(v) => { tracing::warn!("Ignore unknown SETUP parameter {}", v); @@ -54,7 +57,14 @@ pub(crate) fn setup_handler( } } - if client.role().is_none() { + if client.role() == Some(RoleCase::Subscriber) { + // Generate producer that manages namespaces and subscriptions with subscribers. + // FIXME: max_subscribe_id for subscriber is fixed at 100 for now. + track_namespace_manager_repository.setup_subscriber(100, client.id); + } else if client.role() == Some(RoleCase::Publisher) { + // Generate consumer that manages namespaces and subscriptions with producers. + track_namespace_manager_repository.setup_publisher(max_subscribe_id, client.id); + } else if client.role().is_none() { bail!("Role parameter is required in SETUP parameter from client."); } @@ -73,14 +83,17 @@ pub(crate) fn setup_handler( #[cfg(test)] mod success { - use std::vec; - + use crate::modules::track_namespace_manager::{ + track_namespace_manager, TrackCommand, TrackNamespaceManager, + }; use crate::{constants, modules::handlers::server_setup_handler::setup_handler}; use moqt_core::messages::control_messages::{ client_setup::ClientSetup, setup_parameters::{Path, Role, RoleCase, SetupParameter}, }; use moqt_core::moqt_client::MOQTClient; + use std::vec; + use tokio::sync::mpsc; #[test] fn only_role() { @@ -90,7 +103,18 @@ mod success { ClientSetup::new(vec![constants::MOQ_TRANSPORT_VERSION], setup_parameters); let underlay_type = crate::constants::UnderlayType::WebTransport; - let server_setup_message = setup_handler(client_setup_message, underlay_type, &mut client); + // Generate TrackNamespaceManager + let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + tokio::spawn(async move { track_namespace_manager(&mut track_namespace_rx).await }); + let mut track_namespace_manager: TrackNamespaceManager = + TrackNamespaceManager::new(track_namespace_tx); + + let server_setup_message = setup_handler( + client_setup_message, + underlay_type, + &mut client, + &mut track_namespace_manager, + ); assert!(server_setup_message.is_ok()); let _server_setup_message = server_setup_message.unwrap(); // TODO: Not implemented yet @@ -107,7 +131,18 @@ mod success { ClientSetup::new(vec![constants::MOQ_TRANSPORT_VERSION], setup_parameters); let underlay_type = crate::constants::UnderlayType::QUIC; - let server_setup_message = setup_handler(client_setup_message, underlay_type, &mut client); + // Generate TrackNamespaceManager + let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + tokio::spawn(async move { track_namespace_manager(&mut track_namespace_rx).await }); + let mut track_namespace_manager: TrackNamespaceManager = + TrackNamespaceManager::new(track_namespace_tx); + + let server_setup_message = setup_handler( + client_setup_message, + underlay_type, + &mut client, + &mut track_namespace_manager, + ); assert!(server_setup_message.is_ok()); let _server_setup_message = server_setup_message.unwrap(); // TODO: Not implemented yet @@ -116,14 +151,17 @@ mod success { #[cfg(test)] mod failure { - use std::vec; - + use crate::modules::track_namespace_manager::{ + track_namespace_manager, TrackCommand, TrackNamespaceManager, + }; use crate::{constants, modules::handlers::server_setup_handler::setup_handler}; use moqt_core::messages::control_messages::{ client_setup::ClientSetup, setup_parameters::{Path, Role, RoleCase, SetupParameter}, }; use moqt_core::moqt_client::MOQTClient; + use std::vec; + use tokio::sync::mpsc; #[test] fn no_role_parameter() { @@ -133,7 +171,18 @@ mod failure { ClientSetup::new(vec![constants::MOQ_TRANSPORT_VERSION], setup_parameters); let underlay_type = crate::constants::UnderlayType::WebTransport; - let server_setup_message = setup_handler(client_setup_message, underlay_type, &mut client); + // Generate TrackNamespaceManager + let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + tokio::spawn(async move { track_namespace_manager(&mut track_namespace_rx).await }); + let mut track_namespace_manager: TrackNamespaceManager = + TrackNamespaceManager::new(track_namespace_tx); + + let server_setup_message = setup_handler( + client_setup_message, + underlay_type, + &mut client, + &mut track_namespace_manager, + ); assert!(server_setup_message.is_err()); } @@ -146,7 +195,18 @@ mod failure { ClientSetup::new(vec![constants::MOQ_TRANSPORT_VERSION], setup_parameters); let underlay_type = crate::constants::UnderlayType::WebTransport; - let server_setup_message = setup_handler(client_setup_message, underlay_type, &mut client); + // Generate TrackNamespaceManager + let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + tokio::spawn(async move { track_namespace_manager(&mut track_namespace_rx).await }); + let mut track_namespace_manager: TrackNamespaceManager = + TrackNamespaceManager::new(track_namespace_tx); + + let server_setup_message = setup_handler( + client_setup_message, + underlay_type, + &mut client, + &mut track_namespace_manager, + ); assert!(server_setup_message.is_err()); } @@ -159,7 +219,18 @@ mod failure { ClientSetup::new(vec![constants::MOQ_TRANSPORT_VERSION], setup_parameters); let underlay_type = crate::constants::UnderlayType::QUIC; - let server_setup_message = setup_handler(client_setup_message, underlay_type, &mut client); + // Generate TrackNamespaceManager + let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + tokio::spawn(async move { track_namespace_manager(&mut track_namespace_rx).await }); + let mut track_namespace_manager: TrackNamespaceManager = + TrackNamespaceManager::new(track_namespace_tx); + + let server_setup_message = setup_handler( + client_setup_message, + underlay_type, + &mut client, + &mut track_namespace_manager, + ); assert!(server_setup_message.is_err()); } @@ -173,7 +244,18 @@ mod failure { let client_setup_message = ClientSetup::new(vec![unsupported_version], setup_parameters); let underlay_type = crate::constants::UnderlayType::WebTransport; - let server_setup_message = setup_handler(client_setup_message, underlay_type, &mut client); + // Generate TrackNamespaceManager + let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + tokio::spawn(async move { track_namespace_manager(&mut track_namespace_rx).await }); + let mut track_namespace_manager: TrackNamespaceManager = + TrackNamespaceManager::new(track_namespace_tx); + + let server_setup_message = setup_handler( + client_setup_message, + underlay_type, + &mut client, + &mut track_namespace_manager, + ); assert_ne!(unsupported_version, constants::MOQ_TRANSPORT_VERSION); // assert unsupported_version is unsupport assert!(server_setup_message.is_err()); @@ -190,7 +272,18 @@ mod failure { ClientSetup::new(vec![constants::MOQ_TRANSPORT_VERSION], setup_parameters); let underlay_type = crate::constants::UnderlayType::WebTransport; - let server_setup_message = setup_handler(client_setup_message, underlay_type, &mut client); + // Generate TrackNamespaceManager + let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + tokio::spawn(async move { track_namespace_manager(&mut track_namespace_rx).await }); + let mut track_namespace_manager: TrackNamespaceManager = + TrackNamespaceManager::new(track_namespace_tx); + + let server_setup_message = setup_handler( + client_setup_message, + underlay_type, + &mut client, + &mut track_namespace_manager, + ); assert!(server_setup_message.is_ok()); } diff --git a/moqt-server/src/modules/handlers/subscribe_handler.rs b/moqt-server/src/modules/handlers/subscribe_handler.rs index 09ee269b..264b06b6 100644 --- a/moqt-server/src/modules/handlers/subscribe_handler.rs +++ b/moqt-server/src/modules/handlers/subscribe_handler.rs @@ -2,7 +2,13 @@ use anyhow::{bail, Result}; use moqt_core::{ constants::StreamDirection, - messages::{control_messages::subscribe::Subscribe, moqt_payload::MOQTPayload}, + messages::{ + control_messages::{ + subscribe::{self, Subscribe}, + subscribe_ok::SubscribeOk, + }, + moqt_payload::MOQTPayload, + }, MOQTClient, SendStreamDispatcherRepository, TrackNamespaceManagerRepository, }; @@ -11,41 +17,85 @@ pub(crate) async fn subscribe_handler( client: &mut MOQTClient, track_namespace_manager_repository: &mut dyn TrackNamespaceManagerRepository, send_stream_dispatcher_repository: &mut dyn SendStreamDispatcherRepository, -) -> Result<()> { +) -> Result> { tracing::trace!("subscribe_handler start."); tracing::debug!("subscribe_message: {:#?}", subscribe_message); - // TODO: If the track exists, return it as it is + if !track_namespace_manager_repository + .is_valid_subscriber_subscribe_id(subscribe_message.subscribe_id(), client.id) + .await? + { + // TODO: return TerminationErrorCode + bail!("TooManySubscribers"); + } + if !track_namespace_manager_repository + .is_valid_subscriber_track_alias(subscribe_message.track_alias(), client.id) + .await? + { + // TODO: return TerminationErrorCode + bail!("DuplicateTrackAlias"); + } + + // If the track exists, return ther track as it is + if track_namespace_manager_repository + .is_track_existing( + subscribe_message.track_namespace().to_vec(), + subscribe_message.track_name().to_string(), + ) + .await + .unwrap() + { + set_only_subscriber_subscription( + track_namespace_manager_repository, + &subscribe_message, + client, + ) + .await; + + // TODO: generate and return subscribe_ok message + } // Since only the track_namespace is recorded in ANNOUNCE, use track_namespace to determine the publisher + // TODO: multiple publishers for the same track_namespace let publisher_session_id = track_namespace_manager_repository - .get_publisher_session_id_by_track_namespace(subscribe_message.track_namespace().clone()) - .await; + .get_publisher_session_id(subscribe_message.track_namespace().clone()) + .await + .unwrap(); match publisher_session_id { Some(session_id) => { - // Record the SUBSCRIBER who sent the SUBSCRIBE message - match track_namespace_manager_repository - .set_subscriber( - subscribe_message.track_namespace().clone(), - client.id, - subscribe_message.track_name(), + let (upstream_subscribe_id, upstream_track_alias) = + match set_subscriber_and_publisher_subscription( + track_namespace_manager_repository, + &subscribe_message, + client, + session_id, ) .await - { - Ok(_) => {} - Err(e) => { - bail!("cannot register subscriber: {:?}", e); - } - } - // Notify the publisher about the SUBSCRIBE message - let message: Box = Box::new(subscribe_message.clone()); + { + Ok((upstream_subscribe_id, upstream_track_alias)) => { + (upstream_subscribe_id, upstream_track_alias) + } + Err(e) => { + bail!("cannot register publisher and subscriber: {:?}", e); + } + }; + + let mut relaying_subscribe_message = subscribe_message.clone(); + + // Replace the subscribe_id and track_alias in the SUBSCRIBE message to request to the upstream publisher + relaying_subscribe_message + .replace_subscribe_id_and_track_alias(upstream_subscribe_id, upstream_track_alias); + let message: Box = Box::new(relaying_subscribe_message.clone()); + tracing::debug!( "message: {:#?} is sent to relay handler for client {:?}", - subscribe_message, + relaying_subscribe_message, session_id ); + // Notify to the publisher about the SUBSCRIBE message + // TODO: Wait for the SUBSCRIBE_OK message to be returned on a transaction match send_stream_dispatcher_repository .send_message_to_send_stream_thread(session_id, message, StreamDirection::Bi) .await @@ -53,11 +103,11 @@ pub(crate) async fn subscribe_handler( Ok(_) => { tracing::info!( "subscribed track_namespace: {:?}", - subscribe_message.track_namespace(), + relaying_subscribe_message.track_namespace(), ); tracing::info!( "subscribed track_name: {:?}", - subscribe_message.track_name() + relaying_subscribe_message.track_name() ); tracing::trace!("subscribe_handler complete."); } @@ -67,7 +117,7 @@ pub(crate) async fn subscribe_handler( } } - Ok(()) + Ok(None) } // TODO: Check if “publisher not found” should turn into closing connection @@ -75,6 +125,144 @@ pub(crate) async fn subscribe_handler( } } +async fn set_only_subscriber_subscription( + track_namespace_manager_repository: &mut dyn TrackNamespaceManagerRepository, + subscribe_message: &Subscribe, + client: &MOQTClient, +) -> Result<()> { + let subscriber_client_id = client.id; + let subscriber_subscribe_id = subscribe_message.subscribe_id(); + let subscriber_track_alias = subscribe_message.track_alias(); + let subscriber_track_namespace = subscribe_message.track_namespace().to_vec(); + let subscriber_track_name = subscribe_message.track_name().to_string(); + let subscriber_priority = subscribe_message.subscriber_priority(); + let subscriber_group_order = subscribe_message.group_order(); + let subscriber_filter_type = subscribe_message.filter_type(); + let subscriber_start_group = subscribe_message.start_group(); + let subscriber_start_object = subscribe_message.start_object(); + let subscriber_end_group = subscribe_message.end_group(); + let subscriber_end_object = subscribe_message.end_object(); + + let publisher_subscription = track_namespace_manager_repository + .get_publisher_subscription_by_full_track_name( + subscriber_track_namespace.clone(), + subscriber_track_name.clone(), + ) + .await? + .unwrap(); + + track_namespace_manager_repository + .set_subscriber_subscription( + subscriber_client_id, + subscriber_subscribe_id, + subscriber_track_alias, + subscriber_track_namespace.clone(), + subscriber_track_name.clone(), + subscriber_priority, + subscriber_group_order, + subscriber_filter_type, + subscriber_start_group, + subscriber_start_object, + subscriber_end_group, + subscriber_end_object, + ) + .await?; + + let publisher_session_id = track_namespace_manager_repository + .get_publisher_session_id(subscriber_track_namespace) + .await? + .unwrap(); + + let (publisher_track_namespace, publisher_track_name) = + publisher_subscription.get_track_namespace_and_name(); + + let publisher_subscribe_id = track_namespace_manager_repository + .get_publisher_subscribe_id( + publisher_track_namespace, + publisher_track_name, + publisher_session_id, + ) + .await? + .unwrap(); + + track_namespace_manager_repository + .register_pubsup_relation( + publisher_session_id, + publisher_subscribe_id, + subscriber_client_id, + subscriber_subscribe_id, + ) + .await?; + + track_namespace_manager_repository + .activate_subscriber_subscription(subscriber_client_id, subscriber_subscribe_id); + + Ok(()) +} + +async fn set_subscriber_and_publisher_subscription( + track_namespace_manager_repository: &mut dyn TrackNamespaceManagerRepository, + subscribe_message: &Subscribe, + client: &MOQTClient, + publisher_session_id: usize, +) -> Result<(u64, u64)> { + let subscriber_client_id = client.id; + let subscriber_subscribe_id = subscribe_message.subscribe_id(); + let subscriber_track_alias = subscribe_message.track_alias(); + let subscriber_track_namespace = subscribe_message.track_namespace().to_vec(); + let subscriber_track_name = subscribe_message.track_name().to_string(); + let subscriber_priority = subscribe_message.subscriber_priority(); + let subscriber_group_order = subscribe_message.group_order(); + let subscriber_filter_type = subscribe_message.filter_type(); + let subscriber_start_group = subscribe_message.start_group(); + let subscriber_start_object = subscribe_message.start_object(); + let subscriber_end_group = subscribe_message.end_group(); + let subscriber_end_object = subscribe_message.end_object(); + + track_namespace_manager_repository + .set_subscriber_subscription( + subscriber_client_id, + subscriber_subscribe_id, + subscriber_track_alias, + subscriber_track_namespace.clone(), + subscriber_track_name.clone(), + subscriber_priority, + subscriber_group_order, + subscriber_filter_type, + subscriber_start_group, + subscriber_start_object, + subscriber_end_group, + subscriber_end_object, + ) + .await?; + + let (upstream_subscribe_id, upstream_track_alias) = track_namespace_manager_repository + .set_publisher_subscription( + publisher_session_id, + subscriber_track_namespace.clone(), + subscriber_track_name.clone(), + subscriber_priority, + subscriber_group_order, + subscriber_filter_type, + subscriber_start_group, + subscriber_start_object, + subscriber_end_group, + subscriber_end_object, + ) + .await?; + + track_namespace_manager_repository + .register_pubsup_relation( + publisher_session_id, + upstream_subscribe_id, + subscriber_client_id, + subscriber_subscribe_id, + ) + .await?; + + Ok((upstream_subscribe_id, upstream_track_alias)) +} + #[cfg(test)] mod success { use crate::modules::handlers::subscribe_handler::subscribe_handler; @@ -142,8 +330,13 @@ mod success { TrackNamespaceManager::new(track_namespace_tx); let publisher_session_id = 1; + let max_subscribe_id = 10; + + let _ = track_namespace_manager + .setup_publisher(max_subscribe_id, publisher_session_id) + .await; let _ = track_namespace_manager - .set_publisher(track_namespace.clone(), publisher_session_id) + .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) .await; // Generate SendStreamDispacher @@ -243,9 +436,16 @@ mod failure { let publisher_session_id = 1; let track_id = 0; + let max_subscribe_id = 10; let _ = track_namespace_manager - .set_publisher(track_namespace.clone(), publisher_session_id) + .setup_publisher(max_subscribe_id, publisher_session_id) + .await; + let _ = track_namespace_manager + .setup_subscriber(max_subscribe_id, subscriber_session_id) + .await; + let _ = track_namespace_manager + .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) .await; let _ = track_namespace_manager .set_subscriber(track_namespace.clone(), subscriber_session_id, track_name) @@ -330,8 +530,13 @@ mod failure { TrackNamespaceManager::new(track_namespace_tx); let publisher_session_id = 1; + let max_subscribe_id = 10; + + let _ = track_namespace_manager + .setup_publisher(max_subscribe_id, publisher_session_id) + .await; let _ = track_namespace_manager - .set_publisher(track_namespace.clone(), publisher_session_id) + .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) .await; // Generate SendStreamDispacher (without set sender) diff --git a/moqt-server/src/modules/handlers/subscribe_ok_handler.rs b/moqt-server/src/modules/handlers/subscribe_ok_handler.rs index c5b74193..f38d5a9c 100644 --- a/moqt-server/src/modules/handlers/subscribe_ok_handler.rs +++ b/moqt-server/src/modules/handlers/subscribe_ok_handler.rs @@ -3,53 +3,58 @@ use anyhow::Result; use moqt_core::{ constants::StreamDirection, messages::{control_messages::subscribe_ok::SubscribeOk, moqt_payload::MOQTPayload}, - SendStreamDispatcherRepository, TrackNamespaceManagerRepository, + MOQTClient, SendStreamDispatcherRepository, TrackNamespaceManagerRepository, }; pub(crate) async fn subscribe_ok_handler( subscribe_ok_message: SubscribeOk, track_namespace_manager_repository: &mut dyn TrackNamespaceManagerRepository, send_stream_dispatcher_repository: &mut dyn SendStreamDispatcherRepository, + client: &mut MOQTClient, ) -> Result<()> { tracing::trace!("subscribe_ok_handler start."); tracing::debug!("subscribe_ok_message: {:#?}", subscribe_ok_message); + let publisher_session_id = client.id; + let publisher_subscribe_id = subscribe_ok_message.subscribe_id(); + // Determine the SUBSCRIBER who sent the SUBSCRIBE using the track_namespace and track_name - let subscriber_session_ids = track_namespace_manager_repository - .get_subscriber_session_ids_by_track_namespace_and_track_name( - subscribe_ok_message.track_namespace().clone(), - subscribe_ok_message.track_name(), + let ids = track_namespace_manager_repository + .get_requesting_subscriber_session_ids_and_subscribe_ids( + publisher_subscribe_id, + publisher_session_id, ) - .await; - match subscriber_session_ids { - Some(session_ids) => { + .await?; + match ids { + Some(ids) => { + let _ = track_namespace_manager_repository + .activate_publisher_subscription(publisher_session_id, publisher_subscribe_id); + // Notify all waiting subscribers with the SUBSCRIBE_OK message - for session_id in session_ids.iter() { + for (subscriber_session_id, subscriber_subscribe_id) in ids.iter() { let message: Box = Box::new(subscribe_ok_message.clone()); + // TODO: replace subscribe_id and track_alias + tracing::debug!( "message: {:#?} is sent to relay handler for client {:?}", subscribe_ok_message, - session_id + subscriber_session_id ); + match send_stream_dispatcher_repository - .send_message_to_send_stream_thread(*session_id, message, StreamDirection::Bi) + .send_message_to_send_stream_thread( + *subscriber_session_id, + message, + StreamDirection::Bi, + ) .await { Ok(_) => { - // Record the track_id upon success and activate the subscriber - let _ = track_namespace_manager_repository - .set_track_id( - subscribe_ok_message.track_namespace().clone(), - subscribe_ok_message.track_name(), - subscribe_ok_message.track_id(), - ) - .await; let _ = track_namespace_manager_repository - .activate_subscriber( - subscribe_ok_message.track_namespace().clone(), - subscribe_ok_message.track_name(), - *session_id, + .activate_subscriber_subscription( + *subscriber_session_id, + *subscriber_subscribe_id, ) .await; @@ -58,7 +63,7 @@ pub(crate) async fn subscribe_ok_handler( Err(e) => { tracing::warn!( "relay subscribe ok failed at session id {:?}: {:?}", - session_id, + subscriber_session_id, e ); // Failure on SUBSCRIBE_OK relay doesn't turn into closing connection diff --git a/moqt-server/src/modules/handlers/unannounce_handler.rs b/moqt-server/src/modules/handlers/unannounce_handler.rs index 820ed5fa..ecbd6ce5 100644 --- a/moqt-server/src/modules/handlers/unannounce_handler.rs +++ b/moqt-server/src/modules/handlers/unannounce_handler.rs @@ -16,7 +16,10 @@ pub(crate) async fn unannounce_handler( // Remove the announced Track Namespace let delete_result = track_namespace_manager_repository - .delete_publisher_by_namespace(unannounce_message.track_namespace().clone()) + .delete_publisher_announced_namespace( + unannounce_message.track_namespace().clone(), + _client.id, + ) .await; match delete_result { diff --git a/moqt-server/src/modules/server_processes/client_setup_message.rs b/moqt-server/src/modules/server_processes/client_setup_message.rs index ffb91c25..6439576a 100644 --- a/moqt-server/src/modules/server_processes/client_setup_message.rs +++ b/moqt-server/src/modules/server_processes/client_setup_message.rs @@ -5,7 +5,7 @@ use anyhow::{bail, Result}; use moqt_core::{ constants::UnderlayType, messages::{control_messages::client_setup::ClientSetup, moqt_payload::MOQTPayload}, - MOQTClient, + MOQTClient, TrackNamespaceManagerRepository, }; pub(crate) fn process_client_setup_message( @@ -13,6 +13,7 @@ pub(crate) fn process_client_setup_message( client: &mut MOQTClient, underlay_type: UnderlayType, write_buf: &mut BytesMut, + track_namespace_manager_repository: &mut dyn TrackNamespaceManagerRepository, ) -> Result<()> { let client_setup_message = match ClientSetup::depacketize(payload_buf) { Ok(client_setup_message) => client_setup_message, @@ -22,7 +23,12 @@ pub(crate) fn process_client_setup_message( } }; - let server_setup_message = setup_handler(client_setup_message, underlay_type, client)?; + let server_setup_message = setup_handler( + client_setup_message, + underlay_type, + client, + track_namespace_manager_repository, + )?; server_setup_message.packetize(write_buf); diff --git a/moqt-server/src/modules/server_processes/subscribe_message.rs b/moqt-server/src/modules/server_processes/subscribe_message.rs index 13a716e0..efd99d07 100644 --- a/moqt-server/src/modules/server_processes/subscribe_message.rs +++ b/moqt-server/src/modules/server_processes/subscribe_message.rs @@ -2,16 +2,20 @@ use crate::modules::handlers::subscribe_handler::subscribe_handler; use anyhow::{bail, Result}; use bytes::BytesMut; use moqt_core::{ - messages::{control_messages::subscribe::Subscribe, moqt_payload::MOQTPayload}, + messages::{ + control_messages::{subscribe::Subscribe, subscribe_ok::SubscribeOk}, + moqt_payload::MOQTPayload, + }, MOQTClient, SendStreamDispatcherRepository, TrackNamespaceManagerRepository, }; pub(crate) async fn process_subscribe_message( payload_buf: &mut BytesMut, client: &mut MOQTClient, + write_buf: &mut BytesMut, track_namespace_manager_repository: &mut dyn TrackNamespaceManagerRepository, send_stream_dispatcher_repository: &mut dyn SendStreamDispatcherRepository, -) -> Result<()> { +) -> Result> { let subscribe_request_message = match Subscribe::depacketize(payload_buf) { Ok(subscribe_request_message) => subscribe_request_message, Err(err) => { @@ -20,11 +24,26 @@ pub(crate) async fn process_subscribe_message( } }; - subscribe_handler( + let result = subscribe_handler( subscribe_request_message, client, track_namespace_manager_repository, send_stream_dispatcher_repository, ) - .await + .await; + + match result.as_ref() { + Ok(subscribe_ok) => { + if subscribe_ok.is_some() { + // If the Track already exists, the Relay responds with a SubscribeOK message. + subscribe_ok.as_ref().unwrap().packetize(write_buf); + } + + return result; + } + Err(err) => { + tracing::error!("subscribe_handler: err: {:?}", err.to_string()); + bail!(err.to_string()); + } + } } diff --git a/moqt-server/src/modules/server_processes/subscribe_ok_message.rs b/moqt-server/src/modules/server_processes/subscribe_ok_message.rs index 654f0015..0f14f5e7 100644 --- a/moqt-server/src/modules/server_processes/subscribe_ok_message.rs +++ b/moqt-server/src/modules/server_processes/subscribe_ok_message.rs @@ -3,13 +3,14 @@ use anyhow::{bail, Result}; use bytes::BytesMut; use moqt_core::{ messages::{control_messages::subscribe_ok::SubscribeOk, moqt_payload::MOQTPayload}, - SendStreamDispatcherRepository, TrackNamespaceManagerRepository, + MOQTClient, SendStreamDispatcherRepository, TrackNamespaceManagerRepository, }; pub(crate) async fn process_subscribe_ok_message( payload_buf: &mut BytesMut, track_namespace_manager_repository: &mut dyn TrackNamespaceManagerRepository, send_stream_dispatcher_repository: &mut dyn SendStreamDispatcherRepository, + client: &mut MOQTClient, ) -> Result<()> { let subscribe_ok_message = match SubscribeOk::depacketize(payload_buf) { Ok(subscribe_ok_message) => subscribe_ok_message, @@ -23,6 +24,7 @@ pub(crate) async fn process_subscribe_ok_message( subscribe_ok_message, track_namespace_manager_repository, send_stream_dispatcher_repository, + client, ) .await } From 030c80bf6b2b5d2ba1bb36c378a4fe74dbb5f6cc Mon Sep 17 00:00:00 2001 From: tetta maeda Date: Sun, 6 Oct 2024 10:14:02 +0900 Subject: [PATCH 03/17] impl: subscribe_ok --- .../messages/control_messages/subscribe_ok.rs | 355 ++++++++++++++---- .../modules/handlers/subscribe_ok_handler.rs | 105 ++++-- 2 files changed, 340 insertions(+), 120 deletions(-) diff --git a/moqt-core/src/modules/messages/control_messages/subscribe_ok.rs b/moqt-core/src/modules/messages/control_messages/subscribe_ok.rs index 60e9cd67..0f404ced 100644 --- a/moqt-core/src/modules/messages/control_messages/subscribe_ok.rs +++ b/moqt-core/src/modules/messages/control_messages/subscribe_ok.rs @@ -1,44 +1,51 @@ +use crate::messages::control_messages::version_specific_parameters::VersionSpecificParameter; +use crate::messages::moqt_payload::MOQTPayload; +use crate::modules::variable_integer::{read_variable_integer_from_buffer, write_variable_integer}; +use crate::variable_bytes::read_fixed_length_bytes_from_buffer; +use anyhow::bail; use anyhow::Context; use serde::Serialize; use std::any::Any; -use crate::messages::moqt_payload::MOQTPayload; -use crate::modules::{ - variable_bytes::{read_variable_bytes_from_buffer, write_variable_bytes}, - variable_integer::{read_variable_integer_from_buffer, write_variable_integer}, -}; +use super::subscribe::GroupOrder; #[derive(Debug, Serialize, Clone, PartialEq)] pub struct SubscribeOk { - track_namespace: Vec, - track_name: String, - track_id: u64, + subscribe_id: u64, expires: u64, + group_order: GroupOrder, + content_exists: bool, + largest_group_id: Option, + largest_object_id: Option, + number_of_parameters: u64, + subscribe_parameters: Vec, } impl SubscribeOk { pub fn new( - track_namespace: Vec, - track_name: String, - track_id: u64, + subscribe_id: u64, expires: u64, - ) -> SubscribeOk { + group_order: GroupOrder, + content_exists: bool, + largest_group_id: Option, + largest_object_id: Option, + subscribe_parameters: Vec, + ) -> Self { + let number_of_parameters = subscribe_parameters.len() as u64; SubscribeOk { - track_namespace, - track_name, - track_id, + subscribe_id, expires, + group_order, + content_exists, + largest_group_id, + largest_object_id, + number_of_parameters, + subscribe_parameters, } } - pub fn track_namespace(&self) -> &Vec { - &self.track_namespace - } - pub fn track_name(&self) -> &str { - &self.track_name - } - pub fn track_id(&self) -> u64 { - self.track_id + pub fn subscribe_id(&self) -> u64 { + self.subscribe_id } } @@ -47,40 +54,79 @@ impl MOQTPayload for SubscribeOk { where Self: Sized, { - let track_namespace_tuple_length = u8::try_from(read_variable_integer_from_buffer(buf)?) - .context("track namespace length")?; - let mut track_namespace_tuple: Vec = Vec::new(); - for _ in 0..track_namespace_tuple_length { - let track_namespace = String::from_utf8(read_variable_bytes_from_buffer(buf)?) - .context("track namespace")?; - track_namespace_tuple.push(track_namespace); - } - let track_name = - String::from_utf8(read_variable_bytes_from_buffer(buf)?).context("track name")?; - let track_id = read_variable_integer_from_buffer(buf).context("track id")?; + let subscribe_id = read_variable_integer_from_buffer(buf).context("subscribe_id")?; let expires = read_variable_integer_from_buffer(buf).context("expires")?; + let group_order_u8 = read_fixed_length_bytes_from_buffer(buf, 1)?[0]; + + // Values larger than 0x2 are a protocol error. + let group_order = match GroupOrder::try_from(group_order_u8).context("group order") { + Ok(group_order) => group_order, + Err(err) => { + // TODO: return Termination Error Code + bail!(err); + } + }; + + let content_exists = + match read_fixed_length_bytes_from_buffer(buf, 1).context("content_exists")?[0] { + 0 => false, + 1 => true, + _ => { + // TODO: return Termination Error Code + bail!("Invalid content_exists value: Protocol Violation"); + } + }; + + let (largest_group_id, largest_object_id) = if content_exists { + let largest_group_id = + read_variable_integer_from_buffer(buf).context("largest_group_id")?; + let largest_object_id = + read_variable_integer_from_buffer(buf).context("largest_object_id")?; + (Some(largest_group_id), Some(largest_object_id)) + } else { + (None, None) + }; + + let number_of_parameters = + read_variable_integer_from_buffer(buf).context("number of parameters")?; + let mut subscribe_parameters = Vec::new(); + for _ in 0..number_of_parameters { + let version_specific_parameter = VersionSpecificParameter::depacketize(buf)?; + if let VersionSpecificParameter::Unknown(code) = version_specific_parameter { + tracing::warn!("unknown track request parameter {}", code); + } else { + subscribe_parameters.push(version_specific_parameter); + } + } tracing::trace!("Depacketized Subscribe OK message."); Ok(SubscribeOk { - track_namespace: track_namespace_tuple, - track_name, - track_id, + subscribe_id, expires, + group_order, + content_exists, + largest_group_id, + largest_object_id, + number_of_parameters, + subscribe_parameters, }) } fn packetize(&self, buf: &mut bytes::BytesMut) { // Track Namespace Number of elements - let track_namespace_tuple_length = self.track_namespace.len(); - buf.extend(write_variable_integer(track_namespace_tuple_length as u64)); - for track_namespace in &self.track_namespace { - // Track Namespace - buf.extend(write_variable_bytes(&track_namespace.as_bytes().to_vec())); - } - buf.extend(write_variable_bytes(&self.track_name.as_bytes().to_vec())); - buf.extend(write_variable_integer(self.track_id)); + buf.extend(write_variable_integer(self.subscribe_id)); buf.extend(write_variable_integer(self.expires)); + buf.extend(u8::from(self.group_order).to_be_bytes()); + buf.extend(u8::from(self.content_exists).to_be_bytes()); + if self.content_exists { + buf.extend(write_variable_integer(self.largest_group_id.unwrap())); + buf.extend(write_variable_integer(self.largest_object_id.unwrap())); + } + buf.extend(write_variable_integer(self.number_of_parameters)); + for version_specific_parameter in &self.subscribe_parameters { + version_specific_parameter.packetize(buf); + } tracing::trace!("Packetized Subscribe OK message."); } @@ -92,6 +138,10 @@ impl MOQTPayload for SubscribeOk { #[cfg(test)] mod success { + use crate::messages::control_messages::{ + subscribe_ok::GroupOrder, + version_specific_parameters::{AuthorizationInfo, VersionSpecificParameter}, + }; use crate::{ messages::moqt_payload::MOQTPayload, modules::messages::control_messages::subscribe_ok::SubscribeOk, @@ -99,64 +149,207 @@ mod success { use bytes::BytesMut; #[test] - fn packetize_subscribe_ok() { - let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); - let track_name = "track_name".to_string(); - let track_id = 1; - let expires = 2; + fn packetize_subscribe_ok_content_not_exists() { + let subscribe_id = 0; + let expires = 1; + let group_order = GroupOrder::Ascending; + let content_exists = false; + let largest_group_id = None; + let largest_object_id = None; + let version_specific_parameter = + VersionSpecificParameter::AuthorizationInfo(AuthorizationInfo::new("test".to_string())); + let subscribe_parameters = vec![version_specific_parameter]; + + let subscribe_ok = SubscribeOk::new( + subscribe_id, + expires, + group_order, + content_exists, + largest_group_id, + largest_object_id, + subscribe_parameters, + ); + let mut buf = bytes::BytesMut::new(); + subscribe_ok.packetize(&mut buf); + + let expected_bytes_array = [ + 0, // Subscribe ID (i) + 1, // Expires (i) + 1, // Group Order (8) + 0, // Content Exists (f) + 1, // Track Request Parameters (..): Number of Parameters + 2, // Parameter Type (i): AuthorizationInfo + 4, // Parameter Length (i) + 116, 101, 115, 116, // Parameter Value (..): test + ]; + assert_eq!(buf.as_ref(), expected_bytes_array.as_slice()); + } + + #[test] + fn packetize_subscribe_ok_content_exists() { + let subscribe_id = 0; + let expires = 1; + let group_order = GroupOrder::Descending; + let content_exists = true; + let largest_group_id = Some(10); + let largest_object_id = Some(20); + let version_specific_parameter = + VersionSpecificParameter::AuthorizationInfo(AuthorizationInfo::new("test".to_string())); + let subscribe_parameters = vec![version_specific_parameter]; + let subscribe_ok = SubscribeOk::new( - track_namespace.clone(), - track_name.clone(), - track_id, + subscribe_id, expires, + group_order, + content_exists, + largest_group_id, + largest_object_id, + subscribe_parameters, ); let mut buf = bytes::BytesMut::new(); subscribe_ok.packetize(&mut buf); let expected_bytes_array = [ - 2, // Track Namespace(tuple): Number of elements - 4, // Track Namespace(b): Length - 116, 101, 115, 116, // Track Namespace(b): Value("test") - 4, // Track Namespace(b): Length - 116, 101, 115, 116, // Track Namespace(b): Value("test") - 10, // Track Name (b): Length - 116, 114, 97, 99, 107, 95, 110, 97, 109, - 101, // Track Name (b): Value("track_name") - 1, // Track ID (i) - 2, // Expires (i) + 0, // Subscribe ID (i) + 1, // Expires (i) + 2, // Group Order (8) + 1, // Content Exists (f) + 10, // Largest Group ID (i) + 20, // Largest Object ID (i) + 1, // Track Request Parameters (..): Number of Parameters + 2, // Parameter Type (i): AuthorizationInfo + 4, // Parameter Length (i) + 116, 101, 115, 116, // Parameter Value (..): test ]; assert_eq!(buf.as_ref(), expected_bytes_array.as_slice()); } #[test] - fn depacketize_subscribe_ok() { + fn depacketize_subscribe_ok_content_not_exists() { + let bytes_array = [ + 0, // Subscribe ID (i) + 1, // Expires (i) + 2, // Group Order (8) + 0, // Content Exists (f) + 1, // Track Request Parameters (..): Number of Parameters + 2, // Parameter Type (i): AuthorizationInfo + 4, // Parameter Length (i) + 116, 101, 115, 116, // Parameter Value (..): test + ]; + let mut buf = BytesMut::with_capacity(bytes_array.len()); + buf.extend_from_slice(&bytes_array); + let depacketized_subscribe_ok = SubscribeOk::depacketize(&mut buf).unwrap(); + + let subscribe_id = 0; + let expires = 1; + let group_order = GroupOrder::Descending; + let content_exists = false; + let largest_group_id = None; + let largest_object_id = None; + let version_specific_parameter = + VersionSpecificParameter::AuthorizationInfo(AuthorizationInfo::new("test".to_string())); + let subscribe_parameters = vec![version_specific_parameter]; + + let expected_subscribe_ok = SubscribeOk::new( + subscribe_id, + expires, + group_order, + content_exists, + largest_group_id, + largest_object_id, + subscribe_parameters, + ); + + assert_eq!(depacketized_subscribe_ok, expected_subscribe_ok); + } + + #[test] + fn depacketize_subscribe_ok_content_exists() { let bytes_array = [ - 2, // Track Namespace(tuple): Number of elements - 4, // Track Namespace(b): Length - 116, 101, 115, 116, // Track Namespace(b): Value("test") - 4, // Track Namespace(b): Length - 116, 101, 115, 116, // Track Namespace(b): Value("test") - 10, // Track Name (b): Length - 116, 114, 97, 99, 107, 95, 110, 97, 109, - 101, // Track Name (b): Value("track_name") - 1, // Track ID (i) - 2, // Expires (i) + 0, // Subscribe ID (i) + 1, // Expires (i) + 1, // Group Order (8) + 1, // Content Exists (f) + 0, // Largest Group ID (i) + 5, // Largest Object ID (i) + 1, // Track Request Parameters (..): Number of Parameters + 2, // Parameter Type (i): AuthorizationInfo + 4, // Parameter Length (i) + 116, 101, 115, 116, // Parameter Value (..): test ]; let mut buf = BytesMut::with_capacity(bytes_array.len()); buf.extend_from_slice(&bytes_array); let depacketized_subscribe_ok = SubscribeOk::depacketize(&mut buf).unwrap(); - let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); - let track_name = "track_name".to_string(); - let track_id = 1; - let expires = 2; + let subscribe_id = 0; + let expires = 1; + let group_order = GroupOrder::Ascending; + let content_exists = true; + let largest_group_id = Some(0); + let largest_object_id = Some(5); + let version_specific_parameter = + VersionSpecificParameter::AuthorizationInfo(AuthorizationInfo::new("test".to_string())); + let subscribe_parameters = vec![version_specific_parameter]; + let expected_subscribe_ok = SubscribeOk::new( - track_namespace.clone(), - track_name.clone(), - track_id, + subscribe_id, expires, + group_order, + content_exists, + largest_group_id, + largest_object_id, + subscribe_parameters, ); assert_eq!(depacketized_subscribe_ok, expected_subscribe_ok); } } + +#[cfg(test)] +mod failure { + use crate::{ + messages::moqt_payload::MOQTPayload, + modules::messages::control_messages::subscribe_ok::SubscribeOk, + }; + use bytes::BytesMut; + + #[test] + fn depacketize_subscribe_ok_invalid_group_order() { + let bytes_array = [ + 0, // Subscribe ID (i) + 1, // Expires (i) + 20, // Group Order (8) + 0, // Content Exists (f) + 1, // Track Request Parameters (..): Number of Parameters + 2, // Parameter Type (i): AuthorizationInfo + 4, // Parameter Length (i) + 116, 101, 115, 116, // Parameter Value (..): test + ]; + let mut buf = BytesMut::with_capacity(bytes_array.len()); + buf.extend_from_slice(&bytes_array); + let depacketized_subscribe_ok = SubscribeOk::depacketize(&mut buf); + + assert!(depacketized_subscribe_ok.is_err()); + } + + #[test] + fn depacketize_subscribe_ok_invalid_content_exist() { + let bytes_array = [ + 0, // Subscribe ID (i) + 1, // Expires (i) + 1, // Group Order (8) + 5, // Content Exists (f) + 0, // Largest Group ID (i) + 5, // Largest Object ID (i) + 1, // Track Request Parameters (..): Number of Parameters + 2, // Parameter Type (i): AuthorizationInfo + 4, // Parameter Length (i) + 116, 101, 115, 116, // Parameter Value (..): test + ]; + let mut buf = BytesMut::with_capacity(bytes_array.len()); + buf.extend_from_slice(&bytes_array); + let depacketized_subscribe_ok = SubscribeOk::depacketize(&mut buf); + + assert!(depacketized_subscribe_ok.is_err()); + } +} diff --git a/moqt-server/src/modules/handlers/subscribe_ok_handler.rs b/moqt-server/src/modules/handlers/subscribe_ok_handler.rs index c5b74193..1a4f4d28 100644 --- a/moqt-server/src/modules/handlers/subscribe_ok_handler.rs +++ b/moqt-server/src/modules/handlers/subscribe_ok_handler.rs @@ -18,9 +18,9 @@ pub(crate) async fn subscribe_ok_handler( // Determine the SUBSCRIBER who sent the SUBSCRIBE using the track_namespace and track_name let subscriber_session_ids = track_namespace_manager_repository .get_subscriber_session_ids_by_track_namespace_and_track_name( - subscribe_ok_message.track_namespace().clone(), - subscribe_ok_message.track_name(), - ) + vec!["namespace".to_string()], + "name", + ) // Fix soon .await; match subscriber_session_ids { Some(session_ids) => { @@ -39,18 +39,10 @@ pub(crate) async fn subscribe_ok_handler( Ok(_) => { // Record the track_id upon success and activate the subscriber let _ = track_namespace_manager_repository - .set_track_id( - subscribe_ok_message.track_namespace().clone(), - subscribe_ok_message.track_name(), - subscribe_ok_message.track_id(), - ) + .set_track_id(vec!["namespace".to_string()], "name", 0) // Fix soon .await; let _ = track_namespace_manager_repository - .activate_subscriber( - subscribe_ok_message.track_namespace().clone(), - subscribe_ok_message.track_name(), - *session_id, - ) + .activate_subscriber(vec!["namespace".to_string()], "name", *session_id) // Fix soon .await; tracing::trace!("subscribe_ok_handler complete."); @@ -85,6 +77,10 @@ mod success { track_namespace_manager, TrackCommand, TrackNamespaceManager, }; use moqt_core::constants::StreamDirection; + use moqt_core::messages::control_messages::subscribe::GroupOrder; + use moqt_core::messages::control_messages::version_specific_parameters::{ + AuthorizationInfo, VersionSpecificParameter, + }; use moqt_core::messages::{ control_messages::subscribe_ok::SubscribeOk, moqt_payload::MOQTPayload, }; @@ -95,15 +91,24 @@ mod success { #[tokio::test] async fn normal_case() { // Generate SUBSCRIBE_OK message - let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); - let track_name = "track_name"; - let track_id = 1; - let expires = 2; + let subscribe_id = 0; + let expires = 1; + let group_order = GroupOrder::Ascending; + let content_exists = false; + let largest_group_id = None; + let largest_object_id = None; + let version_specific_parameter = + VersionSpecificParameter::AuthorizationInfo(AuthorizationInfo::new("test".to_string())); + let subscribe_parameters = vec![version_specific_parameter]; + let subscribe_ok = SubscribeOk::new( - track_namespace.clone(), - track_name.to_string(), - track_id, + subscribe_id, expires, + group_order, + content_exists, + largest_group_id, + largest_object_id, + subscribe_parameters, ); let mut buf = bytes::BytesMut::new(); subscribe_ok.packetize(&mut buf); @@ -118,10 +123,10 @@ mod success { let subscriber_session_id = 2; let _ = track_namespace_manager - .set_publisher(track_namespace.clone(), publisher_session_id) + .set_publisher(vec!["namespace".to_string()], publisher_session_id) .await; let _ = track_namespace_manager - .set_subscriber(track_namespace.clone(), subscriber_session_id, track_name) + .set_subscriber(vec!["namespace".to_string()], subscriber_session_id, "name") .await; // Generate SendStreamDispacher @@ -162,6 +167,10 @@ mod failure { track_namespace_manager, TrackCommand, TrackNamespaceManager, }; use moqt_core::constants::StreamDirection; + use moqt_core::messages::control_messages::subscribe::GroupOrder; + use moqt_core::messages::control_messages::version_specific_parameters::{ + AuthorizationInfo, VersionSpecificParameter, + }; use moqt_core::messages::{ control_messages::subscribe_ok::SubscribeOk, moqt_payload::MOQTPayload, }; @@ -172,15 +181,24 @@ mod failure { #[tokio::test] async fn relay_fail() { // Generate SUBSCRIBE_OK message - let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); - let track_name = "track_name"; - let track_id = 1; - let expires = 2; + let subscribe_id = 0; + let expires = 1; + let group_order = GroupOrder::Ascending; + let content_exists = false; + let largest_group_id = None; + let largest_object_id = None; + let version_specific_parameter = + VersionSpecificParameter::AuthorizationInfo(AuthorizationInfo::new("test".to_string())); + let subscribe_parameters = vec![version_specific_parameter]; + let subscribe_ok = SubscribeOk::new( - track_namespace.clone(), - track_name.to_string(), - track_id, + subscribe_id, expires, + group_order, + content_exists, + largest_group_id, + largest_object_id, + subscribe_parameters, ); let mut buf = bytes::BytesMut::new(); subscribe_ok.packetize(&mut buf); @@ -195,10 +213,10 @@ mod failure { let subscriber_session_id = 2; let _ = track_namespace_manager - .set_publisher(track_namespace.clone(), publisher_session_id) + .set_publisher(vec!["namespace".to_string()], publisher_session_id) .await; let _ = track_namespace_manager - .set_subscriber(track_namespace.clone(), subscriber_session_id, track_name) + .set_subscriber(vec!["namespace".to_string()], subscriber_session_id, "name") .await; // Generate SendStreamDispacher (without set sender) @@ -222,15 +240,24 @@ mod failure { #[tokio::test] async fn subscriber_not_found() { // Generate SUBSCRIBE_OK message - let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); - let track_name = "track_name"; - let track_id = 1; - let expires = 2; + let subscribe_id = 0; + let expires = 1; + let group_order = GroupOrder::Ascending; + let content_exists = false; + let largest_group_id = None; + let largest_object_id = None; + let version_specific_parameter = + VersionSpecificParameter::AuthorizationInfo(AuthorizationInfo::new("test".to_string())); + let subscribe_parameters = vec![version_specific_parameter]; + let subscribe_ok = SubscribeOk::new( - track_namespace.clone(), - track_name.to_string(), - track_id, + subscribe_id, expires, + group_order, + content_exists, + largest_group_id, + largest_object_id, + subscribe_parameters, ); let mut buf = bytes::BytesMut::new(); subscribe_ok.packetize(&mut buf); @@ -245,7 +272,7 @@ mod failure { let subscriber_session_id = 2; let _ = track_namespace_manager - .set_publisher(track_namespace.clone(), publisher_session_id) + .set_publisher(vec!["namespace".to_string()], publisher_session_id) .await; // Generate SendStreamDispacher From b7e224e7b10af4cdc4870e940043d84b36bf01b8 Mon Sep 17 00:00:00 2001 From: tetta maeda Date: Mon, 7 Oct 2024 16:53:19 +0900 Subject: [PATCH 04/17] impl: tests --- .../src/modules/track_namespace_manager.rs | 1066 ++++++++++------- 1 file changed, 643 insertions(+), 423 deletions(-) diff --git a/moqt-server/src/modules/track_namespace_manager.rs b/moqt-server/src/modules/track_namespace_manager.rs index 6cfe434a..ac20aca4 100644 --- a/moqt-server/src/modules/track_namespace_manager.rs +++ b/moqt-server/src/modules/track_namespace_manager.rs @@ -13,7 +13,12 @@ type SubscriberSessionId = usize; type PublisherSessionId = usize; type PublishedSubscribeId = u64; type SubscribedSubscribeId = u64; -struct PubSubRelation { + +type Consumers = HashMap; +type Producers = HashMap; + +#[derive(Debug, Clone)] +pub(crate) struct PubSubRelation { records: HashMap< (PublisherSessionId, PublishedSubscribeId), Vec<(SubscriberSessionId, SubscribedSubscribeId)>, @@ -70,8 +75,8 @@ impl PubSubRelation { pub(crate) async fn track_namespace_manager(rx: &mut mpsc::Receiver) { tracing::trace!("track_namespace_manager start"); - let mut consumers: HashMap = HashMap::new(); - let mut producers: HashMap = HashMap::new(); + let mut consumers: Consumers = HashMap::new(); + let mut producers: Producers = HashMap::new(); let mut pubsub_relation = PubSubRelation::new(); while let Some(cmd) = rx.recv().await { @@ -366,7 +371,7 @@ pub(crate) async fn track_namespace_manager(rx: &mut mpsc::Receiver { + let consumer = consumers.clone(); + let producer = producers.clone(); + let relation = pubsub_relation.clone(); + + resp.send(Ok((consumer, producer, relation))).unwrap(); + } } } @@ -596,7 +609,7 @@ pub(crate) enum TrackCommand { end_object: Option, resp: oneshot::Sender>, }, - RegisterPubSupRelation { + RegisterPubSubRelation { publisher_session_id: usize, published_subscribe_id: u64, subscriber_session_id: usize, @@ -622,6 +635,10 @@ pub(crate) enum TrackCommand { session_id: usize, resp: oneshot::Sender>, }, + #[cfg(test)] + GetNodeAndRelationClone { + resp: oneshot::Sender>, + }, } // Wrapper to encapsulate channel-related operations @@ -925,7 +942,7 @@ impl TrackNamespaceManagerRepository for TrackNamespaceManager { subscribed_subscribe_id: u64, ) -> Result<()> { let (resp_tx, resp_rx) = oneshot::channel::>(); - let cmd = RegisterPubSupRelation { + let cmd = RegisterPubSubRelation { publisher_session_id, published_subscribe_id, subscriber_session_id, @@ -1014,13 +1031,35 @@ impl TrackNamespaceManagerRepository for TrackNamespaceManager { } } +#[cfg(test)] +mod test_fn { + + use crate::modules::track_namespace_manager::{ + Consumers, Producers, PubSubRelation, TrackCommand, TrackNamespaceManager, + }; + use anyhow::Result; + use tokio::sync::oneshot; + + pub(crate) async fn get_node_and_relation_clone( + track_namespace_manager: &TrackNamespaceManager, + ) -> (Consumers, Producers, PubSubRelation) { + let (resp_tx, resp_rx) = oneshot::channel::>(); + let cmd = TrackCommand::GetNodeAndRelationClone { resp: resp_tx }; + track_namespace_manager.tx.send(cmd).await.unwrap(); + + resp_rx.await.unwrap().unwrap() + } +} + // TrackNamespaceManagerの中身を一新したのですべてのテストを新たに書き直す #[cfg(test)] mod success { + use crate::modules::track_namespace_manager::test_fn; use crate::modules::track_namespace_manager::{ track_namespace_manager, TrackCommand, TrackNamespaceManager, }; use moqt_core::messages::control_messages::subscribe::{FilterType, GroupOrder}; + use moqt_core::subscription_models::subscription_nodes::SubscriptionNodeRegistory; use moqt_core::subscription_models::subscriptions::Subscription; use moqt_core::TrackNamespaceManagerRepository; use tokio::sync::mpsc; @@ -1038,8 +1077,14 @@ mod success { let result = track_namespace_manager .setup_publisher(max_subscribe_id, publisher_session_id) .await; - assert!(result.is_ok()); + + // Check if the publisher is created + let (consumers, _, _) = + test_fn::get_node_and_relation_clone(&track_namespace_manager).await; + let length = consumers.len(); + + assert_eq!(length, 1); } #[tokio::test] @@ -1057,10 +1102,19 @@ mod success { .setup_publisher(max_subscribe_id, publisher_session_id) .await; let result = track_namespace_manager - .set_publisher_announced_namespace(track_namespace, publisher_session_id) + .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) .await; - assert!(result.is_ok()); + + // Check if the track_namespace is set + let (consumers, _, _) = + test_fn::get_node_and_relation_clone(&track_namespace_manager).await; + + let consumer = consumers.get(&publisher_session_id).unwrap(); + let announced_namespaces = consumer.get_namespaces().unwrap(); + let announced_namespace = announced_namespaces.first().unwrap().to_vec(); + + assert_eq!(announced_namespace, track_namespace); } #[tokio::test] @@ -1076,8 +1130,14 @@ mod success { let result = track_namespace_manager .setup_subscriber(max_subscribe_id, subscriber_session_id) .await; - assert!(result.is_ok()); + + // Check if the subscriber is created + let (_, producers, _) = + test_fn::get_node_and_relation_clone(&track_namespace_manager).await; + let length = producers.len(); + + assert_eq!(length, 1); } #[tokio::test] @@ -1095,11 +1155,11 @@ mod success { .setup_subscriber(max_subscribe_id, subscriber_session_id) .await; - let is_valid = track_namespace_manager + let result = track_namespace_manager .is_valid_subscriber_subscribe_id(subscribe_id, subscriber_session_id) - .await - .unwrap(); + .await; + let is_valid = result.unwrap(); assert!(is_valid); } @@ -1118,11 +1178,11 @@ mod success { .setup_subscriber(max_subscribe_id, subscriber_session_id) .await; - let is_valid = track_namespace_manager + let result = track_namespace_manager .is_valid_subscriber_subscribe_id(subscribe_id, subscriber_session_id) - .await - .unwrap(); + .await; + let is_valid = result.unwrap(); assert!(!is_valid); } @@ -1141,11 +1201,11 @@ mod success { .setup_subscriber(max_subscribe_id, subscriber_session_id) .await; - let is_valid = track_namespace_manager + let result = track_namespace_manager .is_valid_subscriber_track_alias(track_alias, subscriber_session_id) - .await - .unwrap(); + .await; + let is_valid = result.unwrap(); assert!(is_valid); } @@ -1190,11 +1250,12 @@ mod success { ) .await; - let is_valid = track_namespace_manager + let result = track_namespace_manager .is_valid_subscriber_track_alias(track_alias, subscriber_session_id) - .await - .unwrap(); + .await; + assert!(result.is_ok()); + let is_valid = result.unwrap(); assert!(!is_valid); } @@ -1241,11 +1302,12 @@ mod success { ) .await; - let is_existing = track_namespace_manager + let result = track_namespace_manager .is_track_existing(track_namespace, track_name) - .await - .unwrap(); + .await; + assert!(result.is_ok()); + let is_existing = result.unwrap(); assert!(is_existing); } @@ -1259,11 +1321,12 @@ mod success { tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); - let is_existing = track_namespace_manager + let result = track_namespace_manager .is_track_existing(track_namespace, track_name) - .await - .unwrap(); + .await; + assert!(result.is_ok()); + let is_existing = result.unwrap(); assert!(!is_existing); } @@ -1448,7 +1511,7 @@ mod success { (subscriber_session_ids[1], subscriber_subscribe_ids[1]), ]; - assert_eq!(list, expected_list,); + assert_eq!(list, expected_list); } #[tokio::test] @@ -1531,8 +1594,8 @@ mod success { subscriber_session_id, subscribe_id, track_alias, - track_namespace, - track_name, + track_namespace.clone(), + track_name.clone(), subscriber_priority, group_order, filter_type, @@ -1544,6 +1607,28 @@ mod success { .await; assert!(result.is_ok()); + + // Assert that the subscription is set + let (_, producers, _) = + test_fn::get_node_and_relation_clone(&track_namespace_manager).await; + let producer = producers.get(&subscriber_session_id).unwrap(); + let subscription = producer.get_subscription(subscribe_id).unwrap().unwrap(); + + let expected_subscription = Subscription::new( + track_alias, + track_namespace, + track_name, + subscriber_priority, + group_order, + filter_type, + start_group, + start_object, + end_group, + end_object, + None, + ); + + assert_eq!(subscription, expected_subscription); } #[tokio::test] @@ -1584,15 +1669,42 @@ mod success { .await; assert!(result.is_ok()); + + let (publisher_subscribe_id, publisher_track_alias) = result.unwrap(); + + // Assert that the subscription is set + let (consumers, _, _) = + test_fn::get_node_and_relation_clone(&track_namespace_manager).await; + let consumer = consumers.get(&publisher_session_id).unwrap(); + let subscription = consumer + .get_subscription(publisher_subscribe_id) + .unwrap() + .unwrap(); + + let expected_subscription = Subscription::new( + publisher_track_alias, + track_namespace, + track_name, + subscriber_priority, + group_order, + filter_type, + start_group, + start_object, + end_group, + end_object, + None, + ); + + assert_eq!(subscription, expected_subscription); } #[tokio::test] async fn register_pubsup_relation() { let max_subscribe_id = 10; let publisher_session_id = 1; - let subscriber_session_id = 2; - let subscriber_subscribe_id = 3; - let subscriber_track_alias = 4; + let subscriber_session_ids = [2, 3]; + let subscriber_subscribe_ids = [4, 5]; + let subscriber_track_aliases = [6, 7]; let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); let track_name = "track_name".to_string(); let subscriber_priority = 0; @@ -1609,6 +1721,7 @@ mod success { let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); + // pub 1 <- sub 2, 3 let _ = track_namespace_manager .setup_publisher(max_subscribe_id, publisher_session_id) .await; @@ -1631,35 +1744,54 @@ mod success { .await .unwrap(); - let _ = track_namespace_manager - .setup_subscriber(max_subscribe_id, subscriber_session_id) - .await; - let _ = track_namespace_manager - .set_subscriber_subscription( - subscriber_session_id, - subscriber_subscribe_id, - subscriber_track_alias, - track_namespace.clone(), - track_name.clone(), - subscriber_priority, - group_order, - filter_type, - start_group, - start_object, - end_group, - end_object, - ) - .await; - let result = track_namespace_manager - .register_pubsup_relation( - publisher_session_id, - publisher_subscribe_id, - subscriber_session_id, - subscriber_subscribe_id, - ) - .await; + for i in [0, 1] { + let _ = track_namespace_manager + .setup_subscriber(max_subscribe_id, subscriber_session_ids[i]) + .await; + let _ = track_namespace_manager + .set_subscriber_subscription( + subscriber_session_ids[i], + subscriber_subscribe_ids[i], + subscriber_track_aliases[i], + track_namespace.clone(), + track_name.clone(), + subscriber_priority, + group_order, + filter_type, + start_group, + start_object, + end_group, + end_object, + ) + .await; + let result = track_namespace_manager + .register_pubsup_relation( + publisher_session_id, + publisher_subscribe_id, + subscriber_session_ids[i], + subscriber_subscribe_ids[i], + ) + .await; - assert!(result.is_ok()); + assert!(result.is_ok()); + } + + // Assert that the relation is registered + let (_, _, pubsub_relation) = + test_fn::get_node_and_relation_clone(&track_namespace_manager).await; + + let subscriber = pubsub_relation + .records + .get(&(publisher_session_id, publisher_subscribe_id)) + .unwrap() + .to_vec(); + + let expected_subscriber = vec![ + (subscriber_session_ids[0], subscriber_subscribe_ids[0]), + (subscriber_session_ids[1], subscriber_subscribe_ids[1]), + ]; + + assert_eq!(subscriber, expected_subscriber); } #[tokio::test] @@ -1709,6 +1841,14 @@ mod success { .unwrap(); assert!(activate_occured); + + // Assert that the subscription is active + let (_, producers, _) = + test_fn::get_node_and_relation_clone(&track_namespace_manager).await; + let producer = producers.get(&subscriber_session_id).unwrap(); + let subscription = producer.get_subscription(subscribe_id).unwrap().unwrap(); + + assert!(subscription.is_active()); } #[tokio::test] @@ -1755,6 +1895,17 @@ mod success { .unwrap(); assert!(activate_occured); + + // Assert that the subscription is active + let (consumers, _, _) = + test_fn::get_node_and_relation_clone(&track_namespace_manager).await; + let consumer = consumers.get(&publisher_session_id).unwrap(); + let subscription = consumer + .get_subscription(publisher_subscribe_id) + .unwrap() + .unwrap(); + + assert!(subscription.is_active()); } #[tokio::test] @@ -1778,8 +1929,42 @@ mod success { let result = track_namespace_manager .delete_publisher_announced_namespace(track_namespace, publisher_session_id) .await; + assert!(result.is_ok()); + + let delete_occured = result.unwrap(); + assert!(delete_occured); + + // Assert that the announced namespace is deleted + let (consumers, _, _) = + test_fn::get_node_and_relation_clone(&track_namespace_manager).await; + let consumer = consumers.get(&publisher_session_id).unwrap(); + let announced_namespaces = consumer.get_namespaces().unwrap().to_vec(); + + assert!(announced_namespaces.is_empty()); + } + + #[tokio::test] + async fn delete_publisher_announced_namespace_not_exists() { + let max_subscribe_id = 10; + let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); + let publisher_session_id = 1; + + // Start track management thread + let (track_tx, mut track_rx) = mpsc::channel::(1024); + tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); + + let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); + let _ = track_namespace_manager + .setup_publisher(max_subscribe_id, publisher_session_id) + .await; + let result = track_namespace_manager + .delete_publisher_announced_namespace(track_namespace, publisher_session_id) + .await; assert!(result.is_ok()); + + let delete_occured = result.unwrap(); + assert!(!delete_occured); } #[tokio::test] @@ -1791,7 +1976,7 @@ mod success { ]; let publisher_session_ids = [1, 2]; let mut publisher_subscribe_ids = vec![]; - let mut subscriber_session_ids = vec![2, 3, 4]; + let subscriber_session_ids = [2, 3, 4]; let subscriber_subscribe_ids = [2, 3, 4]; let subscriber_track_aliases = [2, 3, 4]; let track_name = "test_name".to_string(); @@ -1895,19 +2080,19 @@ mod success { subscriber_subscribe_ids[j], ) .await; - // let _ = track_namespace_manager - // .activate_subscriber_subscription( - // subscriber_session_ids[j], - // subscriber_subscribe_ids[j], - // ) - // .await; - - // let _ = track_namespace_manager - // .activate_publisher_subscription( - // publisher_session_ids[i], - // publisher_subscribe_ids[i], - // ) - // .await; + let _ = track_namespace_manager + .activate_subscriber_subscription( + subscriber_session_ids[j], + subscriber_subscribe_ids[j], + ) + .await; + + let _ = track_namespace_manager + .activate_publisher_subscription( + publisher_session_ids[i], + publisher_subscribe_ids[i], + ) + .await; } } @@ -1920,368 +2105,403 @@ mod success { subscriber_subscribe_ids[0], ) .await; - // let _ = track_namespace_manager - // .activate_subscriber_subscription( - // subscriber_session_ids[0], - // subscriber_subscribe_ids[0], - // ) - // .await; + let _ = track_namespace_manager + .activate_subscriber_subscription( + subscriber_session_ids[0], + subscriber_subscribe_ids[0], + ) + .await; - // let _ = track_namespace_manager - // .activate_publisher_subscription(publisher_session_ids[0], publisher_subscribe_ids[0]) - // .await; + let _ = track_namespace_manager + .activate_publisher_subscription(publisher_session_ids[0], publisher_subscribe_ids[0]) + .await; // Delete: pub 2, sub 2 // Remain: pub 1 <- sub 3, 4 - let _ = track_namespace_manager + let result = track_namespace_manager .delete_client(subscriber_session_ids[0]) .await; + assert!(result.is_ok()); + + let delete_occured = result.unwrap(); + assert!(delete_occured); - // Test for subscriber + let (consumers, producers, pubsub_relation) = + test_fn::get_node_and_relation_clone(&track_namespace_manager).await; + + // Assert that sub 2 is deleted // Remain: sub 3, 4 - subscriber_session_ids.remove(0); + let sub2 = producers.get(&subscriber_session_ids[0]); + assert!(sub2.is_none()); + + let sub3 = producers.get(&subscriber_session_ids[1]); + assert!(sub3.is_some()); + + let sub4 = producers.get(&subscriber_session_ids[2]); + assert!(sub4.is_some()); + + // Assert that pub 2 is deleted + // Remain: pub 1 + let pub1 = consumers.get(&publisher_session_ids[1]); + assert!(pub1.is_none()); + + let pub2 = consumers.get(&publisher_session_ids[0]); + assert!(pub2.is_some()); + + // Assert that the relation is deleted + // Remain: pub 1 <- sub 3, 4 + let pub1_relation = pubsub_relation + .records + .get(&(publisher_session_ids[0], publisher_subscribe_ids[0])); + assert!(pub1_relation.is_some()); + + let pub2_relation = pubsub_relation + .records + .get(&(publisher_session_ids[1], publisher_subscribe_ids[1])); + assert!(pub2_relation.is_none()); + } + + #[tokio::test] + async fn delete_client_not_exists() { + let session_id = 1; + + // Start track management thread + let (track_tx, mut track_rx) = mpsc::channel::(1024); + tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); + + let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); + + let result = track_namespace_manager.delete_client(session_id).await; + assert!(result.is_ok()); + + let delete_occured = result.unwrap(); + assert!(!delete_occured); + } +} + +#[cfg(test)] +mod failure { + use crate::modules::track_namespace_manager::{ + track_namespace_manager, TrackCommand, TrackNamespaceManager, + }; + use moqt_core::messages::control_messages::subscribe::{FilterType, GroupOrder}; + use moqt_core::TrackNamespaceManagerRepository; + use tokio::sync::mpsc; + + #[tokio::test] + async fn setup_publisher_already_exist() { + let max_subscribe_id = 10; + let publisher_session_id = 1; + + // Start track management thread + let (track_tx, mut track_rx) = mpsc::channel::(1024); + tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); + + let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); + let _ = track_namespace_manager + .setup_publisher(max_subscribe_id, publisher_session_id) + .await; + + // Register the same publisher + let result = track_namespace_manager + .setup_publisher(max_subscribe_id, publisher_session_id) + .await; + + assert!(result.is_err()); + } - // TODO: Replace not requesting version - let mut delete_subscriber_result = track_namespace_manager + #[tokio::test] + async fn set_publisher_announced_namespace_already_exist() { + let max_subscribe_id = 10; + let publisher_session_id = 1; + let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); + + // Start track management thread + let (track_tx, mut track_rx) = mpsc::channel::(1024); + tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); + + let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); + let _ = track_namespace_manager + .setup_publisher(max_subscribe_id, publisher_session_id) + .await; + let _ = track_namespace_manager + .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) + .await; + + // Register the same track namespace + let result = track_namespace_manager + .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) + .await; + + assert!(result.is_err()); + } + + #[tokio::test] + async fn setup_subscriber_already_exist() { + let max_subscribe_id = 10; + let subscriber_session_id = 1; + + // Start track management thread + let (track_tx, mut track_rx) = mpsc::channel::(1024); + tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); + + let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); + let _ = track_namespace_manager + .setup_subscriber(max_subscribe_id, subscriber_session_id) + .await; + + // Register the same subscriber + let result = track_namespace_manager + .setup_subscriber(max_subscribe_id, subscriber_session_id) + .await; + + assert!(result.is_err()); + } + + #[tokio::test] + async fn is_valid_subscriber_subscribe_id_subscriber_not_found() { + let max_subscribe_id = 10; + let subscriber_session_id = 1; + let subscriber_subscribe_id = 0; + let invalid_subscriber_session_id = 2; + + // Start track management thread + let (track_tx, mut track_rx) = mpsc::channel::(1024); + tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); + + let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); + let _ = track_namespace_manager + .setup_subscriber(max_subscribe_id, subscriber_session_id) + .await; + + let result = track_namespace_manager + .is_valid_subscriber_subscribe_id( + subscriber_subscribe_id, + invalid_subscriber_session_id, + ) + .await; + + assert!(result.is_err()); + } + + #[tokio::test] + async fn is_valid_subscriber_track_alias_subscriber_not_found() { + let max_subscribe_id = 10; + let subscriber_session_id = 1; + let subscriber_track_alias = 0; + let invalid_subscriber_session_id = 2; + + // Start track management thread + let (track_tx, mut track_rx) = mpsc::channel::(1024); + tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); + + let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); + let _ = track_namespace_manager + .setup_subscriber(max_subscribe_id, subscriber_session_id) + .await; + + let result = track_namespace_manager + .is_valid_subscriber_track_alias(subscriber_track_alias, invalid_subscriber_session_id) + .await; + + assert!(result.is_err()); + } + + #[tokio::test] + async fn get_requesting_subscriber_session_ids_and_subscribe_ids_publisher_not_found() { + let publisher_session_id = 0; + let publisher_subscribe_id = 0; + // Start track management thread + let (track_tx, mut track_rx) = mpsc::channel::(1024); + tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); + + let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); + + let result = track_namespace_manager .get_requesting_subscriber_session_ids_and_subscribe_ids( - publisher_subscribe_ids[0], - publisher_session_ids[0], + publisher_subscribe_id, + publisher_session_id, ) - .await - .unwrap() - .unwrap(); + .await; - delete_subscriber_result.sort(); + assert!(result.is_err()); + } - let expected_list = vec![ - (subscriber_session_ids[0], subscriber_subscribe_ids[1]), - (subscriber_session_ids[1], subscriber_subscribe_ids[2]), - ]; + #[tokio::test] + async fn get_publisher_subscribe_id_publisher_not_found() { + let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); + let track_name = "track_name".to_string(); + let invalid_publisher_session_id = 1; - assert_eq!(delete_subscriber_result, expected_list); + // Start track management thread + let (track_tx, mut track_rx) = mpsc::channel::(1024); + tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); - // Test for publisher - // Remain: pub 1 - let delete_publisher_result_1 = track_namespace_manager - .get_publisher_session_id(track_namespaces[0].clone()) - .await - .unwrap() - .unwrap(); - let delete_publisher_result_2 = track_namespace_manager - .get_publisher_session_id(track_namespaces[1].clone()) - .await - .unwrap(); + let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); - assert_eq!(delete_publisher_result_1, publisher_session_ids[0]); - assert!(delete_publisher_result_2.is_none()); + let result = track_namespace_manager + .get_publisher_subscribe_id(track_namespace, track_name, invalid_publisher_session_id) + .await; + + assert!(result.is_err()); + } + + #[tokio::test] + async fn set_subscriber_subscription_subscriber_not_found() { + let max_subscribe_id = 10; + let subscriber_session_id = 1; + let subscribe_id = 0; + let track_alias = 0; + let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); + let track_name = "track_name".to_string(); + let subscriber_priority = 0; + let group_order = GroupOrder::Ascending; + let filter_type = FilterType::AbsoluteStart; + let start_group = Some(0); + let start_object = Some(0); + let end_group = None; + let end_object = None; + let invalid_subscriber_session_id = 2; + + // Start track management thread + let (track_tx, mut track_rx) = mpsc::channel::(1024); + tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); + + let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); + + let _ = track_namespace_manager + .setup_subscriber(max_subscribe_id, subscriber_session_id) + .await; + + let result = track_namespace_manager + .set_subscriber_subscription( + invalid_subscriber_session_id, + subscribe_id, + track_alias, + track_namespace, + track_name, + subscriber_priority, + group_order, + filter_type, + start_group, + start_object, + end_group, + end_object, + ) + .await; + + assert!(result.is_err()); } -} -// #[cfg(test)] -// mod failure { -// use crate::modules::track_namespace_manager::{ -// track_namespace_manager, TrackNamespaceManager, TrackNamespaceManagerRepository, -// }; -// use crate::TrackCommand; -// use tokio::sync::mpsc; - -// #[tokio::test] -// async fn setup_publisher_already_exist() { -// let max_subscribe_id = 10; -// let publisher_session_id = 1; - -// // Start track management thread -// let (track_tx, mut track_rx) = mpsc::channel::(1024); -// tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); - -// let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); -// let _ = track_namespace_manager -// .setup_publisher(max_subscribe_id, publisher_session_id) -// .await; - -// // Register the same publisher -// let result = track_namespace_manager -// .setup_publisher(max_subscribe_id, publisher_session_id) -// .await; - -// assert!(result.is_err()); -// } - -// #[tokio::test] -// async fn set_publisher_announced_namespace_already_exist() { -// let max_subscribe_id = 10; -// let publisher_session_id = 1; -// let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); - -// // Start track management thread -// let (track_tx, mut track_rx) = mpsc::channel::(1024); -// tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); - -// let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); -// let _ = track_namespace_manager -// .setup_publisher(max_subscribe_id, publisher_session_id) -// .await; -// let _ = track_namespace_manager -// .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) -// .await; - -// // Register the same track namespace -// let result = track_namespace_manager -// .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) -// .await; - -// assert!(result.is_err()); -// } - -// #[tokio::test] -// async fn setup_subscriber_already_exist() { -// let max_subscribe_id = 10; -// let subscriber_session_id = 1; - -// // Start track management thread -// let (track_tx, mut track_rx) = mpsc::channel::(1024); -// tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); - -// let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); -// let _ = track_namespace_manager -// .setup_subscriber(max_subscribe_id, subscriber_session_id) -// .await; - -// // Register the same subscriber -// let result = track_namespace_manager -// .setup_subscriber(max_subscribe_id, subscriber_session_id) -// .await; - -// assert!(result.is_err()); -// } - -// #[tokio::test] -// async fn is_valid_subscriber_subscribe_id_subscriber_not_found() { -// let max_subscribe_id = 10; -// let subscriber_session_id = 1; -// let subscriber_subscribe_id = 0; -// let invalid_subscriber_session_id = 2; - -// // Start track management thread -// let (track_tx, mut track_rx) = mpsc::channel::(1024); -// tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); - -// let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); -// let _ = track_namespace_manager -// .setup_subscriber(max_subscribe_id, subscriber_session_id) -// .await; - -// let result = track_namespace_manager -// .is_valid_subscriber_subscribe_id( -// subscriber_subscribe_id, -// invalid_subscriber_session_id, -// ) -// .await; - -// assert!(result.is_err()); -// } - -// #[tokio::test] -// async fn is_valid_subscriber_track_alias_subscriber_not_found() { -// let max_subscribe_id = 10; -// let subscriber_session_id = 1; -// let subscriber_track_alias = 0; -// let invalid_subscriber_session_id = 2; - -// // Start track management thread -// let (track_tx, mut track_rx) = mpsc::channel::(1024); -// tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); - -// let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); -// let _ = track_namespace_manager -// .setup_subscriber(max_subscribe_id, subscriber_session_id) -// .await; - -// let result = track_namespace_manager -// .is_valid_subscriber_track_alias(subscriber_track_alias, invalid_subscriber_session_id) -// .await; - -// assert!(result.is_err()); -// } -// } - -// #[tokio::test] -// async fn delete_publisher_by_namespace_not_found() { -// let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); - -// // Start track management thread -// let (track_tx, mut track_rx) = mpsc::channel::(1024); -// tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); - -// let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); - -// let result = track_namespace_manager -// .delete_publisher_by_namespace(track_namespace) -// .await; - -// assert!(result.is_err()); -// } - -// #[tokio::test] -// async fn get_publisher_session_id_not_found() { -// let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); - -// // Start track management thread -// let (track_tx, mut track_rx) = mpsc::channel::(1024); -// tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); - -// let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); - -// let result = track_namespace_manager -// .get_publisher_session_id(track_namespace) -// .await; - -// assert_eq!(result, None); -// } - -// #[tokio::test] -// async fn set_subscriber_already_exist() { -// let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); -// let publisher_session_id = 1; -// let subscriber_session_id = 2; -// let track_name = "test_name"; - -// // Start track management thread -// let (track_tx, mut track_rx) = mpsc::channel::(1024); -// tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); - -// let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); -// let _ = track_namespace_manager -// .set_publisher(track_namespace.clone(), publisher_session_id) -// .await; -// let _ = track_namespace_manager -// .set_subscriber(track_namespace.clone(), subscriber_session_id, track_name) -// .await; - -// // Register the same subscriber -// let result = track_namespace_manager -// .set_subscriber(track_namespace.clone(), subscriber_session_id, track_name) -// .await; - -// assert!(result.is_err()); -// } - -// #[tokio::test] -// async fn set_subscriber_track_namespace_not_found() { -// let track_namespace_1 = Vec::from(["test".to_string(), "test".to_string()]); -// let track_namespace_2 = Vec::from(["unexisted".to_string(), "namespace".to_string()]); -// let publisher_session_id = 1; -// let subscriber_session_id = 2; -// let track_name = "test_name"; - -// // Start track management thread -// let (track_tx, mut track_rx) = mpsc::channel::(1024); -// tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); - -// let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); -// let _ = track_namespace_manager -// .set_publisher(track_namespace_1, publisher_session_id) -// .await; - -// // Register a new subscriber with a new track -// let result = track_namespace_manager -// .set_subscriber(track_namespace_2, subscriber_session_id, track_name) -// .await; - -// assert!(result.is_err()); -// } - -// #[tokio::test] -// async fn delete_subscriber_subscriber_id_not_found() { -// let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); -// let publisher_session_id = 1; -// let subscriber_session_id_1 = 2; -// let subscriber_session_id_2 = 3; -// let track_name = "test_name"; - -// // Start track management thread -// let (track_tx, mut track_rx) = mpsc::channel::(1024); -// tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); - -// let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); -// let _ = track_namespace_manager -// .set_publisher(track_namespace.clone(), publisher_session_id) -// .await; -// let _ = track_namespace_manager -// .set_subscriber(track_namespace.clone(), subscriber_session_id_1, track_name) -// .await; - -// let result = track_namespace_manager -// .delete_subscriber(track_namespace.clone(), track_name, subscriber_session_id_2) -// .await; - -// assert!(result.is_err()); -// } - -// #[tokio::test] -// async fn delete_subscriber_track_name_not_found() { -// let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); -// let publisher_session_id = 1; -// let subscriber_session_id = 2; -// let track_name = "test_name"; - -// // Start track management thread -// let (track_tx, mut track_rx) = mpsc::channel::(1024); -// tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); - -// let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); -// let _ = track_namespace_manager -// .set_publisher(track_namespace.clone(), publisher_session_id) -// .await; - -// let result = track_namespace_manager -// .delete_subscriber(track_namespace.clone(), track_name, subscriber_session_id) -// .await; - -// assert!(result.is_err()); -// } - -// #[tokio::test] -// async fn delete_subscriber_track_namespace_not_found() { -// let track_namespace_1 = Vec::from(["test".to_string(), "test".to_string()]); -// let track_namespace_2 = Vec::from(["unexisted".to_string(), "namespace".to_string()]); -// let publisher_session_id = 1; -// let subscriber_session_id = 2; -// let track_name = "test_name"; - -// // Start track management thread -// let (track_tx, mut track_rx) = mpsc::channel::(1024); -// tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); - -// let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); -// let _ = track_namespace_manager -// .set_publisher(track_namespace_1.clone(), publisher_session_id) -// .await; -// let _ = track_namespace_manager -// .set_subscriber(track_namespace_1.clone(), subscriber_session_id, track_name) -// .await; - -// let result = track_namespace_manager -// .delete_subscriber(track_namespace_2.clone(), track_name, subscriber_session_id) -// .await; - -// assert!(result.is_err()); -// } - -// #[tokio::test] -// async fn get_subscriber_session_ids_by_track_id_not_found() { -// let track_id = 3; - -// // Start track management thread -// let (track_tx, mut track_rx) = mpsc::channel::(1024); -// tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); - -// let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); - -// let result = track_namespace_manager -// .get_subscriber_session_ids_by_track_id(track_id) -// .await; - -// assert_eq!(result, None); -// } -// } + #[tokio::test] + async fn set_publisher_subscription_publisher_not_found() { + let max_subscribe_id = 10; + let publisher_session_id = 1; + let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); + let track_name = "track_name".to_string(); + let subscriber_priority = 0; + let group_order = GroupOrder::Ascending; + let filter_type = FilterType::AbsoluteStart; + let start_group = Some(0); + let start_object = Some(0); + let end_group = None; + let end_object = None; + let invalid_publisher_session_id = 2; + + // Start track management thread + let (track_tx, mut track_rx) = mpsc::channel::(1024); + tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); + + let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); + + let _ = track_namespace_manager + .setup_publisher(max_subscribe_id, publisher_session_id) + .await; + + let result = track_namespace_manager + .set_publisher_subscription( + invalid_publisher_session_id, + track_namespace, + track_name, + subscriber_priority, + group_order, + filter_type, + start_group, + start_object, + end_group, + end_object, + ) + .await; + + assert!(result.is_err()); + } + + #[tokio::test] + async fn activate_subscriber_subscription_subscriber_not_found() { + let max_subscribe_id = 10; + let subscriber_session_id = 1; + let subscribe_id = 0; + let invalid_subscriber_session_id = 2; + + // Start track management thread + let (track_tx, mut track_rx) = mpsc::channel::(1024); + tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); + + let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); + let _ = track_namespace_manager + .setup_subscriber(max_subscribe_id, subscriber_session_id) + .await; + + let result = track_namespace_manager + .activate_subscriber_subscription(invalid_subscriber_session_id, subscribe_id) + .await; + + assert!(result.is_err()); + } + + #[tokio::test] + async fn activate_publisher_subscription_publisher_not_found() { + let max_subscribe_id = 10; + let publisher_session_id = 1; + let publisher_subscribe_id = 0; + let invalid_publisher_session_id = 2; + + // Start track management thread + let (track_tx, mut track_rx) = mpsc::channel::(1024); + tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); + + let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); + let _ = track_namespace_manager + .setup_publisher(max_subscribe_id, publisher_session_id) + .await; + + let result = track_namespace_manager + .activate_publisher_subscription(invalid_publisher_session_id, publisher_subscribe_id) + .await; + + assert!(result.is_err()); + } + + #[tokio::test] + async fn delete_publisher_announced_namespace_publisher_not_found() { + let max_subscribe_id = 10; + let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); + let publisher_session_id = 1; + let invalid_publisher_session_id = 2; + + // Start track management thread + let (track_tx, mut track_rx) = mpsc::channel::(1024); + tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); + + let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); + let _ = track_namespace_manager + .setup_publisher(max_subscribe_id, publisher_session_id) + .await; + + let result = track_namespace_manager + .delete_publisher_announced_namespace(track_namespace, invalid_publisher_session_id) + .await; + + assert!(result.is_err()); + } +} From bc2f215b6f191d9851ea12de7c1b3d523c1a5a60 Mon Sep 17 00:00:00 2001 From: tetta maeda Date: Mon, 7 Oct 2024 21:10:30 +0900 Subject: [PATCH 05/17] impl: unit test --- .../messages/control_messages/subscribe_ok.rs | 4 + .../subscription_models/subscription_nodes.rs | 8 +- .../track_namespace_manager_repository.rs | 6 +- .../src/modules/handlers/announce_handler.rs | 21 +- .../src/modules/handlers/subscribe_handler.rs | 1151 +++++++++++------ .../modules/handlers/subscribe_ok_handler.rs | 921 +++++++++---- .../server_processes/subscribe_message.rs | 2 +- .../src/modules/track_namespace_manager.rs | 78 +- 8 files changed, 1494 insertions(+), 697 deletions(-) diff --git a/moqt-core/src/modules/messages/control_messages/subscribe_ok.rs b/moqt-core/src/modules/messages/control_messages/subscribe_ok.rs index 0f404ced..1dce87ac 100644 --- a/moqt-core/src/modules/messages/control_messages/subscribe_ok.rs +++ b/moqt-core/src/modules/messages/control_messages/subscribe_ok.rs @@ -47,6 +47,10 @@ impl SubscribeOk { pub fn subscribe_id(&self) -> u64 { self.subscribe_id } + + pub fn replace_subscribe_id(&mut self, subscribe_id: u64) { + self.subscribe_id = subscribe_id; + } } impl MOQTPayload for SubscribeOk { diff --git a/moqt-core/src/modules/subscription_models/subscription_nodes.rs b/moqt-core/src/modules/subscription_models/subscription_nodes.rs index 3e3d71c5..41c28c74 100644 --- a/moqt-core/src/modules/subscription_models/subscription_nodes.rs +++ b/moqt-core/src/modules/subscription_models/subscription_nodes.rs @@ -1,14 +1,13 @@ use crate::messages::control_messages::subscribe::{FilterType, GroupOrder}; use crate::subscription_models::subscriptions::Subscription; -use crate::subscription_models::tracks::ForwardingPreference; use anyhow::{bail, Result}; -use std::any; use std::collections::HashMap; type SubscribeId = u64; type TrackNamespace = Vec; type TrackAlias = u64; +#[derive(Debug, Clone, PartialEq, Eq)] pub struct Consumer { max_subscriber_id: u64, announced_namespaces: Vec, @@ -27,6 +26,7 @@ impl Consumer { } } +#[derive(Debug, Clone, PartialEq, Eq)] pub struct Producer { max_subscriber_id: u64, announcing_namespaces: Vec, @@ -202,7 +202,7 @@ impl SubscriptionNodeRegistory for Consumer { .any(|subscription| subscription.get_track_alias() == track_alias) } fn find_unused_subscribe_id_and_track_alias(&self) -> Result<(SubscribeId, TrackAlias)> { - for subscribe_id in 1..=self.max_subscriber_id { + for subscribe_id in 0..=self.max_subscriber_id { if !self.subscriptions.contains_key(&subscribe_id) { for track_alias in 0.. { if !self @@ -305,7 +305,7 @@ impl SubscriptionNodeRegistory for Producer { start_object, end_group, end_object, - Some(ForwardingPreference::Track), + None, ); self.subscriptions.insert(subscribe_id, subscription); diff --git a/moqt-core/src/modules/track_namespace_manager_repository.rs b/moqt-core/src/modules/track_namespace_manager_repository.rs index 61f05ebe..3588dea9 100644 --- a/moqt-core/src/modules/track_namespace_manager_repository.rs +++ b/moqt-core/src/modules/track_namespace_manager_repository.rs @@ -44,7 +44,7 @@ pub trait TrackNamespaceManagerRepository: Send + Sync { -> Result>; async fn get_requesting_subscriber_session_ids_and_subscribe_ids( &self, - published_subscribe_id: u64, + publisher_subscribe_id: u64, publisher_session_id: usize, ) -> Result>>; async fn get_publisher_subscribe_id( @@ -86,9 +86,9 @@ pub trait TrackNamespaceManagerRepository: Send + Sync { async fn register_pubsup_relation( &self, publisher_session_id: usize, - published_subscribe_id: u64, + publisher_subscribe_id: u64, subscriber_session_id: usize, - subscribed_subscribe_id: u64, + subscriber_subscribe_id: u64, ) -> Result<()>; async fn activate_subscriber_subscription( &self, diff --git a/moqt-server/src/modules/handlers/announce_handler.rs b/moqt-server/src/modules/handlers/announce_handler.rs index 74d5d56c..199f701c 100644 --- a/moqt-server/src/modules/handlers/announce_handler.rs +++ b/moqt-server/src/modules/handlers/announce_handler.rs @@ -58,6 +58,7 @@ mod success { track_namespace_manager, TrackCommand, TrackNamespaceManager, }; use moqt_core::messages::moqt_payload::MOQTPayload; + use moqt_core::TrackNamespaceManagerRepository; use moqt_core::{ messages::control_messages::{ announce::Announce, @@ -84,8 +85,8 @@ mod success { announce_message.packetize(&mut buf); // Generate client - let stable_id = 0; - let mut client = MOQTClient::new(stable_id as usize); + let publisher_session_id = 0; + let mut client = MOQTClient::new(publisher_session_id); // Generate TrackNamespaceManager let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); @@ -93,6 +94,12 @@ mod success { let mut track_namespace_manager: TrackNamespaceManager = TrackNamespaceManager::new(track_namespace_tx); + let max_subscribe_id = 10; + + let _ = track_namespace_manager + .setup_publisher(max_subscribe_id, publisher_session_id) + .await; + // Execute announce_handler and get result let result = announce_handler(announce_message, &mut client, &mut track_namespace_manager) .await @@ -138,8 +145,8 @@ mod failure { announce_message.packetize(&mut buf); // Generate client - let stable_id = 0; - let mut client = MOQTClient::new(stable_id as usize); + let publisher_session_id = 0; + let mut client = MOQTClient::new(publisher_session_id); // Generate TrackNamespaceManager let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); @@ -147,6 +154,12 @@ mod failure { let mut track_namespace_manager: TrackNamespaceManager = TrackNamespaceManager::new(track_namespace_tx); + let max_subscribe_id = 10; + + let _ = track_namespace_manager + .setup_publisher(max_subscribe_id, publisher_session_id) + .await; + // Set the duplicated publisher in advance let _ = track_namespace_manager .set_publisher_announced_namespace( diff --git a/moqt-server/src/modules/handlers/subscribe_handler.rs b/moqt-server/src/modules/handlers/subscribe_handler.rs index e41a9559..7e329d0c 100644 --- a/moqt-server/src/modules/handlers/subscribe_handler.rs +++ b/moqt-server/src/modules/handlers/subscribe_handler.rs @@ -3,10 +3,7 @@ use anyhow::{bail, Result}; use moqt_core::{ constants::StreamDirection, messages::{ - control_messages::{ - subscribe::{self, Subscribe}, - subscribe_ok::SubscribeOk, - }, + control_messages::{subscribe::Subscribe, subscribe_ok::SubscribeOk}, moqt_payload::MOQTPayload, }, MOQTClient, SendStreamDispatcherRepository, TrackNamespaceManagerRepository, @@ -53,7 +50,26 @@ pub(crate) async fn subscribe_handler( ) .await; - // TODO: generate and return subscribe_ok message + // Generate and return subscribe_ok message + + // TODO: Implement the get object info when implement cache mechanism + let expires = 0; + let content_exist = false; + let largest_group_id = None; + let largest_object_id = None; + let subscribe_parameters = vec![]; + + let subscribe_ok = SubscribeOk::new( + subscribe_message.subscribe_id(), + expires, + subscribe_message.group_order(), + content_exist, + largest_group_id, + largest_object_id, + subscribe_parameters, + ); + + return Ok(Some(subscribe_ok)); } // Since only the track_namespace is recorded in ANNOUNCE, use track_namespace to determine the publisher @@ -64,7 +80,7 @@ pub(crate) async fn subscribe_handler( .unwrap(); match publisher_session_id { Some(session_id) => { - let (upstream_subscribe_id, upstream_track_alias) = + let (publisher_subscribe_id, publisher_track_alias) = match set_subscriber_and_publisher_subscription( track_namespace_manager_repository, &subscribe_message, @@ -73,8 +89,8 @@ pub(crate) async fn subscribe_handler( ) .await { - Ok((upstream_subscribe_id, upstream_track_alias)) => { - (upstream_subscribe_id, upstream_track_alias) + Ok((publisher_subscribe_id, publisher_track_alias)) => { + (publisher_subscribe_id, publisher_track_alias) } Err(e) => { bail!("cannot register publisher and subscriber: {:?}", e); @@ -84,8 +100,10 @@ pub(crate) async fn subscribe_handler( let mut relaying_subscribe_message = subscribe_message.clone(); // Replace the subscribe_id and track_alias in the SUBSCRIBE message to request to the upstream publisher - relaying_subscribe_message - .replace_subscribe_id_and_track_alias(upstream_subscribe_id, upstream_track_alias); + relaying_subscribe_message.replace_subscribe_id_and_track_alias( + publisher_subscribe_id, + publisher_track_alias, + ); let message: Box = Box::new(relaying_subscribe_message.clone()); tracing::debug!( @@ -113,7 +131,9 @@ pub(crate) async fn subscribe_handler( } Err(e) => { tracing::warn!("relay subscribe failed: {:?}", e); - // Failure on SUBSCRIBE_OK relay doesn't turn into closing connection + + // TODO: return TerminationErrorCode + bail!("relay subscribe failed"); } } @@ -239,7 +259,7 @@ async fn set_subscriber_and_publisher_subscription( ) .await?; - let (upstream_subscribe_id, upstream_track_alias) = track_namespace_manager_repository + let (publisher_subscribe_id, publisher_track_alias) = track_namespace_manager_repository .set_publisher_subscription( publisher_session_id, subscriber_track_namespace.clone(), @@ -257,381 +277,746 @@ async fn set_subscriber_and_publisher_subscription( track_namespace_manager_repository .register_pubsup_relation( publisher_session_id, - upstream_subscribe_id, + publisher_subscribe_id, subscriber_client_id, subscriber_subscribe_id, ) .await?; - Ok((upstream_subscribe_id, upstream_track_alias)) + Ok((publisher_subscribe_id, publisher_track_alias)) } -// #[cfg(test)] -// mod success { -// use crate::modules::handlers::subscribe_handler::subscribe_handler; -// use crate::modules::send_stream_dispatcher::{ -// send_stream_dispatcher, SendStreamDispatchCommand, SendStreamDispatcher, -// }; -// use crate::modules::track_namespace_manager::{ -// track_namespace_manager, TrackCommand, TrackNamespaceManager, -// }; -// use moqt_core::constants::StreamDirection; -// use moqt_core::messages::{ -// control_messages::{ -// subscribe::{FilterType, GroupOrder, Subscribe}, -// version_specific_parameters::{AuthorizationInfo, VersionSpecificParameter}, -// }, -// moqt_payload::MOQTPayload, -// }; -// use moqt_core::MOQTClient; -// use moqt_core::TrackNamespaceManagerRepository; -// use std::sync::Arc; -// use tokio::sync::mpsc; - -// #[tokio::test] -// async fn normal_case() { -// // Generate SUBSCRIBE message -// let subscribe_id = 0; -// let track_alias = 0; -// let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); -// let track_name = "track_name".to_string(); -// let subscriber_priority = 0; -// let group_order = GroupOrder::Ascending; -// let filter_type = FilterType::LatestGroup; -// let start_group = None; -// let start_object = None; -// let end_group = None; -// let end_object = None; -// let version_specific_parameter = -// VersionSpecificParameter::AuthorizationInfo(AuthorizationInfo::new("test".to_string())); -// let subscribe_parameters = vec![version_specific_parameter]; - -// let subscribe = Subscribe::new( -// subscribe_id, -// track_alias, -// track_namespace.clone(), -// track_name, -// subscriber_priority, -// group_order, -// filter_type, -// start_group, -// start_object, -// end_group, -// end_object, -// subscribe_parameters, -// ) -// .unwrap(); - -// // Generate client -// let subscriber_sessin_id = 0; -// let mut client = MOQTClient::new(subscriber_sessin_id); - -// // Generate TrackNamespaceManager -// let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); -// tokio::spawn(async move { track_namespace_manager(&mut track_namespace_rx).await }); -// let mut track_namespace_manager: TrackNamespaceManager = -// TrackNamespaceManager::new(track_namespace_tx); - -// let publisher_session_id = 1; -// let max_subscribe_id = 10; - -// let _ = track_namespace_manager -// .setup_publisher(max_subscribe_id, publisher_session_id) -// .await; -// let _ = track_namespace_manager -// .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) -// .await; - -// // Generate SendStreamDispacher -// let (send_stream_tx, mut send_stream_rx) = mpsc::channel::(1024); - -// tokio::spawn(async move { send_stream_dispatcher(&mut send_stream_rx).await }); -// let mut send_stream_dispatcher: SendStreamDispatcher = -// SendStreamDispatcher::new(send_stream_tx.clone()); - -// let (uni_relay_tx, _) = mpsc::channel::>>(1024); -// let _ = send_stream_tx -// .send(SendStreamDispatchCommand::Set { -// session_id: publisher_session_id, -// stream_direction: StreamDirection::Bi, -// sender: uni_relay_tx, -// }) -// .await; - -// // Execute subscribe_handler and get result -// let result = subscribe_handler( -// subscribe, -// &mut client, -// &mut track_namespace_manager, -// &mut send_stream_dispatcher, -// ) -// .await; - -// assert!(result.is_ok()); -// } -// } - -// #[cfg(test)] -// mod failure { -// use crate::modules::handlers::subscribe_handler::subscribe_handler; -// use crate::modules::send_stream_dispatcher::{ -// send_stream_dispatcher, SendStreamDispatchCommand, SendStreamDispatcher, -// }; -// use crate::modules::track_namespace_manager::{ -// track_namespace_manager, TrackCommand, TrackNamespaceManager, -// }; -// use moqt_core::constants::StreamDirection; -// use moqt_core::messages::{ -// control_messages::{ -// subscribe::{FilterType, GroupOrder, Subscribe}, -// version_specific_parameters::{AuthorizationInfo, VersionSpecificParameter}, -// }, -// moqt_payload::MOQTPayload, -// }; -// use moqt_core::MOQTClient; -// use moqt_core::TrackNamespaceManagerRepository; -// use std::sync::Arc; -// use tokio::sync::mpsc; - -// #[tokio::test] -// async fn cannot_register() { -// // Generate SUBSCRIBE message -// let subscribe_id = 0; -// let track_alias = 0; -// let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); -// let track_name = "track_name"; -// let subscriber_priority = 0; -// let group_order = GroupOrder::Ascending; -// let filter_type = FilterType::LatestGroup; -// let start_group = None; -// let start_object = None; -// let end_group = None; -// let end_object = None; -// let version_specific_parameter = -// VersionSpecificParameter::AuthorizationInfo(AuthorizationInfo::new("test".to_string())); -// let subscribe_parameters = vec![version_specific_parameter]; - -// let subscribe = Subscribe::new( -// subscribe_id, -// track_alias, -// track_namespace.clone(), -// track_name.to_string(), -// subscriber_priority, -// group_order, -// filter_type, -// start_group, -// start_object, -// end_group, -// end_object, -// subscribe_parameters, -// ) -// .unwrap(); - -// // Generate client -// let subscriber_session_id = 0; -// let mut client = MOQTClient::new(subscriber_session_id); - -// // Generate TrackNamespaceManager (register subscriber in advance) -// let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); -// tokio::spawn(async move { track_namespace_manager(&mut track_namespace_rx).await }); -// let mut track_namespace_manager: TrackNamespaceManager = -// TrackNamespaceManager::new(track_namespace_tx); - -// let publisher_session_id = 1; -// let track_id = 0; -// let max_subscribe_id = 10; - -// let _ = track_namespace_manager -// .setup_publisher(max_subscribe_id, publisher_session_id) -// .await; -// let _ = track_namespace_manager -// .setup_subscriber(max_subscribe_id, subscriber_session_id) -// .await; -// let _ = track_namespace_manager -// .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) -// .await; -// let _ = track_namespace_manager -// .set_subscriber(track_namespace.clone(), subscriber_session_id, track_name) -// .await; -// let _ = track_namespace_manager -// .set_track_id(track_namespace.clone(), track_name, track_id) -// .await; -// let _ = track_namespace_manager -// .activate_subscriber(track_namespace.clone(), track_name, subscriber_session_id) -// .await; - -// // Generate SendStreamDispacher -// let (send_stream_tx, mut send_stream_rx) = mpsc::channel::(1024); - -// tokio::spawn(async move { send_stream_dispatcher(&mut send_stream_rx).await }); -// let mut send_stream_dispatcher: SendStreamDispatcher = -// SendStreamDispatcher::new(send_stream_tx.clone()); - -// let (uni_relay_tx, _) = mpsc::channel::>>(1024); -// let _ = send_stream_tx -// .send(SendStreamDispatchCommand::Set { -// session_id: publisher_session_id, -// stream_direction: StreamDirection::Bi, -// sender: uni_relay_tx, -// }) -// .await; - -// // Execute subscribe_handler and get result -// let result = subscribe_handler( -// subscribe, -// &mut client, -// &mut track_namespace_manager, -// &mut send_stream_dispatcher, -// ) -// .await; - -// assert!(result.is_err()); -// } - -// #[tokio::test] -// async fn relay_fail() { -// // Generate SUBSCRIBE message -// let subscribe_id = 0; -// let track_alias = 0; -// let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); -// let track_name = "track_name"; -// let subscriber_priority = 0; -// let group_order = GroupOrder::Ascending; -// let filter_type = FilterType::LatestGroup; -// let start_group = None; -// let start_object = None; -// let end_group = None; -// let end_object = None; -// let version_specific_parameter = -// VersionSpecificParameter::AuthorizationInfo(AuthorizationInfo::new("test".to_string())); -// let subscribe_parameters = vec![version_specific_parameter]; - -// let subscribe = Subscribe::new( -// subscribe_id, -// track_alias, -// track_namespace.clone(), -// track_name.to_string(), -// subscriber_priority, -// group_order, -// filter_type, -// start_group, -// start_object, -// end_group, -// end_object, -// subscribe_parameters, -// ) -// .unwrap(); - -// // Generate client -// let subscriber_session_id = 0; -// let mut client = MOQTClient::new(subscriber_session_id); - -// // Generate TrackNamespaceManager -// let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); -// tokio::spawn(async move { track_namespace_manager(&mut track_namespace_rx).await }); -// let mut track_namespace_manager: TrackNamespaceManager = -// TrackNamespaceManager::new(track_namespace_tx); - -// let publisher_session_id = 1; -// let max_subscribe_id = 10; - -// let _ = track_namespace_manager -// .setup_publisher(max_subscribe_id, publisher_session_id) -// .await; -// let _ = track_namespace_manager -// .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) -// .await; - -// // Generate SendStreamDispacher (without set sender) -// let (send_stream_tx, mut send_stream_rx) = mpsc::channel::(1024); - -// tokio::spawn(async move { send_stream_dispatcher(&mut send_stream_rx).await }); -// let mut send_stream_dispatcher: SendStreamDispatcher = -// SendStreamDispatcher::new(send_stream_tx.clone()); - -// // Execute subscribe_handler and get result -// let result = subscribe_handler( -// subscribe, -// &mut client, -// &mut track_namespace_manager, -// &mut send_stream_dispatcher, -// ) -// .await; - -// assert!(result.is_ok()); -// } - -// #[tokio::test] -// async fn publisher_not_found() { -// // Generate SUBSCRIBE message -// let subscribe_id = 0; -// let track_alias = 0; -// let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); -// let track_name = "track_name"; -// let subscriber_priority = 0; -// let group_order = GroupOrder::Ascending; -// let filter_type = FilterType::LatestGroup; -// let start_group = None; -// let start_object = None; -// let end_group = None; -// let end_object = None; -// let version_specific_parameter = -// VersionSpecificParameter::AuthorizationInfo(AuthorizationInfo::new("test".to_string())); -// let subscribe_parameters = vec![version_specific_parameter]; - -// let subscribe = Subscribe::new( -// subscribe_id, -// track_alias, -// track_namespace, -// track_name.to_string(), -// subscriber_priority, -// group_order, -// filter_type, -// start_group, -// start_object, -// end_group, -// end_object, -// subscribe_parameters, -// ) -// .unwrap(); - -// // Generate client -// let subscriber_session_id = 0; -// let mut client = MOQTClient::new(subscriber_session_id); - -// // Generate TrackNamespaceManager (without set publisher) -// let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); -// tokio::spawn(async move { track_namespace_manager(&mut track_namespace_rx).await }); -// let mut track_namespace_manager: TrackNamespaceManager = -// TrackNamespaceManager::new(track_namespace_tx); - -// let publisher_session_id = 1; - -// // Generate SendStreamDispacher -// let (send_stream_tx, mut send_stream_rx) = mpsc::channel::(1024); - -// tokio::spawn(async move { send_stream_dispatcher(&mut send_stream_rx).await }); -// let mut send_stream_dispatcher: SendStreamDispatcher = -// SendStreamDispatcher::new(send_stream_tx.clone()); - -// let (uni_relay_tx, _) = mpsc::channel::>>(1024); -// let _ = send_stream_tx -// .send(SendStreamDispatchCommand::Set { -// session_id: publisher_session_id, -// stream_direction: StreamDirection::Bi, -// sender: uni_relay_tx, -// }) -// .await; - -// // Execute subscribe_handler and get result -// let result = subscribe_handler( -// subscribe, -// &mut client, -// &mut track_namespace_manager, -// &mut send_stream_dispatcher, -// ) -// .await; - -// assert!(result.is_err()); -// } -// } +#[cfg(test)] +mod success { + use crate::modules::handlers::subscribe_handler::subscribe_handler; + use crate::modules::send_stream_dispatcher::{ + send_stream_dispatcher, SendStreamDispatchCommand, SendStreamDispatcher, + }; + use crate::modules::track_namespace_manager::test_fn; + use crate::modules::track_namespace_manager::{ + track_namespace_manager, TrackCommand, TrackNamespaceManager, + }; + use moqt_core::constants::StreamDirection; + use moqt_core::messages::{ + control_messages::{ + subscribe::{FilterType, GroupOrder, Subscribe}, + version_specific_parameters::{AuthorizationInfo, VersionSpecificParameter}, + }, + moqt_payload::MOQTPayload, + }; + use moqt_core::MOQTClient; + use moqt_core::TrackNamespaceManagerRepository; + use std::sync::Arc; + use tokio::sync::mpsc; + + #[tokio::test] + async fn normal_case_track_not_exists() { + // Generate SUBSCRIBE message + let expected_publisher_subscribe_id = 0; + let subscribe_id = 0; + let track_alias = 0; + let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); + let track_name = "track_name".to_string(); + let subscriber_priority = 0; + let group_order = GroupOrder::Ascending; + let filter_type = FilterType::LatestGroup; + let start_group = None; + let start_object = None; + let end_group = None; + let end_object = None; + let version_specific_parameter = + VersionSpecificParameter::AuthorizationInfo(AuthorizationInfo::new("test".to_string())); + let subscribe_parameters = vec![version_specific_parameter]; + + let subscribe = Subscribe::new( + subscribe_id, + track_alias, + track_namespace.clone(), + track_name.clone(), + subscriber_priority, + group_order, + filter_type, + start_group, + start_object, + end_group, + end_object, + subscribe_parameters, + ) + .unwrap(); + + // Generate client + let subscriber_session_id = 0; + let mut client = MOQTClient::new(subscriber_session_id); + + // Generate TrackNamespaceManager + let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + tokio::spawn(async move { track_namespace_manager(&mut track_namespace_rx).await }); + let mut track_namespace_manager: TrackNamespaceManager = + TrackNamespaceManager::new(track_namespace_tx); + + let publisher_session_id = 1; + let max_subscribe_id = 10; + + // Register the publisher track in advance + let _ = track_namespace_manager + .setup_publisher(max_subscribe_id, publisher_session_id) + .await; + let _ = track_namespace_manager + .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) + .await; + + let _ = track_namespace_manager + .setup_subscriber(max_subscribe_id, subscriber_session_id) + .await; + + // Generate SendStreamDispacher + let (send_stream_tx, mut send_stream_rx) = mpsc::channel::(1024); + + tokio::spawn(async move { send_stream_dispatcher(&mut send_stream_rx).await }); + let mut send_stream_dispatcher: SendStreamDispatcher = + SendStreamDispatcher::new(send_stream_tx.clone()); + + let (uni_relay_tx, _) = mpsc::channel::>>(1024); + let _ = send_stream_tx + .send(SendStreamDispatchCommand::Set { + session_id: publisher_session_id, + stream_direction: StreamDirection::Bi, + sender: uni_relay_tx, + }) + .await; + + // Execute subscribe_handler and get result + let result = subscribe_handler( + subscribe, + &mut client, + &mut track_namespace_manager, + &mut send_stream_dispatcher, + ) + .await; + + assert!(result.is_ok()); + + let message = result.unwrap(); + assert!(message.is_none()); + + // Check the subscriber is registered + let (_, producers, pubsub_relation) = + test_fn::get_node_and_relation_clone(&track_namespace_manager).await; + + assert_eq!(producers.len(), 1); + + println!("{:?}", pubsub_relation); + + let subscribers = pubsub_relation + .get_subscribers(publisher_session_id, expected_publisher_subscribe_id) + .unwrap(); + + let (subscriber_session_id, subscriber_subscribe_id) = subscribers.first().unwrap(); + + assert_eq!(subscriber_session_id, subscriber_session_id); + assert_eq!(subscriber_subscribe_id, subscriber_subscribe_id); + } + + #[tokio::test] + async fn normal_case_track_exists() { + // Generate SUBSCRIBE message + let subscribe_id = 0; + let track_alias = 0; + let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); + let track_name = "track_name".to_string(); + let subscriber_priority = 0; + let group_order = GroupOrder::Ascending; + let filter_type = FilterType::LatestGroup; + let start_group = None; + let start_object = None; + let end_group = None; + let end_object = None; + let version_specific_parameter = + VersionSpecificParameter::AuthorizationInfo(AuthorizationInfo::new("test".to_string())); + let subscribe_parameters = vec![version_specific_parameter]; + + let subscribe = Subscribe::new( + subscribe_id, + track_alias, + track_namespace.clone(), + track_name.clone(), + subscriber_priority, + group_order, + filter_type, + start_group, + start_object, + end_group, + end_object, + subscribe_parameters, + ) + .unwrap(); + + // Generate client + let subscriber_session_id = 0; + let mut client = MOQTClient::new(subscriber_session_id); + + // Generate TrackNamespaceManager + let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + tokio::spawn(async move { track_namespace_manager(&mut track_namespace_rx).await }); + let mut track_namespace_manager: TrackNamespaceManager = + TrackNamespaceManager::new(track_namespace_tx); + + let publisher_session_id = 1; + let max_subscribe_id = 10; + + // Register the publisher track in advance + let _ = track_namespace_manager + .setup_publisher(max_subscribe_id, publisher_session_id) + .await; + let _ = track_namespace_manager + .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) + .await; + let (publisher_subscribe_id, _) = track_namespace_manager + .set_publisher_subscription( + publisher_session_id, + track_namespace, + track_name, + subscriber_priority, + group_order, + filter_type, + start_group, + start_object, + end_group, + end_object, + ) + .await + .unwrap(); + + let _ = track_namespace_manager + .setup_subscriber(max_subscribe_id, subscriber_session_id) + .await; + + // Generate SendStreamDispacher + let (send_stream_tx, mut send_stream_rx) = mpsc::channel::(1024); + + tokio::spawn(async move { send_stream_dispatcher(&mut send_stream_rx).await }); + let mut send_stream_dispatcher: SendStreamDispatcher = + SendStreamDispatcher::new(send_stream_tx.clone()); + + let (uni_relay_tx, _) = mpsc::channel::>>(1024); + let _ = send_stream_tx + .send(SendStreamDispatchCommand::Set { + session_id: publisher_session_id, + stream_direction: StreamDirection::Bi, + sender: uni_relay_tx, + }) + .await; + + // Execute subscribe_handler and get result + let result = subscribe_handler( + subscribe, + &mut client, + &mut track_namespace_manager, + &mut send_stream_dispatcher, + ) + .await; + + assert!(result.is_ok()); + + let message = result.unwrap(); + assert!(message.is_some()); + + // Check the subscriber is registered + let (_, producers, pubsub_relation) = + test_fn::get_node_and_relation_clone(&track_namespace_manager).await; + + assert_eq!(producers.len(), 1); + + let subscribers = pubsub_relation + .get_subscribers(publisher_session_id, publisher_subscribe_id) + .unwrap(); + + let (subscriber_session_id, subscriber_subscribe_id) = subscribers.first().unwrap(); + + assert_eq!(subscriber_session_id, subscriber_session_id); + assert_eq!(subscriber_subscribe_id, subscriber_subscribe_id); + } +} + +#[cfg(test)] +mod failure { + use crate::modules::handlers::subscribe_handler::subscribe_handler; + use crate::modules::send_stream_dispatcher::{ + send_stream_dispatcher, SendStreamDispatchCommand, SendStreamDispatcher, + }; + use crate::modules::track_namespace_manager::{ + track_namespace_manager, TrackCommand, TrackNamespaceManager, + }; + use moqt_core::constants::StreamDirection; + use moqt_core::messages::{ + control_messages::{ + subscribe::{FilterType, GroupOrder, Subscribe}, + version_specific_parameters::{AuthorizationInfo, VersionSpecificParameter}, + }, + moqt_payload::MOQTPayload, + }; + use moqt_core::MOQTClient; + use moqt_core::TrackNamespaceManagerRepository; + use std::sync::Arc; + use tokio::sync::mpsc; + + #[tokio::test] + async fn cannot_register() { + // Generate SUBSCRIBE message + let subscribe_id = 0; + let track_alias = 0; + let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); + let track_name = "track_name".to_string(); + let subscriber_priority = 0; + let group_order = GroupOrder::Ascending; + let filter_type = FilterType::LatestGroup; + let start_group = None; + let start_object = None; + let end_group = None; + let end_object = None; + let version_specific_parameter = + VersionSpecificParameter::AuthorizationInfo(AuthorizationInfo::new("test".to_string())); + let subscribe_parameters = vec![version_specific_parameter]; + + let subscribe = Subscribe::new( + subscribe_id, + track_alias, + track_namespace.clone(), + track_name.clone(), + subscriber_priority, + group_order, + filter_type, + start_group, + start_object, + end_group, + end_object, + subscribe_parameters, + ) + .unwrap(); + + // Generate client + let subscriber_session_id = 0; + let mut client = MOQTClient::new(subscriber_session_id); + + // Generate TrackNamespaceManager (register subscriber in advance) + let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + tokio::spawn(async move { track_namespace_manager(&mut track_namespace_rx).await }); + let mut track_namespace_manager: TrackNamespaceManager = + TrackNamespaceManager::new(track_namespace_tx); + + let publisher_session_id = 1; + let max_subscribe_id = 10; + + let _ = track_namespace_manager + .setup_publisher(max_subscribe_id, publisher_session_id) + .await; + let _ = track_namespace_manager + .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) + .await; + + let _ = track_namespace_manager + .setup_subscriber(max_subscribe_id, subscriber_session_id) + .await; + let _ = track_namespace_manager + .set_subscriber_subscription( + subscriber_session_id, + subscribe_id, + track_alias, + track_namespace, + track_name, + subscriber_priority, + group_order, + filter_type, + start_group, + start_object, + end_group, + end_object, + ) + .await; + + // Generate SendStreamDispacher + let (send_stream_tx, mut send_stream_rx) = mpsc::channel::(1024); + + tokio::spawn(async move { send_stream_dispatcher(&mut send_stream_rx).await }); + let mut send_stream_dispatcher: SendStreamDispatcher = + SendStreamDispatcher::new(send_stream_tx.clone()); + + let (uni_relay_tx, _) = mpsc::channel::>>(1024); + let _ = send_stream_tx + .send(SendStreamDispatchCommand::Set { + session_id: publisher_session_id, + stream_direction: StreamDirection::Bi, + sender: uni_relay_tx, + }) + .await; + + // Execute subscribe_handler and get result + let result = subscribe_handler( + subscribe, + &mut client, + &mut track_namespace_manager, + &mut send_stream_dispatcher, + ) + .await; + + assert!(result.is_err()); + } + + #[tokio::test] + async fn relay_fail() { + // Generate SUBSCRIBE message + let subscribe_id = 0; + let track_alias = 0; + let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); + let track_name = "track_name".to_string(); + let subscriber_priority = 0; + let group_order = GroupOrder::Ascending; + let filter_type = FilterType::LatestGroup; + let start_group = None; + let start_object = None; + let end_group = None; + let end_object = None; + let version_specific_parameter = + VersionSpecificParameter::AuthorizationInfo(AuthorizationInfo::new("test".to_string())); + let subscribe_parameters = vec![version_specific_parameter]; + + let subscribe = Subscribe::new( + subscribe_id, + track_alias, + track_namespace.clone(), + track_name.clone(), + subscriber_priority, + group_order, + filter_type, + start_group, + start_object, + end_group, + end_object, + subscribe_parameters, + ) + .unwrap(); + + // Generate client + let subscriber_session_id = 0; + let mut client = MOQTClient::new(subscriber_session_id); + + // Generate TrackNamespaceManager + let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + tokio::spawn(async move { track_namespace_manager(&mut track_namespace_rx).await }); + let mut track_namespace_manager: TrackNamespaceManager = + TrackNamespaceManager::new(track_namespace_tx); + + let publisher_session_id = 1; + let max_subscribe_id = 10; + + let _ = track_namespace_manager + .setup_publisher(max_subscribe_id, publisher_session_id) + .await; + let _ = track_namespace_manager + .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) + .await; + + let _ = track_namespace_manager + .setup_subscriber(max_subscribe_id, subscriber_session_id) + .await; + + // Generate SendStreamDispacher (without set sender) + let (send_stream_tx, mut send_stream_rx) = mpsc::channel::(1024); + + tokio::spawn(async move { send_stream_dispatcher(&mut send_stream_rx).await }); + let mut send_stream_dispatcher: SendStreamDispatcher = + SendStreamDispatcher::new(send_stream_tx.clone()); + + // Execute subscribe_handler and get result + let result = subscribe_handler( + subscribe, + &mut client, + &mut track_namespace_manager, + &mut send_stream_dispatcher, + ) + .await; + + assert!(result.is_err()); + } + + #[tokio::test] + async fn publisher_not_found() { + // Generate SUBSCRIBE message + let subscribe_id = 0; + let track_alias = 0; + let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); + let track_name = "track_name"; + let subscriber_priority = 0; + let group_order = GroupOrder::Ascending; + let filter_type = FilterType::LatestGroup; + let start_group = None; + let start_object = None; + let end_group = None; + let end_object = None; + let version_specific_parameter = + VersionSpecificParameter::AuthorizationInfo(AuthorizationInfo::new("test".to_string())); + let subscribe_parameters = vec![version_specific_parameter]; + + let subscribe = Subscribe::new( + subscribe_id, + track_alias, + track_namespace, + track_name.to_string(), + subscriber_priority, + group_order, + filter_type, + start_group, + start_object, + end_group, + end_object, + subscribe_parameters, + ) + .unwrap(); + + // Generate client + let subscriber_session_id = 0; + let mut client = MOQTClient::new(subscriber_session_id); + + // Generate TrackNamespaceManager (without set publisher) + let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + tokio::spawn(async move { track_namespace_manager(&mut track_namespace_rx).await }); + let mut track_namespace_manager: TrackNamespaceManager = + TrackNamespaceManager::new(track_namespace_tx); + + let publisher_session_id = 1; + let max_subscribe_id = 10; + + let _ = track_namespace_manager + .setup_subscriber(max_subscribe_id, subscriber_session_id) + .await; + + // Generate SendStreamDispacher + let (send_stream_tx, mut send_stream_rx) = mpsc::channel::(1024); + + tokio::spawn(async move { send_stream_dispatcher(&mut send_stream_rx).await }); + let mut send_stream_dispatcher: SendStreamDispatcher = + SendStreamDispatcher::new(send_stream_tx.clone()); + + let (uni_relay_tx, _) = mpsc::channel::>>(1024); + let _ = send_stream_tx + .send(SendStreamDispatchCommand::Set { + session_id: publisher_session_id, + stream_direction: StreamDirection::Bi, + sender: uni_relay_tx, + }) + .await; + + // Execute subscribe_handler and get result + let result = subscribe_handler( + subscribe, + &mut client, + &mut track_namespace_manager, + &mut send_stream_dispatcher, + ) + .await; + + assert!(result.is_err()); + } + + #[tokio::test] + async fn too_many_subscriber() { + // Generate SUBSCRIBE message + let subscribe_ids = [0, 1]; + let track_aliases = [0, 1]; + let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); + let track_name = "track_name".to_string(); + let subscriber_priority = 0; + let group_order = GroupOrder::Ascending; + let filter_type = FilterType::LatestGroup; + let start_group = None; + let start_object = None; + let end_group = None; + let end_object = None; + let version_specific_parameter = + VersionSpecificParameter::AuthorizationInfo(AuthorizationInfo::new("test".to_string())); + let subscribe_parameters = vec![version_specific_parameter]; + + let mut subscribes = vec![]; + + for i in [0, 1] { + let subscribe = Subscribe::new( + subscribe_ids[i], + track_aliases[i], + track_namespace.clone(), + track_name.clone(), + subscriber_priority, + group_order, + filter_type, + start_group, + start_object, + end_group, + end_object, + subscribe_parameters.clone(), + ) + .unwrap(); + + subscribes.push(subscribe); + } + + // Generate client + let subscriber_session_id = 0; + let mut client = MOQTClient::new(subscriber_session_id); + + // Generate TrackNamespaceManager + let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + tokio::spawn(async move { track_namespace_manager(&mut track_namespace_rx).await }); + let mut track_namespace_manager: TrackNamespaceManager = + TrackNamespaceManager::new(track_namespace_tx); + + let publisher_session_id = 1; + let max_subscribe_id = 0; + + // Register the publisher track in advance + let _ = track_namespace_manager + .setup_publisher(max_subscribe_id, publisher_session_id) + .await; + let _ = track_namespace_manager + .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) + .await; + + let _ = track_namespace_manager + .setup_subscriber(max_subscribe_id, subscriber_session_id) + .await; + + // Generate SendStreamDispacher + let (send_stream_tx, mut send_stream_rx) = mpsc::channel::(1024); + + tokio::spawn(async move { send_stream_dispatcher(&mut send_stream_rx).await }); + let mut send_stream_dispatcher: SendStreamDispatcher = + SendStreamDispatcher::new(send_stream_tx.clone()); + + let (uni_relay_tx, _) = mpsc::channel::>>(1024); + let _ = send_stream_tx + .send(SendStreamDispatchCommand::Set { + session_id: publisher_session_id, + stream_direction: StreamDirection::Bi, + sender: uni_relay_tx, + }) + .await; + + // Execute subscribe_handler and get result + let _ = subscribe_handler( + subscribes[0].clone(), + &mut client, + &mut track_namespace_manager, + &mut send_stream_dispatcher, + ) + .await; + + let result = subscribe_handler( + subscribes[1].clone(), + &mut client, + &mut track_namespace_manager, + &mut send_stream_dispatcher, + ) + .await; + + assert!(result.is_err()); + } + + #[tokio::test] + async fn duplicate_track_alias() { + // Generate SUBSCRIBE message + let subscribe_ids = [0, 1]; + let track_alias = 0; + let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); + let track_name = "track_name".to_string(); + let subscriber_priority = 0; + let group_order = GroupOrder::Ascending; + let filter_type = FilterType::LatestGroup; + let start_group = None; + let start_object = None; + let end_group = None; + let end_object = None; + let version_specific_parameter = + VersionSpecificParameter::AuthorizationInfo(AuthorizationInfo::new("test".to_string())); + let subscribe_parameters = vec![version_specific_parameter]; + + let mut subscribes = vec![]; + + for i in [0, 1] { + let subscribe = Subscribe::new( + subscribe_ids[i], + track_alias, + track_namespace.clone(), + track_name.clone(), + subscriber_priority, + group_order, + filter_type, + start_group, + start_object, + end_group, + end_object, + subscribe_parameters.clone(), + ) + .unwrap(); + + subscribes.push(subscribe); + } + + // Generate client + let subscriber_session_id = 0; + let mut client = MOQTClient::new(subscriber_session_id); + + // Generate TrackNamespaceManager + let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + tokio::spawn(async move { track_namespace_manager(&mut track_namespace_rx).await }); + let mut track_namespace_manager: TrackNamespaceManager = + TrackNamespaceManager::new(track_namespace_tx); + + let publisher_session_id = 1; + let max_subscribe_id = 10; + + // Register the publisher track in advance + let _ = track_namespace_manager + .setup_publisher(max_subscribe_id, publisher_session_id) + .await; + let _ = track_namespace_manager + .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) + .await; + + let _ = track_namespace_manager + .setup_subscriber(max_subscribe_id, subscriber_session_id) + .await; + + // Generate SendStreamDispacher + let (send_stream_tx, mut send_stream_rx) = mpsc::channel::(1024); + + tokio::spawn(async move { send_stream_dispatcher(&mut send_stream_rx).await }); + let mut send_stream_dispatcher: SendStreamDispatcher = + SendStreamDispatcher::new(send_stream_tx.clone()); + + let (uni_relay_tx, _) = mpsc::channel::>>(1024); + let _ = send_stream_tx + .send(SendStreamDispatchCommand::Set { + session_id: publisher_session_id, + stream_direction: StreamDirection::Bi, + sender: uni_relay_tx, + }) + .await; + + // Execute subscribe_handler and get result + let _ = subscribe_handler( + subscribes[0].clone(), + &mut client, + &mut track_namespace_manager, + &mut send_stream_dispatcher, + ) + .await; + + let result = subscribe_handler( + subscribes[1].clone(), + &mut client, + &mut track_namespace_manager, + &mut send_stream_dispatcher, + ) + .await; + + assert!(result.is_err()); + } +} diff --git a/moqt-server/src/modules/handlers/subscribe_ok_handler.rs b/moqt-server/src/modules/handlers/subscribe_ok_handler.rs index d1523ea5..df413ed2 100644 --- a/moqt-server/src/modules/handlers/subscribe_ok_handler.rs +++ b/moqt-server/src/modules/handlers/subscribe_ok_handler.rs @@ -34,285 +34,684 @@ pub(crate) async fn subscribe_ok_handler( // Notify all waiting subscribers with the SUBSCRIBE_OK message for (subscriber_session_id, subscriber_subscribe_id) in ids.iter() { - let message: Box = Box::new(subscribe_ok_message.clone()); - // TODO: replace subscribe_id and track_alias + let mut relaying_subscribe_ok_message = subscribe_ok_message.clone(); + relaying_subscribe_ok_message.replace_subscribe_id(*subscriber_subscribe_id); + let message: Box = Box::new(relaying_subscribe_ok_message.clone()); tracing::debug!( "message: {:#?} is sent to relay handler for client {:?}", - subscribe_ok_message, + relaying_subscribe_ok_message, subscriber_session_id ); - match send_stream_dispatcher_repository + send_stream_dispatcher_repository .send_message_to_send_stream_thread( *subscriber_session_id, message, StreamDirection::Bi, ) - .await - { - Ok(_) => { - let _ = track_namespace_manager_repository - .activate_subscriber_subscription( - *subscriber_session_id, - *subscriber_subscribe_id, - ) - .await; - - tracing::trace!("subscribe_ok_handler complete."); - } - Err(e) => { - tracing::warn!( - "relay subscribe ok failed at session id {:?}: {:?}", - subscriber_session_id, - e - ); - // Failure on SUBSCRIBE_OK relay doesn't turn into closing connection - } - } + .await?; + + track_namespace_manager_repository + .activate_subscriber_subscription( + *subscriber_session_id, + *subscriber_subscribe_id, + ) + .await?; + + tracing::trace!("subscribe_ok_handler complete."); } } None => { tracing::warn!("waiting subscriber session ids not found"); - // Absence of SUBSCRIBE_OK relay doesn't turn into closing connection + + // Activate the publisher because it is in the Requesting state + let _ = track_namespace_manager_repository + .activate_publisher_subscription(publisher_session_id, publisher_subscribe_id) + .await; } } Ok(()) } -// #[cfg(test)] -// mod success { -// use crate::modules::handlers::subscribe_ok_handler::subscribe_ok_handler; -// use crate::modules::send_stream_dispatcher::{ -// send_stream_dispatcher, SendStreamDispatchCommand, SendStreamDispatcher, -// }; -// use crate::modules::track_namespace_manager::{ -// track_namespace_manager, TrackCommand, TrackNamespaceManager, -// }; -// use moqt_core::constants::StreamDirection; -// use moqt_core::messages::control_messages::subscribe::GroupOrder; -// use moqt_core::messages::control_messages::version_specific_parameters::{ -// AuthorizationInfo, VersionSpecificParameter, -// }; -// use moqt_core::messages::{ -// control_messages::subscribe_ok::SubscribeOk, moqt_payload::MOQTPayload, -// }; -// use moqt_core::TrackNamespaceManagerRepository; -// use std::sync::Arc; -// use tokio::sync::mpsc; - -// #[tokio::test] -// async fn normal_case() { -// // Generate SUBSCRIBE_OK message -// let subscribe_id = 0; -// let expires = 1; -// let group_order = GroupOrder::Ascending; -// let content_exists = false; -// let largest_group_id = None; -// let largest_object_id = None; -// let version_specific_parameter = -// VersionSpecificParameter::AuthorizationInfo(AuthorizationInfo::new("test".to_string())); -// let subscribe_parameters = vec![version_specific_parameter]; - -// let subscribe_ok = SubscribeOk::new( -// subscribe_id, -// expires, -// group_order, -// content_exists, -// largest_group_id, -// largest_object_id, -// subscribe_parameters, -// ); -// let mut buf = bytes::BytesMut::new(); -// subscribe_ok.packetize(&mut buf); - -// // Generate TrackNamespaceManager -// let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); -// tokio::spawn(async move { track_namespace_manager(&mut track_namespace_rx).await }); -// let mut track_namespace_manager: TrackNamespaceManager = -// TrackNamespaceManager::new(track_namespace_tx); - -// let publisher_session_id = 1; -// let subscriber_session_id = 2; - -// let _ = track_namespace_manager -// .set_publisher(vec!["namespace".to_string()], publisher_session_id) -// .await; -// let _ = track_namespace_manager -// .set_subscriber(vec!["namespace".to_string()], subscriber_session_id, "name") -// .await; - -// // Generate SendStreamDispacher -// let (send_stream_tx, mut send_stream_rx) = mpsc::channel::(1024); - -// tokio::spawn(async move { send_stream_dispatcher(&mut send_stream_rx).await }); -// let mut send_stream_dispatcher: SendStreamDispatcher = -// SendStreamDispatcher::new(send_stream_tx.clone()); - -// let (uni_relay_tx, _) = mpsc::channel::>>(1024); -// let _ = send_stream_tx -// .send(SendStreamDispatchCommand::Set { -// session_id: subscriber_session_id, -// stream_direction: StreamDirection::Bi, -// sender: uni_relay_tx, -// }) -// .await; - -// // Execute subscribe_ok_handler and get result -// let result = subscribe_ok_handler( -// subscribe_ok, -// &mut track_namespace_manager, -// &mut send_stream_dispatcher, -// ) -// .await; - -// assert!(result.is_ok()); -// } -// } - -// #[cfg(test)] -// mod failure { -// use crate::modules::handlers::subscribe_ok_handler::subscribe_ok_handler; -// use crate::modules::send_stream_dispatcher::{ -// send_stream_dispatcher, SendStreamDispatchCommand, SendStreamDispatcher, -// }; -// use crate::modules::track_namespace_manager::{ -// track_namespace_manager, TrackCommand, TrackNamespaceManager, -// }; -// use moqt_core::constants::StreamDirection; -// use moqt_core::messages::control_messages::subscribe::GroupOrder; -// use moqt_core::messages::control_messages::version_specific_parameters::{ -// AuthorizationInfo, VersionSpecificParameter, -// }; -// use moqt_core::messages::{ -// control_messages::subscribe_ok::SubscribeOk, moqt_payload::MOQTPayload, -// }; -// use moqt_core::TrackNamespaceManagerRepository; -// use std::sync::Arc; -// use tokio::sync::mpsc; - -// #[tokio::test] -// async fn relay_fail() { -// // Generate SUBSCRIBE_OK message -// let subscribe_id = 0; -// let expires = 1; -// let group_order = GroupOrder::Ascending; -// let content_exists = false; -// let largest_group_id = None; -// let largest_object_id = None; -// let version_specific_parameter = -// VersionSpecificParameter::AuthorizationInfo(AuthorizationInfo::new("test".to_string())); -// let subscribe_parameters = vec![version_specific_parameter]; - -// let subscribe_ok = SubscribeOk::new( -// subscribe_id, -// expires, -// group_order, -// content_exists, -// largest_group_id, -// largest_object_id, -// subscribe_parameters, -// ); -// let mut buf = bytes::BytesMut::new(); -// subscribe_ok.packetize(&mut buf); - -// // Generate TrackNamespaceManager -// let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); -// tokio::spawn(async move { track_namespace_manager(&mut track_namespace_rx).await }); -// let mut track_namespace_manager: TrackNamespaceManager = -// TrackNamespaceManager::new(track_namespace_tx); - -// let publisher_session_id = 1; -// let subscriber_session_id = 2; - -// let _ = track_namespace_manager -// .set_publisher(vec!["namespace".to_string()], publisher_session_id) -// .await; -// let _ = track_namespace_manager -// .set_subscriber(vec!["namespace".to_string()], subscriber_session_id, "name") -// .await; - -// // Generate SendStreamDispacher (without set sender) -// let (send_stream_tx, mut send_stream_rx) = mpsc::channel::(1024); - -// tokio::spawn(async move { send_stream_dispatcher(&mut send_stream_rx).await }); -// let mut send_stream_dispatcher: SendStreamDispatcher = -// SendStreamDispatcher::new(send_stream_tx.clone()); - -// // Execute subscribe_ok_handler and get result -// let result = subscribe_ok_handler( -// subscribe_ok, -// &mut track_namespace_manager, -// &mut send_stream_dispatcher, -// ) -// .await; - -// assert!(result.is_ok()); -// } - -// #[tokio::test] -// async fn subscriber_not_found() { -// // Generate SUBSCRIBE_OK message -// let subscribe_id = 0; -// let expires = 1; -// let group_order = GroupOrder::Ascending; -// let content_exists = false; -// let largest_group_id = None; -// let largest_object_id = None; -// let version_specific_parameter = -// VersionSpecificParameter::AuthorizationInfo(AuthorizationInfo::new("test".to_string())); -// let subscribe_parameters = vec![version_specific_parameter]; - -// let subscribe_ok = SubscribeOk::new( -// subscribe_id, -// expires, -// group_order, -// content_exists, -// largest_group_id, -// largest_object_id, -// subscribe_parameters, -// ); -// let mut buf = bytes::BytesMut::new(); -// subscribe_ok.packetize(&mut buf); - -// // Generate TrackNamespaceManager (without set subscriber) -// let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); -// tokio::spawn(async move { track_namespace_manager(&mut track_namespace_rx).await }); -// let mut track_namespace_manager: TrackNamespaceManager = -// TrackNamespaceManager::new(track_namespace_tx); - -// let publisher_session_id = 1; -// let subscriber_session_id = 2; - -// let _ = track_namespace_manager -// .set_publisher(vec!["namespace".to_string()], publisher_session_id) -// .await; - -// // Generate SendStreamDispacher -// let (send_stream_tx, mut send_stream_rx) = mpsc::channel::(1024); - -// tokio::spawn(async move { send_stream_dispatcher(&mut send_stream_rx).await }); -// let mut send_stream_dispatcher: SendStreamDispatcher = -// SendStreamDispatcher::new(send_stream_tx.clone()); - -// let (uni_relay_tx, _) = mpsc::channel::>>(1024); -// let _ = send_stream_tx -// .send(SendStreamDispatchCommand::Set { -// session_id: subscriber_session_id, -// stream_direction: StreamDirection::Bi, -// sender: uni_relay_tx, -// }) -// .await; - -// // Execute subscribe_ok_handler and get result -// let result = subscribe_ok_handler( -// subscribe_ok, -// &mut track_namespace_manager, -// &mut send_stream_dispatcher, -// ) -// .await; - -// assert!(result.is_ok()); -// } -// } +#[cfg(test)] +mod success { + use crate::modules::handlers::subscribe_ok_handler::subscribe_ok_handler; + use crate::modules::send_stream_dispatcher::{ + send_stream_dispatcher, SendStreamDispatchCommand, SendStreamDispatcher, + }; + use crate::modules::track_namespace_manager::{ + track_namespace_manager, TrackCommand, TrackNamespaceManager, + }; + use crate::MOQTClient; + use moqt_core::constants::StreamDirection; + use moqt_core::messages::control_messages::subscribe::{FilterType, GroupOrder}; + use moqt_core::messages::control_messages::version_specific_parameters::{ + AuthorizationInfo, VersionSpecificParameter, + }; + use moqt_core::messages::{ + control_messages::subscribe_ok::SubscribeOk, moqt_payload::MOQTPayload, + }; + use moqt_core::TrackNamespaceManagerRepository; + use std::sync::Arc; + use tokio::sync::mpsc; + + #[tokio::test] + async fn normal_case() { + // Generate SUBSCRIBE_OK message + let subscriber_subscribe_id = 0; + let track_alias = 0; + let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); + let track_name = "track_name".to_string(); + let subscriber_priority = 0; + let expires = 1; + let group_order = GroupOrder::Ascending; + let content_exists = false; + let largest_group_id = None; + let largest_object_id = None; + let filter_type = FilterType::LatestGroup; + let start_group = None; + let start_object = None; + let end_group = None; + let end_object = None; + let version_specific_parameter = + VersionSpecificParameter::AuthorizationInfo(AuthorizationInfo::new("test".to_string())); + let subscribe_parameters = vec![version_specific_parameter]; + + let subscribe_ok = SubscribeOk::new( + subscriber_subscribe_id, + expires, + group_order, + content_exists, + largest_group_id, + largest_object_id, + subscribe_parameters, + ); + let mut buf = bytes::BytesMut::new(); + subscribe_ok.packetize(&mut buf); + + // Generate client + let publisher_session_id = 1; + let mut client = MOQTClient::new(publisher_session_id); + + // Generate TrackNamespaceManager + let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + tokio::spawn(async move { track_namespace_manager(&mut track_namespace_rx).await }); + let mut track_namespace_manager: TrackNamespaceManager = + TrackNamespaceManager::new(track_namespace_tx); + + let subscriber_session_id = 2; + let max_subscribe_id = 10; + + let _ = track_namespace_manager + .setup_publisher(max_subscribe_id, publisher_session_id) + .await; + let _ = track_namespace_manager + .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) + .await; + let (publisher_subscribe_id, _) = track_namespace_manager + .set_publisher_subscription( + publisher_session_id, + track_namespace.clone(), + track_name.clone(), + subscriber_priority, + group_order, + filter_type, + start_group, + start_object, + end_group, + end_object, + ) + .await + .unwrap(); + + let _ = track_namespace_manager + .setup_subscriber(max_subscribe_id, subscriber_session_id) + .await; + let _ = track_namespace_manager + .set_subscriber_subscription( + subscriber_session_id, + subscriber_subscribe_id, + track_alias, + track_namespace, + track_name, + subscriber_priority, + group_order, + filter_type, + start_group, + start_object, + end_group, + end_object, + ) + .await; + let _ = track_namespace_manager + .register_pubsup_relation( + publisher_session_id, + publisher_subscribe_id, + subscriber_session_id, + subscriber_subscribe_id, + ) + .await; + + // Generate SendStreamDispacher + let (send_stream_tx, mut send_stream_rx) = mpsc::channel::(1024); + + tokio::spawn(async move { send_stream_dispatcher(&mut send_stream_rx).await }); + let mut send_stream_dispatcher: SendStreamDispatcher = + SendStreamDispatcher::new(send_stream_tx.clone()); + + let (uni_relay_tx, _) = mpsc::channel::>>(1024); + let _ = send_stream_tx + .send(SendStreamDispatchCommand::Set { + session_id: subscriber_session_id, + stream_direction: StreamDirection::Bi, + sender: uni_relay_tx, + }) + .await; + + // Execute subscribe_ok_handler and get result + let result = subscribe_ok_handler( + subscribe_ok, + &mut track_namespace_manager, + &mut send_stream_dispatcher, + &mut client, + ) + .await; + + assert!(result.is_ok()); + } +} + +#[cfg(test)] +mod failure { + use crate::modules::handlers::subscribe_ok_handler::subscribe_ok_handler; + use crate::modules::send_stream_dispatcher::{ + send_stream_dispatcher, SendStreamDispatchCommand, SendStreamDispatcher, + }; + use crate::modules::track_namespace_manager::{ + track_namespace_manager, TrackCommand, TrackNamespaceManager, + }; + use crate::MOQTClient; + use moqt_core::constants::StreamDirection; + use moqt_core::messages::control_messages::subscribe::{FilterType, GroupOrder}; + use moqt_core::messages::control_messages::version_specific_parameters::{ + AuthorizationInfo, VersionSpecificParameter, + }; + use moqt_core::messages::{ + control_messages::subscribe_ok::SubscribeOk, moqt_payload::MOQTPayload, + }; + use moqt_core::TrackNamespaceManagerRepository; + use std::sync::Arc; + use tokio::sync::mpsc; + + #[tokio::test] + async fn relay_fail() { + // Generate SUBSCRIBE_OK message + let subscriber_subscribe_id = 0; + let track_alias = 0; + let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); + let track_name = "track_name".to_string(); + let subscriber_priority = 0; + let expires = 1; + let group_order = GroupOrder::Ascending; + let content_exists = false; + let largest_group_id = None; + let largest_object_id = None; + let filter_type = FilterType::LatestGroup; + let start_group = None; + let start_object = None; + let end_group = None; + let end_object = None; + let version_specific_parameter = + VersionSpecificParameter::AuthorizationInfo(AuthorizationInfo::new("test".to_string())); + let subscribe_parameters = vec![version_specific_parameter]; + + let subscribe_ok = SubscribeOk::new( + subscriber_subscribe_id, + expires, + group_order, + content_exists, + largest_group_id, + largest_object_id, + subscribe_parameters, + ); + let mut buf = bytes::BytesMut::new(); + subscribe_ok.packetize(&mut buf); + + // Generate client + let publisher_session_id = 1; + let mut client = MOQTClient::new(publisher_session_id); + + // Generate TrackNamespaceManager + let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + tokio::spawn(async move { track_namespace_manager(&mut track_namespace_rx).await }); + let mut track_namespace_manager: TrackNamespaceManager = + TrackNamespaceManager::new(track_namespace_tx); + + let subscriber_session_id = 2; + let max_subscribe_id = 10; + + let _ = track_namespace_manager + .setup_publisher(max_subscribe_id, publisher_session_id) + .await; + let _ = track_namespace_manager + .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) + .await; + let (publisher_subscribe_id, _) = track_namespace_manager + .set_publisher_subscription( + publisher_session_id, + track_namespace.clone(), + track_name.clone(), + subscriber_priority, + group_order, + filter_type, + start_group, + start_object, + end_group, + end_object, + ) + .await + .unwrap(); + + let _ = track_namespace_manager + .setup_subscriber(max_subscribe_id, subscriber_session_id) + .await; + let _ = track_namespace_manager + .set_subscriber_subscription( + subscriber_session_id, + subscriber_subscribe_id, + track_alias, + track_namespace, + track_name, + subscriber_priority, + group_order, + filter_type, + start_group, + start_object, + end_group, + end_object, + ) + .await; + let _ = track_namespace_manager + .register_pubsup_relation( + publisher_session_id, + publisher_subscribe_id, + subscriber_session_id, + subscriber_subscribe_id, + ) + .await; + + // Generate SendStreamDispacher (without set sender) + let (send_stream_tx, mut send_stream_rx) = mpsc::channel::(1024); + + tokio::spawn(async move { send_stream_dispatcher(&mut send_stream_rx).await }); + let mut send_stream_dispatcher: SendStreamDispatcher = + SendStreamDispatcher::new(send_stream_tx.clone()); + + // Execute subscribe_ok_handler and get result + let result = subscribe_ok_handler( + subscribe_ok, + &mut track_namespace_manager, + &mut send_stream_dispatcher, + &mut client, + ) + .await; + + assert!(result.is_err()); + } + + #[tokio::test] + async fn subscriber_not_found() { + // Generate SUBSCRIBE_OK message + let subscriber_subscribe_id = 0; + let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); + let track_name = "track_name".to_string(); + let subscriber_priority = 0; + let expires = 1; + let group_order = GroupOrder::Ascending; + let content_exists = false; + let largest_group_id = None; + let largest_object_id = None; + let filter_type = FilterType::LatestGroup; + let start_group = None; + let start_object = None; + let end_group = None; + let end_object = None; + let version_specific_parameter = + VersionSpecificParameter::AuthorizationInfo(AuthorizationInfo::new("test".to_string())); + let subscribe_parameters = vec![version_specific_parameter]; + + let subscribe_ok = SubscribeOk::new( + subscriber_subscribe_id, + expires, + group_order, + content_exists, + largest_group_id, + largest_object_id, + subscribe_parameters, + ); + let mut buf = bytes::BytesMut::new(); + subscribe_ok.packetize(&mut buf); + + // Generate client + let publisher_session_id = 1; + let mut client = MOQTClient::new(publisher_session_id); + + // Generate TrackNamespaceManager + let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + tokio::spawn(async move { track_namespace_manager(&mut track_namespace_rx).await }); + let mut track_namespace_manager: TrackNamespaceManager = + TrackNamespaceManager::new(track_namespace_tx); + + let subscriber_session_id = 2; + let max_subscribe_id = 10; + + let _ = track_namespace_manager + .setup_publisher(max_subscribe_id, publisher_session_id) + .await; + let _ = track_namespace_manager + .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) + .await; + let _ = track_namespace_manager + .set_publisher_subscription( + publisher_session_id, + track_namespace.clone(), + track_name.clone(), + subscriber_priority, + group_order, + filter_type, + start_group, + start_object, + end_group, + end_object, + ) + .await + .unwrap(); + + // Generate SendStreamDispacher + let (send_stream_tx, mut send_stream_rx) = mpsc::channel::(1024); + + tokio::spawn(async move { send_stream_dispatcher(&mut send_stream_rx).await }); + let mut send_stream_dispatcher: SendStreamDispatcher = + SendStreamDispatcher::new(send_stream_tx.clone()); + + let (uni_relay_tx, _) = mpsc::channel::>>(1024); + let _ = send_stream_tx + .send(SendStreamDispatchCommand::Set { + session_id: subscriber_session_id, + stream_direction: StreamDirection::Bi, + sender: uni_relay_tx, + }) + .await; + + // Execute subscribe_ok_handler and get result + let result = subscribe_ok_handler( + subscribe_ok, + &mut track_namespace_manager, + &mut send_stream_dispatcher, + &mut client, + ) + .await; + + assert!(result.is_err()); + } + + #[tokio::test] + async fn subscriber_already_activated() { + // Generate SUBSCRIBE_OK message + let subscriber_subscribe_id = 0; + let track_alias = 0; + let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); + let track_name = "track_name".to_string(); + let subscriber_priority = 0; + let expires = 1; + let group_order = GroupOrder::Ascending; + let content_exists = false; + let largest_group_id = None; + let largest_object_id = None; + let filter_type = FilterType::LatestGroup; + let start_group = None; + let start_object = None; + let end_group = None; + let end_object = None; + let version_specific_parameter = + VersionSpecificParameter::AuthorizationInfo(AuthorizationInfo::new("test".to_string())); + let subscribe_parameters = vec![version_specific_parameter]; + + let subscribe_ok = SubscribeOk::new( + subscriber_subscribe_id, + expires, + group_order, + content_exists, + largest_group_id, + largest_object_id, + subscribe_parameters, + ); + let mut buf = bytes::BytesMut::new(); + subscribe_ok.packetize(&mut buf); + + // Generate client + let publisher_session_id = 1; + let mut client = MOQTClient::new(publisher_session_id); + + // Generate TrackNamespaceManager + let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + tokio::spawn(async move { track_namespace_manager(&mut track_namespace_rx).await }); + let mut track_namespace_manager: TrackNamespaceManager = + TrackNamespaceManager::new(track_namespace_tx); + + let subscriber_session_id = 2; + let max_subscribe_id = 10; + + let _ = track_namespace_manager + .setup_publisher(max_subscribe_id, publisher_session_id) + .await; + let _ = track_namespace_manager + .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) + .await; + let (publisher_subscribe_id, _) = track_namespace_manager + .set_publisher_subscription( + publisher_session_id, + track_namespace.clone(), + track_name.clone(), + subscriber_priority, + group_order, + filter_type, + start_group, + start_object, + end_group, + end_object, + ) + .await + .unwrap(); + + let _ = track_namespace_manager + .setup_subscriber(max_subscribe_id, subscriber_session_id) + .await; + let _ = track_namespace_manager + .set_subscriber_subscription( + subscriber_session_id, + subscriber_subscribe_id, + track_alias, + track_namespace, + track_name, + subscriber_priority, + group_order, + filter_type, + start_group, + start_object, + end_group, + end_object, + ) + .await; + let _ = track_namespace_manager + .register_pubsup_relation( + publisher_session_id, + publisher_subscribe_id, + subscriber_session_id, + subscriber_subscribe_id, + ) + .await; + + let _ = track_namespace_manager + .activate_subscriber_subscription(subscriber_session_id, subscriber_subscribe_id) + .await; + + // Generate SendStreamDispacher + let (send_stream_tx, mut send_stream_rx) = mpsc::channel::(1024); + + tokio::spawn(async move { send_stream_dispatcher(&mut send_stream_rx).await }); + let mut send_stream_dispatcher: SendStreamDispatcher = + SendStreamDispatcher::new(send_stream_tx.clone()); + + let (uni_relay_tx, _) = mpsc::channel::>>(1024); + let _ = send_stream_tx + .send(SendStreamDispatchCommand::Set { + session_id: subscriber_session_id, + stream_direction: StreamDirection::Bi, + sender: uni_relay_tx, + }) + .await; + + // Execute subscribe_ok_handler and get result + let result = subscribe_ok_handler( + subscribe_ok, + &mut track_namespace_manager, + &mut send_stream_dispatcher, + &mut client, + ) + .await; + + assert!(result.is_ok()); + } + + #[tokio::test] + async fn publisher_already_activated() { + // Generate SUBSCRIBE_OK message + let subscriber_subscribe_id = 0; + let track_alias = 0; + let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); + let track_name = "track_name".to_string(); + let subscriber_priority = 0; + let expires = 1; + let group_order = GroupOrder::Ascending; + let content_exists = false; + let largest_group_id = None; + let largest_object_id = None; + let filter_type = FilterType::LatestGroup; + let start_group = None; + let start_object = None; + let end_group = None; + let end_object = None; + let version_specific_parameter = + VersionSpecificParameter::AuthorizationInfo(AuthorizationInfo::new("test".to_string())); + let subscribe_parameters = vec![version_specific_parameter]; + + let subscribe_ok = SubscribeOk::new( + subscriber_subscribe_id, + expires, + group_order, + content_exists, + largest_group_id, + largest_object_id, + subscribe_parameters, + ); + let mut buf = bytes::BytesMut::new(); + subscribe_ok.packetize(&mut buf); + + // Generate client + let publisher_session_id = 1; + let mut client = MOQTClient::new(publisher_session_id); + + // Generate TrackNamespaceManager + let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + tokio::spawn(async move { track_namespace_manager(&mut track_namespace_rx).await }); + let mut track_namespace_manager: TrackNamespaceManager = + TrackNamespaceManager::new(track_namespace_tx); + + let subscriber_session_id = 2; + let max_subscribe_id = 10; + + let _ = track_namespace_manager + .setup_publisher(max_subscribe_id, publisher_session_id) + .await; + let _ = track_namespace_manager + .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) + .await; + let (publisher_subscribe_id, _) = track_namespace_manager + .set_publisher_subscription( + publisher_session_id, + track_namespace.clone(), + track_name.clone(), + subscriber_priority, + group_order, + filter_type, + start_group, + start_object, + end_group, + end_object, + ) + .await + .unwrap(); + + let _ = track_namespace_manager + .setup_subscriber(max_subscribe_id, subscriber_session_id) + .await; + let _ = track_namespace_manager + .set_subscriber_subscription( + subscriber_session_id, + subscriber_subscribe_id, + track_alias, + track_namespace, + track_name, + subscriber_priority, + group_order, + filter_type, + start_group, + start_object, + end_group, + end_object, + ) + .await; + let _ = track_namespace_manager + .register_pubsup_relation( + publisher_session_id, + publisher_subscribe_id, + subscriber_session_id, + subscriber_subscribe_id, + ) + .await; + + let _ = track_namespace_manager + .activate_publisher_subscription(publisher_session_id, publisher_subscribe_id) + .await; + + // Generate SendStreamDispacher + let (send_stream_tx, mut send_stream_rx) = mpsc::channel::(1024); + + tokio::spawn(async move { send_stream_dispatcher(&mut send_stream_rx).await }); + let mut send_stream_dispatcher: SendStreamDispatcher = + SendStreamDispatcher::new(send_stream_tx.clone()); + + let (uni_relay_tx, _) = mpsc::channel::>>(1024); + let _ = send_stream_tx + .send(SendStreamDispatchCommand::Set { + session_id: subscriber_session_id, + stream_direction: StreamDirection::Bi, + sender: uni_relay_tx, + }) + .await; + + // Execute subscribe_ok_handler and get result + let result = subscribe_ok_handler( + subscribe_ok, + &mut track_namespace_manager, + &mut send_stream_dispatcher, + &mut client, + ) + .await; + + assert!(result.is_ok()); + } +} diff --git a/moqt-server/src/modules/server_processes/subscribe_message.rs b/moqt-server/src/modules/server_processes/subscribe_message.rs index efd99d07..0736b445 100644 --- a/moqt-server/src/modules/server_processes/subscribe_message.rs +++ b/moqt-server/src/modules/server_processes/subscribe_message.rs @@ -39,7 +39,7 @@ pub(crate) async fn process_subscribe_message( subscribe_ok.as_ref().unwrap().packetize(write_buf); } - return result; + result } Err(err) => { tracing::error!("subscribe_handler: err: {:?}", err.to_string()); diff --git a/moqt-server/src/modules/track_namespace_manager.rs b/moqt-server/src/modules/track_namespace_manager.rs index ac20aca4..8050f431 100644 --- a/moqt-server/src/modules/track_namespace_manager.rs +++ b/moqt-server/src/modules/track_namespace_manager.rs @@ -11,8 +11,8 @@ use TrackCommand::*; type SubscriberSessionId = usize; type PublisherSessionId = usize; -type PublishedSubscribeId = u64; -type SubscribedSubscribeId = u64; +type PublisherSubscribeId = u64; +type SubscriberSubscribeId = u64; type Consumers = HashMap; type Producers = HashMap; @@ -20,8 +20,8 @@ type Producers = HashMap; #[derive(Debug, Clone)] pub(crate) struct PubSubRelation { records: HashMap< - (PublisherSessionId, PublishedSubscribeId), - Vec<(SubscriberSessionId, SubscribedSubscribeId)>, + (PublisherSessionId, PublisherSubscribeId), + Vec<(SubscriberSessionId, SubscriberSubscribeId)>, >, } @@ -35,12 +35,12 @@ impl PubSubRelation { fn add_relation( &mut self, publisher_session_id: PublisherSessionId, - published_subscribe_id: PublishedSubscribeId, + publisher_subscribe_id: PublisherSubscribeId, subscriber_session_id: SubscriberSessionId, - subscribed_subscribe_id: SubscribedSubscribeId, + subscriber_subscribe_id: SubscriberSubscribeId, ) -> Result<()> { - let key = (publisher_session_id, published_subscribe_id); - let value = (subscriber_session_id, subscribed_subscribe_id); + let key = (publisher_session_id, publisher_subscribe_id); + let value = (subscriber_session_id, subscriber_subscribe_id); match self.records.get_mut(&key) { // If the key exists, add the value to the existing vector @@ -56,12 +56,12 @@ impl PubSubRelation { Ok(()) } - fn get_subscribers( + pub(crate) fn get_subscribers( &self, publisher_session_id: PublisherSessionId, - published_subscribe_id: PublishedSubscribeId, - ) -> Option<&Vec<(SubscriberSessionId, SubscribedSubscribeId)>> { - let key = (publisher_session_id, published_subscribe_id); + publisher_subscribe_id: PublisherSubscribeId, + ) -> Option<&Vec<(SubscriberSessionId, SubscriberSubscribeId)>> { + let key = (publisher_session_id, publisher_subscribe_id); self.records.get(&key) } @@ -182,13 +182,13 @@ pub(crate) async fn track_namespace_manager(rx: &mut mpsc::Receiver { if !pubsub_relation .records - .contains_key(&(publisher_session_id, published_subscribe_id)) + .contains_key(&(publisher_session_id, publisher_subscribe_id)) { let msg = "publisher not found in pubsub relation"; tracing::error!(msg); @@ -197,18 +197,18 @@ pub(crate) async fn track_namespace_manager(rx: &mut mpsc::Receiver> = match subscribers { Some(subscribers) => { let mut requesting_subscribers = vec![]; - for (subscriber_session_id, subscribed_subscribe_id) in subscribers { + for (subscriber_session_id, subscriber_subscribe_id) in subscribers { let producer = producers.get(subscriber_session_id).unwrap(); - if producer.is_requesting(*subscribed_subscribe_id) { + if producer.is_requesting(*subscriber_subscribe_id) { requesting_subscribers - .push((*subscriber_session_id, *subscribed_subscribe_id)); + .push((*subscriber_session_id, *subscriber_subscribe_id)); } } @@ -373,16 +373,16 @@ pub(crate) async fn track_namespace_manager(rx: &mut mpsc::Receiver { let result = pubsub_relation.add_relation( publisher_session_id, - published_subscribe_id, + publisher_subscribe_id, subscriber_session_id, - subscribed_subscribe_id, + subscriber_subscribe_id, ); match result { @@ -570,7 +570,7 @@ pub(crate) enum TrackCommand { resp: oneshot::Sender>>, }, GetRequestingSubscriberSessionIdsAndSubscribeIds { - published_subscribe_id: u64, + publisher_subscribe_id: u64, publisher_session_id: usize, #[allow(clippy::type_complexity)] resp: oneshot::Sender>>>, @@ -611,9 +611,9 @@ pub(crate) enum TrackCommand { }, RegisterPubSubRelation { publisher_session_id: usize, - published_subscribe_id: u64, + publisher_subscribe_id: u64, subscriber_session_id: usize, - subscribed_subscribe_id: u64, + subscriber_subscribe_id: u64, resp: oneshot::Sender>, }, ActivateSubscriberSubscription { @@ -817,12 +817,12 @@ impl TrackNamespaceManagerRepository for TrackNamespaceManager { } async fn get_requesting_subscriber_session_ids_and_subscribe_ids( &self, - published_subscribe_id: u64, + publisher_subscribe_id: u64, publisher_session_id: usize, ) -> Result>> { let (resp_tx, resp_rx) = oneshot::channel::>>>(); let cmd = TrackCommand::GetRequestingSubscriberSessionIdsAndSubscribeIds { - published_subscribe_id, + publisher_subscribe_id, publisher_session_id, resp: resp_tx, }; @@ -937,16 +937,16 @@ impl TrackNamespaceManagerRepository for TrackNamespaceManager { async fn register_pubsup_relation( &self, publisher_session_id: usize, - published_subscribe_id: u64, + publisher_subscribe_id: u64, subscriber_session_id: usize, - subscribed_subscribe_id: u64, + subscriber_subscribe_id: u64, ) -> Result<()> { let (resp_tx, resp_rx) = oneshot::channel::>(); let cmd = RegisterPubSubRelation { publisher_session_id, - published_subscribe_id, + publisher_subscribe_id, subscriber_session_id, - subscribed_subscribe_id, + subscriber_subscribe_id, resp: resp_tx, }; @@ -1032,7 +1032,7 @@ impl TrackNamespaceManagerRepository for TrackNamespaceManager { } #[cfg(test)] -mod test_fn { +pub(crate) mod test_fn { use crate::modules::track_namespace_manager::{ Consumers, Producers, PubSubRelation, TrackCommand, TrackNamespaceManager, @@ -1051,7 +1051,6 @@ mod test_fn { } } -// TrackNamespaceManagerの中身を一新したのですべてのテストを新たに書き直す #[cfg(test)] mod success { use crate::modules::track_namespace_manager::test_fn; @@ -1781,8 +1780,7 @@ mod success { test_fn::get_node_and_relation_clone(&track_namespace_manager).await; let subscriber = pubsub_relation - .records - .get(&(publisher_session_id, publisher_subscribe_id)) + .get_subscribers(publisher_session_id, publisher_subscribe_id) .unwrap() .to_vec(); @@ -2150,14 +2148,12 @@ mod success { // Assert that the relation is deleted // Remain: pub 1 <- sub 3, 4 - let pub1_relation = pubsub_relation - .records - .get(&(publisher_session_ids[0], publisher_subscribe_ids[0])); + let pub1_relation = + pubsub_relation.get_subscribers(publisher_session_ids[0], publisher_subscribe_ids[0]); assert!(pub1_relation.is_some()); - let pub2_relation = pubsub_relation - .records - .get(&(publisher_session_ids[1], publisher_subscribe_ids[1])); + let pub2_relation = + pubsub_relation.get_subscribers(publisher_session_ids[1], publisher_subscribe_ids[1]); assert!(pub2_relation.is_none()); } From 98fd06dc1e9655e05bc6ada9d7c920a3c6e336fe Mon Sep 17 00:00:00 2001 From: tetta maeda Date: Mon, 7 Oct 2024 21:12:19 +0900 Subject: [PATCH 06/17] refactor: function name --- .../src/modules/control_message_handler.rs | 14 +++++------ .../src/modules/handlers/subscribe_handler.rs | 6 ++--- .../src/modules/track_namespace_manager.rs | 24 +++++++++---------- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/moqt-server/src/modules/control_message_handler.rs b/moqt-server/src/modules/control_message_handler.rs index 3cd5d15d..69ea73fe 100644 --- a/moqt-server/src/modules/control_message_handler.rs +++ b/moqt-server/src/modules/control_message_handler.rs @@ -308,7 +308,7 @@ pub async fn control_message_handler( } #[cfg(test)] -pub(crate) mod test_fn { +pub(crate) mod test_utils { use crate::modules::control_message_handler::control_message_handler; use crate::modules::control_message_handler::MessageProcessResult; @@ -369,14 +369,14 @@ mod success { use moqt_core::control_message_type::ControlMessageType; use moqt_core::moqt_client::MOQTClientStatus; - use crate::modules::control_message_handler::test_fn; + use crate::modules::control_message_handler::test_utils; async fn assert_success( message_type_u8: u8, bytes_array: &[u8], client_status: MOQTClientStatus, ) { - let result = test_fn::packetize_buf_and_execute_control_message_handler( + let result = test_utils::packetize_buf_and_execute_control_message_handler( message_type_u8, bytes_array, client_status, @@ -432,14 +432,14 @@ mod failure { use moqt_core::control_message_type::ControlMessageType; use moqt_core::moqt_client::MOQTClientStatus; - use crate::modules::control_message_handler::test_fn; + use crate::modules::control_message_handler::test_utils; async fn assert_protocol_violation( message_type_u8: u8, bytes_array: &[u8], client_status: MOQTClientStatus, ) { - let result = test_fn::packetize_buf_and_execute_control_message_handler( + let result = test_utils::packetize_buf_and_execute_control_message_handler( message_type_u8, bytes_array, client_status, @@ -459,7 +459,7 @@ mod failure { bytes_array: &[u8], client_status: MOQTClientStatus, ) { - let result = test_fn::packetize_buf_and_execute_control_message_handler( + let result = test_utils::packetize_buf_and_execute_control_message_handler( message_type_u8, bytes_array, client_status, @@ -479,7 +479,7 @@ mod failure { bytes_array: &[u8], client_status: MOQTClientStatus, ) { - let result = test_fn::packetize_buf_and_execute_control_message_handler( + let result = test_utils::packetize_buf_and_execute_control_message_handler( message_type_u8, bytes_array, client_status, diff --git a/moqt-server/src/modules/handlers/subscribe_handler.rs b/moqt-server/src/modules/handlers/subscribe_handler.rs index 7e329d0c..a30c71d8 100644 --- a/moqt-server/src/modules/handlers/subscribe_handler.rs +++ b/moqt-server/src/modules/handlers/subscribe_handler.rs @@ -292,7 +292,7 @@ mod success { use crate::modules::send_stream_dispatcher::{ send_stream_dispatcher, SendStreamDispatchCommand, SendStreamDispatcher, }; - use crate::modules::track_namespace_manager::test_fn; + use crate::modules::track_namespace_manager::test_utils; use crate::modules::track_namespace_manager::{ track_namespace_manager, TrackCommand, TrackNamespaceManager, }; @@ -401,7 +401,7 @@ mod success { // Check the subscriber is registered let (_, producers, pubsub_relation) = - test_fn::get_node_and_relation_clone(&track_namespace_manager).await; + test_utils::get_node_and_relation_clone(&track_namespace_manager).await; assert_eq!(producers.len(), 1); @@ -523,7 +523,7 @@ mod success { // Check the subscriber is registered let (_, producers, pubsub_relation) = - test_fn::get_node_and_relation_clone(&track_namespace_manager).await; + test_utils::get_node_and_relation_clone(&track_namespace_manager).await; assert_eq!(producers.len(), 1); diff --git a/moqt-server/src/modules/track_namespace_manager.rs b/moqt-server/src/modules/track_namespace_manager.rs index 8050f431..1b9f7dea 100644 --- a/moqt-server/src/modules/track_namespace_manager.rs +++ b/moqt-server/src/modules/track_namespace_manager.rs @@ -1032,7 +1032,7 @@ impl TrackNamespaceManagerRepository for TrackNamespaceManager { } #[cfg(test)] -pub(crate) mod test_fn { +pub(crate) mod test_utils { use crate::modules::track_namespace_manager::{ Consumers, Producers, PubSubRelation, TrackCommand, TrackNamespaceManager, @@ -1053,7 +1053,7 @@ pub(crate) mod test_fn { #[cfg(test)] mod success { - use crate::modules::track_namespace_manager::test_fn; + use crate::modules::track_namespace_manager::test_utils; use crate::modules::track_namespace_manager::{ track_namespace_manager, TrackCommand, TrackNamespaceManager, }; @@ -1080,7 +1080,7 @@ mod success { // Check if the publisher is created let (consumers, _, _) = - test_fn::get_node_and_relation_clone(&track_namespace_manager).await; + test_utils::get_node_and_relation_clone(&track_namespace_manager).await; let length = consumers.len(); assert_eq!(length, 1); @@ -1107,7 +1107,7 @@ mod success { // Check if the track_namespace is set let (consumers, _, _) = - test_fn::get_node_and_relation_clone(&track_namespace_manager).await; + test_utils::get_node_and_relation_clone(&track_namespace_manager).await; let consumer = consumers.get(&publisher_session_id).unwrap(); let announced_namespaces = consumer.get_namespaces().unwrap(); @@ -1133,7 +1133,7 @@ mod success { // Check if the subscriber is created let (_, producers, _) = - test_fn::get_node_and_relation_clone(&track_namespace_manager).await; + test_utils::get_node_and_relation_clone(&track_namespace_manager).await; let length = producers.len(); assert_eq!(length, 1); @@ -1609,7 +1609,7 @@ mod success { // Assert that the subscription is set let (_, producers, _) = - test_fn::get_node_and_relation_clone(&track_namespace_manager).await; + test_utils::get_node_and_relation_clone(&track_namespace_manager).await; let producer = producers.get(&subscriber_session_id).unwrap(); let subscription = producer.get_subscription(subscribe_id).unwrap().unwrap(); @@ -1673,7 +1673,7 @@ mod success { // Assert that the subscription is set let (consumers, _, _) = - test_fn::get_node_and_relation_clone(&track_namespace_manager).await; + test_utils::get_node_and_relation_clone(&track_namespace_manager).await; let consumer = consumers.get(&publisher_session_id).unwrap(); let subscription = consumer .get_subscription(publisher_subscribe_id) @@ -1777,7 +1777,7 @@ mod success { // Assert that the relation is registered let (_, _, pubsub_relation) = - test_fn::get_node_and_relation_clone(&track_namespace_manager).await; + test_utils::get_node_and_relation_clone(&track_namespace_manager).await; let subscriber = pubsub_relation .get_subscribers(publisher_session_id, publisher_subscribe_id) @@ -1842,7 +1842,7 @@ mod success { // Assert that the subscription is active let (_, producers, _) = - test_fn::get_node_and_relation_clone(&track_namespace_manager).await; + test_utils::get_node_and_relation_clone(&track_namespace_manager).await; let producer = producers.get(&subscriber_session_id).unwrap(); let subscription = producer.get_subscription(subscribe_id).unwrap().unwrap(); @@ -1896,7 +1896,7 @@ mod success { // Assert that the subscription is active let (consumers, _, _) = - test_fn::get_node_and_relation_clone(&track_namespace_manager).await; + test_utils::get_node_and_relation_clone(&track_namespace_manager).await; let consumer = consumers.get(&publisher_session_id).unwrap(); let subscription = consumer .get_subscription(publisher_subscribe_id) @@ -1934,7 +1934,7 @@ mod success { // Assert that the announced namespace is deleted let (consumers, _, _) = - test_fn::get_node_and_relation_clone(&track_namespace_manager).await; + test_utils::get_node_and_relation_clone(&track_namespace_manager).await; let consumer = consumers.get(&publisher_session_id).unwrap(); let announced_namespaces = consumer.get_namespaces().unwrap().to_vec(); @@ -2125,7 +2125,7 @@ mod success { assert!(delete_occured); let (consumers, producers, pubsub_relation) = - test_fn::get_node_and_relation_clone(&track_namespace_manager).await; + test_utils::get_node_and_relation_clone(&track_namespace_manager).await; // Assert that sub 2 is deleted // Remain: sub 3, 4 From 161df975dfb42f9d4c5420a33fcfc5af94e5331d Mon Sep 17 00:00:00 2001 From: tetta maeda Date: Mon, 7 Oct 2024 21:15:26 +0900 Subject: [PATCH 07/17] refactor: comment fix --- moqt-core/src/modules/messages/control_messages/subscribe.rs | 2 +- moqt-core/src/modules/messages/control_messages/subscribe_ok.rs | 2 +- moqt-core/src/modules/messages/data_streams/object_datagram.rs | 2 +- .../src/modules/messages/data_streams/object_stream_subgroup.rs | 2 +- .../src/modules/messages/data_streams/object_stream_track.rs | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/moqt-core/src/modules/messages/control_messages/subscribe.rs b/moqt-core/src/modules/messages/control_messages/subscribe.rs index 0002877e..586864da 100644 --- a/moqt-core/src/modules/messages/control_messages/subscribe.rs +++ b/moqt-core/src/modules/messages/control_messages/subscribe.rs @@ -138,7 +138,7 @@ impl MOQTPayload for Subscribe { read_fixed_length_bytes_from_buffer(buf, 1).context("subscriber priority")?[0]; let group_order_u8 = read_fixed_length_bytes_from_buffer(buf, 1)?[0]; - // Values larger than 0x2 are a protocol error. + // Values larger than 0x2 are a Protocol Violation. let group_order = match GroupOrder::try_from(group_order_u8).context("group order") { Ok(group_order) => group_order, Err(err) => { diff --git a/moqt-core/src/modules/messages/control_messages/subscribe_ok.rs b/moqt-core/src/modules/messages/control_messages/subscribe_ok.rs index 0f404ced..dfc6439d 100644 --- a/moqt-core/src/modules/messages/control_messages/subscribe_ok.rs +++ b/moqt-core/src/modules/messages/control_messages/subscribe_ok.rs @@ -58,7 +58,7 @@ impl MOQTPayload for SubscribeOk { let expires = read_variable_integer_from_buffer(buf).context("expires")?; let group_order_u8 = read_fixed_length_bytes_from_buffer(buf, 1)?[0]; - // Values larger than 0x2 are a protocol error. + // Values larger than 0x2 are a Protocol Violation. let group_order = match GroupOrder::try_from(group_order_u8).context("group order") { Ok(group_order) => group_order, Err(err) => { diff --git a/moqt-core/src/modules/messages/data_streams/object_datagram.rs b/moqt-core/src/modules/messages/data_streams/object_datagram.rs index 6b8cb28a..0f0e0c4b 100644 --- a/moqt-core/src/modules/messages/data_streams/object_datagram.rs +++ b/moqt-core/src/modules/messages/data_streams/object_datagram.rs @@ -79,7 +79,7 @@ impl MOQTPayload for ObjectDatagram { match ObjectStatus::try_from(object_status_u64 as u8).context("object status") { Ok(status) => status, Err(err) => { - // Any other value SHOULD be treated as a protocol error and terminate the session with a Protocol Violation + // Any other value SHOULD be treated as a Protocol Violation and terminate the session with a Protocol Violation // TODO: return Termination Error Code bail!(err); } diff --git a/moqt-core/src/modules/messages/data_streams/object_stream_subgroup.rs b/moqt-core/src/modules/messages/data_streams/object_stream_subgroup.rs index 9d73beeb..6d2bcb8d 100644 --- a/moqt-core/src/modules/messages/data_streams/object_stream_subgroup.rs +++ b/moqt-core/src/modules/messages/data_streams/object_stream_subgroup.rs @@ -58,7 +58,7 @@ impl MOQTPayload for ObjectStreamSubgroup { match ObjectStatus::try_from(object_status_u64 as u8).context("object status") { Ok(status) => status, Err(err) => { - // Any other value SHOULD be treated as a protocol error and terminate the session with a Protocol Violation + // Any other value SHOULD be treated as a Protocol Violation and terminate the session with a Protocol Violation // TODO: return Termination Error Code bail!(err); } diff --git a/moqt-core/src/modules/messages/data_streams/object_stream_track.rs b/moqt-core/src/modules/messages/data_streams/object_stream_track.rs index b53c8448..881b7ba4 100644 --- a/moqt-core/src/modules/messages/data_streams/object_stream_track.rs +++ b/moqt-core/src/modules/messages/data_streams/object_stream_track.rs @@ -62,7 +62,7 @@ impl MOQTPayload for ObjectStreamTrack { match ObjectStatus::try_from(object_status_u64 as u8).context("object status") { Ok(status) => status, Err(err) => { - // Any other value SHOULD be treated as a protocol error and terminate the session with a Protocol Violation + // Any other value SHOULD be treated as a Protocol Violation and terminate the session with a Protocol Violation // TODO: return Termination Error Code bail!(err); } From bbecdb5de800de6eb6ec136912f33b5ed35a42e9 Mon Sep 17 00:00:00 2001 From: tetta maeda Date: Mon, 7 Oct 2024 22:45:50 +0900 Subject: [PATCH 08/17] refactor: file structure --- moqt-core/src/lib.rs | 2 +- moqt-core/src/modules.rs | 2 +- ... => pubsub_relation_manager_repository.rs} | 3 +- moqt-core/src/modules/subscription_models.rs | 4 +- .../subscription_models/consumer_node.rs | 214 ++++ .../subscription_models/node_registory.rs | 54 + .../subscription_models/producer_node.rs | 202 ++++ .../subscription_models/subscription_nodes.rs | 453 -------- moqt-server/src/lib.rs | 23 +- moqt-server/src/modules.rs | 2 +- .../src/modules/control_message_handler.rs | 34 +- .../src/modules/handlers/announce_handler.rs | 47 +- .../modules/handlers/server_setup_handler.rs | 90 +- .../src/modules/handlers/subscribe_handler.rs | 180 +-- .../modules/handlers/subscribe_ok_handler.rs | 140 +-- .../modules/handlers/unannounce_handler.rs | 11 +- .../modules/handlers/unsubscribe_handler.rs | 9 +- moqt-server/src/modules/relation_manager.rs | 4 + .../src/modules/relation_manager/commands.rs | 123 ++ .../interface.rs} | 1019 ++++------------- .../src/modules/relation_manager/manager.rs | 474 ++++++++ .../src/modules/relation_manager/relation.rs | 59 + .../server_processes/announce_message.rs | 7 +- .../server_processes/client_setup_message.rs | 7 +- .../server_processes/subscribe_message.rs | 7 +- .../server_processes/subscribe_ok_message.rs | 7 +- 26 files changed, 1619 insertions(+), 1558 deletions(-) rename moqt-core/src/modules/{track_namespace_manager_repository.rs => pubsub_relation_manager_repository.rs} (98%) create mode 100644 moqt-core/src/modules/subscription_models/consumer_node.rs create mode 100644 moqt-core/src/modules/subscription_models/node_registory.rs create mode 100644 moqt-core/src/modules/subscription_models/producer_node.rs delete mode 100644 moqt-core/src/modules/subscription_models/subscription_nodes.rs create mode 100644 moqt-server/src/modules/relation_manager.rs create mode 100644 moqt-server/src/modules/relation_manager/commands.rs rename moqt-server/src/modules/{track_namespace_manager.rs => relation_manager/interface.rs} (60%) create mode 100644 moqt-server/src/modules/relation_manager/manager.rs create mode 100644 moqt-server/src/modules/relation_manager/relation.rs diff --git a/moqt-core/src/lib.rs b/moqt-core/src/lib.rs index ffb86d32..dc017bd6 100644 --- a/moqt-core/src/lib.rs +++ b/moqt-core/src/lib.rs @@ -1,5 +1,5 @@ mod modules; pub use modules::moqt_client::MOQTClient; +pub use modules::pubsub_relation_manager_repository::PubSubRelationManagerRepository; pub use modules::send_stream_dispatcher_repository::SendStreamDispatcherRepository; -pub use modules::track_namespace_manager_repository::TrackNamespaceManagerRepository; pub use modules::*; diff --git a/moqt-core/src/modules.rs b/moqt-core/src/modules.rs index 1a5fec31..5e301015 100644 --- a/moqt-core/src/modules.rs +++ b/moqt-core/src/modules.rs @@ -2,8 +2,8 @@ pub mod constants; pub mod control_message_type; pub mod messages; pub mod moqt_client; +pub mod pubsub_relation_manager_repository; pub mod send_stream_dispatcher_repository; pub mod subscription_models; -pub mod track_namespace_manager_repository; pub mod variable_bytes; pub mod variable_integer; diff --git a/moqt-core/src/modules/track_namespace_manager_repository.rs b/moqt-core/src/modules/pubsub_relation_manager_repository.rs similarity index 98% rename from moqt-core/src/modules/track_namespace_manager_repository.rs rename to moqt-core/src/modules/pubsub_relation_manager_repository.rs index 3588dea9..5aff5ade 100644 --- a/moqt-core/src/modules/track_namespace_manager_repository.rs +++ b/moqt-core/src/modules/pubsub_relation_manager_repository.rs @@ -1,10 +1,11 @@ use crate::messages::control_messages::subscribe::{FilterType, GroupOrder}; use crate::subscription_models::subscriptions::Subscription; + use anyhow::Result; use async_trait::async_trait; #[async_trait] -pub trait TrackNamespaceManagerRepository: Send + Sync { +pub trait PubSubRelationManagerRepository: Send + Sync { async fn setup_publisher( &self, max_subscribe_id: u64, diff --git a/moqt-core/src/modules/subscription_models.rs b/moqt-core/src/modules/subscription_models.rs index 8026549f..26e3c284 100644 --- a/moqt-core/src/modules/subscription_models.rs +++ b/moqt-core/src/modules/subscription_models.rs @@ -1,3 +1,5 @@ -pub mod subscription_nodes; +pub mod consumer_node; +pub mod node_registory; +pub mod producer_node; pub mod subscriptions; pub mod tracks; diff --git a/moqt-core/src/modules/subscription_models/consumer_node.rs b/moqt-core/src/modules/subscription_models/consumer_node.rs new file mode 100644 index 00000000..6711311b --- /dev/null +++ b/moqt-core/src/modules/subscription_models/consumer_node.rs @@ -0,0 +1,214 @@ +use crate::messages::control_messages::subscribe::{FilterType, GroupOrder}; +use crate::subscription_models::node_registory::SubscriptionNodeRegistory; +use crate::subscription_models::subscriptions::Subscription; +use anyhow::{bail, Result}; +use std::collections::HashMap; + +type SubscribeId = u64; +type TrackNamespace = Vec; +type TrackAlias = u64; + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Consumer { + max_subscriber_id: u64, + announced_namespaces: Vec, + subscribing_namespace_prefixes: Vec, + subscriptions: HashMap, +} + +impl Consumer { + pub fn new(max_subscriber_id: u64) -> Self { + Consumer { + max_subscriber_id, + announced_namespaces: Vec::new(), + subscribing_namespace_prefixes: Vec::new(), + subscriptions: HashMap::new(), + } + } +} + +impl SubscriptionNodeRegistory for Consumer { + fn set_subscription( + &mut self, + subscribe_id: SubscribeId, + track_alias: u64, + track_namespace: Vec, + track_name: String, + subscriber_priority: u8, + group_order: GroupOrder, + filter_type: FilterType, + start_group: Option, + start_object: Option, + end_group: Option, + end_object: Option, + ) -> Result<()> { + // Subscriber cannot define forwarding preference until it receives object message. + let subscription = Subscription::new( + track_alias, + track_namespace, + track_name, + subscriber_priority, + group_order, + filter_type, + start_group, + start_object, + end_group, + end_object, + None, + ); + + self.subscriptions.insert(subscribe_id, subscription); + + Ok(()) + } + + fn get_subscription(&self, subscribe_id: SubscribeId) -> Result> { + Ok(self.subscriptions.get(&subscribe_id).cloned()) + } + + fn get_subscription_by_full_track_name( + &self, + track_namespace: TrackNamespace, + track_name: String, + ) -> Result> { + Ok(self + .subscriptions + .values() + .find(|subscription| { + subscription.get_track_namespace_and_name() + == (track_namespace.clone(), track_name.clone()) + }) + .cloned()) + } + + fn get_subscribe_id( + &self, + track_namespace: TrackNamespace, + track_name: String, + ) -> Result> { + Ok(self + .subscriptions + .iter() + .find(|(_, subscription)| { + subscription.get_track_namespace_and_name() + == (track_namespace.clone(), track_name.clone()) + }) + .map(|(subscribe_id, _)| *subscribe_id)) + } + + fn has_track(&self, track_namespace: TrackNamespace, track_name: String) -> bool { + self.subscriptions.values().any(|subscription| { + subscription.get_track_namespace_and_name() + == (track_namespace.clone(), track_name.clone()) + }) + } + + fn activate_subscription(&mut self, subscribe_id: SubscribeId) -> Result { + let subscription = self.subscriptions.get_mut(&subscribe_id).unwrap(); + let activate = subscription.active(); + + Ok(activate) + } + + fn is_requesting(&self, subscribe_id: SubscribeId) -> bool { + self.subscriptions + .get(&subscribe_id) + .map(|subscription| subscription.is_requesting()) + .unwrap_or(false) + } + + fn delete_subscription(&mut self, subscribe_id: SubscribeId) -> Result<()> { + self.subscriptions.remove(&subscribe_id); + Ok(()) + } + + fn is_within_max_subscribe_id(&self, subscribe_id: SubscribeId) -> bool { + subscribe_id <= self.max_subscriber_id + } + + fn is_subscribe_id_unique(&self, subscribe_id: SubscribeId) -> bool { + !self.subscriptions.contains_key(&subscribe_id) + } + + fn is_track_alias_unique(&self, track_alias: TrackAlias) -> bool { + !self + .subscriptions + .values() + .any(|subscription| subscription.get_track_alias() == track_alias) + } + fn find_unused_subscribe_id_and_track_alias(&self) -> Result<(SubscribeId, TrackAlias)> { + for subscribe_id in 0..=self.max_subscriber_id { + if !self.subscriptions.contains_key(&subscribe_id) { + for track_alias in 0.. { + if !self + .subscriptions + .values() + .any(|subscription| subscription.get_track_alias() == track_alias) + { + return Ok((subscribe_id, track_alias)); + } + } + } + } + + bail!("No available subscribe_id and track_alias."); + } + + fn set_namespace(&mut self, namespace: TrackNamespace) -> Result<()> { + if self.announced_namespaces.contains(&namespace) { + bail!("Namespace already exists."); + } + + self.announced_namespaces.push(namespace); + + Ok(()) + } + + fn get_namespaces(&self) -> Result<&Vec> { + Ok(&self.announced_namespaces) + } + + fn has_namespace(&self, namespace: TrackNamespace) -> bool { + self.announced_namespaces.contains(&namespace) + } + + fn delete_namespace(&mut self, namespace: TrackNamespace) -> Result<()> { + if let Some(index) = self + .announced_namespaces + .iter() + .position(|x| x == &namespace) + { + self.announced_namespaces.remove(index); + } + + Ok(()) + } + fn set_namespace_prefix(&mut self, namespace_prefix: TrackNamespace) -> Result<()> { + if self + .subscribing_namespace_prefixes + .contains(&namespace_prefix) + { + bail!("Namespace prefix already exists."); + } + + self.subscribing_namespace_prefixes.push(namespace_prefix); + + Ok(()) + } + + fn get_namespace_prefixes(&self) -> Result<&Vec> { + Ok(&self.subscribing_namespace_prefixes) + } + + fn delete_namespace_prefix(&mut self, namespace_prefix: TrackNamespace) -> Result<()> { + if let Some(index) = self + .subscribing_namespace_prefixes + .iter() + .position(|x| x == &namespace_prefix) + { + self.subscribing_namespace_prefixes.remove(index); + } + + Ok(()) + } +} diff --git a/moqt-core/src/modules/subscription_models/node_registory.rs b/moqt-core/src/modules/subscription_models/node_registory.rs new file mode 100644 index 00000000..5cc59861 --- /dev/null +++ b/moqt-core/src/modules/subscription_models/node_registory.rs @@ -0,0 +1,54 @@ +use crate::messages::control_messages::subscribe::{FilterType, GroupOrder}; +use crate::subscription_models::subscriptions::Subscription; +use anyhow::Result; + +type SubscribeId = u64; +type TrackNamespace = Vec; +type TrackAlias = u64; + +pub trait SubscriptionNodeRegistory { + #[allow(clippy::too_many_arguments)] + fn set_subscription( + &mut self, + subscribe_id: SubscribeId, + track_alias: u64, + track_namespace: TrackNamespace, + track_name: String, + subscriber_priority: u8, + group_order: GroupOrder, + filter_type: FilterType, + start_group: Option, + start_object: Option, + end_group: Option, + end_object: Option, + ) -> Result<()>; + fn get_subscription(&self, subscribe_id: SubscribeId) -> Result>; + fn get_subscription_by_full_track_name( + &self, + track_namespace: TrackNamespace, + track_name: String, + ) -> Result>; + fn get_subscribe_id( + &self, + track_namespace: TrackNamespace, + track_name: String, + ) -> Result>; + fn has_track(&self, track_namespace: TrackNamespace, track_name: String) -> bool; + fn activate_subscription(&mut self, subscribe_id: SubscribeId) -> Result; + fn is_requesting(&self, subscribe_id: SubscribeId) -> bool; + fn delete_subscription(&mut self, subscribe_id: SubscribeId) -> Result<()>; + + fn is_within_max_subscribe_id(&self, subscribe_id: SubscribeId) -> bool; + fn is_subscribe_id_unique(&self, subscribe_id: SubscribeId) -> bool; + fn is_track_alias_unique(&self, track_alias: TrackAlias) -> bool; + fn find_unused_subscribe_id_and_track_alias(&self) -> Result<(SubscribeId, TrackAlias)>; + + fn set_namespace(&mut self, namespace: TrackNamespace) -> Result<()>; + fn get_namespaces(&self) -> Result<&Vec>; + fn has_namespace(&self, namespace: TrackNamespace) -> bool; + fn delete_namespace(&mut self, namespace: TrackNamespace) -> Result<()>; + + fn set_namespace_prefix(&mut self, namespace_prefix: TrackNamespace) -> Result<()>; + fn get_namespace_prefixes(&self) -> Result<&Vec>; + fn delete_namespace_prefix(&mut self, namespace_prefix: TrackNamespace) -> Result<()>; +} diff --git a/moqt-core/src/modules/subscription_models/producer_node.rs b/moqt-core/src/modules/subscription_models/producer_node.rs new file mode 100644 index 00000000..64dd0910 --- /dev/null +++ b/moqt-core/src/modules/subscription_models/producer_node.rs @@ -0,0 +1,202 @@ +use crate::messages::control_messages::subscribe::{FilterType, GroupOrder}; +use crate::subscription_models::node_registory::SubscriptionNodeRegistory; +use crate::subscription_models::subscriptions::Subscription; +use anyhow::{bail, Result}; +use std::collections::HashMap; + +type SubscribeId = u64; +type TrackNamespace = Vec; +type TrackAlias = u64; + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Producer { + max_subscriber_id: u64, + announcing_namespaces: Vec, + subscribed_namespace_prefixes: Vec, + subscriptions: HashMap, +} + +impl Producer { + pub fn new(max_subscriber_id: u64) -> Self { + Producer { + max_subscriber_id, + announcing_namespaces: Vec::new(), + subscribed_namespace_prefixes: Vec::new(), + subscriptions: HashMap::new(), + } + } +} + +impl SubscriptionNodeRegistory for Producer { + fn set_subscription( + &mut self, + subscribe_id: SubscribeId, + track_alias: u64, + track_namespace: Vec, + track_name: String, + subscriber_priority: u8, + group_order: GroupOrder, + filter_type: FilterType, + start_group: Option, + start_object: Option, + end_group: Option, + end_object: Option, + ) -> Result<()> { + // Publisher can define forwarding preference when it publishes track. + let subscription = Subscription::new( + track_alias, + track_namespace, + track_name, + subscriber_priority, + group_order, + filter_type, + start_group, + start_object, + end_group, + end_object, + None, + ); + + self.subscriptions.insert(subscribe_id, subscription); + + Ok(()) + } + + fn get_subscription(&self, subscribe_id: SubscribeId) -> Result> { + Ok(self.subscriptions.get(&subscribe_id).cloned()) + } + + fn get_subscription_by_full_track_name( + &self, + track_namespace: TrackNamespace, + track_name: String, + ) -> Result> { + Ok(self + .subscriptions + .values() + .find(|subscription| { + subscription.get_track_namespace_and_name() + == (track_namespace.clone(), track_name.clone()) + }) + .cloned()) + } + + fn get_subscribe_id( + &self, + track_namespace: TrackNamespace, + track_name: String, + ) -> Result> { + Ok(self + .subscriptions + .iter() + .find(|(_, subscription)| { + subscription.get_track_namespace_and_name() + == (track_namespace.clone(), track_name.clone()) + }) + .map(|(subscribe_id, _)| *subscribe_id)) + } + + fn has_track(&self, track_namespace: TrackNamespace, track_name: String) -> bool { + self.subscriptions.values().any(|subscription| { + subscription.get_track_namespace_and_name() + == (track_namespace.clone(), track_name.clone()) + }) + } + + fn activate_subscription(&mut self, subscribe_id: SubscribeId) -> Result { + let subscription = self.subscriptions.get_mut(&subscribe_id).unwrap(); + let activate = subscription.active(); + + Ok(activate) + } + + fn is_requesting(&self, subscribe_id: SubscribeId) -> bool { + self.subscriptions + .get(&subscribe_id) + .map(|subscription| subscription.is_requesting()) + .unwrap_or(false) + } + + fn delete_subscription(&mut self, subscribe_id: SubscribeId) -> Result<()> { + self.subscriptions.remove(&subscribe_id); + Ok(()) + } + + fn is_within_max_subscribe_id(&self, subscribe_id: SubscribeId) -> bool { + subscribe_id <= self.max_subscriber_id + } + + fn is_subscribe_id_unique(&self, subscribe_id: SubscribeId) -> bool { + !self.subscriptions.contains_key(&subscribe_id) + } + + fn is_track_alias_unique(&self, track_alias: TrackAlias) -> bool { + !self + .subscriptions + .values() + .any(|subscription| subscription.get_track_alias() == track_alias) + } + + fn find_unused_subscribe_id_and_track_alias(&self) -> Result<(SubscribeId, TrackAlias)> { + unimplemented!() + } + + fn set_namespace(&mut self, namespace: TrackNamespace) -> Result<()> { + if self.announcing_namespaces.contains(&namespace) { + bail!("Namespace already exists."); + } + + self.announcing_namespaces.push(namespace); + + Ok(()) + } + + fn get_namespaces(&self) -> Result<&Vec> { + Ok(&self.announcing_namespaces) + } + + fn has_namespace(&self, namespace: TrackNamespace) -> bool { + self.announcing_namespaces.contains(&namespace) + } + + fn delete_namespace(&mut self, namespace: TrackNamespace) -> Result<()> { + if let Some(index) = self + .announcing_namespaces + .iter() + .position(|x| x == &namespace) + { + self.announcing_namespaces.remove(index); + } + + Ok(()) + } + + fn set_namespace_prefix(&mut self, namespace_prefix: TrackNamespace) -> Result<()> { + if self + .subscribed_namespace_prefixes + .contains(&namespace_prefix) + { + bail!("Namespace prefix already exists."); + } + + self.subscribed_namespace_prefixes.push(namespace_prefix); + + Ok(()) + } + + fn get_namespace_prefixes(&self) -> Result<&Vec> { + Ok(&self.subscribed_namespace_prefixes) + } + + fn delete_namespace_prefix(&mut self, namespace_prefix: TrackNamespace) -> Result<()> { + if let Some(index) = self + .subscribed_namespace_prefixes + .iter() + .position(|x| x == &namespace_prefix) + { + self.subscribed_namespace_prefixes.remove(index); + } + + Ok(()) + } +} diff --git a/moqt-core/src/modules/subscription_models/subscription_nodes.rs b/moqt-core/src/modules/subscription_models/subscription_nodes.rs deleted file mode 100644 index 41c28c74..00000000 --- a/moqt-core/src/modules/subscription_models/subscription_nodes.rs +++ /dev/null @@ -1,453 +0,0 @@ -use crate::messages::control_messages::subscribe::{FilterType, GroupOrder}; -use crate::subscription_models::subscriptions::Subscription; -use anyhow::{bail, Result}; -use std::collections::HashMap; - -type SubscribeId = u64; -type TrackNamespace = Vec; -type TrackAlias = u64; - -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct Consumer { - max_subscriber_id: u64, - announced_namespaces: Vec, - subscribing_namespace_prefixes: Vec, - subscriptions: HashMap, -} - -impl Consumer { - pub fn new(max_subscriber_id: u64) -> Self { - Consumer { - max_subscriber_id, - announced_namespaces: Vec::new(), - subscribing_namespace_prefixes: Vec::new(), - subscriptions: HashMap::new(), - } - } -} - -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct Producer { - max_subscriber_id: u64, - announcing_namespaces: Vec, - subscribed_namespace_prefixes: Vec, - subscriptions: HashMap, -} - -impl Producer { - pub fn new(max_subscriber_id: u64) -> Self { - Producer { - max_subscriber_id, - announcing_namespaces: Vec::new(), - subscribed_namespace_prefixes: Vec::new(), - subscriptions: HashMap::new(), - } - } -} - -pub trait SubscriptionNodeRegistory { - #[allow(clippy::too_many_arguments)] - fn set_subscription( - &mut self, - subscribe_id: SubscribeId, - track_alias: u64, - track_namespace: TrackNamespace, - track_name: String, - subscriber_priority: u8, - group_order: GroupOrder, - filter_type: FilterType, - start_group: Option, - start_object: Option, - end_group: Option, - end_object: Option, - ) -> Result<()>; - fn get_subscription(&self, subscribe_id: SubscribeId) -> Result>; - fn get_subscription_by_full_track_name( - &self, - track_namespace: TrackNamespace, - track_name: String, - ) -> Result>; - fn get_subscribe_id( - &self, - track_namespace: TrackNamespace, - track_name: String, - ) -> Result>; - fn has_track(&self, track_namespace: TrackNamespace, track_name: String) -> bool; - fn activate_subscription(&mut self, subscribe_id: SubscribeId) -> Result; - fn is_requesting(&self, subscribe_id: SubscribeId) -> bool; - fn delete_subscription(&mut self, subscribe_id: SubscribeId) -> Result<()>; - - fn is_within_max_subscribe_id(&self, subscribe_id: SubscribeId) -> bool; - fn is_subscribe_id_unique(&self, subscribe_id: SubscribeId) -> bool; - fn is_track_alias_unique(&self, track_alias: TrackAlias) -> bool; - fn find_unused_subscribe_id_and_track_alias(&self) -> Result<(SubscribeId, TrackAlias)>; - - fn set_namespace(&mut self, namespace: TrackNamespace) -> Result<()>; - fn get_namespaces(&self) -> Result<&Vec>; - fn has_namespace(&self, namespace: TrackNamespace) -> bool; - fn delete_namespace(&mut self, namespace: TrackNamespace) -> Result<()>; - - fn set_namespace_prefix(&mut self, namespace_prefix: TrackNamespace) -> Result<()>; - fn get_namespace_prefixes(&self) -> Result<&Vec>; - fn delete_namespace_prefix(&mut self, namespace_prefix: TrackNamespace) -> Result<()>; -} - -impl SubscriptionNodeRegistory for Consumer { - fn set_subscription( - &mut self, - subscribe_id: SubscribeId, - track_alias: u64, - track_namespace: Vec, - track_name: String, - subscriber_priority: u8, - group_order: GroupOrder, - filter_type: FilterType, - start_group: Option, - start_object: Option, - end_group: Option, - end_object: Option, - ) -> Result<()> { - // Subscriber cannot define forwarding preference until it receives object message. - let subscription = Subscription::new( - track_alias, - track_namespace, - track_name, - subscriber_priority, - group_order, - filter_type, - start_group, - start_object, - end_group, - end_object, - None, - ); - - self.subscriptions.insert(subscribe_id, subscription); - - Ok(()) - } - - fn get_subscription(&self, subscribe_id: SubscribeId) -> Result> { - Ok(self.subscriptions.get(&subscribe_id).cloned()) - } - - fn get_subscription_by_full_track_name( - &self, - track_namespace: TrackNamespace, - track_name: String, - ) -> Result> { - Ok(self - .subscriptions - .values() - .find(|subscription| { - subscription.get_track_namespace_and_name() - == (track_namespace.clone(), track_name.clone()) - }) - .cloned()) - } - - fn get_subscribe_id( - &self, - track_namespace: TrackNamespace, - track_name: String, - ) -> Result> { - Ok(self - .subscriptions - .iter() - .find(|(_, subscription)| { - subscription.get_track_namespace_and_name() - == (track_namespace.clone(), track_name.clone()) - }) - .map(|(subscribe_id, _)| *subscribe_id)) - } - - fn has_track(&self, track_namespace: TrackNamespace, track_name: String) -> bool { - self.subscriptions.values().any(|subscription| { - subscription.get_track_namespace_and_name() - == (track_namespace.clone(), track_name.clone()) - }) - } - - fn activate_subscription(&mut self, subscribe_id: SubscribeId) -> Result { - let subscription = self.subscriptions.get_mut(&subscribe_id).unwrap(); - let activate = subscription.active(); - - Ok(activate) - } - - fn is_requesting(&self, subscribe_id: SubscribeId) -> bool { - self.subscriptions - .get(&subscribe_id) - .map(|subscription| subscription.is_requesting()) - .unwrap_or(false) - } - - fn delete_subscription(&mut self, subscribe_id: SubscribeId) -> Result<()> { - self.subscriptions.remove(&subscribe_id); - Ok(()) - } - - fn is_within_max_subscribe_id(&self, subscribe_id: SubscribeId) -> bool { - subscribe_id <= self.max_subscriber_id - } - - fn is_subscribe_id_unique(&self, subscribe_id: SubscribeId) -> bool { - !self.subscriptions.contains_key(&subscribe_id) - } - - fn is_track_alias_unique(&self, track_alias: TrackAlias) -> bool { - !self - .subscriptions - .values() - .any(|subscription| subscription.get_track_alias() == track_alias) - } - fn find_unused_subscribe_id_and_track_alias(&self) -> Result<(SubscribeId, TrackAlias)> { - for subscribe_id in 0..=self.max_subscriber_id { - if !self.subscriptions.contains_key(&subscribe_id) { - for track_alias in 0.. { - if !self - .subscriptions - .values() - .any(|subscription| subscription.get_track_alias() == track_alias) - { - return Ok((subscribe_id, track_alias)); - } - } - } - } - - bail!("No available subscribe_id and track_alias."); - } - - fn set_namespace(&mut self, namespace: TrackNamespace) -> Result<()> { - if self.announced_namespaces.contains(&namespace) { - bail!("Namespace already exists."); - } - - self.announced_namespaces.push(namespace); - - Ok(()) - } - - fn get_namespaces(&self) -> Result<&Vec> { - Ok(&self.announced_namespaces) - } - - fn has_namespace(&self, namespace: TrackNamespace) -> bool { - self.announced_namespaces.contains(&namespace) - } - - fn delete_namespace(&mut self, namespace: TrackNamespace) -> Result<()> { - if let Some(index) = self - .announced_namespaces - .iter() - .position(|x| x == &namespace) - { - self.announced_namespaces.remove(index); - } - - Ok(()) - } - fn set_namespace_prefix(&mut self, namespace_prefix: TrackNamespace) -> Result<()> { - if self - .subscribing_namespace_prefixes - .contains(&namespace_prefix) - { - bail!("Namespace prefix already exists."); - } - - self.subscribing_namespace_prefixes.push(namespace_prefix); - - Ok(()) - } - - fn get_namespace_prefixes(&self) -> Result<&Vec> { - Ok(&self.subscribing_namespace_prefixes) - } - - fn delete_namespace_prefix(&mut self, namespace_prefix: TrackNamespace) -> Result<()> { - if let Some(index) = self - .subscribing_namespace_prefixes - .iter() - .position(|x| x == &namespace_prefix) - { - self.subscribing_namespace_prefixes.remove(index); - } - - Ok(()) - } -} - -impl SubscriptionNodeRegistory for Producer { - fn set_subscription( - &mut self, - subscribe_id: SubscribeId, - track_alias: u64, - track_namespace: Vec, - track_name: String, - subscriber_priority: u8, - group_order: GroupOrder, - filter_type: FilterType, - start_group: Option, - start_object: Option, - end_group: Option, - end_object: Option, - ) -> Result<()> { - // Publisher can define forwarding preference when it publishes track. - let subscription = Subscription::new( - track_alias, - track_namespace, - track_name, - subscriber_priority, - group_order, - filter_type, - start_group, - start_object, - end_group, - end_object, - None, - ); - - self.subscriptions.insert(subscribe_id, subscription); - - Ok(()) - } - - fn get_subscription(&self, subscribe_id: SubscribeId) -> Result> { - Ok(self.subscriptions.get(&subscribe_id).cloned()) - } - - fn get_subscription_by_full_track_name( - &self, - track_namespace: TrackNamespace, - track_name: String, - ) -> Result> { - Ok(self - .subscriptions - .values() - .find(|subscription| { - subscription.get_track_namespace_and_name() - == (track_namespace.clone(), track_name.clone()) - }) - .cloned()) - } - - fn get_subscribe_id( - &self, - track_namespace: TrackNamespace, - track_name: String, - ) -> Result> { - Ok(self - .subscriptions - .iter() - .find(|(_, subscription)| { - subscription.get_track_namespace_and_name() - == (track_namespace.clone(), track_name.clone()) - }) - .map(|(subscribe_id, _)| *subscribe_id)) - } - - fn has_track(&self, track_namespace: TrackNamespace, track_name: String) -> bool { - self.subscriptions.values().any(|subscription| { - subscription.get_track_namespace_and_name() - == (track_namespace.clone(), track_name.clone()) - }) - } - - fn activate_subscription(&mut self, subscribe_id: SubscribeId) -> Result { - let subscription = self.subscriptions.get_mut(&subscribe_id).unwrap(); - let activate = subscription.active(); - - Ok(activate) - } - - fn is_requesting(&self, subscribe_id: SubscribeId) -> bool { - self.subscriptions - .get(&subscribe_id) - .map(|subscription| subscription.is_requesting()) - .unwrap_or(false) - } - - fn delete_subscription(&mut self, subscribe_id: SubscribeId) -> Result<()> { - self.subscriptions.remove(&subscribe_id); - Ok(()) - } - - fn is_within_max_subscribe_id(&self, subscribe_id: SubscribeId) -> bool { - subscribe_id <= self.max_subscriber_id - } - - fn is_subscribe_id_unique(&self, subscribe_id: SubscribeId) -> bool { - !self.subscriptions.contains_key(&subscribe_id) - } - - fn is_track_alias_unique(&self, track_alias: TrackAlias) -> bool { - !self - .subscriptions - .values() - .any(|subscription| subscription.get_track_alias() == track_alias) - } - - fn find_unused_subscribe_id_and_track_alias(&self) -> Result<(SubscribeId, TrackAlias)> { - unimplemented!() - } - - fn set_namespace(&mut self, namespace: TrackNamespace) -> Result<()> { - if self.announcing_namespaces.contains(&namespace) { - bail!("Namespace already exists."); - } - - self.announcing_namespaces.push(namespace); - - Ok(()) - } - - fn get_namespaces(&self) -> Result<&Vec> { - Ok(&self.announcing_namespaces) - } - - fn has_namespace(&self, namespace: TrackNamespace) -> bool { - self.announcing_namespaces.contains(&namespace) - } - - fn delete_namespace(&mut self, namespace: TrackNamespace) -> Result<()> { - if let Some(index) = self - .announcing_namespaces - .iter() - .position(|x| x == &namespace) - { - self.announcing_namespaces.remove(index); - } - - Ok(()) - } - - fn set_namespace_prefix(&mut self, namespace_prefix: TrackNamespace) -> Result<()> { - if self - .subscribed_namespace_prefixes - .contains(&namespace_prefix) - { - bail!("Namespace prefix already exists."); - } - - self.subscribed_namespace_prefixes.push(namespace_prefix); - - Ok(()) - } - - fn get_namespace_prefixes(&self) -> Result<&Vec> { - Ok(&self.subscribed_namespace_prefixes) - } - - fn delete_namespace_prefix(&mut self, namespace_prefix: TrackNamespace) -> Result<()> { - if let Some(index) = self - .subscribed_namespace_prefixes - .iter() - .position(|x| x == &namespace_prefix) - { - self.subscribed_namespace_prefixes.remove(index); - } - - Ok(()) - } -} diff --git a/moqt-server/src/lib.rs b/moqt-server/src/lib.rs index 46d9bd54..782c8bbc 100644 --- a/moqt-server/src/lib.rs +++ b/moqt-server/src/lib.rs @@ -3,12 +3,13 @@ use crate::modules::{ buffer_manager, buffer_manager::{buffer_manager, BufferCommand}, control_message_handler::*, + relation_manager::{commands::TrackCommand, manager::pubsub_relation_manager}, send_stream_dispatcher::{send_stream_dispatcher, SendStreamDispatchCommand}, - track_namespace_manager::{track_namespace_manager, TrackCommand}, }; use anyhow::{bail, Context, Ok, Result}; use bytes::BytesMut; pub use moqt_core::constants; +use moqt_core::pubsub_relation_manager_repository::PubSubRelationManagerRepository; use moqt_core::{ constants::{StreamDirection, UnderlayType}, control_message_type::ControlMessageType, @@ -19,7 +20,7 @@ use moqt_core::{ moqt_payload::MOQTPayload, }, variable_integer::write_variable_integer, - MOQTClient, TrackNamespaceManagerRepository, + MOQTClient, }; use std::sync::Arc; use std::time::Duration; @@ -114,7 +115,7 @@ impl MOQT { tokio::spawn(async move { buffer_manager(&mut buffer_rx).await }); // Start track management thread - tokio::spawn(async move { track_namespace_manager(&mut track_namespace_rx).await }); + tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); // Start stream management thread tokio::spawn(async move { send_stream_dispatcher(&mut send_stream_rx).await }); @@ -279,9 +280,11 @@ async fn handle_connection( } // Delete pub/sub information related to the client - let track_namespace_manager = - modules::track_namespace_manager::TrackNamespaceManager::new(track_namespace_tx.clone()); - let _ = track_namespace_manager.delete_client(stable_id).await; + let pubsub_relation_manager = + modules::relation_manager::interface::PubSubRelationManagerInterface::new( + track_namespace_tx.clone(), + ); + let _ = pubsub_relation_manager.delete_client(stable_id).await; // Delete senders to the client send_stream_tx @@ -324,8 +327,10 @@ async fn handle_incoming_bi_stream( let recv_stream = &mut stream.recv_stream; let shread_send_stream = &mut stream.shread_send_stream; - let mut track_namespace_manager = - modules::track_namespace_manager::TrackNamespaceManager::new(track_namespace_tx.clone()); + let mut pubsub_relation_manager = + modules::relation_manager::interface::PubSubRelationManagerInterface::new( + track_namespace_tx.clone(), + ); let mut send_stream_dispatcher = modules::send_stream_dispatcher::SendStreamDispatcher::new(send_stream_tx.clone()); @@ -348,7 +353,7 @@ async fn handle_incoming_bi_stream( &mut buf, UnderlayType::WebTransport, &mut client, - &mut track_namespace_manager, + &mut pubsub_relation_manager, &mut send_stream_dispatcher, ) .await; diff --git a/moqt-server/src/modules.rs b/moqt-server/src/modules.rs index b619f5f9..fb8d0137 100644 --- a/moqt-server/src/modules.rs +++ b/moqt-server/src/modules.rs @@ -1,6 +1,6 @@ pub mod buffer_manager; pub(crate) mod control_message_handler; pub(crate) mod handlers; +pub(crate) mod relation_manager; pub(crate) mod send_stream_dispatcher; pub(crate) mod server_processes; -pub(crate) mod track_namespace_manager; diff --git a/moqt-server/src/modules/control_message_handler.rs b/moqt-server/src/modules/control_message_handler.rs index 69ea73fe..8ddd96fe 100644 --- a/moqt-server/src/modules/control_message_handler.rs +++ b/moqt-server/src/modules/control_message_handler.rs @@ -11,13 +11,14 @@ use crate::modules::server_processes::{ }; use anyhow::{bail, Result}; use bytes::{Buf, BytesMut}; +use moqt_core::pubsub_relation_manager_repository::PubSubRelationManagerRepository; use moqt_core::{ constants::UnderlayType, control_message_type::ControlMessageType, messages::{control_messages::unannounce::UnAnnounce, moqt_payload::MOQTPayload}, moqt_client::MOQTClientStatus, variable_integer::{read_variable_integer, write_variable_integer}, - MOQTClient, SendStreamDispatcherRepository, TrackNamespaceManagerRepository, + MOQTClient, SendStreamDispatcherRepository, }; #[derive(Debug, PartialEq)] @@ -49,7 +50,7 @@ pub async fn control_message_handler( read_buf: &mut BytesMut, underlay_type: UnderlayType, client: &mut MOQTClient, - track_namespace_manager_repository: &mut dyn TrackNamespaceManagerRepository, + pubsub_relation_manager_repository: &mut dyn PubSubRelationManagerRepository, send_stream_dispatcher_repository: &mut dyn SendStreamDispatcherRepository, ) -> MessageProcessResult { tracing::trace!("control_message_handler! {}", read_buf.len()); @@ -99,7 +100,7 @@ pub async fn control_message_handler( client, underlay_type, &mut write_buf, - track_namespace_manager_repository, + pubsub_relation_manager_repository, ) .await { @@ -128,7 +129,7 @@ pub async fn control_message_handler( &mut payload_buf, client, &mut write_buf, - track_namespace_manager_repository, + pubsub_relation_manager_repository, send_stream_dispatcher_repository, ) .await @@ -163,7 +164,7 @@ pub async fn control_message_handler( // TODO: Merge to process_subscribe_message. match process_subscribe_ok_message( &mut payload_buf, - track_namespace_manager_repository, + pubsub_relation_manager_repository, send_stream_dispatcher_repository, client, ) @@ -209,7 +210,7 @@ pub async fn control_message_handler( let _unsubscribe_result = unannounce_handler( unsubscribe_message.unwrap(), client, - track_namespace_manager_repository, + pubsub_relation_manager_repository, ); return MessageProcessResult::Success(BytesMut::with_capacity(0)); @@ -228,7 +229,7 @@ pub async fn control_message_handler( &mut payload_buf, client, &mut write_buf, - track_namespace_manager_repository, + pubsub_relation_manager_repository, ) .await { @@ -278,7 +279,7 @@ pub async fn control_message_handler( let _unannounce_result = unannounce_handler( unannounce_message.unwrap(), client, - track_namespace_manager_repository, + pubsub_relation_manager_repository, ) .await; @@ -312,12 +313,13 @@ pub(crate) mod test_utils { use crate::modules::control_message_handler::control_message_handler; use crate::modules::control_message_handler::MessageProcessResult; + use crate::modules::relation_manager::{ + commands::TrackCommand, interface::PubSubRelationManagerInterface, + manager::pubsub_relation_manager, + }; use crate::modules::send_stream_dispatcher::{ send_stream_dispatcher, SendStreamDispatchCommand, SendStreamDispatcher, }; - use crate::modules::track_namespace_manager::{ - track_namespace_manager, TrackCommand, TrackNamespaceManager, - }; use bytes::BytesMut; use moqt_core::constants::UnderlayType; use moqt_core::variable_integer::write_variable_integer; @@ -338,11 +340,11 @@ pub(crate) mod test_utils { let mut client = MOQTClient::new(subscriber_sessin_id); client.update_status(client_status); - // Generate TrackNamespaceManager + // Generate PubSubRelationManagerInterface let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_namespace_rx).await }); - let mut track_namespace_manager: TrackNamespaceManager = - TrackNamespaceManager::new(track_namespace_tx); + tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); + let mut pubsub_relation_manager: PubSubRelationManagerInterface = + PubSubRelationManagerInterface::new(track_namespace_tx); // Generate SendStreamDispacher let (send_stream_tx, mut send_stream_rx) = mpsc::channel::(1024); @@ -356,7 +358,7 @@ pub(crate) mod test_utils { &mut buf, UnderlayType::WebTransport, &mut client, - &mut track_namespace_manager, + &mut pubsub_relation_manager, &mut send_stream_dispatcher, ) .await diff --git a/moqt-server/src/modules/handlers/announce_handler.rs b/moqt-server/src/modules/handlers/announce_handler.rs index 199f701c..50bd5069 100644 --- a/moqt-server/src/modules/handlers/announce_handler.rs +++ b/moqt-server/src/modules/handlers/announce_handler.rs @@ -1,10 +1,9 @@ use anyhow::Result; - +use moqt_core::pubsub_relation_manager_repository::PubSubRelationManagerRepository; use moqt_core::{ messages::control_messages::{ announce::Announce, announce_error::AnnounceError, announce_ok::AnnounceOk, }, - track_namespace_manager_repository::TrackNamespaceManagerRepository, MOQTClient, }; @@ -17,13 +16,13 @@ pub(crate) enum AnnounceResponse { pub(crate) async fn announce_handler( announce_message: Announce, client: &mut MOQTClient, - track_namespace_manager_repository: &mut dyn TrackNamespaceManagerRepository, + pubsub_relation_manager_repository: &mut dyn PubSubRelationManagerRepository, ) -> Result { tracing::trace!("announce_handler start."); tracing::debug!("announce_message: {:#?}", announce_message); // Record the announced Track Namespace - let set_result = track_namespace_manager_repository + let set_result = pubsub_relation_manager_repository .set_publisher_announced_namespace(announce_message.track_namespace().clone(), client.id) .await; @@ -54,11 +53,12 @@ pub(crate) async fn announce_handler( #[cfg(test)] mod success { use crate::modules::handlers::announce_handler::{announce_handler, AnnounceResponse}; - use crate::modules::track_namespace_manager::{ - track_namespace_manager, TrackCommand, TrackNamespaceManager, + use crate::modules::relation_manager::{ + commands::TrackCommand, interface::PubSubRelationManagerInterface, + manager::pubsub_relation_manager, }; use moqt_core::messages::moqt_payload::MOQTPayload; - use moqt_core::TrackNamespaceManagerRepository; + use moqt_core::pubsub_relation_manager_repository::PubSubRelationManagerRepository; use moqt_core::{ messages::control_messages::{ announce::Announce, @@ -88,20 +88,20 @@ mod success { let publisher_session_id = 0; let mut client = MOQTClient::new(publisher_session_id); - // Generate TrackNamespaceManager + // Generate PubSubRelationManagerInterface let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_namespace_rx).await }); - let mut track_namespace_manager: TrackNamespaceManager = - TrackNamespaceManager::new(track_namespace_tx); + tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); + let mut pubsub_relation_manager: PubSubRelationManagerInterface = + PubSubRelationManagerInterface::new(track_namespace_tx); let max_subscribe_id = 10; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .setup_publisher(max_subscribe_id, publisher_session_id) .await; // Execute announce_handler and get result - let result = announce_handler(announce_message, &mut client, &mut track_namespace_manager) + let result = announce_handler(announce_message, &mut client, &mut pubsub_relation_manager) .await .unwrap(); @@ -114,10 +114,12 @@ mod success { #[cfg(test)] mod failure { use crate::modules::handlers::announce_handler::{announce_handler, AnnounceResponse}; - use crate::modules::track_namespace_manager::{ - track_namespace_manager, TrackCommand, TrackNamespaceManager, + use crate::modules::relation_manager::{ + commands::TrackCommand, interface::PubSubRelationManagerInterface, + manager::pubsub_relation_manager, }; use moqt_core::messages::moqt_payload::MOQTPayload; + use moqt_core::pubsub_relation_manager_repository::PubSubRelationManagerRepository; use moqt_core::{ messages::control_messages::{ announce::Announce, @@ -125,7 +127,6 @@ mod failure { version_specific_parameters::{AuthorizationInfo, VersionSpecificParameter}, }, moqt_client::MOQTClient, - TrackNamespaceManagerRepository, }; use tokio::sync::mpsc; @@ -148,20 +149,20 @@ mod failure { let publisher_session_id = 0; let mut client = MOQTClient::new(publisher_session_id); - // Generate TrackNamespaceManager + // Generate PubSubRelationManagerInterface let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_namespace_rx).await }); - let mut track_namespace_manager: TrackNamespaceManager = - TrackNamespaceManager::new(track_namespace_tx); + tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); + let mut pubsub_relation_manager: PubSubRelationManagerInterface = + PubSubRelationManagerInterface::new(track_namespace_tx); let max_subscribe_id = 10; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .setup_publisher(max_subscribe_id, publisher_session_id) .await; // Set the duplicated publisher in advance - let _ = track_namespace_manager + let _ = pubsub_relation_manager .set_publisher_announced_namespace( announce_message.track_namespace().clone(), client.id, @@ -169,7 +170,7 @@ mod failure { .await; // Execute announce_handler and get result - let result = announce_handler(announce_message, &mut client, &mut track_namespace_manager) + let result = announce_handler(announce_message, &mut client, &mut pubsub_relation_manager) .await .unwrap(); diff --git a/moqt-server/src/modules/handlers/server_setup_handler.rs b/moqt-server/src/modules/handlers/server_setup_handler.rs index d7628903..20b4b3b9 100644 --- a/moqt-server/src/modules/handlers/server_setup_handler.rs +++ b/moqt-server/src/modules/handlers/server_setup_handler.rs @@ -1,6 +1,6 @@ use crate::constants; use anyhow::{bail, Result}; - +use moqt_core::pubsub_relation_manager_repository::PubSubRelationManagerRepository; use moqt_core::{ constants::UnderlayType, messages::control_messages::{ @@ -10,14 +10,14 @@ use moqt_core::{ setup_parameters::{Role, RoleCase}, }, moqt_client::MOQTClientStatus, - MOQTClient, TrackNamespaceManagerRepository, + MOQTClient, }; pub(crate) async fn setup_handler( client_setup_message: ClientSetup, underlay_type: UnderlayType, client: &mut MOQTClient, - track_namespace_manager_repository: &mut dyn TrackNamespaceManagerRepository, + pubsub_relation_manager_repository: &mut dyn PubSubRelationManagerRepository, ) -> Result { tracing::trace!("setup_handler start."); @@ -60,12 +60,12 @@ pub(crate) async fn setup_handler( if client.role() == Some(RoleCase::Subscriber) { // Generate producer that manages namespaces and subscriptions with subscribers. // FIXME: max_subscribe_id for subscriber is fixed at 100 for now. - track_namespace_manager_repository + pubsub_relation_manager_repository .setup_subscriber(100, client.id) .await?; } else if client.role() == Some(RoleCase::Publisher) { // Generate consumer that manages namespaces and subscriptions with producers. - track_namespace_manager_repository + pubsub_relation_manager_repository .setup_publisher(max_subscribe_id, client.id) .await?; } else if client.role().is_none() { @@ -87,8 +87,9 @@ pub(crate) async fn setup_handler( #[cfg(test)] mod success { - use crate::modules::track_namespace_manager::{ - track_namespace_manager, TrackCommand, TrackNamespaceManager, + use crate::modules::relation_manager::{ + commands::TrackCommand, interface::PubSubRelationManagerInterface, + manager::pubsub_relation_manager, }; use crate::{constants, modules::handlers::server_setup_handler::setup_handler}; use moqt_core::messages::control_messages::{ @@ -107,17 +108,17 @@ mod success { ClientSetup::new(vec![constants::MOQ_TRANSPORT_VERSION], setup_parameters); let underlay_type = crate::constants::UnderlayType::WebTransport; - // Generate TrackNamespaceManager + // Generate PubSubRelationManagerInterface let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_namespace_rx).await }); - let mut track_namespace_manager: TrackNamespaceManager = - TrackNamespaceManager::new(track_namespace_tx); + tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); + let mut pubsub_relation_manager: PubSubRelationManagerInterface = + PubSubRelationManagerInterface::new(track_namespace_tx); let server_setup_message = setup_handler( client_setup_message, underlay_type, &mut client, - &mut track_namespace_manager, + &mut pubsub_relation_manager, ) .await; @@ -136,17 +137,17 @@ mod success { ClientSetup::new(vec![constants::MOQ_TRANSPORT_VERSION], setup_parameters); let underlay_type = crate::constants::UnderlayType::QUIC; - // Generate TrackNamespaceManager + // Generate PubSubRelationManagerInterface let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_namespace_rx).await }); - let mut track_namespace_manager: TrackNamespaceManager = - TrackNamespaceManager::new(track_namespace_tx); + tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); + let mut pubsub_relation_manager: PubSubRelationManagerInterface = + PubSubRelationManagerInterface::new(track_namespace_tx); let server_setup_message = setup_handler( client_setup_message, underlay_type, &mut client, - &mut track_namespace_manager, + &mut pubsub_relation_manager, ) .await; @@ -157,8 +158,9 @@ mod success { #[cfg(test)] mod failure { - use crate::modules::track_namespace_manager::{ - track_namespace_manager, TrackCommand, TrackNamespaceManager, + use crate::modules::relation_manager::{ + commands::TrackCommand, interface::PubSubRelationManagerInterface, + manager::pubsub_relation_manager, }; use crate::{constants, modules::handlers::server_setup_handler::setup_handler}; use moqt_core::messages::control_messages::{ @@ -177,17 +179,17 @@ mod failure { ClientSetup::new(vec![constants::MOQ_TRANSPORT_VERSION], setup_parameters); let underlay_type = crate::constants::UnderlayType::WebTransport; - // Generate TrackNamespaceManager + // Generate PubSubRelationManagerInterface let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_namespace_rx).await }); - let mut track_namespace_manager: TrackNamespaceManager = - TrackNamespaceManager::new(track_namespace_tx); + tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); + let mut pubsub_relation_manager: PubSubRelationManagerInterface = + PubSubRelationManagerInterface::new(track_namespace_tx); let server_setup_message = setup_handler( client_setup_message, underlay_type, &mut client, - &mut track_namespace_manager, + &mut pubsub_relation_manager, ) .await; @@ -202,17 +204,17 @@ mod failure { ClientSetup::new(vec![constants::MOQ_TRANSPORT_VERSION], setup_parameters); let underlay_type = crate::constants::UnderlayType::WebTransport; - // Generate TrackNamespaceManager + // Generate PubSubRelationManagerInterface let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_namespace_rx).await }); - let mut track_namespace_manager: TrackNamespaceManager = - TrackNamespaceManager::new(track_namespace_tx); + tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); + let mut pubsub_relation_manager: PubSubRelationManagerInterface = + PubSubRelationManagerInterface::new(track_namespace_tx); let server_setup_message = setup_handler( client_setup_message, underlay_type, &mut client, - &mut track_namespace_manager, + &mut pubsub_relation_manager, ) .await; @@ -227,17 +229,17 @@ mod failure { ClientSetup::new(vec![constants::MOQ_TRANSPORT_VERSION], setup_parameters); let underlay_type = crate::constants::UnderlayType::QUIC; - // Generate TrackNamespaceManager + // Generate PubSubRelationManagerInterface let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_namespace_rx).await }); - let mut track_namespace_manager: TrackNamespaceManager = - TrackNamespaceManager::new(track_namespace_tx); + tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); + let mut pubsub_relation_manager: PubSubRelationManagerInterface = + PubSubRelationManagerInterface::new(track_namespace_tx); let server_setup_message = setup_handler( client_setup_message, underlay_type, &mut client, - &mut track_namespace_manager, + &mut pubsub_relation_manager, ) .await; @@ -253,17 +255,17 @@ mod failure { let client_setup_message = ClientSetup::new(vec![unsupported_version], setup_parameters); let underlay_type = crate::constants::UnderlayType::WebTransport; - // Generate TrackNamespaceManager + // Generate PubSubRelationManagerInterface let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_namespace_rx).await }); - let mut track_namespace_manager: TrackNamespaceManager = - TrackNamespaceManager::new(track_namespace_tx); + tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); + let mut pubsub_relation_manager: PubSubRelationManagerInterface = + PubSubRelationManagerInterface::new(track_namespace_tx); let server_setup_message = setup_handler( client_setup_message, underlay_type, &mut client, - &mut track_namespace_manager, + &mut pubsub_relation_manager, ) .await; @@ -282,17 +284,17 @@ mod failure { ClientSetup::new(vec![constants::MOQ_TRANSPORT_VERSION], setup_parameters); let underlay_type = crate::constants::UnderlayType::WebTransport; - // Generate TrackNamespaceManager + // Generate PubSubRelationManagerInterface let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_namespace_rx).await }); - let mut track_namespace_manager: TrackNamespaceManager = - TrackNamespaceManager::new(track_namespace_tx); + tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); + let mut pubsub_relation_manager: PubSubRelationManagerInterface = + PubSubRelationManagerInterface::new(track_namespace_tx); let server_setup_message = setup_handler( client_setup_message, underlay_type, &mut client, - &mut track_namespace_manager, + &mut pubsub_relation_manager, ) .await; diff --git a/moqt-server/src/modules/handlers/subscribe_handler.rs b/moqt-server/src/modules/handlers/subscribe_handler.rs index a30c71d8..07f74278 100644 --- a/moqt-server/src/modules/handlers/subscribe_handler.rs +++ b/moqt-server/src/modules/handlers/subscribe_handler.rs @@ -1,32 +1,32 @@ use anyhow::{bail, Result}; - +use moqt_core::pubsub_relation_manager_repository::PubSubRelationManagerRepository; use moqt_core::{ constants::StreamDirection, messages::{ control_messages::{subscribe::Subscribe, subscribe_ok::SubscribeOk}, moqt_payload::MOQTPayload, }, - MOQTClient, SendStreamDispatcherRepository, TrackNamespaceManagerRepository, + MOQTClient, SendStreamDispatcherRepository, }; pub(crate) async fn subscribe_handler( subscribe_message: Subscribe, client: &mut MOQTClient, - track_namespace_manager_repository: &mut dyn TrackNamespaceManagerRepository, + pubsub_relation_manager_repository: &mut dyn PubSubRelationManagerRepository, send_stream_dispatcher_repository: &mut dyn SendStreamDispatcherRepository, ) -> Result> { tracing::trace!("subscribe_handler start."); tracing::debug!("subscribe_message: {:#?}", subscribe_message); - if !track_namespace_manager_repository + if !pubsub_relation_manager_repository .is_valid_subscriber_subscribe_id(subscribe_message.subscribe_id(), client.id) .await? { // TODO: return TerminationErrorCode bail!("TooManySubscribers"); } - if !track_namespace_manager_repository + if !pubsub_relation_manager_repository .is_valid_subscriber_track_alias(subscribe_message.track_alias(), client.id) .await? { @@ -35,7 +35,7 @@ pub(crate) async fn subscribe_handler( } // If the track exists, return ther track as it is - if track_namespace_manager_repository + if pubsub_relation_manager_repository .is_track_existing( subscribe_message.track_namespace().to_vec(), subscribe_message.track_name().to_string(), @@ -44,7 +44,7 @@ pub(crate) async fn subscribe_handler( .unwrap() { let _ = set_only_subscriber_subscription( - track_namespace_manager_repository, + pubsub_relation_manager_repository, &subscribe_message, client, ) @@ -74,7 +74,7 @@ pub(crate) async fn subscribe_handler( // Since only the track_namespace is recorded in ANNOUNCE, use track_namespace to determine the publisher // TODO: multiple publishers for the same track_namespace - let publisher_session_id = track_namespace_manager_repository + let publisher_session_id = pubsub_relation_manager_repository .get_publisher_session_id(subscribe_message.track_namespace().clone()) .await .unwrap(); @@ -82,7 +82,7 @@ pub(crate) async fn subscribe_handler( Some(session_id) => { let (publisher_subscribe_id, publisher_track_alias) = match set_subscriber_and_publisher_subscription( - track_namespace_manager_repository, + pubsub_relation_manager_repository, &subscribe_message, client, session_id, @@ -146,7 +146,7 @@ pub(crate) async fn subscribe_handler( } async fn set_only_subscriber_subscription( - track_namespace_manager_repository: &mut dyn TrackNamespaceManagerRepository, + pubsub_relation_manager_repository: &mut dyn PubSubRelationManagerRepository, subscribe_message: &Subscribe, client: &MOQTClient, ) -> Result<()> { @@ -164,7 +164,7 @@ async fn set_only_subscriber_subscription( let subscriber_end_object = subscribe_message.end_object(); // Get publisher subscription already exists - let publisher_subscription = track_namespace_manager_repository + let publisher_subscription = pubsub_relation_manager_repository .get_publisher_subscription_by_full_track_name( subscriber_track_namespace.clone(), subscriber_track_name.clone(), @@ -172,7 +172,7 @@ async fn set_only_subscriber_subscription( .await? .unwrap(); - track_namespace_manager_repository + pubsub_relation_manager_repository .set_subscriber_subscription( subscriber_client_id, subscriber_subscribe_id, @@ -189,7 +189,7 @@ async fn set_only_subscriber_subscription( ) .await?; - let publisher_session_id = track_namespace_manager_repository + let publisher_session_id = pubsub_relation_manager_repository .get_publisher_session_id(subscriber_track_namespace) .await? .unwrap(); @@ -198,7 +198,7 @@ async fn set_only_subscriber_subscription( publisher_subscription.get_track_namespace_and_name(); // Get publisher subscribe id to register pubsup relation - let publisher_subscribe_id = track_namespace_manager_repository + let publisher_subscribe_id = pubsub_relation_manager_repository .get_publisher_subscribe_id( publisher_track_namespace, publisher_track_name, @@ -207,7 +207,7 @@ async fn set_only_subscriber_subscription( .await? .unwrap(); - track_namespace_manager_repository + pubsub_relation_manager_repository .register_pubsup_relation( publisher_session_id, publisher_subscribe_id, @@ -216,7 +216,7 @@ async fn set_only_subscriber_subscription( ) .await?; - track_namespace_manager_repository + pubsub_relation_manager_repository .activate_subscriber_subscription(subscriber_client_id, subscriber_subscribe_id) .await?; @@ -224,7 +224,7 @@ async fn set_only_subscriber_subscription( } async fn set_subscriber_and_publisher_subscription( - track_namespace_manager_repository: &mut dyn TrackNamespaceManagerRepository, + pubsub_relation_manager_repository: &mut dyn PubSubRelationManagerRepository, subscribe_message: &Subscribe, client: &MOQTClient, publisher_session_id: usize, @@ -242,7 +242,7 @@ async fn set_subscriber_and_publisher_subscription( let subscriber_end_group = subscribe_message.end_group(); let subscriber_end_object = subscribe_message.end_object(); - track_namespace_manager_repository + pubsub_relation_manager_repository .set_subscriber_subscription( subscriber_client_id, subscriber_subscribe_id, @@ -259,7 +259,7 @@ async fn set_subscriber_and_publisher_subscription( ) .await?; - let (publisher_subscribe_id, publisher_track_alias) = track_namespace_manager_repository + let (publisher_subscribe_id, publisher_track_alias) = pubsub_relation_manager_repository .set_publisher_subscription( publisher_session_id, subscriber_track_namespace.clone(), @@ -274,7 +274,7 @@ async fn set_subscriber_and_publisher_subscription( ) .await?; - track_namespace_manager_repository + pubsub_relation_manager_repository .register_pubsup_relation( publisher_session_id, publisher_subscribe_id, @@ -289,13 +289,14 @@ async fn set_subscriber_and_publisher_subscription( #[cfg(test)] mod success { use crate::modules::handlers::subscribe_handler::subscribe_handler; + use crate::modules::relation_manager::{ + commands::TrackCommand, + interface::{test_utils, PubSubRelationManagerInterface}, + manager::pubsub_relation_manager, + }; use crate::modules::send_stream_dispatcher::{ send_stream_dispatcher, SendStreamDispatchCommand, SendStreamDispatcher, }; - use crate::modules::track_namespace_manager::test_utils; - use crate::modules::track_namespace_manager::{ - track_namespace_manager, TrackCommand, TrackNamespaceManager, - }; use moqt_core::constants::StreamDirection; use moqt_core::messages::{ control_messages::{ @@ -304,8 +305,8 @@ mod success { }, moqt_payload::MOQTPayload, }; + use moqt_core::pubsub_relation_manager_repository::PubSubRelationManagerRepository; use moqt_core::MOQTClient; - use moqt_core::TrackNamespaceManagerRepository; use std::sync::Arc; use tokio::sync::mpsc; @@ -348,24 +349,24 @@ mod success { let subscriber_session_id = 0; let mut client = MOQTClient::new(subscriber_session_id); - // Generate TrackNamespaceManager + // Generate PubSubRelationManagerInterface let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_namespace_rx).await }); - let mut track_namespace_manager: TrackNamespaceManager = - TrackNamespaceManager::new(track_namespace_tx); + tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); + let mut pubsub_relation_manager: PubSubRelationManagerInterface = + PubSubRelationManagerInterface::new(track_namespace_tx); let publisher_session_id = 1; let max_subscribe_id = 10; // Register the publisher track in advance - let _ = track_namespace_manager + let _ = pubsub_relation_manager .setup_publisher(max_subscribe_id, publisher_session_id) .await; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) .await; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .setup_subscriber(max_subscribe_id, subscriber_session_id) .await; @@ -389,7 +390,7 @@ mod success { let result = subscribe_handler( subscribe, &mut client, - &mut track_namespace_manager, + &mut pubsub_relation_manager, &mut send_stream_dispatcher, ) .await; @@ -401,7 +402,7 @@ mod success { // Check the subscriber is registered let (_, producers, pubsub_relation) = - test_utils::get_node_and_relation_clone(&track_namespace_manager).await; + test_utils::get_node_and_relation_clone(&pubsub_relation_manager).await; assert_eq!(producers.len(), 1); @@ -455,23 +456,23 @@ mod success { let subscriber_session_id = 0; let mut client = MOQTClient::new(subscriber_session_id); - // Generate TrackNamespaceManager + // Generate PubSubRelationManagerInterface let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_namespace_rx).await }); - let mut track_namespace_manager: TrackNamespaceManager = - TrackNamespaceManager::new(track_namespace_tx); + tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); + let mut pubsub_relation_manager: PubSubRelationManagerInterface = + PubSubRelationManagerInterface::new(track_namespace_tx); let publisher_session_id = 1; let max_subscribe_id = 10; // Register the publisher track in advance - let _ = track_namespace_manager + let _ = pubsub_relation_manager .setup_publisher(max_subscribe_id, publisher_session_id) .await; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) .await; - let (publisher_subscribe_id, _) = track_namespace_manager + let (publisher_subscribe_id, _) = pubsub_relation_manager .set_publisher_subscription( publisher_session_id, track_namespace, @@ -487,7 +488,7 @@ mod success { .await .unwrap(); - let _ = track_namespace_manager + let _ = pubsub_relation_manager .setup_subscriber(max_subscribe_id, subscriber_session_id) .await; @@ -511,7 +512,7 @@ mod success { let result = subscribe_handler( subscribe, &mut client, - &mut track_namespace_manager, + &mut pubsub_relation_manager, &mut send_stream_dispatcher, ) .await; @@ -523,7 +524,7 @@ mod success { // Check the subscriber is registered let (_, producers, pubsub_relation) = - test_utils::get_node_and_relation_clone(&track_namespace_manager).await; + test_utils::get_node_and_relation_clone(&pubsub_relation_manager).await; assert_eq!(producers.len(), 1); @@ -541,12 +542,13 @@ mod success { #[cfg(test)] mod failure { use crate::modules::handlers::subscribe_handler::subscribe_handler; + use crate::modules::relation_manager::{ + commands::TrackCommand, interface::PubSubRelationManagerInterface, + manager::pubsub_relation_manager, + }; use crate::modules::send_stream_dispatcher::{ send_stream_dispatcher, SendStreamDispatchCommand, SendStreamDispatcher, }; - use crate::modules::track_namespace_manager::{ - track_namespace_manager, TrackCommand, TrackNamespaceManager, - }; use moqt_core::constants::StreamDirection; use moqt_core::messages::{ control_messages::{ @@ -555,8 +557,8 @@ mod failure { }, moqt_payload::MOQTPayload, }; + use moqt_core::pubsub_relation_manager_repository::PubSubRelationManagerRepository; use moqt_core::MOQTClient; - use moqt_core::TrackNamespaceManagerRepository; use std::sync::Arc; use tokio::sync::mpsc; @@ -598,26 +600,26 @@ mod failure { let subscriber_session_id = 0; let mut client = MOQTClient::new(subscriber_session_id); - // Generate TrackNamespaceManager (register subscriber in advance) + // Generate PubSubRelationManagerInterface (register subscriber in advance) let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_namespace_rx).await }); - let mut track_namespace_manager: TrackNamespaceManager = - TrackNamespaceManager::new(track_namespace_tx); + tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); + let mut pubsub_relation_manager: PubSubRelationManagerInterface = + PubSubRelationManagerInterface::new(track_namespace_tx); let publisher_session_id = 1; let max_subscribe_id = 10; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .setup_publisher(max_subscribe_id, publisher_session_id) .await; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) .await; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .setup_subscriber(max_subscribe_id, subscriber_session_id) .await; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .set_subscriber_subscription( subscriber_session_id, subscribe_id, @@ -654,7 +656,7 @@ mod failure { let result = subscribe_handler( subscribe, &mut client, - &mut track_namespace_manager, + &mut pubsub_relation_manager, &mut send_stream_dispatcher, ) .await; @@ -700,23 +702,23 @@ mod failure { let subscriber_session_id = 0; let mut client = MOQTClient::new(subscriber_session_id); - // Generate TrackNamespaceManager + // Generate PubSubRelationManagerInterface let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_namespace_rx).await }); - let mut track_namespace_manager: TrackNamespaceManager = - TrackNamespaceManager::new(track_namespace_tx); + tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); + let mut pubsub_relation_manager: PubSubRelationManagerInterface = + PubSubRelationManagerInterface::new(track_namespace_tx); let publisher_session_id = 1; let max_subscribe_id = 10; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .setup_publisher(max_subscribe_id, publisher_session_id) .await; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) .await; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .setup_subscriber(max_subscribe_id, subscriber_session_id) .await; @@ -731,7 +733,7 @@ mod failure { let result = subscribe_handler( subscribe, &mut client, - &mut track_namespace_manager, + &mut pubsub_relation_manager, &mut send_stream_dispatcher, ) .await; @@ -777,16 +779,16 @@ mod failure { let subscriber_session_id = 0; let mut client = MOQTClient::new(subscriber_session_id); - // Generate TrackNamespaceManager (without set publisher) + // Generate PubSubRelationManagerInterface (without set publisher) let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_namespace_rx).await }); - let mut track_namespace_manager: TrackNamespaceManager = - TrackNamespaceManager::new(track_namespace_tx); + tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); + let mut pubsub_relation_manager: PubSubRelationManagerInterface = + PubSubRelationManagerInterface::new(track_namespace_tx); let publisher_session_id = 1; let max_subscribe_id = 10; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .setup_subscriber(max_subscribe_id, subscriber_session_id) .await; @@ -810,7 +812,7 @@ mod failure { let result = subscribe_handler( subscribe, &mut client, - &mut track_namespace_manager, + &mut pubsub_relation_manager, &mut send_stream_dispatcher, ) .await; @@ -862,24 +864,24 @@ mod failure { let subscriber_session_id = 0; let mut client = MOQTClient::new(subscriber_session_id); - // Generate TrackNamespaceManager + // Generate PubSubRelationManagerInterface let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_namespace_rx).await }); - let mut track_namespace_manager: TrackNamespaceManager = - TrackNamespaceManager::new(track_namespace_tx); + tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); + let mut pubsub_relation_manager: PubSubRelationManagerInterface = + PubSubRelationManagerInterface::new(track_namespace_tx); let publisher_session_id = 1; let max_subscribe_id = 0; // Register the publisher track in advance - let _ = track_namespace_manager + let _ = pubsub_relation_manager .setup_publisher(max_subscribe_id, publisher_session_id) .await; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) .await; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .setup_subscriber(max_subscribe_id, subscriber_session_id) .await; @@ -903,7 +905,7 @@ mod failure { let _ = subscribe_handler( subscribes[0].clone(), &mut client, - &mut track_namespace_manager, + &mut pubsub_relation_manager, &mut send_stream_dispatcher, ) .await; @@ -911,7 +913,7 @@ mod failure { let result = subscribe_handler( subscribes[1].clone(), &mut client, - &mut track_namespace_manager, + &mut pubsub_relation_manager, &mut send_stream_dispatcher, ) .await; @@ -963,24 +965,24 @@ mod failure { let subscriber_session_id = 0; let mut client = MOQTClient::new(subscriber_session_id); - // Generate TrackNamespaceManager + // Generate PubSubRelationManagerInterface let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_namespace_rx).await }); - let mut track_namespace_manager: TrackNamespaceManager = - TrackNamespaceManager::new(track_namespace_tx); + tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); + let mut pubsub_relation_manager: PubSubRelationManagerInterface = + PubSubRelationManagerInterface::new(track_namespace_tx); let publisher_session_id = 1; let max_subscribe_id = 10; // Register the publisher track in advance - let _ = track_namespace_manager + let _ = pubsub_relation_manager .setup_publisher(max_subscribe_id, publisher_session_id) .await; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) .await; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .setup_subscriber(max_subscribe_id, subscriber_session_id) .await; @@ -1004,7 +1006,7 @@ mod failure { let _ = subscribe_handler( subscribes[0].clone(), &mut client, - &mut track_namespace_manager, + &mut pubsub_relation_manager, &mut send_stream_dispatcher, ) .await; @@ -1012,7 +1014,7 @@ mod failure { let result = subscribe_handler( subscribes[1].clone(), &mut client, - &mut track_namespace_manager, + &mut pubsub_relation_manager, &mut send_stream_dispatcher, ) .await; diff --git a/moqt-server/src/modules/handlers/subscribe_ok_handler.rs b/moqt-server/src/modules/handlers/subscribe_ok_handler.rs index df413ed2..c6588944 100644 --- a/moqt-server/src/modules/handlers/subscribe_ok_handler.rs +++ b/moqt-server/src/modules/handlers/subscribe_ok_handler.rs @@ -1,14 +1,14 @@ use anyhow::Result; - +use moqt_core::pubsub_relation_manager_repository::PubSubRelationManagerRepository; use moqt_core::{ constants::StreamDirection, messages::{control_messages::subscribe_ok::SubscribeOk, moqt_payload::MOQTPayload}, - MOQTClient, SendStreamDispatcherRepository, TrackNamespaceManagerRepository, + MOQTClient, SendStreamDispatcherRepository, }; pub(crate) async fn subscribe_ok_handler( subscribe_ok_message: SubscribeOk, - track_namespace_manager_repository: &mut dyn TrackNamespaceManagerRepository, + pubsub_relation_manager_repository: &mut dyn PubSubRelationManagerRepository, send_stream_dispatcher_repository: &mut dyn SendStreamDispatcherRepository, client: &mut MOQTClient, ) -> Result<()> { @@ -20,7 +20,7 @@ pub(crate) async fn subscribe_ok_handler( let publisher_subscribe_id = subscribe_ok_message.subscribe_id(); // Determine the SUBSCRIBER who sent the SUBSCRIBE using the track_namespace and track_name - let ids = track_namespace_manager_repository + let ids = pubsub_relation_manager_repository .get_requesting_subscriber_session_ids_and_subscribe_ids( publisher_subscribe_id, publisher_session_id, @@ -28,7 +28,7 @@ pub(crate) async fn subscribe_ok_handler( .await?; match ids { Some(ids) => { - let _ = track_namespace_manager_repository + let _ = pubsub_relation_manager_repository .activate_publisher_subscription(publisher_session_id, publisher_subscribe_id) .await; @@ -52,7 +52,7 @@ pub(crate) async fn subscribe_ok_handler( ) .await?; - track_namespace_manager_repository + pubsub_relation_manager_repository .activate_subscriber_subscription( *subscriber_session_id, *subscriber_subscribe_id, @@ -66,7 +66,7 @@ pub(crate) async fn subscribe_ok_handler( tracing::warn!("waiting subscriber session ids not found"); // Activate the publisher because it is in the Requesting state - let _ = track_namespace_manager_repository + let _ = pubsub_relation_manager_repository .activate_publisher_subscription(publisher_session_id, publisher_subscribe_id) .await; } @@ -78,12 +78,13 @@ pub(crate) async fn subscribe_ok_handler( #[cfg(test)] mod success { use crate::modules::handlers::subscribe_ok_handler::subscribe_ok_handler; + use crate::modules::relation_manager::{ + commands::TrackCommand, interface::PubSubRelationManagerInterface, + manager::pubsub_relation_manager, + }; use crate::modules::send_stream_dispatcher::{ send_stream_dispatcher, SendStreamDispatchCommand, SendStreamDispatcher, }; - use crate::modules::track_namespace_manager::{ - track_namespace_manager, TrackCommand, TrackNamespaceManager, - }; use crate::MOQTClient; use moqt_core::constants::StreamDirection; use moqt_core::messages::control_messages::subscribe::{FilterType, GroupOrder}; @@ -93,7 +94,7 @@ mod success { use moqt_core::messages::{ control_messages::subscribe_ok::SubscribeOk, moqt_payload::MOQTPayload, }; - use moqt_core::TrackNamespaceManagerRepository; + use moqt_core::pubsub_relation_manager_repository::PubSubRelationManagerRepository; use std::sync::Arc; use tokio::sync::mpsc; @@ -135,22 +136,22 @@ mod success { let publisher_session_id = 1; let mut client = MOQTClient::new(publisher_session_id); - // Generate TrackNamespaceManager + // Generate PubSubRelationManagerInterface let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_namespace_rx).await }); - let mut track_namespace_manager: TrackNamespaceManager = - TrackNamespaceManager::new(track_namespace_tx); + tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); + let mut pubsub_relation_manager: PubSubRelationManagerInterface = + PubSubRelationManagerInterface::new(track_namespace_tx); let subscriber_session_id = 2; let max_subscribe_id = 10; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .setup_publisher(max_subscribe_id, publisher_session_id) .await; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) .await; - let (publisher_subscribe_id, _) = track_namespace_manager + let (publisher_subscribe_id, _) = pubsub_relation_manager .set_publisher_subscription( publisher_session_id, track_namespace.clone(), @@ -166,10 +167,10 @@ mod success { .await .unwrap(); - let _ = track_namespace_manager + let _ = pubsub_relation_manager .setup_subscriber(max_subscribe_id, subscriber_session_id) .await; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .set_subscriber_subscription( subscriber_session_id, subscriber_subscribe_id, @@ -185,7 +186,7 @@ mod success { end_object, ) .await; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .register_pubsup_relation( publisher_session_id, publisher_subscribe_id, @@ -213,7 +214,7 @@ mod success { // Execute subscribe_ok_handler and get result let result = subscribe_ok_handler( subscribe_ok, - &mut track_namespace_manager, + &mut pubsub_relation_manager, &mut send_stream_dispatcher, &mut client, ) @@ -226,12 +227,13 @@ mod success { #[cfg(test)] mod failure { use crate::modules::handlers::subscribe_ok_handler::subscribe_ok_handler; + use crate::modules::relation_manager::{ + commands::TrackCommand, interface::PubSubRelationManagerInterface, + manager::pubsub_relation_manager, + }; use crate::modules::send_stream_dispatcher::{ send_stream_dispatcher, SendStreamDispatchCommand, SendStreamDispatcher, }; - use crate::modules::track_namespace_manager::{ - track_namespace_manager, TrackCommand, TrackNamespaceManager, - }; use crate::MOQTClient; use moqt_core::constants::StreamDirection; use moqt_core::messages::control_messages::subscribe::{FilterType, GroupOrder}; @@ -241,7 +243,7 @@ mod failure { use moqt_core::messages::{ control_messages::subscribe_ok::SubscribeOk, moqt_payload::MOQTPayload, }; - use moqt_core::TrackNamespaceManagerRepository; + use moqt_core::pubsub_relation_manager_repository::PubSubRelationManagerRepository; use std::sync::Arc; use tokio::sync::mpsc; @@ -283,22 +285,22 @@ mod failure { let publisher_session_id = 1; let mut client = MOQTClient::new(publisher_session_id); - // Generate TrackNamespaceManager + // Generate PubSubRelationManagerInterface let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_namespace_rx).await }); - let mut track_namespace_manager: TrackNamespaceManager = - TrackNamespaceManager::new(track_namespace_tx); + tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); + let mut pubsub_relation_manager: PubSubRelationManagerInterface = + PubSubRelationManagerInterface::new(track_namespace_tx); let subscriber_session_id = 2; let max_subscribe_id = 10; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .setup_publisher(max_subscribe_id, publisher_session_id) .await; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) .await; - let (publisher_subscribe_id, _) = track_namespace_manager + let (publisher_subscribe_id, _) = pubsub_relation_manager .set_publisher_subscription( publisher_session_id, track_namespace.clone(), @@ -314,10 +316,10 @@ mod failure { .await .unwrap(); - let _ = track_namespace_manager + let _ = pubsub_relation_manager .setup_subscriber(max_subscribe_id, subscriber_session_id) .await; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .set_subscriber_subscription( subscriber_session_id, subscriber_subscribe_id, @@ -333,7 +335,7 @@ mod failure { end_object, ) .await; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .register_pubsup_relation( publisher_session_id, publisher_subscribe_id, @@ -352,7 +354,7 @@ mod failure { // Execute subscribe_ok_handler and get result let result = subscribe_ok_handler( subscribe_ok, - &mut track_namespace_manager, + &mut pubsub_relation_manager, &mut send_stream_dispatcher, &mut client, ) @@ -398,22 +400,22 @@ mod failure { let publisher_session_id = 1; let mut client = MOQTClient::new(publisher_session_id); - // Generate TrackNamespaceManager + // Generate PubSubRelationManagerInterface let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_namespace_rx).await }); - let mut track_namespace_manager: TrackNamespaceManager = - TrackNamespaceManager::new(track_namespace_tx); + tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); + let mut pubsub_relation_manager: PubSubRelationManagerInterface = + PubSubRelationManagerInterface::new(track_namespace_tx); let subscriber_session_id = 2; let max_subscribe_id = 10; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .setup_publisher(max_subscribe_id, publisher_session_id) .await; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) .await; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .set_publisher_subscription( publisher_session_id, track_namespace.clone(), @@ -448,7 +450,7 @@ mod failure { // Execute subscribe_ok_handler and get result let result = subscribe_ok_handler( subscribe_ok, - &mut track_namespace_manager, + &mut pubsub_relation_manager, &mut send_stream_dispatcher, &mut client, ) @@ -495,22 +497,22 @@ mod failure { let publisher_session_id = 1; let mut client = MOQTClient::new(publisher_session_id); - // Generate TrackNamespaceManager + // Generate PubSubRelationManagerInterface let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_namespace_rx).await }); - let mut track_namespace_manager: TrackNamespaceManager = - TrackNamespaceManager::new(track_namespace_tx); + tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); + let mut pubsub_relation_manager: PubSubRelationManagerInterface = + PubSubRelationManagerInterface::new(track_namespace_tx); let subscriber_session_id = 2; let max_subscribe_id = 10; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .setup_publisher(max_subscribe_id, publisher_session_id) .await; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) .await; - let (publisher_subscribe_id, _) = track_namespace_manager + let (publisher_subscribe_id, _) = pubsub_relation_manager .set_publisher_subscription( publisher_session_id, track_namespace.clone(), @@ -526,10 +528,10 @@ mod failure { .await .unwrap(); - let _ = track_namespace_manager + let _ = pubsub_relation_manager .setup_subscriber(max_subscribe_id, subscriber_session_id) .await; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .set_subscriber_subscription( subscriber_session_id, subscriber_subscribe_id, @@ -545,7 +547,7 @@ mod failure { end_object, ) .await; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .register_pubsup_relation( publisher_session_id, publisher_subscribe_id, @@ -554,7 +556,7 @@ mod failure { ) .await; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .activate_subscriber_subscription(subscriber_session_id, subscriber_subscribe_id) .await; @@ -577,7 +579,7 @@ mod failure { // Execute subscribe_ok_handler and get result let result = subscribe_ok_handler( subscribe_ok, - &mut track_namespace_manager, + &mut pubsub_relation_manager, &mut send_stream_dispatcher, &mut client, ) @@ -624,22 +626,22 @@ mod failure { let publisher_session_id = 1; let mut client = MOQTClient::new(publisher_session_id); - // Generate TrackNamespaceManager + // Generate PubSubRelationManagerInterface let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_namespace_rx).await }); - let mut track_namespace_manager: TrackNamespaceManager = - TrackNamespaceManager::new(track_namespace_tx); + tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); + let mut pubsub_relation_manager: PubSubRelationManagerInterface = + PubSubRelationManagerInterface::new(track_namespace_tx); let subscriber_session_id = 2; let max_subscribe_id = 10; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .setup_publisher(max_subscribe_id, publisher_session_id) .await; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) .await; - let (publisher_subscribe_id, _) = track_namespace_manager + let (publisher_subscribe_id, _) = pubsub_relation_manager .set_publisher_subscription( publisher_session_id, track_namespace.clone(), @@ -655,10 +657,10 @@ mod failure { .await .unwrap(); - let _ = track_namespace_manager + let _ = pubsub_relation_manager .setup_subscriber(max_subscribe_id, subscriber_session_id) .await; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .set_subscriber_subscription( subscriber_session_id, subscriber_subscribe_id, @@ -674,7 +676,7 @@ mod failure { end_object, ) .await; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .register_pubsup_relation( publisher_session_id, publisher_subscribe_id, @@ -683,7 +685,7 @@ mod failure { ) .await; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .activate_publisher_subscription(publisher_session_id, publisher_subscribe_id) .await; @@ -706,7 +708,7 @@ mod failure { // Execute subscribe_ok_handler and get result let result = subscribe_ok_handler( subscribe_ok, - &mut track_namespace_manager, + &mut pubsub_relation_manager, &mut send_stream_dispatcher, &mut client, ) diff --git a/moqt-server/src/modules/handlers/unannounce_handler.rs b/moqt-server/src/modules/handlers/unannounce_handler.rs index ecbd6ce5..4371163e 100644 --- a/moqt-server/src/modules/handlers/unannounce_handler.rs +++ b/moqt-server/src/modules/handlers/unannounce_handler.rs @@ -1,21 +1,18 @@ use anyhow::Result; - -use moqt_core::{ - messages::control_messages::unannounce::UnAnnounce, - track_namespace_manager_repository::TrackNamespaceManagerRepository, MOQTClient, -}; +use moqt_core::pubsub_relation_manager_repository::PubSubRelationManagerRepository; +use moqt_core::{messages::control_messages::unannounce::UnAnnounce, MOQTClient}; pub(crate) async fn unannounce_handler( unannounce_message: UnAnnounce, _client: &mut MOQTClient, // TODO: Not implemented yet - track_namespace_manager_repository: &mut dyn TrackNamespaceManagerRepository, + pubsub_relation_manager_repository: &mut dyn PubSubRelationManagerRepository, ) -> Result<()> { tracing::trace!("unannounce_handler start."); tracing::debug!("unannounce_message: {:#?}", unannounce_message); // Remove the announced Track Namespace - let delete_result = track_namespace_manager_repository + let delete_result = pubsub_relation_manager_repository .delete_publisher_announced_namespace( unannounce_message.track_namespace().clone(), _client.id, diff --git a/moqt-server/src/modules/handlers/unsubscribe_handler.rs b/moqt-server/src/modules/handlers/unsubscribe_handler.rs index 979ea353..5667c731 100644 --- a/moqt-server/src/modules/handlers/unsubscribe_handler.rs +++ b/moqt-server/src/modules/handlers/unsubscribe_handler.rs @@ -1,9 +1,6 @@ use anyhow::Result; - -use moqt_core::{ - messages::control_messages::unsubscribe::Unsubscribe, MOQTClient, - TrackNamespaceManagerRepository, -}; +use moqt_core::pubsub_relation_manager_repository::PubSubRelationManagerRepository; +use moqt_core::{messages::control_messages::unsubscribe::Unsubscribe, MOQTClient}; // TODO: Not implemented yet #[allow(dead_code)] @@ -17,7 +14,7 @@ pub(crate) enum UnSubscribeResponse { pub(crate) async fn _unsubscribe_handler( unsubscribe_message: Unsubscribe, _client: &mut MOQTClient, // TODO: Not implemented yet - _track_namespace_manager_repository: &mut dyn TrackNamespaceManagerRepository, // TODO: Not implemented yet + _pubsub_relation_manager_repository: &mut dyn PubSubRelationManagerRepository, // TODO: Not implemented yet ) -> Result { tracing::trace!("unsubscribe_handler start."); diff --git a/moqt-server/src/modules/relation_manager.rs b/moqt-server/src/modules/relation_manager.rs new file mode 100644 index 00000000..77fc0a54 --- /dev/null +++ b/moqt-server/src/modules/relation_manager.rs @@ -0,0 +1,4 @@ +pub mod commands; +pub mod interface; +pub mod manager; +pub mod relation; diff --git a/moqt-server/src/modules/relation_manager/commands.rs b/moqt-server/src/modules/relation_manager/commands.rs new file mode 100644 index 00000000..cae5e977 --- /dev/null +++ b/moqt-server/src/modules/relation_manager/commands.rs @@ -0,0 +1,123 @@ +use anyhow::Result; +use moqt_core::messages::control_messages::subscribe::{FilterType, GroupOrder}; +use moqt_core::subscription_models::subscriptions::Subscription; +use tokio::sync::oneshot; + +#[cfg(test)] +use crate::modules::relation_manager::{ + manager::{Consumers, Producers}, + relation::PubSubRelation, +}; + +#[derive(Debug)] +pub(crate) enum TrackCommand { + SetupPublisher { + max_subscribe_id: u64, + publisher_session_id: usize, + resp: oneshot::Sender>, + }, + SetPublisherAnnouncedNamespace { + track_namespace: Vec, + publisher_session_id: usize, + resp: oneshot::Sender>, + }, + SetupSubscriber { + max_subscribe_id: u64, + subscriber_session_id: usize, + resp: oneshot::Sender>, + }, + IsValidSubscriberSubscribeId { + subscribe_id: u64, + subscriber_session_id: usize, + resp: oneshot::Sender>, + }, + IsValidSubscriberTrackAlias { + track_alias: u64, + subscriber_session_id: usize, + resp: oneshot::Sender>, + }, + IsTrackExisting { + track_namespace: Vec, + track_name: String, + resp: oneshot::Sender>, + }, + GetPublisherSubscription { + track_namespace: Vec, + track_name: String, + resp: oneshot::Sender>>, + }, + GetPublisherSessionId { + track_namespace: Vec, + resp: oneshot::Sender>>, + }, + GetRequestingSubscriberSessionIdsAndSubscribeIds { + publisher_subscribe_id: u64, + publisher_session_id: usize, + #[allow(clippy::type_complexity)] + resp: oneshot::Sender>>>, + }, + GetPublisherSubscribeId { + track_namespace: Vec, + track_name: String, + publisher_session_id: usize, + resp: oneshot::Sender>>, + }, + SetSubscriberSubscription { + subscriber_session_id: usize, + subscribe_id: u64, + track_alias: u64, + track_namespace: Vec, + track_name: String, + subscriber_priority: u8, + group_order: GroupOrder, + filter_type: FilterType, + start_group: Option, + start_object: Option, + end_group: Option, + end_object: Option, + resp: oneshot::Sender>, + }, + SetPublisherSubscription { + publisher_session_id: usize, + track_namespace: Vec, + track_name: String, + subscriber_priority: u8, + group_order: GroupOrder, + filter_type: FilterType, + start_group: Option, + start_object: Option, + end_group: Option, + end_object: Option, + resp: oneshot::Sender>, + }, + RegisterPubSubRelation { + publisher_session_id: usize, + publisher_subscribe_id: u64, + subscriber_session_id: usize, + subscriber_subscribe_id: u64, + resp: oneshot::Sender>, + }, + ActivateSubscriberSubscription { + subscriber_session_id: usize, + subscribe_id: u64, + resp: oneshot::Sender>, + }, + ActivatePublisherSubscription { + publisher_session_id: usize, + subscribe_id: u64, + resp: oneshot::Sender>, + }, + DeletePublisherAnnouncedNamespace { + track_namespace: Vec, + publisher_session_id: usize, + resp: oneshot::Sender>, + }, + DeleteClient { + session_id: usize, + resp: oneshot::Sender>, + }, + #[cfg(test)] + GetNodeAndRelationClone { + resp: oneshot::Sender>, + }, +} diff --git a/moqt-server/src/modules/track_namespace_manager.rs b/moqt-server/src/modules/relation_manager/interface.rs similarity index 60% rename from moqt-server/src/modules/track_namespace_manager.rs rename to moqt-server/src/modules/relation_manager/interface.rs index 1b9f7dea..a7ff211f 100644 --- a/moqt-server/src/modules/track_namespace_manager.rs +++ b/moqt-server/src/modules/relation_manager/interface.rs @@ -1,659 +1,24 @@ -use anyhow::{anyhow, bail, Result}; +use crate::modules::relation_manager::commands::{TrackCommand, TrackCommand::*}; +use anyhow::{bail, Result}; use async_trait::async_trait; use moqt_core::messages::control_messages::subscribe::{FilterType, GroupOrder}; -use moqt_core::subscription_models::subscription_nodes::SubscriptionNodeRegistory; -use moqt_core::subscription_models::subscription_nodes::{Consumer, Producer}; +use moqt_core::pubsub_relation_manager_repository::PubSubRelationManagerRepository; use moqt_core::subscription_models::subscriptions::Subscription; -use moqt_core::TrackNamespaceManagerRepository; -use std::collections::HashMap; use tokio::sync::{mpsc, oneshot}; -use TrackCommand::*; - -type SubscriberSessionId = usize; -type PublisherSessionId = usize; -type PublisherSubscribeId = u64; -type SubscriberSubscribeId = u64; - -type Consumers = HashMap; -type Producers = HashMap; - -#[derive(Debug, Clone)] -pub(crate) struct PubSubRelation { - records: HashMap< - (PublisherSessionId, PublisherSubscribeId), - Vec<(SubscriberSessionId, SubscriberSubscribeId)>, - >, -} - -impl PubSubRelation { - fn new() -> Self { - Self { - records: HashMap::new(), - } - } - - fn add_relation( - &mut self, - publisher_session_id: PublisherSessionId, - publisher_subscribe_id: PublisherSubscribeId, - subscriber_session_id: SubscriberSessionId, - subscriber_subscribe_id: SubscriberSubscribeId, - ) -> Result<()> { - let key = (publisher_session_id, publisher_subscribe_id); - let value = (subscriber_session_id, subscriber_subscribe_id); - - match self.records.get_mut(&key) { - // If the key exists, add the value to the existing vector - Some(subscribers) => { - subscribers.push(value); - } - // If the key does not exist, create a new vector and insert the value - None => { - self.records.insert(key, vec![value]); - } - } - - Ok(()) - } - - pub(crate) fn get_subscribers( - &self, - publisher_session_id: PublisherSessionId, - publisher_subscribe_id: PublisherSubscribeId, - ) -> Option<&Vec<(SubscriberSessionId, SubscriberSubscribeId)>> { - let key = (publisher_session_id, publisher_subscribe_id); - self.records.get(&key) - } - - // TODO: Define the behavior if the last subscriber unsubscribes from the track - // fn delete_relation -} - -// [Original Publisher: (Producer) ] -> [Relay: (Consumer) - - (Producer) ] -> [End Subscriber: (Consumer)] - -// Called as a separate thread -pub(crate) async fn track_namespace_manager(rx: &mut mpsc::Receiver) { - tracing::trace!("track_namespace_manager start"); - - let mut consumers: Consumers = HashMap::new(); - let mut producers: Producers = HashMap::new(); - let mut pubsub_relation = PubSubRelation::new(); - - while let Some(cmd) = rx.recv().await { - tracing::debug!("command received: {:#?}", cmd); - match cmd { - SetupPublisher { - max_subscribe_id, - publisher_session_id, - resp, - } => { - // Return an error if it already exists - if consumers.contains_key(&publisher_session_id) { - let msg = "publisher already exists"; - tracing::error!(msg); - resp.send(Err(anyhow!(msg))).unwrap(); - continue; - } - consumers.insert(publisher_session_id, Consumer::new(max_subscribe_id)); - resp.send(Ok(())).unwrap(); - } - SetPublisherAnnouncedNamespace { - track_namespace, - publisher_session_id, - resp, - } => { - let consumer = consumers.get_mut(&publisher_session_id).unwrap(); - match consumer.set_namespace(track_namespace) { - Ok(_) => { - resp.send(Ok(())).unwrap(); - } - Err(err) => { - let msg = format!("set_namespace: err: {:?}", err.to_string()); - tracing::error!(msg); - resp.send(Err(anyhow!(msg))).unwrap(); - } - } - } - SetupSubscriber { - max_subscribe_id, - subscriber_session_id, - resp, - } => { - // Return an error if it already exists - if producers.contains_key(&subscriber_session_id) { - let msg = "subscriber already exists"; - tracing::error!(msg); - resp.send(Err(anyhow!(msg))).unwrap(); - continue; - } - - producers.insert(subscriber_session_id, Producer::new(max_subscribe_id)); - resp.send(Ok(())).unwrap(); - } - IsValidSubscriberSubscribeId { - subscribe_id, - subscriber_session_id, - resp, - } => { - // Return an error if the subscriber does not exist - let producer = match producers.get(&subscriber_session_id) { - Some(producer) => producer, - None => { - let msg = "subscriber not found"; - tracing::error!(msg); - resp.send(Err(anyhow!(msg))).unwrap(); - continue; - } - }; - - let is_valid = producer.is_within_max_subscribe_id(subscribe_id) - && producer.is_subscribe_id_unique(subscribe_id); - - resp.send(Ok(is_valid)).unwrap(); - } - IsValidSubscriberTrackAlias { - track_alias, - subscriber_session_id, - resp, - } => { - // Return an error if the subscriber does not exist - let producer = match producers.get(&subscriber_session_id) { - Some(producer) => producer, - None => { - let msg = "subscriber not found"; - tracing::error!(msg); - resp.send(Err(anyhow!(msg))).unwrap(); - continue; - } - }; - - let is_valid = producer.is_track_alias_unique(track_alias); - resp.send(Ok(is_valid)).unwrap(); - } - GetPublisherSessionId { - track_namespace, - resp, - } => { - // Find the publisher that has the track namespace from all consumers - let publisher_session_id = consumers - .iter() - .find(|(_, consumer)| consumer.has_namespace(track_namespace.clone())) - .map(|(session_id, _)| *session_id); - resp.send(Ok(publisher_session_id)).unwrap(); - } - GetRequestingSubscriberSessionIdsAndSubscribeIds { - publisher_subscribe_id, - publisher_session_id, - resp, - } => { - if !pubsub_relation - .records - .contains_key(&(publisher_session_id, publisher_subscribe_id)) - { - let msg = "publisher not found in pubsub relation"; - tracing::error!(msg); - resp.send(Err(anyhow!(msg))).unwrap(); - continue; - } - - let subscribers = - pubsub_relation.get_subscribers(publisher_session_id, publisher_subscribe_id); - - // Check if it is in the requesting state - let requesting_subscribers: Option> = match subscribers { - Some(subscribers) => { - let mut requesting_subscribers = vec![]; - - for (subscriber_session_id, subscriber_subscribe_id) in subscribers { - let producer = producers.get(subscriber_session_id).unwrap(); - if producer.is_requesting(*subscriber_subscribe_id) { - requesting_subscribers - .push((*subscriber_session_id, *subscriber_subscribe_id)); - } - } - - Some(requesting_subscribers) - } - None => None, - }; - - resp.send(Ok(requesting_subscribers)).unwrap(); - } - GetPublisherSubscribeId { - track_namespace, - track_name, - publisher_session_id, - resp, - } => { - // Return an error if the publisher does not exist - let consumer = match consumers.get(&publisher_session_id) { - Some(consumer) => consumer, - None => { - let msg = "publisher not found"; - tracing::error!(msg); - resp.send(Err(anyhow!(msg))).unwrap(); - continue; - } - }; - - let result = consumer.get_subscribe_id(track_namespace, track_name); - - resp.send(result).unwrap(); - } - IsTrackExisting { - track_namespace, - track_name, - resp, - } => { - let consumer = consumers.iter().find(|(_, consumer)| { - consumer.has_track(track_namespace.clone(), track_name.clone()) - }); - let is_existing = consumer.is_some(); - resp.send(Ok(is_existing)).unwrap(); - } - GetPublisherSubscription { - track_namespace, - track_name, - resp, - } => { - let consumer = consumers.iter().find(|(_, consumer)| { - consumer.has_track(track_namespace.clone(), track_name.clone()) - }); - let result = consumer - .map(|(_, consumer)| { - consumer.get_subscription_by_full_track_name(track_namespace, track_name) - }) - .unwrap(); - - resp.send(result).unwrap(); - } - SetSubscriberSubscription { - subscriber_session_id, - subscribe_id, - track_alias, - track_namespace, - track_name, - subscriber_priority, - group_order, - filter_type, - start_group, - start_object, - end_group, - end_object, - resp, - } => { - // Return an error if the subscriber does not exist - let producer = match producers.get_mut(&subscriber_session_id) { - Some(producer) => producer, - None => { - let msg = "subscriber not found"; - tracing::error!(msg); - resp.send(Err(anyhow!(msg))).unwrap(); - continue; - } - }; - - match producer.set_subscription( - subscribe_id, - track_alias, - track_namespace, - track_name, - subscriber_priority, - group_order, - filter_type, - start_group, - start_object, - end_group, - end_object, - ) { - Ok(_) => resp.send(Ok(())).unwrap(), - Err(err) => { - tracing::error!("set_subscriber_subscription: err: {:?}", err.to_string()); - resp.send(Err(anyhow!(err))).unwrap(); - continue; - } - } - } - SetPublisherSubscription { - publisher_session_id, - track_namespace, - track_name, - subscriber_priority, - group_order, - filter_type, - start_group, - start_object, - end_group, - end_object, - resp, - } => { - // Return an error if the publisher does not exist - let consumer = match consumers.get_mut(&publisher_session_id) { - Some(consumer) => consumer, - None => { - let msg = "publisher not found"; - tracing::error!(msg); - resp.send(Err(anyhow!(msg))).unwrap(); - continue; - } - }; - - let (subscribe_id, track_alias) = - match consumer.find_unused_subscribe_id_and_track_alias() { - Ok(result) => result, - Err(err) => { - tracing::error!( - "find_unused_subscribe_id_and_track_alias: err: {:?}", - err.to_string() - ); - resp.send(Err(anyhow!(err))).unwrap(); - continue; - } - }; - match consumer.set_subscription( - subscribe_id, - track_alias, - track_namespace, - track_name, - subscriber_priority, - group_order, - filter_type, - start_group, - start_object, - end_group, - end_object, - ) { - Ok(_) => resp.send(Ok((subscribe_id, track_alias))).unwrap(), - Err(err) => { - tracing::error!("set_subscription: err: {:?}", err.to_string()); - resp.send(Err(anyhow!(err))).unwrap(); - continue; - } - }; - } - RegisterPubSubRelation { - publisher_session_id, - publisher_subscribe_id, - subscriber_session_id, - subscriber_subscribe_id, - resp, - } => { - let result = pubsub_relation.add_relation( - publisher_session_id, - publisher_subscribe_id, - subscriber_session_id, - subscriber_subscribe_id, - ); - - match result { - Ok(_) => resp.send(Ok(())).unwrap(), - Err(err) => { - tracing::error!("add_relation: err: {:?}", err.to_string()); - resp.send(Err(anyhow!(err))).unwrap(); - } - } - } - ActivateSubscriberSubscription { - subscriber_session_id, - subscribe_id, - resp, - } => { - // Return an error if the subscriber does not exist - let producer = match producers.get_mut(&subscriber_session_id) { - Some(producer) => producer, - None => { - let msg = "subscriber not found"; - tracing::error!(msg); - resp.send(Err(anyhow!(msg))).unwrap(); - continue; - } - }; - - match producer.activate_subscription(subscribe_id) { - Ok(activation_occured) => { - // Return bool as a activation is occurred or not - resp.send(Ok(activation_occured)).unwrap() - } - Err(err) => { - tracing::error!("activate_subscription: err: {:?}", err.to_string()); - resp.send(Err(anyhow!(err))).unwrap(); - } - } - } - ActivatePublisherSubscription { - publisher_session_id, - subscribe_id, - resp, - } => { - // Return an error if the publisher does not exist - let consumer = match consumers.get_mut(&publisher_session_id) { - Some(consumer) => consumer, - None => { - let msg = "publisher not found"; - tracing::error!(msg); - resp.send(Err(anyhow!(msg))).unwrap(); - continue; - } - }; - - match consumer.activate_subscription(subscribe_id) { - Ok(activation_occured) => { - // Return bool as a activation is occurred or not - resp.send(Ok(activation_occured)).unwrap() - } - Err(err) => { - tracing::error!("activate_subscription: err: {:?}", err.to_string()); - resp.send(Err(anyhow!(err))).unwrap(); - } - } - } - DeletePublisherAnnouncedNamespace { - track_namespace, - publisher_session_id, - resp, - } => { - // Return an error if the publisher does not exist - let consumer = match consumers.get_mut(&publisher_session_id) { - Some(consumer) => consumer, - None => { - let msg = "publisher not found"; - tracing::error!(msg); - resp.send(Err(anyhow!(msg))).unwrap(); - continue; - } - }; - - // Return false if the track_namespace does not exist - if !consumer.has_namespace(track_namespace.clone()) { - let msg = "track_namespace not found"; - tracing::error!(msg); - resp.send(Ok(false)).unwrap(); - continue; - } - - match consumer.delete_namespace(track_namespace) { - Ok(_) => { - resp.send(Ok(true)).unwrap(); - } - Err(err) => { - tracing::error!("delete_namespace: err: {:?}", err.to_string()); - resp.send(Err(err)).unwrap(); - } - } - } - DeleteClient { session_id, resp } => { - let is_exist_as_consumer = consumers.contains_key(&session_id); - let is_exist_as_producer = producers.contains_key(&session_id); - let is_exist_in_pubsub_relation = pubsub_relation - .records - .iter() - .any(|(key, _)| key.0 == session_id); - - // Return an error if the session does not exist - if !is_exist_as_consumer && !is_exist_as_producer && !is_exist_in_pubsub_relation { - let msg = "session not found"; - tracing::error!(msg); - resp.send(Ok(false)).unwrap(); - continue; - } - - // Delete as a publisher - consumers.remove(&session_id); - pubsub_relation.records.retain(|key, _| key.0 != session_id); - - // Delete as a subscriber - producers.remove(&session_id); - pubsub_relation - .records - .iter_mut() - .for_each(|(_, subscribers)| { - subscribers.retain(|(subscriber_session_id, _)| { - *subscriber_session_id != session_id - }); - }); - - resp.send(Ok(true)).unwrap(); - } - #[cfg(test)] - GetNodeAndRelationClone { resp } => { - let consumer = consumers.clone(); - let producer = producers.clone(); - let relation = pubsub_relation.clone(); - - resp.send(Ok((consumer, producer, relation))).unwrap(); - } - } - } - - tracing::trace!("track_namespace_manager end"); -} - -#[derive(Debug)] -pub(crate) enum TrackCommand { - SetupPublisher { - max_subscribe_id: u64, - publisher_session_id: usize, - resp: oneshot::Sender>, - }, - SetPublisherAnnouncedNamespace { - track_namespace: Vec, - publisher_session_id: usize, - resp: oneshot::Sender>, - }, - SetupSubscriber { - max_subscribe_id: u64, - subscriber_session_id: usize, - resp: oneshot::Sender>, - }, - IsValidSubscriberSubscribeId { - subscribe_id: u64, - subscriber_session_id: usize, - resp: oneshot::Sender>, - }, - IsValidSubscriberTrackAlias { - track_alias: u64, - subscriber_session_id: usize, - resp: oneshot::Sender>, - }, - IsTrackExisting { - track_namespace: Vec, - track_name: String, - resp: oneshot::Sender>, - }, - GetPublisherSubscription { - track_namespace: Vec, - track_name: String, - resp: oneshot::Sender>>, - }, - GetPublisherSessionId { - track_namespace: Vec, - resp: oneshot::Sender>>, - }, - GetRequestingSubscriberSessionIdsAndSubscribeIds { - publisher_subscribe_id: u64, - publisher_session_id: usize, - #[allow(clippy::type_complexity)] - resp: oneshot::Sender>>>, - }, - GetPublisherSubscribeId { - track_namespace: Vec, - track_name: String, - publisher_session_id: usize, - resp: oneshot::Sender>>, - }, - SetSubscriberSubscription { - subscriber_session_id: usize, - subscribe_id: u64, - track_alias: u64, - track_namespace: Vec, - track_name: String, - subscriber_priority: u8, - group_order: GroupOrder, - filter_type: FilterType, - start_group: Option, - start_object: Option, - end_group: Option, - end_object: Option, - resp: oneshot::Sender>, - }, - SetPublisherSubscription { - publisher_session_id: usize, - track_namespace: Vec, - track_name: String, - subscriber_priority: u8, - group_order: GroupOrder, - filter_type: FilterType, - start_group: Option, - start_object: Option, - end_group: Option, - end_object: Option, - resp: oneshot::Sender>, - }, - RegisterPubSubRelation { - publisher_session_id: usize, - publisher_subscribe_id: u64, - subscriber_session_id: usize, - subscriber_subscribe_id: u64, - resp: oneshot::Sender>, - }, - ActivateSubscriberSubscription { - subscriber_session_id: usize, - subscribe_id: u64, - resp: oneshot::Sender>, - }, - ActivatePublisherSubscription { - publisher_session_id: usize, - subscribe_id: u64, - resp: oneshot::Sender>, - }, - DeletePublisherAnnouncedNamespace { - track_namespace: Vec, - publisher_session_id: usize, - resp: oneshot::Sender>, - }, - DeleteClient { - session_id: usize, - resp: oneshot::Sender>, - }, - #[cfg(test)] - GetNodeAndRelationClone { - resp: oneshot::Sender>, - }, -} // Wrapper to encapsulate channel-related operations -pub(crate) struct TrackNamespaceManager { +pub(crate) struct PubSubRelationManagerInterface { tx: mpsc::Sender, } -impl TrackNamespaceManager { +impl PubSubRelationManagerInterface { pub fn new(tx: mpsc::Sender) -> Self { Self { tx } } } #[async_trait] -impl TrackNamespaceManagerRepository for TrackNamespaceManager { +impl PubSubRelationManagerRepository for PubSubRelationManagerInterface { async fn setup_publisher( &self, max_subscribe_id: u64, @@ -1033,19 +398,22 @@ impl TrackNamespaceManagerRepository for TrackNamespaceManager { #[cfg(test)] pub(crate) mod test_utils { - - use crate::modules::track_namespace_manager::{ - Consumers, Producers, PubSubRelation, TrackCommand, TrackNamespaceManager, + use crate::modules::relation_manager::{ + commands::TrackCommand, + interface::PubSubRelationManagerInterface, + manager::{Consumers, Producers}, + relation::PubSubRelation, }; use anyhow::Result; + use tokio::sync::oneshot; pub(crate) async fn get_node_and_relation_clone( - track_namespace_manager: &TrackNamespaceManager, + pubsub_relation_manager: &PubSubRelationManagerInterface, ) -> (Consumers, Producers, PubSubRelation) { let (resp_tx, resp_rx) = oneshot::channel::>(); let cmd = TrackCommand::GetNodeAndRelationClone { resp: resp_tx }; - track_namespace_manager.tx.send(cmd).await.unwrap(); + pubsub_relation_manager.tx.send(cmd).await.unwrap(); resp_rx.await.unwrap().unwrap() } @@ -1053,14 +421,14 @@ pub(crate) mod test_utils { #[cfg(test)] mod success { - use crate::modules::track_namespace_manager::test_utils; - use crate::modules::track_namespace_manager::{ - track_namespace_manager, TrackCommand, TrackNamespaceManager, + use crate::modules::relation_manager::{ + commands::TrackCommand, interface::test_utils, interface::PubSubRelationManagerInterface, + manager::pubsub_relation_manager, }; use moqt_core::messages::control_messages::subscribe::{FilterType, GroupOrder}; - use moqt_core::subscription_models::subscription_nodes::SubscriptionNodeRegistory; + use moqt_core::pubsub_relation_manager_repository::PubSubRelationManagerRepository; + use moqt_core::subscription_models::node_registory::SubscriptionNodeRegistory; use moqt_core::subscription_models::subscriptions::Subscription; - use moqt_core::TrackNamespaceManagerRepository; use tokio::sync::mpsc; #[tokio::test] @@ -1070,17 +438,17 @@ mod success { // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); + tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); - let result = track_namespace_manager + let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let result = pubsub_relation_manager .setup_publisher(max_subscribe_id, publisher_session_id) .await; assert!(result.is_ok()); // Check if the publisher is created let (consumers, _, _) = - test_utils::get_node_and_relation_clone(&track_namespace_manager).await; + test_utils::get_node_and_relation_clone(&pubsub_relation_manager).await; let length = consumers.len(); assert_eq!(length, 1); @@ -1094,20 +462,20 @@ mod success { // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); + tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); - let _ = track_namespace_manager + let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let _ = pubsub_relation_manager .setup_publisher(max_subscribe_id, publisher_session_id) .await; - let result = track_namespace_manager + let result = pubsub_relation_manager .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) .await; assert!(result.is_ok()); // Check if the track_namespace is set let (consumers, _, _) = - test_utils::get_node_and_relation_clone(&track_namespace_manager).await; + test_utils::get_node_and_relation_clone(&pubsub_relation_manager).await; let consumer = consumers.get(&publisher_session_id).unwrap(); let announced_namespaces = consumer.get_namespaces().unwrap(); @@ -1123,17 +491,17 @@ mod success { // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); + tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); - let result = track_namespace_manager + let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let result = pubsub_relation_manager .setup_subscriber(max_subscribe_id, subscriber_session_id) .await; assert!(result.is_ok()); // Check if the subscriber is created let (_, producers, _) = - test_utils::get_node_and_relation_clone(&track_namespace_manager).await; + test_utils::get_node_and_relation_clone(&pubsub_relation_manager).await; let length = producers.len(); assert_eq!(length, 1); @@ -1147,14 +515,14 @@ mod success { // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); + tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); - let _ = track_namespace_manager + let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let _ = pubsub_relation_manager .setup_subscriber(max_subscribe_id, subscriber_session_id) .await; - let result = track_namespace_manager + let result = pubsub_relation_manager .is_valid_subscriber_subscribe_id(subscribe_id, subscriber_session_id) .await; @@ -1170,14 +538,14 @@ mod success { // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); + tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); - let _ = track_namespace_manager + let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let _ = pubsub_relation_manager .setup_subscriber(max_subscribe_id, subscriber_session_id) .await; - let result = track_namespace_manager + let result = pubsub_relation_manager .is_valid_subscriber_subscribe_id(subscribe_id, subscriber_session_id) .await; @@ -1193,14 +561,14 @@ mod success { // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); + tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); - let _ = track_namespace_manager + let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let _ = pubsub_relation_manager .setup_subscriber(max_subscribe_id, subscriber_session_id) .await; - let result = track_namespace_manager + let result = pubsub_relation_manager .is_valid_subscriber_track_alias(track_alias, subscriber_session_id) .await; @@ -1226,13 +594,13 @@ mod success { // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); + tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); - let _ = track_namespace_manager + let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let _ = pubsub_relation_manager .setup_subscriber(max_subscribe_id, subscriber_session_id) .await; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .set_subscriber_subscription( subscriber_session_id, subscribe_id, @@ -1249,7 +617,7 @@ mod success { ) .await; - let result = track_namespace_manager + let result = pubsub_relation_manager .is_valid_subscriber_track_alias(track_alias, subscriber_session_id) .await; assert!(result.is_ok()); @@ -1274,19 +642,19 @@ mod success { // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); + tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); + let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); - let _ = track_namespace_manager + let _ = pubsub_relation_manager .setup_publisher(max_subscribe_id, publisher_session_id) .await; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) .await; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .set_publisher_subscription( publisher_session_id, track_namespace.clone(), @@ -1301,7 +669,7 @@ mod success { ) .await; - let result = track_namespace_manager + let result = pubsub_relation_manager .is_track_existing(track_namespace, track_name) .await; assert!(result.is_ok()); @@ -1317,10 +685,10 @@ mod success { // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); + tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); - let result = track_namespace_manager + let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let result = pubsub_relation_manager .is_track_existing(track_namespace, track_name) .await; assert!(result.is_ok()); @@ -1346,13 +714,13 @@ mod success { // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); + tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); - let _ = track_namespace_manager + let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let _ = pubsub_relation_manager .setup_publisher(max_subscribe_id, publisher_session_id) .await; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .set_publisher_subscription( publisher_session_id, track_namespace.clone(), @@ -1367,7 +735,7 @@ mod success { ) .await; - let subscription = track_namespace_manager + let subscription = pubsub_relation_manager .get_publisher_subscription_by_full_track_name( track_namespace.clone(), track_name.clone(), @@ -1401,18 +769,18 @@ mod success { // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); + tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); + let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); - let _ = track_namespace_manager + let _ = pubsub_relation_manager .setup_publisher(max_subscribe_id, publisher_session_id) .await; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) .await; - let session_id = track_namespace_manager + let session_id = pubsub_relation_manager .get_publisher_session_id(track_namespace.clone()) .await .unwrap() @@ -1440,17 +808,17 @@ mod success { // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); + tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); + let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); - let _ = track_namespace_manager + let _ = pubsub_relation_manager .setup_publisher(max_subscribe_id, publisher_session_id) .await; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) .await; - let (publisher_subscribe_id, _) = track_namespace_manager + let (publisher_subscribe_id, _) = pubsub_relation_manager .set_publisher_subscription( publisher_session_id, track_namespace.clone(), @@ -1467,10 +835,10 @@ mod success { .unwrap(); for i in [0, 1] { - let _ = track_namespace_manager + let _ = pubsub_relation_manager .setup_subscriber(max_subscribe_id, subscriber_session_ids[i]) .await; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .set_subscriber_subscription( subscriber_session_ids[i], subscriber_subscribe_ids[i], @@ -1486,7 +854,7 @@ mod success { end_object, ) .await; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .register_pubsup_relation( publisher_session_id, publisher_subscribe_id, @@ -1496,7 +864,7 @@ mod success { .await; } - let list = track_namespace_manager + let list = pubsub_relation_manager .get_requesting_subscriber_session_ids_and_subscribe_ids( publisher_subscribe_id, publisher_session_id, @@ -1529,17 +897,17 @@ mod success { // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); + tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); + let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); - let _ = track_namespace_manager + let _ = pubsub_relation_manager .setup_publisher(max_subscribe_id, publisher_session_id) .await; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) .await; - let (expected_publisher_subscribe_id, _) = track_namespace_manager + let (expected_publisher_subscribe_id, _) = pubsub_relation_manager .set_publisher_subscription( publisher_session_id, track_namespace.clone(), @@ -1555,7 +923,7 @@ mod success { .await .unwrap(); - let publisher_subscribe_id = track_namespace_manager + let publisher_subscribe_id = pubsub_relation_manager .get_publisher_subscribe_id(track_namespace, track_name, publisher_session_id) .await .unwrap() @@ -1582,13 +950,13 @@ mod success { // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); + tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); - let _ = track_namespace_manager + let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let _ = pubsub_relation_manager .setup_subscriber(max_subscribe_id, subscriber_session_id) .await; - let result = track_namespace_manager + let result = pubsub_relation_manager .set_subscriber_subscription( subscriber_session_id, subscribe_id, @@ -1609,7 +977,7 @@ mod success { // Assert that the subscription is set let (_, producers, _) = - test_utils::get_node_and_relation_clone(&track_namespace_manager).await; + test_utils::get_node_and_relation_clone(&pubsub_relation_manager).await; let producer = producers.get(&subscriber_session_id).unwrap(); let subscription = producer.get_subscription(subscribe_id).unwrap().unwrap(); @@ -1646,13 +1014,13 @@ mod success { // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); + tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); - let _ = track_namespace_manager + let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let _ = pubsub_relation_manager .setup_publisher(max_subscribe_id, publisher_session_id) .await; - let result = track_namespace_manager + let result = pubsub_relation_manager .set_publisher_subscription( publisher_session_id, track_namespace.clone(), @@ -1673,7 +1041,7 @@ mod success { // Assert that the subscription is set let (consumers, _, _) = - test_utils::get_node_and_relation_clone(&track_namespace_manager).await; + test_utils::get_node_and_relation_clone(&pubsub_relation_manager).await; let consumer = consumers.get(&publisher_session_id).unwrap(); let subscription = consumer .get_subscription(publisher_subscribe_id) @@ -1716,18 +1084,18 @@ mod success { // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); + tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); + let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); // pub 1 <- sub 2, 3 - let _ = track_namespace_manager + let _ = pubsub_relation_manager .setup_publisher(max_subscribe_id, publisher_session_id) .await; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) .await; - let (publisher_subscribe_id, _) = track_namespace_manager + let (publisher_subscribe_id, _) = pubsub_relation_manager .set_publisher_subscription( publisher_session_id, track_namespace.clone(), @@ -1744,10 +1112,10 @@ mod success { .unwrap(); for i in [0, 1] { - let _ = track_namespace_manager + let _ = pubsub_relation_manager .setup_subscriber(max_subscribe_id, subscriber_session_ids[i]) .await; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .set_subscriber_subscription( subscriber_session_ids[i], subscriber_subscribe_ids[i], @@ -1763,7 +1131,7 @@ mod success { end_object, ) .await; - let result = track_namespace_manager + let result = pubsub_relation_manager .register_pubsup_relation( publisher_session_id, publisher_subscribe_id, @@ -1777,7 +1145,7 @@ mod success { // Assert that the relation is registered let (_, _, pubsub_relation) = - test_utils::get_node_and_relation_clone(&track_namespace_manager).await; + test_utils::get_node_and_relation_clone(&pubsub_relation_manager).await; let subscriber = pubsub_relation .get_subscribers(publisher_session_id, publisher_subscribe_id) @@ -1810,13 +1178,13 @@ mod success { // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); + tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); - let _ = track_namespace_manager + let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let _ = pubsub_relation_manager .setup_subscriber(max_subscribe_id, subscriber_session_id) .await; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .set_subscriber_subscription( subscriber_session_id, subscribe_id, @@ -1833,7 +1201,7 @@ mod success { ) .await; - let activate_occured = track_namespace_manager + let activate_occured = pubsub_relation_manager .activate_subscriber_subscription(subscriber_session_id, subscribe_id) .await .unwrap(); @@ -1842,7 +1210,7 @@ mod success { // Assert that the subscription is active let (_, producers, _) = - test_utils::get_node_and_relation_clone(&track_namespace_manager).await; + test_utils::get_node_and_relation_clone(&pubsub_relation_manager).await; let producer = producers.get(&subscriber_session_id).unwrap(); let subscription = producer.get_subscription(subscribe_id).unwrap().unwrap(); @@ -1865,13 +1233,13 @@ mod success { // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); + tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); - let _ = track_namespace_manager + let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let _ = pubsub_relation_manager .setup_publisher(max_subscribe_id, publisher_session_id) .await; - let (publisher_subscribe_id, _) = track_namespace_manager + let (publisher_subscribe_id, _) = pubsub_relation_manager .set_publisher_subscription( publisher_session_id, track_namespace.clone(), @@ -1887,7 +1255,7 @@ mod success { .await .unwrap(); - let activate_occured = track_namespace_manager + let activate_occured = pubsub_relation_manager .activate_publisher_subscription(publisher_session_id, publisher_subscribe_id) .await .unwrap(); @@ -1896,7 +1264,7 @@ mod success { // Assert that the subscription is active let (consumers, _, _) = - test_utils::get_node_and_relation_clone(&track_namespace_manager).await; + test_utils::get_node_and_relation_clone(&pubsub_relation_manager).await; let consumer = consumers.get(&publisher_session_id).unwrap(); let subscription = consumer .get_subscription(publisher_subscribe_id) @@ -1914,17 +1282,17 @@ mod success { // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); + tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); - let _ = track_namespace_manager + let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let _ = pubsub_relation_manager .setup_publisher(max_subscribe_id, publisher_session_id) .await; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) .await; - let result = track_namespace_manager + let result = pubsub_relation_manager .delete_publisher_announced_namespace(track_namespace, publisher_session_id) .await; assert!(result.is_ok()); @@ -1934,7 +1302,7 @@ mod success { // Assert that the announced namespace is deleted let (consumers, _, _) = - test_utils::get_node_and_relation_clone(&track_namespace_manager).await; + test_utils::get_node_and_relation_clone(&pubsub_relation_manager).await; let consumer = consumers.get(&publisher_session_id).unwrap(); let announced_namespaces = consumer.get_namespaces().unwrap().to_vec(); @@ -1949,14 +1317,14 @@ mod success { // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); + tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); + let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); - let _ = track_namespace_manager + let _ = pubsub_relation_manager .setup_publisher(max_subscribe_id, publisher_session_id) .await; - let result = track_namespace_manager + let result = pubsub_relation_manager .delete_publisher_announced_namespace(track_namespace, publisher_session_id) .await; assert!(result.is_ok()); @@ -1988,25 +1356,25 @@ mod success { // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); + tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); + let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); // Register: // pub 1 <- sub 2, 3, 4 // pub 2 <- sub 3, 4 for i in [0, 1] { // for pub 1, 2 - let _ = track_namespace_manager + let _ = pubsub_relation_manager .setup_publisher(max_subscribe_id, publisher_session_ids[i]) .await; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .set_publisher_announced_namespace( track_namespaces[i].clone(), publisher_session_ids[i], ) .await; - let (publisher_subscribe_id, _) = track_namespace_manager + let (publisher_subscribe_id, _) = pubsub_relation_manager .set_publisher_subscription( publisher_session_ids[i], track_namespaces[i].clone(), @@ -2026,13 +1394,13 @@ mod success { for j in [0, 1, 2] { // for sub 2, 3, 4 - let _ = track_namespace_manager + let _ = pubsub_relation_manager .setup_subscriber(max_subscribe_id, subscriber_session_ids[j]) .await; } // for sub 2 - let _ = track_namespace_manager + let _ = pubsub_relation_manager .set_subscriber_subscription( subscriber_session_ids[0], subscriber_subscribe_ids[0], @@ -2053,7 +1421,7 @@ mod success { // for pub 1, 2 for j in [1, 2] { // for sub 3, 4 - let _ = track_namespace_manager + let _ = pubsub_relation_manager .set_subscriber_subscription( subscriber_session_ids[j], subscriber_subscribe_ids[j], @@ -2070,7 +1438,7 @@ mod success { ) .await; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .register_pubsup_relation( publisher_session_ids[i], publisher_subscribe_ids[i], @@ -2078,14 +1446,14 @@ mod success { subscriber_subscribe_ids[j], ) .await; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .activate_subscriber_subscription( subscriber_session_ids[j], subscriber_subscribe_ids[j], ) .await; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .activate_publisher_subscription( publisher_session_ids[i], publisher_subscribe_ids[i], @@ -2095,7 +1463,7 @@ mod success { } // for pub 1 and sub 2 - let _ = track_namespace_manager + let _ = pubsub_relation_manager .register_pubsup_relation( publisher_session_ids[0], publisher_subscribe_ids[0], @@ -2103,20 +1471,20 @@ mod success { subscriber_subscribe_ids[0], ) .await; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .activate_subscriber_subscription( subscriber_session_ids[0], subscriber_subscribe_ids[0], ) .await; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .activate_publisher_subscription(publisher_session_ids[0], publisher_subscribe_ids[0]) .await; // Delete: pub 2, sub 2 // Remain: pub 1 <- sub 3, 4 - let result = track_namespace_manager + let result = pubsub_relation_manager .delete_client(subscriber_session_ids[0]) .await; assert!(result.is_ok()); @@ -2125,7 +1493,7 @@ mod success { assert!(delete_occured); let (consumers, producers, pubsub_relation) = - test_utils::get_node_and_relation_clone(&track_namespace_manager).await; + test_utils::get_node_and_relation_clone(&pubsub_relation_manager).await; // Assert that sub 2 is deleted // Remain: sub 3, 4 @@ -2163,11 +1531,11 @@ mod success { // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); + tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); + let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); - let result = track_namespace_manager.delete_client(session_id).await; + let result = pubsub_relation_manager.delete_client(session_id).await; assert!(result.is_ok()); let delete_occured = result.unwrap(); @@ -2177,11 +1545,12 @@ mod success { #[cfg(test)] mod failure { - use crate::modules::track_namespace_manager::{ - track_namespace_manager, TrackCommand, TrackNamespaceManager, + use crate::modules::relation_manager::{ + commands::TrackCommand, interface::PubSubRelationManagerInterface, + manager::pubsub_relation_manager, }; use moqt_core::messages::control_messages::subscribe::{FilterType, GroupOrder}; - use moqt_core::TrackNamespaceManagerRepository; + use moqt_core::pubsub_relation_manager_repository::PubSubRelationManagerRepository; use tokio::sync::mpsc; #[tokio::test] @@ -2191,15 +1560,15 @@ mod failure { // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); + tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); - let _ = track_namespace_manager + let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let _ = pubsub_relation_manager .setup_publisher(max_subscribe_id, publisher_session_id) .await; // Register the same publisher - let result = track_namespace_manager + let result = pubsub_relation_manager .setup_publisher(max_subscribe_id, publisher_session_id) .await; @@ -2214,18 +1583,18 @@ mod failure { // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); + tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); - let _ = track_namespace_manager + let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let _ = pubsub_relation_manager .setup_publisher(max_subscribe_id, publisher_session_id) .await; - let _ = track_namespace_manager + let _ = pubsub_relation_manager .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) .await; // Register the same track namespace - let result = track_namespace_manager + let result = pubsub_relation_manager .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) .await; @@ -2239,15 +1608,15 @@ mod failure { // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); + tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); - let _ = track_namespace_manager + let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let _ = pubsub_relation_manager .setup_subscriber(max_subscribe_id, subscriber_session_id) .await; // Register the same subscriber - let result = track_namespace_manager + let result = pubsub_relation_manager .setup_subscriber(max_subscribe_id, subscriber_session_id) .await; @@ -2263,14 +1632,14 @@ mod failure { // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); + tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); - let _ = track_namespace_manager + let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let _ = pubsub_relation_manager .setup_subscriber(max_subscribe_id, subscriber_session_id) .await; - let result = track_namespace_manager + let result = pubsub_relation_manager .is_valid_subscriber_subscribe_id( subscriber_subscribe_id, invalid_subscriber_session_id, @@ -2289,14 +1658,14 @@ mod failure { // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); + tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); - let _ = track_namespace_manager + let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let _ = pubsub_relation_manager .setup_subscriber(max_subscribe_id, subscriber_session_id) .await; - let result = track_namespace_manager + let result = pubsub_relation_manager .is_valid_subscriber_track_alias(subscriber_track_alias, invalid_subscriber_session_id) .await; @@ -2309,11 +1678,11 @@ mod failure { let publisher_subscribe_id = 0; // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); + tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); + let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); - let result = track_namespace_manager + let result = pubsub_relation_manager .get_requesting_subscriber_session_ids_and_subscribe_ids( publisher_subscribe_id, publisher_session_id, @@ -2331,11 +1700,11 @@ mod failure { // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); + tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); + let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); - let result = track_namespace_manager + let result = pubsub_relation_manager .get_publisher_subscribe_id(track_namespace, track_name, invalid_publisher_session_id) .await; @@ -2361,15 +1730,15 @@ mod failure { // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); + tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); + let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); - let _ = track_namespace_manager + let _ = pubsub_relation_manager .setup_subscriber(max_subscribe_id, subscriber_session_id) .await; - let result = track_namespace_manager + let result = pubsub_relation_manager .set_subscriber_subscription( invalid_subscriber_session_id, subscribe_id, @@ -2406,15 +1775,15 @@ mod failure { // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); + tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); + let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); - let _ = track_namespace_manager + let _ = pubsub_relation_manager .setup_publisher(max_subscribe_id, publisher_session_id) .await; - let result = track_namespace_manager + let result = pubsub_relation_manager .set_publisher_subscription( invalid_publisher_session_id, track_namespace, @@ -2441,14 +1810,14 @@ mod failure { // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); + tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); - let _ = track_namespace_manager + let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let _ = pubsub_relation_manager .setup_subscriber(max_subscribe_id, subscriber_session_id) .await; - let result = track_namespace_manager + let result = pubsub_relation_manager .activate_subscriber_subscription(invalid_subscriber_session_id, subscribe_id) .await; @@ -2464,14 +1833,14 @@ mod failure { // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); + tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); - let _ = track_namespace_manager + let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let _ = pubsub_relation_manager .setup_publisher(max_subscribe_id, publisher_session_id) .await; - let result = track_namespace_manager + let result = pubsub_relation_manager .activate_publisher_subscription(invalid_publisher_session_id, publisher_subscribe_id) .await; @@ -2487,14 +1856,14 @@ mod failure { // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); - tokio::spawn(async move { track_namespace_manager(&mut track_rx).await }); + tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let track_namespace_manager = TrackNamespaceManager::new(track_tx.clone()); - let _ = track_namespace_manager + let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let _ = pubsub_relation_manager .setup_publisher(max_subscribe_id, publisher_session_id) .await; - let result = track_namespace_manager + let result = pubsub_relation_manager .delete_publisher_announced_namespace(track_namespace, invalid_publisher_session_id) .await; diff --git a/moqt-server/src/modules/relation_manager/manager.rs b/moqt-server/src/modules/relation_manager/manager.rs new file mode 100644 index 00000000..a138af21 --- /dev/null +++ b/moqt-server/src/modules/relation_manager/manager.rs @@ -0,0 +1,474 @@ +use crate::modules::relation_manager::{ + commands::{TrackCommand, TrackCommand::*}, + relation::PubSubRelation, +}; +use anyhow::anyhow; +use moqt_core::subscription_models::node_registory::SubscriptionNodeRegistory; +use moqt_core::subscription_models::{consumer_node::Consumer, producer_node::Producer}; +use std::collections::HashMap; +use tokio::sync::mpsc; + +type SubscriberSessionId = usize; +type PublisherSessionId = usize; + +pub(crate) type Consumers = HashMap; +pub(crate) type Producers = HashMap; + +// [Original Publisher: (Producer) ] -> [Relay: (Consumer) - - (Producer) ] -> [End Subscriber: (Consumer)] + +// Called as a separate thread +pub(crate) async fn pubsub_relation_manager(rx: &mut mpsc::Receiver) { + tracing::trace!("pubsub_relation_manager start"); + + let mut consumers: Consumers = HashMap::new(); + let mut producers: Producers = HashMap::new(); + let mut pubsub_relation = PubSubRelation::new(); + + while let Some(cmd) = rx.recv().await { + tracing::debug!("command received: {:#?}", cmd); + match cmd { + SetupPublisher { + max_subscribe_id, + publisher_session_id, + resp, + } => { + // Return an error if it already exists + if consumers.contains_key(&publisher_session_id) { + let msg = "publisher already exists"; + tracing::error!(msg); + resp.send(Err(anyhow!(msg))).unwrap(); + continue; + } + consumers.insert(publisher_session_id, Consumer::new(max_subscribe_id)); + resp.send(Ok(())).unwrap(); + } + SetPublisherAnnouncedNamespace { + track_namespace, + publisher_session_id, + resp, + } => { + let consumer = consumers.get_mut(&publisher_session_id).unwrap(); + match consumer.set_namespace(track_namespace) { + Ok(_) => { + resp.send(Ok(())).unwrap(); + } + Err(err) => { + let msg = format!("set_namespace: err: {:?}", err.to_string()); + tracing::error!(msg); + resp.send(Err(anyhow!(msg))).unwrap(); + } + } + } + SetupSubscriber { + max_subscribe_id, + subscriber_session_id, + resp, + } => { + // Return an error if it already exists + if producers.contains_key(&subscriber_session_id) { + let msg = "subscriber already exists"; + tracing::error!(msg); + resp.send(Err(anyhow!(msg))).unwrap(); + continue; + } + + producers.insert(subscriber_session_id, Producer::new(max_subscribe_id)); + resp.send(Ok(())).unwrap(); + } + IsValidSubscriberSubscribeId { + subscribe_id, + subscriber_session_id, + resp, + } => { + // Return an error if the subscriber does not exist + let producer = match producers.get(&subscriber_session_id) { + Some(producer) => producer, + None => { + let msg = "subscriber not found"; + tracing::error!(msg); + resp.send(Err(anyhow!(msg))).unwrap(); + continue; + } + }; + + let is_valid = producer.is_within_max_subscribe_id(subscribe_id) + && producer.is_subscribe_id_unique(subscribe_id); + + resp.send(Ok(is_valid)).unwrap(); + } + IsValidSubscriberTrackAlias { + track_alias, + subscriber_session_id, + resp, + } => { + // Return an error if the subscriber does not exist + let producer = match producers.get(&subscriber_session_id) { + Some(producer) => producer, + None => { + let msg = "subscriber not found"; + tracing::error!(msg); + resp.send(Err(anyhow!(msg))).unwrap(); + continue; + } + }; + + let is_valid = producer.is_track_alias_unique(track_alias); + resp.send(Ok(is_valid)).unwrap(); + } + GetPublisherSessionId { + track_namespace, + resp, + } => { + // Find the publisher that has the track namespace from all consumers + let publisher_session_id = consumers + .iter() + .find(|(_, consumer)| consumer.has_namespace(track_namespace.clone())) + .map(|(session_id, _)| *session_id); + resp.send(Ok(publisher_session_id)).unwrap(); + } + GetRequestingSubscriberSessionIdsAndSubscribeIds { + publisher_subscribe_id, + publisher_session_id, + resp, + } => { + if !pubsub_relation + .records + .contains_key(&(publisher_session_id, publisher_subscribe_id)) + { + let msg = "publisher not found in pubsub relation"; + tracing::error!(msg); + resp.send(Err(anyhow!(msg))).unwrap(); + continue; + } + + let subscribers = + pubsub_relation.get_subscribers(publisher_session_id, publisher_subscribe_id); + + // Check if it is in the requesting state + let requesting_subscribers: Option> = match subscribers { + Some(subscribers) => { + let mut requesting_subscribers = vec![]; + + for (subscriber_session_id, subscriber_subscribe_id) in subscribers { + let producer = producers.get(subscriber_session_id).unwrap(); + if producer.is_requesting(*subscriber_subscribe_id) { + requesting_subscribers + .push((*subscriber_session_id, *subscriber_subscribe_id)); + } + } + + Some(requesting_subscribers) + } + None => None, + }; + + resp.send(Ok(requesting_subscribers)).unwrap(); + } + GetPublisherSubscribeId { + track_namespace, + track_name, + publisher_session_id, + resp, + } => { + // Return an error if the publisher does not exist + let consumer = match consumers.get(&publisher_session_id) { + Some(consumer) => consumer, + None => { + let msg = "publisher not found"; + tracing::error!(msg); + resp.send(Err(anyhow!(msg))).unwrap(); + continue; + } + }; + + let result = consumer.get_subscribe_id(track_namespace, track_name); + + resp.send(result).unwrap(); + } + IsTrackExisting { + track_namespace, + track_name, + resp, + } => { + let consumer = consumers.iter().find(|(_, consumer)| { + consumer.has_track(track_namespace.clone(), track_name.clone()) + }); + let is_existing = consumer.is_some(); + resp.send(Ok(is_existing)).unwrap(); + } + GetPublisherSubscription { + track_namespace, + track_name, + resp, + } => { + let consumer = consumers.iter().find(|(_, consumer)| { + consumer.has_track(track_namespace.clone(), track_name.clone()) + }); + let result = consumer + .map(|(_, consumer)| { + consumer.get_subscription_by_full_track_name(track_namespace, track_name) + }) + .unwrap(); + + resp.send(result).unwrap(); + } + SetSubscriberSubscription { + subscriber_session_id, + subscribe_id, + track_alias, + track_namespace, + track_name, + subscriber_priority, + group_order, + filter_type, + start_group, + start_object, + end_group, + end_object, + resp, + } => { + // Return an error if the subscriber does not exist + let producer = match producers.get_mut(&subscriber_session_id) { + Some(producer) => producer, + None => { + let msg = "subscriber not found"; + tracing::error!(msg); + resp.send(Err(anyhow!(msg))).unwrap(); + continue; + } + }; + + match producer.set_subscription( + subscribe_id, + track_alias, + track_namespace, + track_name, + subscriber_priority, + group_order, + filter_type, + start_group, + start_object, + end_group, + end_object, + ) { + Ok(_) => resp.send(Ok(())).unwrap(), + Err(err) => { + tracing::error!("set_subscriber_subscription: err: {:?}", err.to_string()); + resp.send(Err(anyhow!(err))).unwrap(); + continue; + } + } + } + SetPublisherSubscription { + publisher_session_id, + track_namespace, + track_name, + subscriber_priority, + group_order, + filter_type, + start_group, + start_object, + end_group, + end_object, + resp, + } => { + // Return an error if the publisher does not exist + let consumer = match consumers.get_mut(&publisher_session_id) { + Some(consumer) => consumer, + None => { + let msg = "publisher not found"; + tracing::error!(msg); + resp.send(Err(anyhow!(msg))).unwrap(); + continue; + } + }; + + let (subscribe_id, track_alias) = + match consumer.find_unused_subscribe_id_and_track_alias() { + Ok(result) => result, + Err(err) => { + tracing::error!( + "find_unused_subscribe_id_and_track_alias: err: {:?}", + err.to_string() + ); + resp.send(Err(anyhow!(err))).unwrap(); + continue; + } + }; + match consumer.set_subscription( + subscribe_id, + track_alias, + track_namespace, + track_name, + subscriber_priority, + group_order, + filter_type, + start_group, + start_object, + end_group, + end_object, + ) { + Ok(_) => resp.send(Ok((subscribe_id, track_alias))).unwrap(), + Err(err) => { + tracing::error!("set_subscription: err: {:?}", err.to_string()); + resp.send(Err(anyhow!(err))).unwrap(); + continue; + } + }; + } + RegisterPubSubRelation { + publisher_session_id, + publisher_subscribe_id, + subscriber_session_id, + subscriber_subscribe_id, + resp, + } => { + let result = pubsub_relation.add_relation( + publisher_session_id, + publisher_subscribe_id, + subscriber_session_id, + subscriber_subscribe_id, + ); + + match result { + Ok(_) => resp.send(Ok(())).unwrap(), + Err(err) => { + tracing::error!("add_relation: err: {:?}", err.to_string()); + resp.send(Err(anyhow!(err))).unwrap(); + } + } + } + ActivateSubscriberSubscription { + subscriber_session_id, + subscribe_id, + resp, + } => { + // Return an error if the subscriber does not exist + let producer = match producers.get_mut(&subscriber_session_id) { + Some(producer) => producer, + None => { + let msg = "subscriber not found"; + tracing::error!(msg); + resp.send(Err(anyhow!(msg))).unwrap(); + continue; + } + }; + + match producer.activate_subscription(subscribe_id) { + Ok(activation_occured) => { + // Return bool as a activation is occurred or not + resp.send(Ok(activation_occured)).unwrap() + } + Err(err) => { + tracing::error!("activate_subscription: err: {:?}", err.to_string()); + resp.send(Err(anyhow!(err))).unwrap(); + } + } + } + ActivatePublisherSubscription { + publisher_session_id, + subscribe_id, + resp, + } => { + // Return an error if the publisher does not exist + let consumer = match consumers.get_mut(&publisher_session_id) { + Some(consumer) => consumer, + None => { + let msg = "publisher not found"; + tracing::error!(msg); + resp.send(Err(anyhow!(msg))).unwrap(); + continue; + } + }; + + match consumer.activate_subscription(subscribe_id) { + Ok(activation_occured) => { + // Return bool as a activation is occurred or not + resp.send(Ok(activation_occured)).unwrap() + } + Err(err) => { + tracing::error!("activate_subscription: err: {:?}", err.to_string()); + resp.send(Err(anyhow!(err))).unwrap(); + } + } + } + DeletePublisherAnnouncedNamespace { + track_namespace, + publisher_session_id, + resp, + } => { + // Return an error if the publisher does not exist + let consumer = match consumers.get_mut(&publisher_session_id) { + Some(consumer) => consumer, + None => { + let msg = "publisher not found"; + tracing::error!(msg); + resp.send(Err(anyhow!(msg))).unwrap(); + continue; + } + }; + + // Return false if the track_namespace does not exist + if !consumer.has_namespace(track_namespace.clone()) { + let msg = "track_namespace not found"; + tracing::error!(msg); + resp.send(Ok(false)).unwrap(); + continue; + } + + match consumer.delete_namespace(track_namespace) { + Ok(_) => { + resp.send(Ok(true)).unwrap(); + } + Err(err) => { + tracing::error!("delete_namespace: err: {:?}", err.to_string()); + resp.send(Err(err)).unwrap(); + } + } + } + DeleteClient { session_id, resp } => { + let is_exist_as_consumer = consumers.contains_key(&session_id); + let is_exist_as_producer = producers.contains_key(&session_id); + let is_exist_in_pubsub_relation = pubsub_relation + .records + .iter() + .any(|(key, _)| key.0 == session_id); + + // Return an error if the session does not exist + if !is_exist_as_consumer && !is_exist_as_producer && !is_exist_in_pubsub_relation { + let msg = "session not found"; + tracing::error!(msg); + resp.send(Ok(false)).unwrap(); + continue; + } + + // Delete as a publisher + consumers.remove(&session_id); + pubsub_relation.records.retain(|key, _| key.0 != session_id); + + // Delete as a subscriber + producers.remove(&session_id); + pubsub_relation + .records + .iter_mut() + .for_each(|(_, subscribers)| { + subscribers.retain(|(subscriber_session_id, _)| { + *subscriber_session_id != session_id + }); + }); + + resp.send(Ok(true)).unwrap(); + } + #[cfg(test)] + GetNodeAndRelationClone { resp } => { + let consumer = consumers.clone(); + let producer = producers.clone(); + let relation = pubsub_relation.clone(); + + resp.send(Ok((consumer, producer, relation))).unwrap(); + } + } + } + + tracing::trace!("pubsub_relation_manager end"); +} diff --git a/moqt-server/src/modules/relation_manager/relation.rs b/moqt-server/src/modules/relation_manager/relation.rs new file mode 100644 index 00000000..16464744 --- /dev/null +++ b/moqt-server/src/modules/relation_manager/relation.rs @@ -0,0 +1,59 @@ +use anyhow::Result; +use std::collections::HashMap; + +type SubscriberSessionId = usize; +type PublisherSessionId = usize; +type PublisherSubscribeId = u64; +type SubscriberSubscribeId = u64; + +#[derive(Debug, Clone)] +pub(crate) struct PubSubRelation { + pub(crate) records: HashMap< + (PublisherSessionId, PublisherSubscribeId), + Vec<(SubscriberSessionId, SubscriberSubscribeId)>, + >, +} + +impl PubSubRelation { + pub(crate) fn new() -> Self { + Self { + records: HashMap::new(), + } + } + + pub(crate) fn add_relation( + &mut self, + publisher_session_id: PublisherSessionId, + publisher_subscribe_id: PublisherSubscribeId, + subscriber_session_id: SubscriberSessionId, + subscriber_subscribe_id: SubscriberSubscribeId, + ) -> Result<()> { + let key = (publisher_session_id, publisher_subscribe_id); + let value = (subscriber_session_id, subscriber_subscribe_id); + + match self.records.get_mut(&key) { + // If the key exists, add the value to the existing vector + Some(subscribers) => { + subscribers.push(value); + } + // If the key does not exist, create a new vector and insert the value + None => { + self.records.insert(key, vec![value]); + } + } + + Ok(()) + } + + pub(crate) fn get_subscribers( + &self, + publisher_session_id: PublisherSessionId, + publisher_subscribe_id: PublisherSubscribeId, + ) -> Option<&Vec<(SubscriberSessionId, SubscriberSubscribeId)>> { + let key = (publisher_session_id, publisher_subscribe_id); + self.records.get(&key) + } + + // TODO: Define the behavior if the last subscriber unsubscribes from the track + // fn delete_relation +} diff --git a/moqt-server/src/modules/server_processes/announce_message.rs b/moqt-server/src/modules/server_processes/announce_message.rs index 777ac582..d44872aa 100644 --- a/moqt-server/src/modules/server_processes/announce_message.rs +++ b/moqt-server/src/modules/server_processes/announce_message.rs @@ -1,16 +1,17 @@ use crate::modules::handlers::announce_handler::{announce_handler, AnnounceResponse}; use anyhow::{bail, Result}; use bytes::BytesMut; +use moqt_core::pubsub_relation_manager_repository::PubSubRelationManagerRepository; use moqt_core::{ messages::{control_messages::announce::Announce, moqt_payload::MOQTPayload}, - MOQTClient, TrackNamespaceManagerRepository, + MOQTClient, }; pub(crate) async fn process_announce_message( payload_buf: &mut BytesMut, client: &mut MOQTClient, write_buf: &mut BytesMut, - track_namespace_manager_repository: &mut dyn TrackNamespaceManagerRepository, + pubsub_relation_manager_repository: &mut dyn PubSubRelationManagerRepository, ) -> Result { let announce_message = match Announce::depacketize(payload_buf) { Ok(announce_message) => announce_message, @@ -21,7 +22,7 @@ pub(crate) async fn process_announce_message( }; let announce_response = - announce_handler(announce_message, client, track_namespace_manager_repository).await; + announce_handler(announce_message, client, pubsub_relation_manager_repository).await; match announce_response { Ok(announce_response_message) => match announce_response_message { diff --git a/moqt-server/src/modules/server_processes/client_setup_message.rs b/moqt-server/src/modules/server_processes/client_setup_message.rs index 979fb616..1e27e408 100644 --- a/moqt-server/src/modules/server_processes/client_setup_message.rs +++ b/moqt-server/src/modules/server_processes/client_setup_message.rs @@ -2,10 +2,11 @@ use bytes::BytesMut; use crate::modules::handlers::server_setup_handler::setup_handler; use anyhow::{bail, Result}; +use moqt_core::pubsub_relation_manager_repository::PubSubRelationManagerRepository; use moqt_core::{ constants::UnderlayType, messages::{control_messages::client_setup::ClientSetup, moqt_payload::MOQTPayload}, - MOQTClient, TrackNamespaceManagerRepository, + MOQTClient, }; pub(crate) async fn process_client_setup_message( @@ -13,7 +14,7 @@ pub(crate) async fn process_client_setup_message( client: &mut MOQTClient, underlay_type: UnderlayType, write_buf: &mut BytesMut, - track_namespace_manager_repository: &mut dyn TrackNamespaceManagerRepository, + pubsub_relation_manager_repository: &mut dyn PubSubRelationManagerRepository, ) -> Result<()> { let client_setup_message = match ClientSetup::depacketize(payload_buf) { Ok(client_setup_message) => client_setup_message, @@ -27,7 +28,7 @@ pub(crate) async fn process_client_setup_message( client_setup_message, underlay_type, client, - track_namespace_manager_repository, + pubsub_relation_manager_repository, ) .await?; diff --git a/moqt-server/src/modules/server_processes/subscribe_message.rs b/moqt-server/src/modules/server_processes/subscribe_message.rs index 0736b445..91dabd92 100644 --- a/moqt-server/src/modules/server_processes/subscribe_message.rs +++ b/moqt-server/src/modules/server_processes/subscribe_message.rs @@ -1,19 +1,20 @@ use crate::modules::handlers::subscribe_handler::subscribe_handler; use anyhow::{bail, Result}; use bytes::BytesMut; +use moqt_core::pubsub_relation_manager_repository::PubSubRelationManagerRepository; use moqt_core::{ messages::{ control_messages::{subscribe::Subscribe, subscribe_ok::SubscribeOk}, moqt_payload::MOQTPayload, }, - MOQTClient, SendStreamDispatcherRepository, TrackNamespaceManagerRepository, + MOQTClient, SendStreamDispatcherRepository, }; pub(crate) async fn process_subscribe_message( payload_buf: &mut BytesMut, client: &mut MOQTClient, write_buf: &mut BytesMut, - track_namespace_manager_repository: &mut dyn TrackNamespaceManagerRepository, + pubsub_relation_manager_repository: &mut dyn PubSubRelationManagerRepository, send_stream_dispatcher_repository: &mut dyn SendStreamDispatcherRepository, ) -> Result> { let subscribe_request_message = match Subscribe::depacketize(payload_buf) { @@ -27,7 +28,7 @@ pub(crate) async fn process_subscribe_message( let result = subscribe_handler( subscribe_request_message, client, - track_namespace_manager_repository, + pubsub_relation_manager_repository, send_stream_dispatcher_repository, ) .await; diff --git a/moqt-server/src/modules/server_processes/subscribe_ok_message.rs b/moqt-server/src/modules/server_processes/subscribe_ok_message.rs index 0f14f5e7..a7ad8645 100644 --- a/moqt-server/src/modules/server_processes/subscribe_ok_message.rs +++ b/moqt-server/src/modules/server_processes/subscribe_ok_message.rs @@ -1,14 +1,15 @@ use crate::modules::handlers::subscribe_ok_handler::subscribe_ok_handler; use anyhow::{bail, Result}; use bytes::BytesMut; +use moqt_core::pubsub_relation_manager_repository::PubSubRelationManagerRepository; use moqt_core::{ messages::{control_messages::subscribe_ok::SubscribeOk, moqt_payload::MOQTPayload}, - MOQTClient, SendStreamDispatcherRepository, TrackNamespaceManagerRepository, + MOQTClient, SendStreamDispatcherRepository, }; pub(crate) async fn process_subscribe_ok_message( payload_buf: &mut BytesMut, - track_namespace_manager_repository: &mut dyn TrackNamespaceManagerRepository, + pubsub_relation_manager_repository: &mut dyn PubSubRelationManagerRepository, send_stream_dispatcher_repository: &mut dyn SendStreamDispatcherRepository, client: &mut MOQTClient, ) -> Result<()> { @@ -22,7 +23,7 @@ pub(crate) async fn process_subscribe_ok_message( subscribe_ok_handler( subscribe_ok_message, - track_namespace_manager_repository, + pubsub_relation_manager_repository, send_stream_dispatcher_repository, client, ) From 0ef738ccbeef41e99803c80414f3f2045acb252b Mon Sep 17 00:00:00 2001 From: tetta maeda Date: Mon, 7 Oct 2024 23:00:01 +0900 Subject: [PATCH 09/17] refactor: rename track command --- moqt-server/src/lib.rs | 9 +- .../src/modules/control_message_handler.rs | 5 +- .../src/modules/handlers/announce_handler.rs | 8 +- .../modules/handlers/server_setup_handler.rs | 18 +-- .../src/modules/handlers/subscribe_handler.rs | 18 +-- .../modules/handlers/subscribe_ok_handler.rs | 14 +-- .../src/modules/relation_manager/commands.rs | 2 +- .../src/modules/relation_manager/interface.rs | 106 +++++++++--------- .../src/modules/relation_manager/manager.rs | 4 +- 9 files changed, 93 insertions(+), 91 deletions(-) diff --git a/moqt-server/src/lib.rs b/moqt-server/src/lib.rs index 782c8bbc..55cde50f 100644 --- a/moqt-server/src/lib.rs +++ b/moqt-server/src/lib.rs @@ -3,7 +3,7 @@ use crate::modules::{ buffer_manager, buffer_manager::{buffer_manager, BufferCommand}, control_message_handler::*, - relation_manager::{commands::TrackCommand, manager::pubsub_relation_manager}, + relation_manager::{commands::PubSubRelationCommand, manager::pubsub_relation_manager}, send_stream_dispatcher::{send_stream_dispatcher, SendStreamDispatchCommand}, }; use anyhow::{bail, Context, Ok, Result}; @@ -103,7 +103,8 @@ impl MOQT { // For buffer management for each stream let (buffer_tx, mut buffer_rx) = mpsc::channel::(1024); // For track management - let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + let (track_namespace_tx, mut track_namespace_rx) = + mpsc::channel::(1024); // For relay handler management let (send_stream_tx, mut send_stream_rx) = mpsc::channel::(1024); @@ -167,7 +168,7 @@ impl MOQT { async fn handle_connection( buffer_tx: mpsc::Sender, - track_namespace_tx: mpsc::Sender, + track_namespace_tx: mpsc::Sender, send_stream_tx: mpsc::Sender, incoming_session: IncomingSession, ) -> Result<()> { @@ -316,7 +317,7 @@ async fn handle_incoming_bi_stream( stream: &mut BiStream, client: Arc>, buffer_tx: Sender, - track_namespace_tx: Sender, + track_namespace_tx: Sender, close_tx: Sender<(u64, String)>, send_stream_tx: Sender, ) -> Result<()> { diff --git a/moqt-server/src/modules/control_message_handler.rs b/moqt-server/src/modules/control_message_handler.rs index 8ddd96fe..ddd2d5fa 100644 --- a/moqt-server/src/modules/control_message_handler.rs +++ b/moqt-server/src/modules/control_message_handler.rs @@ -314,7 +314,7 @@ pub(crate) mod test_utils { use crate::modules::control_message_handler::control_message_handler; use crate::modules::control_message_handler::MessageProcessResult; use crate::modules::relation_manager::{ - commands::TrackCommand, interface::PubSubRelationManagerInterface, + commands::PubSubRelationCommand, interface::PubSubRelationManagerInterface, manager::pubsub_relation_manager, }; use crate::modules::send_stream_dispatcher::{ @@ -341,7 +341,8 @@ pub(crate) mod test_utils { client.update_status(client_status); // Generate PubSubRelationManagerInterface - let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + let (track_namespace_tx, mut track_namespace_rx) = + mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); diff --git a/moqt-server/src/modules/handlers/announce_handler.rs b/moqt-server/src/modules/handlers/announce_handler.rs index 50bd5069..71fa3e93 100644 --- a/moqt-server/src/modules/handlers/announce_handler.rs +++ b/moqt-server/src/modules/handlers/announce_handler.rs @@ -54,7 +54,7 @@ pub(crate) async fn announce_handler( mod success { use crate::modules::handlers::announce_handler::{announce_handler, AnnounceResponse}; use crate::modules::relation_manager::{ - commands::TrackCommand, interface::PubSubRelationManagerInterface, + commands::PubSubRelationCommand, interface::PubSubRelationManagerInterface, manager::pubsub_relation_manager, }; use moqt_core::messages::moqt_payload::MOQTPayload; @@ -89,7 +89,7 @@ mod success { let mut client = MOQTClient::new(publisher_session_id); // Generate PubSubRelationManagerInterface - let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); @@ -115,7 +115,7 @@ mod success { mod failure { use crate::modules::handlers::announce_handler::{announce_handler, AnnounceResponse}; use crate::modules::relation_manager::{ - commands::TrackCommand, interface::PubSubRelationManagerInterface, + commands::PubSubRelationCommand, interface::PubSubRelationManagerInterface, manager::pubsub_relation_manager, }; use moqt_core::messages::moqt_payload::MOQTPayload; @@ -150,7 +150,7 @@ mod failure { let mut client = MOQTClient::new(publisher_session_id); // Generate PubSubRelationManagerInterface - let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); diff --git a/moqt-server/src/modules/handlers/server_setup_handler.rs b/moqt-server/src/modules/handlers/server_setup_handler.rs index 20b4b3b9..6c754533 100644 --- a/moqt-server/src/modules/handlers/server_setup_handler.rs +++ b/moqt-server/src/modules/handlers/server_setup_handler.rs @@ -88,7 +88,7 @@ pub(crate) async fn setup_handler( #[cfg(test)] mod success { use crate::modules::relation_manager::{ - commands::TrackCommand, interface::PubSubRelationManagerInterface, + commands::PubSubRelationCommand, interface::PubSubRelationManagerInterface, manager::pubsub_relation_manager, }; use crate::{constants, modules::handlers::server_setup_handler::setup_handler}; @@ -109,7 +109,7 @@ mod success { let underlay_type = crate::constants::UnderlayType::WebTransport; // Generate PubSubRelationManagerInterface - let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); @@ -138,7 +138,7 @@ mod success { let underlay_type = crate::constants::UnderlayType::QUIC; // Generate PubSubRelationManagerInterface - let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); @@ -159,7 +159,7 @@ mod success { #[cfg(test)] mod failure { use crate::modules::relation_manager::{ - commands::TrackCommand, interface::PubSubRelationManagerInterface, + commands::PubSubRelationCommand, interface::PubSubRelationManagerInterface, manager::pubsub_relation_manager, }; use crate::{constants, modules::handlers::server_setup_handler::setup_handler}; @@ -180,7 +180,7 @@ mod failure { let underlay_type = crate::constants::UnderlayType::WebTransport; // Generate PubSubRelationManagerInterface - let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); @@ -205,7 +205,7 @@ mod failure { let underlay_type = crate::constants::UnderlayType::WebTransport; // Generate PubSubRelationManagerInterface - let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); @@ -230,7 +230,7 @@ mod failure { let underlay_type = crate::constants::UnderlayType::QUIC; // Generate PubSubRelationManagerInterface - let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); @@ -256,7 +256,7 @@ mod failure { let underlay_type = crate::constants::UnderlayType::WebTransport; // Generate PubSubRelationManagerInterface - let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); @@ -285,7 +285,7 @@ mod failure { let underlay_type = crate::constants::UnderlayType::WebTransport; // Generate PubSubRelationManagerInterface - let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); diff --git a/moqt-server/src/modules/handlers/subscribe_handler.rs b/moqt-server/src/modules/handlers/subscribe_handler.rs index 07f74278..d8dc3e79 100644 --- a/moqt-server/src/modules/handlers/subscribe_handler.rs +++ b/moqt-server/src/modules/handlers/subscribe_handler.rs @@ -290,7 +290,7 @@ async fn set_subscriber_and_publisher_subscription( mod success { use crate::modules::handlers::subscribe_handler::subscribe_handler; use crate::modules::relation_manager::{ - commands::TrackCommand, + commands::PubSubRelationCommand, interface::{test_utils, PubSubRelationManagerInterface}, manager::pubsub_relation_manager, }; @@ -350,7 +350,7 @@ mod success { let mut client = MOQTClient::new(subscriber_session_id); // Generate PubSubRelationManagerInterface - let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); @@ -457,7 +457,7 @@ mod success { let mut client = MOQTClient::new(subscriber_session_id); // Generate PubSubRelationManagerInterface - let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); @@ -543,7 +543,7 @@ mod success { mod failure { use crate::modules::handlers::subscribe_handler::subscribe_handler; use crate::modules::relation_manager::{ - commands::TrackCommand, interface::PubSubRelationManagerInterface, + commands::PubSubRelationCommand, interface::PubSubRelationManagerInterface, manager::pubsub_relation_manager, }; use crate::modules::send_stream_dispatcher::{ @@ -601,7 +601,7 @@ mod failure { let mut client = MOQTClient::new(subscriber_session_id); // Generate PubSubRelationManagerInterface (register subscriber in advance) - let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); @@ -703,7 +703,7 @@ mod failure { let mut client = MOQTClient::new(subscriber_session_id); // Generate PubSubRelationManagerInterface - let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); @@ -780,7 +780,7 @@ mod failure { let mut client = MOQTClient::new(subscriber_session_id); // Generate PubSubRelationManagerInterface (without set publisher) - let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); @@ -865,7 +865,7 @@ mod failure { let mut client = MOQTClient::new(subscriber_session_id); // Generate PubSubRelationManagerInterface - let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); @@ -966,7 +966,7 @@ mod failure { let mut client = MOQTClient::new(subscriber_session_id); // Generate PubSubRelationManagerInterface - let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); diff --git a/moqt-server/src/modules/handlers/subscribe_ok_handler.rs b/moqt-server/src/modules/handlers/subscribe_ok_handler.rs index c6588944..82497b36 100644 --- a/moqt-server/src/modules/handlers/subscribe_ok_handler.rs +++ b/moqt-server/src/modules/handlers/subscribe_ok_handler.rs @@ -79,7 +79,7 @@ pub(crate) async fn subscribe_ok_handler( mod success { use crate::modules::handlers::subscribe_ok_handler::subscribe_ok_handler; use crate::modules::relation_manager::{ - commands::TrackCommand, interface::PubSubRelationManagerInterface, + commands::PubSubRelationCommand, interface::PubSubRelationManagerInterface, manager::pubsub_relation_manager, }; use crate::modules::send_stream_dispatcher::{ @@ -137,7 +137,7 @@ mod success { let mut client = MOQTClient::new(publisher_session_id); // Generate PubSubRelationManagerInterface - let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); @@ -228,7 +228,7 @@ mod success { mod failure { use crate::modules::handlers::subscribe_ok_handler::subscribe_ok_handler; use crate::modules::relation_manager::{ - commands::TrackCommand, interface::PubSubRelationManagerInterface, + commands::PubSubRelationCommand, interface::PubSubRelationManagerInterface, manager::pubsub_relation_manager, }; use crate::modules::send_stream_dispatcher::{ @@ -286,7 +286,7 @@ mod failure { let mut client = MOQTClient::new(publisher_session_id); // Generate PubSubRelationManagerInterface - let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); @@ -401,7 +401,7 @@ mod failure { let mut client = MOQTClient::new(publisher_session_id); // Generate PubSubRelationManagerInterface - let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); @@ -498,7 +498,7 @@ mod failure { let mut client = MOQTClient::new(publisher_session_id); // Generate PubSubRelationManagerInterface - let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); @@ -627,7 +627,7 @@ mod failure { let mut client = MOQTClient::new(publisher_session_id); // Generate PubSubRelationManagerInterface - let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); diff --git a/moqt-server/src/modules/relation_manager/commands.rs b/moqt-server/src/modules/relation_manager/commands.rs index cae5e977..7391d253 100644 --- a/moqt-server/src/modules/relation_manager/commands.rs +++ b/moqt-server/src/modules/relation_manager/commands.rs @@ -10,7 +10,7 @@ use crate::modules::relation_manager::{ }; #[derive(Debug)] -pub(crate) enum TrackCommand { +pub(crate) enum PubSubRelationCommand { SetupPublisher { max_subscribe_id: u64, publisher_session_id: usize, diff --git a/moqt-server/src/modules/relation_manager/interface.rs b/moqt-server/src/modules/relation_manager/interface.rs index a7ff211f..ab3b50fe 100644 --- a/moqt-server/src/modules/relation_manager/interface.rs +++ b/moqt-server/src/modules/relation_manager/interface.rs @@ -1,4 +1,4 @@ -use crate::modules::relation_manager::commands::{TrackCommand, TrackCommand::*}; +use crate::modules::relation_manager::commands::{PubSubRelationCommand, PubSubRelationCommand::*}; use anyhow::{bail, Result}; use async_trait::async_trait; use moqt_core::messages::control_messages::subscribe::{FilterType, GroupOrder}; @@ -8,11 +8,11 @@ use tokio::sync::{mpsc, oneshot}; // Wrapper to encapsulate channel-related operations pub(crate) struct PubSubRelationManagerInterface { - tx: mpsc::Sender, + tx: mpsc::Sender, } impl PubSubRelationManagerInterface { - pub fn new(tx: mpsc::Sender) -> Self { + pub fn new(tx: mpsc::Sender) -> Self { Self { tx } } } @@ -26,7 +26,7 @@ impl PubSubRelationManagerRepository for PubSubRelationManagerInterface { ) -> Result<()> { let (resp_tx, resp_rx) = oneshot::channel::>(); - let cmd = TrackCommand::SetupPublisher { + let cmd = PubSubRelationCommand::SetupPublisher { max_subscribe_id, publisher_session_id, resp: resp_tx, @@ -47,7 +47,7 @@ impl PubSubRelationManagerRepository for PubSubRelationManagerInterface { ) -> Result<()> { let (resp_tx, resp_rx) = oneshot::channel::>(); - let cmd = TrackCommand::SetPublisherAnnouncedNamespace { + let cmd = PubSubRelationCommand::SetPublisherAnnouncedNamespace { track_namespace, publisher_session_id, resp: resp_tx, @@ -68,7 +68,7 @@ impl PubSubRelationManagerRepository for PubSubRelationManagerInterface { ) -> Result<()> { let (resp_tx, resp_rx) = oneshot::channel::>(); - let cmd = TrackCommand::SetupSubscriber { + let cmd = PubSubRelationCommand::SetupSubscriber { max_subscribe_id, subscriber_session_id, resp: resp_tx, @@ -88,7 +88,7 @@ impl PubSubRelationManagerRepository for PubSubRelationManagerInterface { subscriber_session_id: usize, ) -> Result { let (resp_tx, resp_rx) = oneshot::channel::>(); - let cmd = TrackCommand::IsValidSubscriberSubscribeId { + let cmd = PubSubRelationCommand::IsValidSubscriberSubscribeId { subscribe_id, subscriber_session_id, resp: resp_tx, @@ -108,7 +108,7 @@ impl PubSubRelationManagerRepository for PubSubRelationManagerInterface { subscriber_session_id: usize, ) -> Result { let (resp_tx, resp_rx) = oneshot::channel::>(); - let cmd = TrackCommand::IsValidSubscriberTrackAlias { + let cmd = PubSubRelationCommand::IsValidSubscriberTrackAlias { track_alias, subscriber_session_id, resp: resp_tx, @@ -128,7 +128,7 @@ impl PubSubRelationManagerRepository for PubSubRelationManagerInterface { track_name: String, ) -> Result { let (resp_tx, resp_rx) = oneshot::channel::>(); - let cmd = TrackCommand::IsTrackExisting { + let cmd = PubSubRelationCommand::IsTrackExisting { track_namespace, track_name, resp: resp_tx, @@ -148,7 +148,7 @@ impl PubSubRelationManagerRepository for PubSubRelationManagerInterface { track_name: String, ) -> Result> { let (resp_tx, resp_rx) = oneshot::channel::>>(); - let cmd = TrackCommand::GetPublisherSubscription { + let cmd = PubSubRelationCommand::GetPublisherSubscription { track_namespace, track_name, resp: resp_tx, @@ -167,7 +167,7 @@ impl PubSubRelationManagerRepository for PubSubRelationManagerInterface { track_namespace: Vec, ) -> Result> { let (resp_tx, resp_rx) = oneshot::channel::>>(); - let cmd = TrackCommand::GetPublisherSessionId { + let cmd = PubSubRelationCommand::GetPublisherSessionId { track_namespace, resp: resp_tx, }; @@ -186,7 +186,7 @@ impl PubSubRelationManagerRepository for PubSubRelationManagerInterface { publisher_session_id: usize, ) -> Result>> { let (resp_tx, resp_rx) = oneshot::channel::>>>(); - let cmd = TrackCommand::GetRequestingSubscriberSessionIdsAndSubscribeIds { + let cmd = PubSubRelationCommand::GetRequestingSubscriberSessionIdsAndSubscribeIds { publisher_subscribe_id, publisher_session_id, resp: resp_tx, @@ -207,7 +207,7 @@ impl PubSubRelationManagerRepository for PubSubRelationManagerInterface { publisher_session_id: usize, ) -> Result> { let (resp_tx, resp_rx) = oneshot::channel::>>(); - let cmd = TrackCommand::GetPublisherSubscribeId { + let cmd = PubSubRelationCommand::GetPublisherSubscribeId { track_namespace, track_name, publisher_session_id, @@ -238,7 +238,7 @@ impl PubSubRelationManagerRepository for PubSubRelationManagerInterface { end_object: Option, ) -> Result<()> { let (resp_tx, resp_rx) = oneshot::channel::>(); - let cmd = TrackCommand::SetSubscriberSubscription { + let cmd = PubSubRelationCommand::SetSubscriberSubscription { subscriber_session_id, subscribe_id, track_alias, @@ -277,7 +277,7 @@ impl PubSubRelationManagerRepository for PubSubRelationManagerInterface { end_object: Option, ) -> Result<(u64, u64)> { let (resp_tx, resp_rx) = oneshot::channel::>(); - let cmd = TrackCommand::SetPublisherSubscription { + let cmd = PubSubRelationCommand::SetPublisherSubscription { publisher_session_id, track_namespace, track_name, @@ -399,7 +399,7 @@ impl PubSubRelationManagerRepository for PubSubRelationManagerInterface { #[cfg(test)] pub(crate) mod test_utils { use crate::modules::relation_manager::{ - commands::TrackCommand, + commands::PubSubRelationCommand, interface::PubSubRelationManagerInterface, manager::{Consumers, Producers}, relation::PubSubRelation, @@ -412,7 +412,7 @@ pub(crate) mod test_utils { pubsub_relation_manager: &PubSubRelationManagerInterface, ) -> (Consumers, Producers, PubSubRelation) { let (resp_tx, resp_rx) = oneshot::channel::>(); - let cmd = TrackCommand::GetNodeAndRelationClone { resp: resp_tx }; + let cmd = PubSubRelationCommand::GetNodeAndRelationClone { resp: resp_tx }; pubsub_relation_manager.tx.send(cmd).await.unwrap(); resp_rx.await.unwrap().unwrap() @@ -422,7 +422,7 @@ pub(crate) mod test_utils { #[cfg(test)] mod success { use crate::modules::relation_manager::{ - commands::TrackCommand, interface::test_utils, interface::PubSubRelationManagerInterface, + commands::PubSubRelationCommand, interface::test_utils, interface::PubSubRelationManagerInterface, manager::pubsub_relation_manager, }; use moqt_core::messages::control_messages::subscribe::{FilterType, GroupOrder}; @@ -437,7 +437,7 @@ mod success { let publisher_session_id = 1; // Start track management thread - let (track_tx, mut track_rx) = mpsc::channel::(1024); + let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); @@ -461,7 +461,7 @@ mod success { let publisher_session_id = 1; // Start track management thread - let (track_tx, mut track_rx) = mpsc::channel::(1024); + let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); @@ -490,7 +490,7 @@ mod success { let subscriber_session_id = 1; // Start track management thread - let (track_tx, mut track_rx) = mpsc::channel::(1024); + let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); @@ -514,7 +514,7 @@ mod success { let subscriber_session_id = 1; // Start track management thread - let (track_tx, mut track_rx) = mpsc::channel::(1024); + let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); @@ -537,7 +537,7 @@ mod success { let subscriber_session_id = 1; // Start track management thread - let (track_tx, mut track_rx) = mpsc::channel::(1024); + let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); @@ -560,7 +560,7 @@ mod success { let subscriber_session_id = 1; // Start track management thread - let (track_tx, mut track_rx) = mpsc::channel::(1024); + let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); @@ -593,7 +593,7 @@ mod success { let end_object = None; // Start track management thread - let (track_tx, mut track_rx) = mpsc::channel::(1024); + let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); @@ -641,7 +641,7 @@ mod success { let end_object = None; // Start track management thread - let (track_tx, mut track_rx) = mpsc::channel::(1024); + let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); @@ -684,7 +684,7 @@ mod success { let track_name = "test_name".to_string(); // Start track management thread - let (track_tx, mut track_rx) = mpsc::channel::(1024); + let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); @@ -713,7 +713,7 @@ mod success { let end_object = None; // Start track management thread - let (track_tx, mut track_rx) = mpsc::channel::(1024); + let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); @@ -768,7 +768,7 @@ mod success { let publisher_session_id = 1; // Start track management thread - let (track_tx, mut track_rx) = mpsc::channel::(1024); + let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); @@ -807,7 +807,7 @@ mod success { let end_object = None; // Start track management thread - let (track_tx, mut track_rx) = mpsc::channel::(1024); + let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); @@ -896,7 +896,7 @@ mod success { let end_object = None; // Start track management thread - let (track_tx, mut track_rx) = mpsc::channel::(1024); + let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); @@ -949,7 +949,7 @@ mod success { let end_object = None; // Start track management thread - let (track_tx, mut track_rx) = mpsc::channel::(1024); + let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); @@ -1013,7 +1013,7 @@ mod success { let end_object = None; // Start track management thread - let (track_tx, mut track_rx) = mpsc::channel::(1024); + let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); @@ -1083,7 +1083,7 @@ mod success { let end_object = None; // Start track management thread - let (track_tx, mut track_rx) = mpsc::channel::(1024); + let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); @@ -1177,7 +1177,7 @@ mod success { let end_object = None; // Start track management thread - let (track_tx, mut track_rx) = mpsc::channel::(1024); + let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); @@ -1232,7 +1232,7 @@ mod success { let end_object = None; // Start track management thread - let (track_tx, mut track_rx) = mpsc::channel::(1024); + let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); @@ -1281,7 +1281,7 @@ mod success { let publisher_session_id = 1; // Start track management thread - let (track_tx, mut track_rx) = mpsc::channel::(1024); + let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); @@ -1316,7 +1316,7 @@ mod success { let publisher_session_id = 1; // Start track management thread - let (track_tx, mut track_rx) = mpsc::channel::(1024); + let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); @@ -1355,7 +1355,7 @@ mod success { let end_object = None; // Start track management thread - let (track_tx, mut track_rx) = mpsc::channel::(1024); + let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); @@ -1530,7 +1530,7 @@ mod success { let session_id = 1; // Start track management thread - let (track_tx, mut track_rx) = mpsc::channel::(1024); + let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); @@ -1546,7 +1546,7 @@ mod success { #[cfg(test)] mod failure { use crate::modules::relation_manager::{ - commands::TrackCommand, interface::PubSubRelationManagerInterface, + commands::PubSubRelationCommand, interface::PubSubRelationManagerInterface, manager::pubsub_relation_manager, }; use moqt_core::messages::control_messages::subscribe::{FilterType, GroupOrder}; @@ -1559,7 +1559,7 @@ mod failure { let publisher_session_id = 1; // Start track management thread - let (track_tx, mut track_rx) = mpsc::channel::(1024); + let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); @@ -1582,7 +1582,7 @@ mod failure { let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); // Start track management thread - let (track_tx, mut track_rx) = mpsc::channel::(1024); + let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); @@ -1607,7 +1607,7 @@ mod failure { let subscriber_session_id = 1; // Start track management thread - let (track_tx, mut track_rx) = mpsc::channel::(1024); + let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); @@ -1631,7 +1631,7 @@ mod failure { let invalid_subscriber_session_id = 2; // Start track management thread - let (track_tx, mut track_rx) = mpsc::channel::(1024); + let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); @@ -1657,7 +1657,7 @@ mod failure { let invalid_subscriber_session_id = 2; // Start track management thread - let (track_tx, mut track_rx) = mpsc::channel::(1024); + let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); @@ -1677,7 +1677,7 @@ mod failure { let publisher_session_id = 0; let publisher_subscribe_id = 0; // Start track management thread - let (track_tx, mut track_rx) = mpsc::channel::(1024); + let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); @@ -1699,7 +1699,7 @@ mod failure { let invalid_publisher_session_id = 1; // Start track management thread - let (track_tx, mut track_rx) = mpsc::channel::(1024); + let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); @@ -1729,7 +1729,7 @@ mod failure { let invalid_subscriber_session_id = 2; // Start track management thread - let (track_tx, mut track_rx) = mpsc::channel::(1024); + let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); @@ -1774,7 +1774,7 @@ mod failure { let invalid_publisher_session_id = 2; // Start track management thread - let (track_tx, mut track_rx) = mpsc::channel::(1024); + let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); @@ -1809,7 +1809,7 @@ mod failure { let invalid_subscriber_session_id = 2; // Start track management thread - let (track_tx, mut track_rx) = mpsc::channel::(1024); + let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); @@ -1832,7 +1832,7 @@ mod failure { let invalid_publisher_session_id = 2; // Start track management thread - let (track_tx, mut track_rx) = mpsc::channel::(1024); + let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); @@ -1855,7 +1855,7 @@ mod failure { let invalid_publisher_session_id = 2; // Start track management thread - let (track_tx, mut track_rx) = mpsc::channel::(1024); + let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); diff --git a/moqt-server/src/modules/relation_manager/manager.rs b/moqt-server/src/modules/relation_manager/manager.rs index a138af21..da9f119e 100644 --- a/moqt-server/src/modules/relation_manager/manager.rs +++ b/moqt-server/src/modules/relation_manager/manager.rs @@ -1,5 +1,5 @@ use crate::modules::relation_manager::{ - commands::{TrackCommand, TrackCommand::*}, + commands::{PubSubRelationCommand, PubSubRelationCommand::*}, relation::PubSubRelation, }; use anyhow::anyhow; @@ -17,7 +17,7 @@ pub(crate) type Producers = HashMap; // [Original Publisher: (Producer) ] -> [Relay: (Consumer) - - (Producer) ] -> [End Subscriber: (Consumer)] // Called as a separate thread -pub(crate) async fn pubsub_relation_manager(rx: &mut mpsc::Receiver) { +pub(crate) async fn pubsub_relation_manager(rx: &mut mpsc::Receiver) { tracing::trace!("pubsub_relation_manager start"); let mut consumers: Consumers = HashMap::new(); From b0388bbaad8a420f21d470cbdaa53e03654ccf21 Mon Sep 17 00:00:00 2001 From: tetta maeda Date: Mon, 7 Oct 2024 23:10:51 +0900 Subject: [PATCH 10/17] refactor: fix format --- .../src/modules/handlers/announce_handler.rs | 6 ++++-- .../modules/handlers/server_setup_handler.rs | 21 ++++++++++++------- .../src/modules/handlers/subscribe_handler.rs | 21 ++++++++++++------- .../modules/handlers/subscribe_ok_handler.rs | 15 ++++++++----- .../src/modules/relation_manager/interface.rs | 4 ++-- 5 files changed, 44 insertions(+), 23 deletions(-) diff --git a/moqt-server/src/modules/handlers/announce_handler.rs b/moqt-server/src/modules/handlers/announce_handler.rs index 71fa3e93..ce533fd5 100644 --- a/moqt-server/src/modules/handlers/announce_handler.rs +++ b/moqt-server/src/modules/handlers/announce_handler.rs @@ -89,7 +89,8 @@ mod success { let mut client = MOQTClient::new(publisher_session_id); // Generate PubSubRelationManagerInterface - let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + let (track_namespace_tx, mut track_namespace_rx) = + mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); @@ -150,7 +151,8 @@ mod failure { let mut client = MOQTClient::new(publisher_session_id); // Generate PubSubRelationManagerInterface - let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + let (track_namespace_tx, mut track_namespace_rx) = + mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); diff --git a/moqt-server/src/modules/handlers/server_setup_handler.rs b/moqt-server/src/modules/handlers/server_setup_handler.rs index 6c754533..2f9d4038 100644 --- a/moqt-server/src/modules/handlers/server_setup_handler.rs +++ b/moqt-server/src/modules/handlers/server_setup_handler.rs @@ -109,7 +109,8 @@ mod success { let underlay_type = crate::constants::UnderlayType::WebTransport; // Generate PubSubRelationManagerInterface - let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + let (track_namespace_tx, mut track_namespace_rx) = + mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); @@ -138,7 +139,8 @@ mod success { let underlay_type = crate::constants::UnderlayType::QUIC; // Generate PubSubRelationManagerInterface - let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + let (track_namespace_tx, mut track_namespace_rx) = + mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); @@ -180,7 +182,8 @@ mod failure { let underlay_type = crate::constants::UnderlayType::WebTransport; // Generate PubSubRelationManagerInterface - let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + let (track_namespace_tx, mut track_namespace_rx) = + mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); @@ -205,7 +208,8 @@ mod failure { let underlay_type = crate::constants::UnderlayType::WebTransport; // Generate PubSubRelationManagerInterface - let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + let (track_namespace_tx, mut track_namespace_rx) = + mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); @@ -230,7 +234,8 @@ mod failure { let underlay_type = crate::constants::UnderlayType::QUIC; // Generate PubSubRelationManagerInterface - let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + let (track_namespace_tx, mut track_namespace_rx) = + mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); @@ -256,7 +261,8 @@ mod failure { let underlay_type = crate::constants::UnderlayType::WebTransport; // Generate PubSubRelationManagerInterface - let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + let (track_namespace_tx, mut track_namespace_rx) = + mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); @@ -285,7 +291,8 @@ mod failure { let underlay_type = crate::constants::UnderlayType::WebTransport; // Generate PubSubRelationManagerInterface - let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + let (track_namespace_tx, mut track_namespace_rx) = + mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); diff --git a/moqt-server/src/modules/handlers/subscribe_handler.rs b/moqt-server/src/modules/handlers/subscribe_handler.rs index d8dc3e79..0a80e7a3 100644 --- a/moqt-server/src/modules/handlers/subscribe_handler.rs +++ b/moqt-server/src/modules/handlers/subscribe_handler.rs @@ -350,7 +350,8 @@ mod success { let mut client = MOQTClient::new(subscriber_session_id); // Generate PubSubRelationManagerInterface - let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + let (track_namespace_tx, mut track_namespace_rx) = + mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); @@ -457,7 +458,8 @@ mod success { let mut client = MOQTClient::new(subscriber_session_id); // Generate PubSubRelationManagerInterface - let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + let (track_namespace_tx, mut track_namespace_rx) = + mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); @@ -601,7 +603,8 @@ mod failure { let mut client = MOQTClient::new(subscriber_session_id); // Generate PubSubRelationManagerInterface (register subscriber in advance) - let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + let (track_namespace_tx, mut track_namespace_rx) = + mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); @@ -703,7 +706,8 @@ mod failure { let mut client = MOQTClient::new(subscriber_session_id); // Generate PubSubRelationManagerInterface - let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + let (track_namespace_tx, mut track_namespace_rx) = + mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); @@ -780,7 +784,8 @@ mod failure { let mut client = MOQTClient::new(subscriber_session_id); // Generate PubSubRelationManagerInterface (without set publisher) - let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + let (track_namespace_tx, mut track_namespace_rx) = + mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); @@ -865,7 +870,8 @@ mod failure { let mut client = MOQTClient::new(subscriber_session_id); // Generate PubSubRelationManagerInterface - let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + let (track_namespace_tx, mut track_namespace_rx) = + mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); @@ -966,7 +972,8 @@ mod failure { let mut client = MOQTClient::new(subscriber_session_id); // Generate PubSubRelationManagerInterface - let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + let (track_namespace_tx, mut track_namespace_rx) = + mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); diff --git a/moqt-server/src/modules/handlers/subscribe_ok_handler.rs b/moqt-server/src/modules/handlers/subscribe_ok_handler.rs index 82497b36..38f3a4b3 100644 --- a/moqt-server/src/modules/handlers/subscribe_ok_handler.rs +++ b/moqt-server/src/modules/handlers/subscribe_ok_handler.rs @@ -137,7 +137,8 @@ mod success { let mut client = MOQTClient::new(publisher_session_id); // Generate PubSubRelationManagerInterface - let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + let (track_namespace_tx, mut track_namespace_rx) = + mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); @@ -286,7 +287,8 @@ mod failure { let mut client = MOQTClient::new(publisher_session_id); // Generate PubSubRelationManagerInterface - let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + let (track_namespace_tx, mut track_namespace_rx) = + mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); @@ -401,7 +403,8 @@ mod failure { let mut client = MOQTClient::new(publisher_session_id); // Generate PubSubRelationManagerInterface - let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + let (track_namespace_tx, mut track_namespace_rx) = + mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); @@ -498,7 +501,8 @@ mod failure { let mut client = MOQTClient::new(publisher_session_id); // Generate PubSubRelationManagerInterface - let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + let (track_namespace_tx, mut track_namespace_rx) = + mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); @@ -627,7 +631,8 @@ mod failure { let mut client = MOQTClient::new(publisher_session_id); // Generate PubSubRelationManagerInterface - let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); + let (track_namespace_tx, mut track_namespace_rx) = + mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); diff --git a/moqt-server/src/modules/relation_manager/interface.rs b/moqt-server/src/modules/relation_manager/interface.rs index ab3b50fe..0e88757b 100644 --- a/moqt-server/src/modules/relation_manager/interface.rs +++ b/moqt-server/src/modules/relation_manager/interface.rs @@ -422,8 +422,8 @@ pub(crate) mod test_utils { #[cfg(test)] mod success { use crate::modules::relation_manager::{ - commands::PubSubRelationCommand, interface::test_utils, interface::PubSubRelationManagerInterface, - manager::pubsub_relation_manager, + commands::PubSubRelationCommand, interface::test_utils, + interface::PubSubRelationManagerInterface, manager::pubsub_relation_manager, }; use moqt_core::messages::control_messages::subscribe::{FilterType, GroupOrder}; use moqt_core::pubsub_relation_manager_repository::PubSubRelationManagerRepository; From 98ae00ec57ed50d9d6022e83b0c9a4cf44098294 Mon Sep 17 00:00:00 2001 From: tetta maeda Date: Tue, 8 Oct 2024 08:53:43 +0900 Subject: [PATCH 11/17] impl: add test --- .../subscription_models/consumer_node.rs | 477 ++++++++++++++++++ .../subscription_models/producer_node.rs | 459 +++++++++++++++++ 2 files changed, 936 insertions(+) diff --git a/moqt-core/src/modules/subscription_models/consumer_node.rs b/moqt-core/src/modules/subscription_models/consumer_node.rs index 6711311b..0ed710b9 100644 --- a/moqt-core/src/modules/subscription_models/consumer_node.rs +++ b/moqt-core/src/modules/subscription_models/consumer_node.rs @@ -212,3 +212,480 @@ impl SubscriptionNodeRegistory for Consumer { Ok(()) } } + +#[cfg(test)] +pub(crate) mod test_utils { + use super::{Consumer, TrackNamespace}; + use crate::subscription_models::consumer_node::{FilterType, GroupOrder}; + + #[derive(Debug, Clone)] + pub(crate) struct SubscriptionUtils { + pub(crate) consumer: Consumer, + pub(crate) subscribe_id: u64, + pub(crate) track_alias: u64, + pub(crate) track_namespace: TrackNamespace, + pub(crate) track_name: String, + pub(crate) subscriber_priority: u8, + pub(crate) group_order: GroupOrder, + pub(crate) filter_type: FilterType, + pub(crate) start_group: Option, + pub(crate) start_object: Option, + pub(crate) end_group: Option, + pub(crate) end_object: Option, + } + + impl SubscriptionUtils { + pub(crate) fn normal_variable(subscribe_id: u64) -> Self { + let consumer = Consumer::new(10); + let track_alias = 0; + let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); + let track_name = "track_name".to_string(); + let subscriber_priority = 0; + let group_order = GroupOrder::Ascending; + let filter_type = FilterType::AbsoluteStart; + let start_group = Some(0); + let start_object = Some(0); + let end_group = None; + let end_object = None; + + SubscriptionUtils { + consumer, + subscribe_id, + track_alias, + track_namespace, + track_name, + subscriber_priority, + group_order, + filter_type, + start_group, + start_object, + end_group, + end_object, + } + } + } +} + +#[cfg(test)] +mod success { + use super::*; + use crate::subscription_models::consumer_node::test_utils::SubscriptionUtils; + use crate::subscription_models::subscriptions::Subscription; + + #[test] + fn set_subscription() { + let subscribe_id = 0; + let mut variables = SubscriptionUtils::normal_variable(subscribe_id); + + let result = variables.consumer.set_subscription( + variables.subscribe_id, + variables.track_alias, + variables.track_namespace, + variables.track_name, + variables.subscriber_priority, + variables.group_order, + variables.filter_type, + variables.start_group, + variables.start_object, + variables.end_group, + variables.end_object, + ); + + assert!(result.is_ok()); + } + + #[test] + fn get_subscription() { + let subscribe_id = 0; + let variables = SubscriptionUtils::normal_variable(subscribe_id); + + let mut variables_clone = variables.clone(); + let _ = variables_clone.consumer.set_subscription( + variables_clone.subscribe_id, + variables_clone.track_alias, + variables_clone.track_namespace, + variables_clone.track_name, + variables_clone.subscriber_priority, + variables_clone.group_order, + variables_clone.filter_type, + variables_clone.start_group, + variables_clone.start_object, + variables_clone.end_group, + variables_clone.end_object, + ); + + let subscription = variables_clone + .consumer + .get_subscription(variables.clone().subscribe_id) + .unwrap(); + + let expected_subscription = Some(Subscription::new( + variables.track_alias, + variables.track_namespace, + variables.track_name, + variables.subscriber_priority, + variables.group_order, + variables.filter_type, + variables.start_group, + variables.start_object, + variables.end_group, + variables.end_object, + None, + )); + + assert_eq!(subscription, expected_subscription); + } + + #[test] + fn get_subscription_by_full_track_name() { + let subscribe_id = 0; + let variables = SubscriptionUtils::normal_variable(subscribe_id); + + let mut variables_clone = variables.clone(); + let _ = variables_clone.consumer.set_subscription( + variables_clone.subscribe_id, + variables_clone.track_alias, + variables_clone.track_namespace, + variables_clone.track_name, + variables_clone.subscriber_priority, + variables_clone.group_order, + variables_clone.filter_type, + variables_clone.start_group, + variables_clone.start_object, + variables_clone.end_group, + variables_clone.end_object, + ); + + let subscription = variables_clone + .consumer + .get_subscription_by_full_track_name( + variables.track_namespace.clone(), + variables.track_name.clone(), + ) + .unwrap(); + + let expected_subscription = Some(Subscription::new( + variables.track_alias, + variables.track_namespace, + variables.track_name, + variables.subscriber_priority, + variables.group_order, + variables.filter_type, + variables.start_group, + variables.start_object, + variables.end_group, + variables.end_object, + None, + )); + + assert_eq!(subscription, expected_subscription); + } + + #[test] + fn get_subscribe_id() { + let subscribe_id = 0; + let mut variables = SubscriptionUtils::normal_variable(subscribe_id); + + let _ = variables.consumer.set_subscription( + variables.subscribe_id, + variables.track_alias, + variables.track_namespace.clone(), + variables.track_name.clone(), + variables.subscriber_priority, + variables.group_order, + variables.filter_type, + variables.start_group, + variables.start_object, + variables.end_group, + variables.end_object, + ); + + let expected_subscribe_id = variables.subscribe_id; + + let result_subscribe_id = variables + .consumer + .get_subscribe_id(variables.track_namespace, variables.track_name) + .unwrap() + .unwrap(); + + assert_eq!(result_subscribe_id, expected_subscribe_id); + } + + #[test] + fn has_track() { + let subscribe_id = 0; + let mut variables = SubscriptionUtils::normal_variable(subscribe_id); + + let _ = variables.consumer.set_subscription( + variables.subscribe_id, + variables.track_alias, + variables.track_namespace.clone(), + variables.track_name.clone(), + variables.subscriber_priority, + variables.group_order, + variables.filter_type, + variables.start_group, + variables.start_object, + variables.end_group, + variables.end_object, + ); + + let result = variables + .consumer + .has_track(variables.track_namespace, variables.track_name); + + assert!(result); + } + + #[test] + fn activate_subscription() { + let subscribe_id = 0; + let mut variables = SubscriptionUtils::normal_variable(subscribe_id); + + let _ = variables.consumer.set_subscription( + variables.subscribe_id, + variables.track_alias, + variables.track_namespace.clone(), + variables.track_name.clone(), + variables.subscriber_priority, + variables.group_order, + variables.filter_type, + variables.start_group, + variables.start_object, + variables.end_group, + variables.end_object, + ); + + let result = variables + .consumer + .activate_subscription(variables.subscribe_id) + .unwrap(); + + assert!(result); + } + + #[test] + fn is_requesting() { + let subscribe_id = 0; + let mut variables = SubscriptionUtils::normal_variable(subscribe_id); + + let _ = variables.consumer.set_subscription( + variables.subscribe_id, + variables.track_alias, + variables.track_namespace.clone(), + variables.track_name.clone(), + variables.subscriber_priority, + variables.group_order, + variables.filter_type, + variables.start_group, + variables.start_object, + variables.end_group, + variables.end_object, + ); + + let result = variables.consumer.is_requesting(variables.subscribe_id); + + assert!(result); + } + + #[test] + fn delete_subscription() { + let subscribe_id = 0; + let mut variables = SubscriptionUtils::normal_variable(subscribe_id); + + let _ = variables.consumer.set_subscription( + variables.subscribe_id, + variables.track_alias, + variables.track_namespace.clone(), + variables.track_name.clone(), + variables.subscriber_priority, + variables.group_order, + variables.filter_type, + variables.start_group, + variables.start_object, + variables.end_group, + variables.end_object, + ); + + let result = variables + .consumer + .delete_subscription(variables.subscribe_id); + + assert!(result.is_ok()); + } + + #[test] + fn is_within_max_subscribe_id() { + let max_subscribe_id = 10; + let subscribe_id = 5; + + let consumer = Consumer::new(max_subscribe_id); + let result = consumer.is_within_max_subscribe_id(subscribe_id); + + assert!(result); + } + + #[test] + fn is_subscribe_id_unique() { + let subscribe_id = 0; + let mut variables = SubscriptionUtils::normal_variable(subscribe_id); + + let _ = variables.consumer.set_subscription( + variables.subscribe_id, + variables.track_alias, + variables.track_namespace.clone(), + variables.track_name.clone(), + variables.subscriber_priority, + variables.group_order, + variables.filter_type, + variables.start_group, + variables.start_object, + variables.end_group, + variables.end_object, + ); + + let result = variables + .consumer + .is_subscribe_id_unique(variables.subscribe_id); + + assert!(!result); + } + + #[test] + fn is_track_alias_unique() { + let track_alias = 100; + let mut variables = SubscriptionUtils::normal_variable(0); + + let _ = variables.consumer.set_subscription( + variables.subscribe_id, + variables.track_alias, + variables.track_namespace.clone(), + variables.track_name.clone(), + variables.subscriber_priority, + variables.group_order, + variables.filter_type, + variables.start_group, + variables.start_object, + variables.end_group, + variables.end_object, + ); + + let result = variables.consumer.is_track_alias_unique(track_alias); + + assert!(result); + } + + #[test] + fn find_unused_subscribe_id_and_track_alias() { + let subscribe_id = 0; + let mut variables = SubscriptionUtils::normal_variable(subscribe_id); + + let _ = variables.consumer.set_subscription( + variables.subscribe_id, + variables.track_alias, + variables.track_namespace.clone(), + variables.track_name.clone(), + variables.subscriber_priority, + variables.group_order, + variables.filter_type, + variables.start_group, + variables.start_object, + variables.end_group, + variables.end_object, + ); + + let result = variables + .consumer + .find_unused_subscribe_id_and_track_alias(); + + assert!(result.is_ok()); + } + + #[test] + fn set_namespace() { + let namespace = Vec::from(["test".to_string(), "test".to_string()]); + let mut consumer = Consumer::new(10); + + let result = consumer.set_namespace(namespace); + + assert!(result.is_ok()); + } + + #[test] + fn get_namespaces() { + let namespace = Vec::from(["test".to_string(), "test".to_string()]); + let mut consumer = Consumer::new(10); + + let _ = consumer.set_namespace(namespace.clone()); + + let expected_result = &vec![namespace]; + + let result = consumer.get_namespaces().unwrap(); + + assert_eq!(result, expected_result); + } + + #[test] + fn has_namespace() { + let namespace = Vec::from(["test".to_string(), "test".to_string()]); + let mut consumer = Consumer::new(10); + + let _ = consumer.set_namespace(namespace.clone()); + + let result = consumer.has_namespace(namespace); + + assert!(result); + } + + #[test] + fn delete_namespace() { + let namespace = Vec::from(["test".to_string(), "test".to_string()]); + let mut consumer = Consumer::new(10); + + let _ = consumer.set_namespace(namespace.clone()); + + let result = consumer.delete_namespace(namespace); + assert!(result.is_ok()); + + let namespaces = consumer.get_namespaces().unwrap(); + assert!(namespaces.is_empty()); + } + + #[test] + fn set_namespace_prefix() { + let namespace_prefix = Vec::from(["test".to_string()]); + let mut consumer = Consumer::new(10); + + let result = consumer.set_namespace_prefix(namespace_prefix); + + assert!(result.is_ok()); + } + + #[test] + fn get_namespace_prefixes() { + let namespace_prefix = Vec::from(["test".to_string()]); + let mut consumer = Consumer::new(10); + + let _ = consumer.set_namespace_prefix(namespace_prefix.clone()); + + let expected_result = &vec![namespace_prefix]; + + let result = consumer.get_namespace_prefixes().unwrap(); + + assert_eq!(result, expected_result); + } + + #[test] + fn delete_namespace_prefix() { + let namespace_prefix = Vec::from(["test".to_string()]); + let mut consumer = Consumer::new(10); + + let _ = consumer.set_namespace_prefix(namespace_prefix.clone()); + + let result = consumer.delete_namespace_prefix(namespace_prefix); + assert!(result.is_ok()); + + let namespace_prefixes = consumer.get_namespace_prefixes().unwrap(); + assert!(namespace_prefixes.is_empty()); + } +} diff --git a/moqt-core/src/modules/subscription_models/producer_node.rs b/moqt-core/src/modules/subscription_models/producer_node.rs index 64dd0910..0d793e45 100644 --- a/moqt-core/src/modules/subscription_models/producer_node.rs +++ b/moqt-core/src/modules/subscription_models/producer_node.rs @@ -200,3 +200,462 @@ impl SubscriptionNodeRegistory for Producer { Ok(()) } } + +#[cfg(test)] +pub(crate) mod test_utils { + use super::{Producer, TrackNamespace}; + use crate::subscription_models::producer_node::{FilterType, GroupOrder}; + + #[derive(Debug, Clone)] + pub(crate) struct SubscriptionUtils { + pub(crate) producer: Producer, + pub(crate) subscribe_id: u64, + pub(crate) track_alias: u64, + pub(crate) track_namespace: TrackNamespace, + pub(crate) track_name: String, + pub(crate) subscriber_priority: u8, + pub(crate) group_order: GroupOrder, + pub(crate) filter_type: FilterType, + pub(crate) start_group: Option, + pub(crate) start_object: Option, + pub(crate) end_group: Option, + pub(crate) end_object: Option, + } + + impl SubscriptionUtils { + pub(crate) fn normal_variable(subscribe_id: u64) -> Self { + let producer = Producer::new(10); + let track_alias = 0; + let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); + let track_name = "track_name".to_string(); + let subscriber_priority = 0; + let group_order = GroupOrder::Ascending; + let filter_type = FilterType::AbsoluteStart; + let start_group = Some(0); + let start_object = Some(0); + let end_group = None; + let end_object = None; + + SubscriptionUtils { + producer, + subscribe_id, + track_alias, + track_namespace, + track_name, + subscriber_priority, + group_order, + filter_type, + start_group, + start_object, + end_group, + end_object, + } + } + } +} + +#[cfg(test)] +mod success { + use super::*; + use crate::subscription_models::producer_node::test_utils::SubscriptionUtils; + use crate::subscription_models::subscriptions::Subscription; + + #[test] + fn set_subscription() { + let subscribe_id = 0; + let mut variables = SubscriptionUtils::normal_variable(subscribe_id); + + let result = variables.producer.set_subscription( + variables.subscribe_id, + variables.track_alias, + variables.track_namespace, + variables.track_name, + variables.subscriber_priority, + variables.group_order, + variables.filter_type, + variables.start_group, + variables.start_object, + variables.end_group, + variables.end_object, + ); + + assert!(result.is_ok()); + } + + #[test] + fn get_subscription() { + let subscribe_id = 0; + let variables = SubscriptionUtils::normal_variable(subscribe_id); + + let mut variables_clone = variables.clone(); + let _ = variables_clone.producer.set_subscription( + variables_clone.subscribe_id, + variables_clone.track_alias, + variables_clone.track_namespace, + variables_clone.track_name, + variables_clone.subscriber_priority, + variables_clone.group_order, + variables_clone.filter_type, + variables_clone.start_group, + variables_clone.start_object, + variables_clone.end_group, + variables_clone.end_object, + ); + + let subscription = variables_clone + .producer + .get_subscription(variables.clone().subscribe_id) + .unwrap(); + + let expected_subscription = Some(Subscription::new( + variables.track_alias, + variables.track_namespace, + variables.track_name, + variables.subscriber_priority, + variables.group_order, + variables.filter_type, + variables.start_group, + variables.start_object, + variables.end_group, + variables.end_object, + None, + )); + + assert_eq!(subscription, expected_subscription); + } + + #[test] + fn get_subscription_by_full_track_name() { + let subscribe_id = 0; + let variables = SubscriptionUtils::normal_variable(subscribe_id); + + let mut variables_clone = variables.clone(); + let _ = variables_clone.producer.set_subscription( + variables_clone.subscribe_id, + variables_clone.track_alias, + variables_clone.track_namespace, + variables_clone.track_name, + variables_clone.subscriber_priority, + variables_clone.group_order, + variables_clone.filter_type, + variables_clone.start_group, + variables_clone.start_object, + variables_clone.end_group, + variables_clone.end_object, + ); + + let subscription = variables_clone + .producer + .get_subscription_by_full_track_name( + variables.track_namespace.clone(), + variables.track_name.clone(), + ) + .unwrap(); + + let expected_subscription = Some(Subscription::new( + variables.track_alias, + variables.track_namespace, + variables.track_name, + variables.subscriber_priority, + variables.group_order, + variables.filter_type, + variables.start_group, + variables.start_object, + variables.end_group, + variables.end_object, + None, + )); + + assert_eq!(subscription, expected_subscription); + } + + #[test] + fn get_subscribe_id() { + let subscribe_id = 0; + let mut variables = SubscriptionUtils::normal_variable(subscribe_id); + + let _ = variables.producer.set_subscription( + variables.subscribe_id, + variables.track_alias, + variables.track_namespace.clone(), + variables.track_name.clone(), + variables.subscriber_priority, + variables.group_order, + variables.filter_type, + variables.start_group, + variables.start_object, + variables.end_group, + variables.end_object, + ); + + let expected_subscribe_id = variables.subscribe_id; + + let result_subscribe_id = variables + .producer + .get_subscribe_id(variables.track_namespace, variables.track_name) + .unwrap() + .unwrap(); + + assert_eq!(result_subscribe_id, expected_subscribe_id); + } + + #[test] + fn has_track() { + let subscribe_id = 0; + let mut variables = SubscriptionUtils::normal_variable(subscribe_id); + + let _ = variables.producer.set_subscription( + variables.subscribe_id, + variables.track_alias, + variables.track_namespace.clone(), + variables.track_name.clone(), + variables.subscriber_priority, + variables.group_order, + variables.filter_type, + variables.start_group, + variables.start_object, + variables.end_group, + variables.end_object, + ); + + let result = variables + .producer + .has_track(variables.track_namespace, variables.track_name); + + assert!(result); + } + + #[test] + fn activate_subscription() { + let subscribe_id = 0; + let mut variables = SubscriptionUtils::normal_variable(subscribe_id); + + let _ = variables.producer.set_subscription( + variables.subscribe_id, + variables.track_alias, + variables.track_namespace.clone(), + variables.track_name.clone(), + variables.subscriber_priority, + variables.group_order, + variables.filter_type, + variables.start_group, + variables.start_object, + variables.end_group, + variables.end_object, + ); + + let result = variables + .producer + .activate_subscription(variables.subscribe_id) + .unwrap(); + + assert!(result); + } + + #[test] + fn is_requesting() { + let subscribe_id = 0; + let mut variables = SubscriptionUtils::normal_variable(subscribe_id); + + let _ = variables.producer.set_subscription( + variables.subscribe_id, + variables.track_alias, + variables.track_namespace.clone(), + variables.track_name.clone(), + variables.subscriber_priority, + variables.group_order, + variables.filter_type, + variables.start_group, + variables.start_object, + variables.end_group, + variables.end_object, + ); + + let result = variables.producer.is_requesting(variables.subscribe_id); + + assert!(result); + } + + #[test] + fn delete_subscription() { + let subscribe_id = 0; + let mut variables = SubscriptionUtils::normal_variable(subscribe_id); + + let _ = variables.producer.set_subscription( + variables.subscribe_id, + variables.track_alias, + variables.track_namespace.clone(), + variables.track_name.clone(), + variables.subscriber_priority, + variables.group_order, + variables.filter_type, + variables.start_group, + variables.start_object, + variables.end_group, + variables.end_object, + ); + + let result = variables + .producer + .delete_subscription(variables.subscribe_id); + + assert!(result.is_ok()); + } + + #[test] + fn is_within_max_subscribe_id() { + let max_subscribe_id = 10; + let subscribe_id = 5; + + let producer = Producer::new(max_subscribe_id); + let result = producer.is_within_max_subscribe_id(subscribe_id); + + assert!(result); + } + + #[test] + fn is_subscribe_id_unique() { + let subscribe_id = 0; + let mut variables = SubscriptionUtils::normal_variable(subscribe_id); + + let _ = variables.producer.set_subscription( + variables.subscribe_id, + variables.track_alias, + variables.track_namespace.clone(), + variables.track_name.clone(), + variables.subscriber_priority, + variables.group_order, + variables.filter_type, + variables.start_group, + variables.start_object, + variables.end_group, + variables.end_object, + ); + + let result = variables + .producer + .is_subscribe_id_unique(variables.subscribe_id); + + assert!(!result); + } + + #[test] + fn is_track_alias_unique() { + let track_alias = 100; + let mut variables = SubscriptionUtils::normal_variable(0); + + let _ = variables.producer.set_subscription( + variables.subscribe_id, + variables.track_alias, + variables.track_namespace.clone(), + variables.track_name.clone(), + variables.subscriber_priority, + variables.group_order, + variables.filter_type, + variables.start_group, + variables.start_object, + variables.end_group, + variables.end_object, + ); + + let result = variables.producer.is_track_alias_unique(track_alias); + + assert!(result); + } + + #[test] + #[should_panic] + fn find_unused_subscribe_id_and_track_alias() { + let producer = Producer::new(10); + + let _ = producer.find_unused_subscribe_id_and_track_alias(); + } + + #[test] + fn set_namespace() { + let namespace = Vec::from(["test".to_string(), "test".to_string()]); + let mut producer = Producer::new(10); + + let result = producer.set_namespace(namespace); + + assert!(result.is_ok()); + } + + #[test] + fn get_namespaces() { + let namespace = Vec::from(["test".to_string(), "test".to_string()]); + let mut producer = Producer::new(10); + + let _ = producer.set_namespace(namespace.clone()); + + let expected_result = &vec![namespace]; + + let result = producer.get_namespaces().unwrap(); + + assert_eq!(result, expected_result); + } + + #[test] + fn has_namespace() { + let namespace = Vec::from(["test".to_string(), "test".to_string()]); + let mut producer = Producer::new(10); + + let _ = producer.set_namespace(namespace.clone()); + + let result = producer.has_namespace(namespace); + + assert!(result); + } + + #[test] + fn delete_namespace() { + let namespace = Vec::from(["test".to_string(), "test".to_string()]); + let mut producer = Producer::new(10); + + let _ = producer.set_namespace(namespace.clone()); + + let result = producer.delete_namespace(namespace); + assert!(result.is_ok()); + + let namespaces = producer.get_namespaces().unwrap(); + assert!(namespaces.is_empty()); + } + + #[test] + fn set_namespace_prefix() { + let namespace_prefix = Vec::from(["test".to_string()]); + let mut producer = Producer::new(10); + + let result = producer.set_namespace_prefix(namespace_prefix); + + assert!(result.is_ok()); + } + + #[test] + fn get_namespace_prefixes() { + let namespace_prefix = Vec::from(["test".to_string()]); + let mut producer = Producer::new(10); + + let _ = producer.set_namespace_prefix(namespace_prefix.clone()); + + let expected_result = &vec![namespace_prefix]; + + let result = producer.get_namespace_prefixes().unwrap(); + + assert_eq!(result, expected_result); + } + + #[test] + fn delete_namespace_prefix() { + let namespace_prefix = Vec::from(["test".to_string()]); + let mut producer = Producer::new(10); + + let _ = producer.set_namespace_prefix(namespace_prefix.clone()); + + let result = producer.delete_namespace_prefix(namespace_prefix); + assert!(result.is_ok()); + + let namespace_prefixes = producer.get_namespace_prefixes().unwrap(); + assert!(namespace_prefixes.is_empty()); + } +} From c1c65c75f840c88d1746380058cd5c1c9e5af303 Mon Sep 17 00:00:00 2001 From: tetta maeda Date: Tue, 8 Oct 2024 09:10:01 +0900 Subject: [PATCH 12/17] impl: add subscription/track tests --- .../subscription_models/consumer_node.rs | 2 +- .../subscription_models/producer_node.rs | 2 +- .../subscription_models/subscriptions.rs | 215 +++++++++++++++++- .../src/modules/subscription_models/tracks.rs | 83 +++++++ 4 files changed, 299 insertions(+), 3 deletions(-) diff --git a/moqt-core/src/modules/subscription_models/consumer_node.rs b/moqt-core/src/modules/subscription_models/consumer_node.rs index 0ed710b9..d115f276 100644 --- a/moqt-core/src/modules/subscription_models/consumer_node.rs +++ b/moqt-core/src/modules/subscription_models/consumer_node.rs @@ -105,7 +105,7 @@ impl SubscriptionNodeRegistory for Consumer { fn activate_subscription(&mut self, subscribe_id: SubscribeId) -> Result { let subscription = self.subscriptions.get_mut(&subscribe_id).unwrap(); - let activate = subscription.active(); + let activate = subscription.activate(); Ok(activate) } diff --git a/moqt-core/src/modules/subscription_models/producer_node.rs b/moqt-core/src/modules/subscription_models/producer_node.rs index 0d793e45..babd907e 100644 --- a/moqt-core/src/modules/subscription_models/producer_node.rs +++ b/moqt-core/src/modules/subscription_models/producer_node.rs @@ -105,7 +105,7 @@ impl SubscriptionNodeRegistory for Producer { fn activate_subscription(&mut self, subscribe_id: SubscribeId) -> Result { let subscription = self.subscriptions.get_mut(&subscribe_id).unwrap(); - let activate = subscription.active(); + let activate = subscription.activate(); Ok(activate) } diff --git a/moqt-core/src/modules/subscription_models/subscriptions.rs b/moqt-core/src/modules/subscription_models/subscriptions.rs index 9741ab4f..4ca86c7d 100644 --- a/moqt-core/src/modules/subscription_models/subscriptions.rs +++ b/moqt-core/src/modules/subscription_models/subscriptions.rs @@ -56,7 +56,7 @@ impl Subscription { } } - pub fn active(&mut self) -> bool { + pub fn activate(&mut self) -> bool { if self.is_active() { false } else { @@ -85,3 +85,216 @@ impl Subscription { self.track.get_track_alias() } } + +#[cfg(test)] +pub(crate) mod test_utils { + use crate::messages::control_messages::subscribe::{FilterType, GroupOrder}; + + #[derive(Debug, Clone)] + pub(crate) struct SubscriptionUtils { + pub(crate) track_alias: u64, + pub(crate) track_namespace: Vec, + pub(crate) track_name: String, + pub(crate) subscriber_priority: u8, + pub(crate) group_order: GroupOrder, + pub(crate) filter_type: FilterType, + pub(crate) start_group: Option, + pub(crate) start_object: Option, + pub(crate) end_group: Option, + pub(crate) end_object: Option, + } + + impl SubscriptionUtils { + pub(crate) fn normal_variable() -> Self { + let track_alias = 0; + let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); + let track_name = "track_name".to_string(); + let subscriber_priority = 0; + let group_order = GroupOrder::Ascending; + let filter_type = FilterType::AbsoluteStart; + let start_group = Some(0); + let start_object = Some(0); + let end_group = None; + let end_object = None; + + SubscriptionUtils { + track_alias, + track_namespace, + track_name, + subscriber_priority, + group_order, + filter_type, + start_group, + start_object, + end_group, + end_object, + } + } + } +} + +#[cfg(test)] +mod success { + use super::*; + use crate::subscription_models::tracks::ForwardingPreference; + + #[test] + fn new() { + let track_alias = 1; + let track_namespace = vec!["namespace".to_string()]; + let track_name = "track_name".to_string(); + let priority = 1; + let group_order = GroupOrder::Ascending; + let filter_type = FilterType::AbsoluteRange; + let start_group = Some(1); + let start_object = Some(1); + let end_group = Some(1); + let end_object = Some(1); + let forwarding_preference = Some(ForwardingPreference::Track); + + let subscription = Subscription::new( + track_alias, + track_namespace.clone(), + track_name.clone(), + priority, + group_order, + filter_type, + start_group, + start_object, + end_group, + end_object, + forwarding_preference, + ); + + assert_eq!(subscription.track.get_track_alias(), track_alias); + assert_eq!( + subscription.get_track_namespace_and_name(), + (track_namespace, track_name) + ); + assert_eq!(subscription.priority, priority); + assert_eq!(subscription.group_order, group_order); + assert_eq!(subscription.filter_type, filter_type); + assert_eq!(subscription.start_group, start_group); + assert_eq!(subscription.start_object, start_object); + assert_eq!(subscription.end_group, end_group); + assert_eq!(subscription.end_object, end_object); + } + + #[test] + fn activate() { + let variable = test_utils::SubscriptionUtils::normal_variable(); + + let mut subscription = Subscription::new( + variable.track_alias, + variable.track_namespace, + variable.track_name, + variable.subscriber_priority, + variable.group_order, + variable.filter_type, + variable.start_group, + variable.start_object, + variable.end_group, + variable.end_object, + None, + ); + + let result = subscription.activate(); + assert!(result); + + let result = subscription.activate(); + assert!(!result); + } + + #[test] + fn is_active() { + let variable = test_utils::SubscriptionUtils::normal_variable(); + + let mut subscription = Subscription::new( + variable.track_alias, + variable.track_namespace, + variable.track_name, + variable.subscriber_priority, + variable.group_order, + variable.filter_type, + variable.start_group, + variable.start_object, + variable.end_group, + variable.end_object, + None, + ); + + assert!(!subscription.is_active()); + + subscription.activate(); + assert!(subscription.is_active()); + } + + #[test] + fn is_requesting() { + let variable = test_utils::SubscriptionUtils::normal_variable(); + + let mut subscription = Subscription::new( + variable.track_alias, + variable.track_namespace, + variable.track_name, + variable.subscriber_priority, + variable.group_order, + variable.filter_type, + variable.start_group, + variable.start_object, + variable.end_group, + variable.end_object, + None, + ); + + assert!(subscription.is_requesting()); + + subscription.activate(); + assert!(!subscription.is_requesting()); + } + + #[test] + fn get_track_namespace_and_name() { + let variable = test_utils::SubscriptionUtils::normal_variable(); + + let subscription = Subscription::new( + variable.track_alias, + variable.track_namespace.clone(), + variable.track_name.clone(), + variable.subscriber_priority, + variable.group_order, + variable.filter_type, + variable.start_group, + variable.start_object, + variable.end_group, + variable.end_object, + None, + ); + + assert_eq!( + subscription.get_track_namespace_and_name(), + (variable.track_namespace, variable.track_name) + ); + } + + #[test] + fn get_track_alias() { + let variable = test_utils::SubscriptionUtils::normal_variable(); + + let subscription = Subscription::new( + variable.track_alias, + variable.track_namespace, + variable.track_name, + variable.subscriber_priority, + variable.group_order, + variable.filter_type, + variable.start_group, + variable.start_object, + variable.end_group, + variable.end_object, + None, + ); + + assert_eq!(subscription.get_track_alias(), variable.track_alias); + } +} diff --git a/moqt-core/src/modules/subscription_models/tracks.rs b/moqt-core/src/modules/subscription_models/tracks.rs index 368f1d65..8ca6dbfc 100644 --- a/moqt-core/src/modules/subscription_models/tracks.rs +++ b/moqt-core/src/modules/subscription_models/tracks.rs @@ -40,3 +40,86 @@ impl Track { self.track_alias } } + +#[cfg(test)] +mod succsess { + use super::*; + + #[test] + fn new() { + let track_alias = 0; + let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); + let track_name = "track_name".to_string(); + let forwarding_preference = Some(ForwardingPreference::Datagram); + + let track = Track::new( + track_alias, + track_namespace.clone(), + track_name.clone(), + forwarding_preference.clone(), + ); + + assert_eq!(track.track_alias, track_alias); + assert_eq!(track.track_namespace, track_namespace); + assert_eq!(track.track_name, track_name); + assert_eq!(track.forwarding_preference, forwarding_preference); + } + + #[test] + fn set_forwarding_preference() { + let track_alias = 0; + let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); + let track_name = "track_name".to_string(); + let expected_forwarding_preference = Some(ForwardingPreference::Subgroup); + + let mut track = Track::new( + track_alias, + track_namespace.clone(), + track_name.clone(), + None, + ); + track.set_forwarding_preference(ForwardingPreference::Subgroup); + + assert_eq!(track.track_alias, track_alias); + assert_eq!(track.track_namespace, track_namespace); + assert_eq!(track.track_name, track_name); + assert_eq!(track.forwarding_preference, expected_forwarding_preference); + } + + #[test] + fn get_track_namespace_and_name() { + let track_alias = 0; + let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); + let track_name = "track_name".to_string(); + let forwarding_preference = Some(ForwardingPreference::Datagram); + + let track = Track::new( + track_alias, + track_namespace.clone(), + track_name.clone(), + forwarding_preference, + ); + + assert_eq!( + track.get_track_namespace_and_name(), + (track_namespace, track_name) + ); + } + + #[test] + fn test_get_track_alias() { + let track_alias = 0; + let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); + let track_name = "track_name".to_string(); + let forwarding_preference = Some(ForwardingPreference::Datagram); + + let track = Track::new( + track_alias, + track_namespace.clone(), + track_name.clone(), + forwarding_preference, + ); + + assert_eq!(track.get_track_alias(), track_alias); + } +} From 56fcb6fc837635d14c1c8cbe0059623ceb2a5613 Mon Sep 17 00:00:00 2001 From: tetta maeda Date: Tue, 8 Oct 2024 09:26:38 +0900 Subject: [PATCH 13/17] refactor: refine folder structure --- moqt-core/src/modules/subscription_models.rs | 4 +--- moqt-core/src/modules/subscription_models/nodes.rs | 3 +++ .../subscription_models/{ => nodes}/consumer_node.rs | 6 +++--- .../subscription_models/{ => nodes}/node_registory.rs | 0 .../subscription_models/{ => nodes}/producer_node.rs | 6 +++--- moqt-server/src/modules/relation_manager/interface.rs | 2 +- moqt-server/src/modules/relation_manager/manager.rs | 5 +++-- 7 files changed, 14 insertions(+), 12 deletions(-) create mode 100644 moqt-core/src/modules/subscription_models/nodes.rs rename moqt-core/src/modules/subscription_models/{ => nodes}/consumer_node.rs (98%) rename moqt-core/src/modules/subscription_models/{ => nodes}/node_registory.rs (100%) rename moqt-core/src/modules/subscription_models/{ => nodes}/producer_node.rs (98%) diff --git a/moqt-core/src/modules/subscription_models.rs b/moqt-core/src/modules/subscription_models.rs index 26e3c284..39e1df2a 100644 --- a/moqt-core/src/modules/subscription_models.rs +++ b/moqt-core/src/modules/subscription_models.rs @@ -1,5 +1,3 @@ -pub mod consumer_node; -pub mod node_registory; -pub mod producer_node; +pub mod nodes; pub mod subscriptions; pub mod tracks; diff --git a/moqt-core/src/modules/subscription_models/nodes.rs b/moqt-core/src/modules/subscription_models/nodes.rs new file mode 100644 index 00000000..f8f51b62 --- /dev/null +++ b/moqt-core/src/modules/subscription_models/nodes.rs @@ -0,0 +1,3 @@ +pub mod consumer_node; +pub mod node_registory; +pub mod producer_node; diff --git a/moqt-core/src/modules/subscription_models/consumer_node.rs b/moqt-core/src/modules/subscription_models/nodes/consumer_node.rs similarity index 98% rename from moqt-core/src/modules/subscription_models/consumer_node.rs rename to moqt-core/src/modules/subscription_models/nodes/consumer_node.rs index d115f276..be508cde 100644 --- a/moqt-core/src/modules/subscription_models/consumer_node.rs +++ b/moqt-core/src/modules/subscription_models/nodes/consumer_node.rs @@ -1,5 +1,5 @@ use crate::messages::control_messages::subscribe::{FilterType, GroupOrder}; -use crate::subscription_models::node_registory::SubscriptionNodeRegistory; +use crate::subscription_models::nodes::node_registory::SubscriptionNodeRegistory; use crate::subscription_models::subscriptions::Subscription; use anyhow::{bail, Result}; use std::collections::HashMap; @@ -216,7 +216,7 @@ impl SubscriptionNodeRegistory for Consumer { #[cfg(test)] pub(crate) mod test_utils { use super::{Consumer, TrackNamespace}; - use crate::subscription_models::consumer_node::{FilterType, GroupOrder}; + use crate::subscription_models::nodes::consumer_node::{FilterType, GroupOrder}; #[derive(Debug, Clone)] pub(crate) struct SubscriptionUtils { @@ -269,7 +269,7 @@ pub(crate) mod test_utils { #[cfg(test)] mod success { use super::*; - use crate::subscription_models::consumer_node::test_utils::SubscriptionUtils; + use crate::subscription_models::nodes::consumer_node::test_utils::SubscriptionUtils; use crate::subscription_models::subscriptions::Subscription; #[test] diff --git a/moqt-core/src/modules/subscription_models/node_registory.rs b/moqt-core/src/modules/subscription_models/nodes/node_registory.rs similarity index 100% rename from moqt-core/src/modules/subscription_models/node_registory.rs rename to moqt-core/src/modules/subscription_models/nodes/node_registory.rs diff --git a/moqt-core/src/modules/subscription_models/producer_node.rs b/moqt-core/src/modules/subscription_models/nodes/producer_node.rs similarity index 98% rename from moqt-core/src/modules/subscription_models/producer_node.rs rename to moqt-core/src/modules/subscription_models/nodes/producer_node.rs index babd907e..f4a49ace 100644 --- a/moqt-core/src/modules/subscription_models/producer_node.rs +++ b/moqt-core/src/modules/subscription_models/nodes/producer_node.rs @@ -1,5 +1,5 @@ use crate::messages::control_messages::subscribe::{FilterType, GroupOrder}; -use crate::subscription_models::node_registory::SubscriptionNodeRegistory; +use crate::subscription_models::nodes::node_registory::SubscriptionNodeRegistory; use crate::subscription_models::subscriptions::Subscription; use anyhow::{bail, Result}; use std::collections::HashMap; @@ -204,7 +204,7 @@ impl SubscriptionNodeRegistory for Producer { #[cfg(test)] pub(crate) mod test_utils { use super::{Producer, TrackNamespace}; - use crate::subscription_models::producer_node::{FilterType, GroupOrder}; + use crate::subscription_models::nodes::producer_node::{FilterType, GroupOrder}; #[derive(Debug, Clone)] pub(crate) struct SubscriptionUtils { @@ -257,7 +257,7 @@ pub(crate) mod test_utils { #[cfg(test)] mod success { use super::*; - use crate::subscription_models::producer_node::test_utils::SubscriptionUtils; + use crate::subscription_models::nodes::producer_node::test_utils::SubscriptionUtils; use crate::subscription_models::subscriptions::Subscription; #[test] diff --git a/moqt-server/src/modules/relation_manager/interface.rs b/moqt-server/src/modules/relation_manager/interface.rs index 0e88757b..db1b3f08 100644 --- a/moqt-server/src/modules/relation_manager/interface.rs +++ b/moqt-server/src/modules/relation_manager/interface.rs @@ -427,7 +427,7 @@ mod success { }; use moqt_core::messages::control_messages::subscribe::{FilterType, GroupOrder}; use moqt_core::pubsub_relation_manager_repository::PubSubRelationManagerRepository; - use moqt_core::subscription_models::node_registory::SubscriptionNodeRegistory; + use moqt_core::subscription_models::nodes::node_registory::SubscriptionNodeRegistory; use moqt_core::subscription_models::subscriptions::Subscription; use tokio::sync::mpsc; diff --git a/moqt-server/src/modules/relation_manager/manager.rs b/moqt-server/src/modules/relation_manager/manager.rs index da9f119e..a911f7d1 100644 --- a/moqt-server/src/modules/relation_manager/manager.rs +++ b/moqt-server/src/modules/relation_manager/manager.rs @@ -3,8 +3,9 @@ use crate::modules::relation_manager::{ relation::PubSubRelation, }; use anyhow::anyhow; -use moqt_core::subscription_models::node_registory::SubscriptionNodeRegistory; -use moqt_core::subscription_models::{consumer_node::Consumer, producer_node::Producer}; +use moqt_core::subscription_models::nodes::{ + consumer_node::Consumer, node_registory::SubscriptionNodeRegistory, producer_node::Producer, +}; use std::collections::HashMap; use tokio::sync::mpsc; From 448f1289f2a3a4da614a119997729676728110c5 Mon Sep 17 00:00:00 2001 From: tetta maeda Date: Tue, 8 Oct 2024 09:30:28 +0900 Subject: [PATCH 14/17] refactor: rename relation_manager folder --- moqt-server/src/lib.rs | 6 +++--- moqt-server/src/modules.rs | 2 +- moqt-server/src/modules/control_message_handler.rs | 2 +- moqt-server/src/modules/handlers/announce_handler.rs | 4 ++-- .../src/modules/handlers/server_setup_handler.rs | 4 ++-- moqt-server/src/modules/handlers/subscribe_handler.rs | 4 ++-- .../src/modules/handlers/subscribe_ok_handler.rs | 4 ++-- ...{relation_manager.rs => pubsub_relation_manager.rs} | 0 .../commands.rs | 2 +- .../interface.rs | 10 ++++++---- .../manager.rs | 2 +- .../relation.rs | 0 12 files changed, 21 insertions(+), 19 deletions(-) rename moqt-server/src/modules/{relation_manager.rs => pubsub_relation_manager.rs} (100%) rename moqt-server/src/modules/{relation_manager => pubsub_relation_manager}/commands.rs (98%) rename moqt-server/src/modules/{relation_manager => pubsub_relation_manager}/interface.rs (99%) rename moqt-server/src/modules/{relation_manager => pubsub_relation_manager}/manager.rs (99%) rename moqt-server/src/modules/{relation_manager => pubsub_relation_manager}/relation.rs (100%) diff --git a/moqt-server/src/lib.rs b/moqt-server/src/lib.rs index 55cde50f..cbc2b0a0 100644 --- a/moqt-server/src/lib.rs +++ b/moqt-server/src/lib.rs @@ -3,7 +3,7 @@ use crate::modules::{ buffer_manager, buffer_manager::{buffer_manager, BufferCommand}, control_message_handler::*, - relation_manager::{commands::PubSubRelationCommand, manager::pubsub_relation_manager}, + pubsub_relation_manager::{commands::PubSubRelationCommand, manager::pubsub_relation_manager}, send_stream_dispatcher::{send_stream_dispatcher, SendStreamDispatchCommand}, }; use anyhow::{bail, Context, Ok, Result}; @@ -282,7 +282,7 @@ async fn handle_connection( // Delete pub/sub information related to the client let pubsub_relation_manager = - modules::relation_manager::interface::PubSubRelationManagerInterface::new( + modules::pubsub_relation_manager::interface::PubSubRelationManagerInterface::new( track_namespace_tx.clone(), ); let _ = pubsub_relation_manager.delete_client(stable_id).await; @@ -329,7 +329,7 @@ async fn handle_incoming_bi_stream( let shread_send_stream = &mut stream.shread_send_stream; let mut pubsub_relation_manager = - modules::relation_manager::interface::PubSubRelationManagerInterface::new( + modules::pubsub_relation_manager::interface::PubSubRelationManagerInterface::new( track_namespace_tx.clone(), ); let mut send_stream_dispatcher = diff --git a/moqt-server/src/modules.rs b/moqt-server/src/modules.rs index fb8d0137..821823b4 100644 --- a/moqt-server/src/modules.rs +++ b/moqt-server/src/modules.rs @@ -1,6 +1,6 @@ pub mod buffer_manager; pub(crate) mod control_message_handler; pub(crate) mod handlers; -pub(crate) mod relation_manager; +pub(crate) mod pubsub_relation_manager; pub(crate) mod send_stream_dispatcher; pub(crate) mod server_processes; diff --git a/moqt-server/src/modules/control_message_handler.rs b/moqt-server/src/modules/control_message_handler.rs index ddd2d5fa..74759806 100644 --- a/moqt-server/src/modules/control_message_handler.rs +++ b/moqt-server/src/modules/control_message_handler.rs @@ -313,7 +313,7 @@ pub(crate) mod test_utils { use crate::modules::control_message_handler::control_message_handler; use crate::modules::control_message_handler::MessageProcessResult; - use crate::modules::relation_manager::{ + use crate::modules::pubsub_relation_manager::{ commands::PubSubRelationCommand, interface::PubSubRelationManagerInterface, manager::pubsub_relation_manager, }; diff --git a/moqt-server/src/modules/handlers/announce_handler.rs b/moqt-server/src/modules/handlers/announce_handler.rs index ce533fd5..0542cdf2 100644 --- a/moqt-server/src/modules/handlers/announce_handler.rs +++ b/moqt-server/src/modules/handlers/announce_handler.rs @@ -53,7 +53,7 @@ pub(crate) async fn announce_handler( #[cfg(test)] mod success { use crate::modules::handlers::announce_handler::{announce_handler, AnnounceResponse}; - use crate::modules::relation_manager::{ + use crate::modules::pubsub_relation_manager::{ commands::PubSubRelationCommand, interface::PubSubRelationManagerInterface, manager::pubsub_relation_manager, }; @@ -115,7 +115,7 @@ mod success { #[cfg(test)] mod failure { use crate::modules::handlers::announce_handler::{announce_handler, AnnounceResponse}; - use crate::modules::relation_manager::{ + use crate::modules::pubsub_relation_manager::{ commands::PubSubRelationCommand, interface::PubSubRelationManagerInterface, manager::pubsub_relation_manager, }; diff --git a/moqt-server/src/modules/handlers/server_setup_handler.rs b/moqt-server/src/modules/handlers/server_setup_handler.rs index 2f9d4038..3cbdf6d7 100644 --- a/moqt-server/src/modules/handlers/server_setup_handler.rs +++ b/moqt-server/src/modules/handlers/server_setup_handler.rs @@ -87,7 +87,7 @@ pub(crate) async fn setup_handler( #[cfg(test)] mod success { - use crate::modules::relation_manager::{ + use crate::modules::pubsub_relation_manager::{ commands::PubSubRelationCommand, interface::PubSubRelationManagerInterface, manager::pubsub_relation_manager, }; @@ -160,7 +160,7 @@ mod success { #[cfg(test)] mod failure { - use crate::modules::relation_manager::{ + use crate::modules::pubsub_relation_manager::{ commands::PubSubRelationCommand, interface::PubSubRelationManagerInterface, manager::pubsub_relation_manager, }; diff --git a/moqt-server/src/modules/handlers/subscribe_handler.rs b/moqt-server/src/modules/handlers/subscribe_handler.rs index 0a80e7a3..419fe9d9 100644 --- a/moqt-server/src/modules/handlers/subscribe_handler.rs +++ b/moqt-server/src/modules/handlers/subscribe_handler.rs @@ -289,7 +289,7 @@ async fn set_subscriber_and_publisher_subscription( #[cfg(test)] mod success { use crate::modules::handlers::subscribe_handler::subscribe_handler; - use crate::modules::relation_manager::{ + use crate::modules::pubsub_relation_manager::{ commands::PubSubRelationCommand, interface::{test_utils, PubSubRelationManagerInterface}, manager::pubsub_relation_manager, @@ -544,7 +544,7 @@ mod success { #[cfg(test)] mod failure { use crate::modules::handlers::subscribe_handler::subscribe_handler; - use crate::modules::relation_manager::{ + use crate::modules::pubsub_relation_manager::{ commands::PubSubRelationCommand, interface::PubSubRelationManagerInterface, manager::pubsub_relation_manager, }; diff --git a/moqt-server/src/modules/handlers/subscribe_ok_handler.rs b/moqt-server/src/modules/handlers/subscribe_ok_handler.rs index 38f3a4b3..3caefa51 100644 --- a/moqt-server/src/modules/handlers/subscribe_ok_handler.rs +++ b/moqt-server/src/modules/handlers/subscribe_ok_handler.rs @@ -78,7 +78,7 @@ pub(crate) async fn subscribe_ok_handler( #[cfg(test)] mod success { use crate::modules::handlers::subscribe_ok_handler::subscribe_ok_handler; - use crate::modules::relation_manager::{ + use crate::modules::pubsub_relation_manager::{ commands::PubSubRelationCommand, interface::PubSubRelationManagerInterface, manager::pubsub_relation_manager, }; @@ -228,7 +228,7 @@ mod success { #[cfg(test)] mod failure { use crate::modules::handlers::subscribe_ok_handler::subscribe_ok_handler; - use crate::modules::relation_manager::{ + use crate::modules::pubsub_relation_manager::{ commands::PubSubRelationCommand, interface::PubSubRelationManagerInterface, manager::pubsub_relation_manager, }; diff --git a/moqt-server/src/modules/relation_manager.rs b/moqt-server/src/modules/pubsub_relation_manager.rs similarity index 100% rename from moqt-server/src/modules/relation_manager.rs rename to moqt-server/src/modules/pubsub_relation_manager.rs diff --git a/moqt-server/src/modules/relation_manager/commands.rs b/moqt-server/src/modules/pubsub_relation_manager/commands.rs similarity index 98% rename from moqt-server/src/modules/relation_manager/commands.rs rename to moqt-server/src/modules/pubsub_relation_manager/commands.rs index 7391d253..9878469c 100644 --- a/moqt-server/src/modules/relation_manager/commands.rs +++ b/moqt-server/src/modules/pubsub_relation_manager/commands.rs @@ -4,7 +4,7 @@ use moqt_core::subscription_models::subscriptions::Subscription; use tokio::sync::oneshot; #[cfg(test)] -use crate::modules::relation_manager::{ +use crate::modules::pubsub_relation_manager::{ manager::{Consumers, Producers}, relation::PubSubRelation, }; diff --git a/moqt-server/src/modules/relation_manager/interface.rs b/moqt-server/src/modules/pubsub_relation_manager/interface.rs similarity index 99% rename from moqt-server/src/modules/relation_manager/interface.rs rename to moqt-server/src/modules/pubsub_relation_manager/interface.rs index db1b3f08..a83104d3 100644 --- a/moqt-server/src/modules/relation_manager/interface.rs +++ b/moqt-server/src/modules/pubsub_relation_manager/interface.rs @@ -1,4 +1,6 @@ -use crate::modules::relation_manager::commands::{PubSubRelationCommand, PubSubRelationCommand::*}; +use crate::modules::pubsub_relation_manager::commands::{ + PubSubRelationCommand, PubSubRelationCommand::*, +}; use anyhow::{bail, Result}; use async_trait::async_trait; use moqt_core::messages::control_messages::subscribe::{FilterType, GroupOrder}; @@ -398,7 +400,7 @@ impl PubSubRelationManagerRepository for PubSubRelationManagerInterface { #[cfg(test)] pub(crate) mod test_utils { - use crate::modules::relation_manager::{ + use crate::modules::pubsub_relation_manager::{ commands::PubSubRelationCommand, interface::PubSubRelationManagerInterface, manager::{Consumers, Producers}, @@ -421,7 +423,7 @@ pub(crate) mod test_utils { #[cfg(test)] mod success { - use crate::modules::relation_manager::{ + use crate::modules::pubsub_relation_manager::{ commands::PubSubRelationCommand, interface::test_utils, interface::PubSubRelationManagerInterface, manager::pubsub_relation_manager, }; @@ -1545,7 +1547,7 @@ mod success { #[cfg(test)] mod failure { - use crate::modules::relation_manager::{ + use crate::modules::pubsub_relation_manager::{ commands::PubSubRelationCommand, interface::PubSubRelationManagerInterface, manager::pubsub_relation_manager, }; diff --git a/moqt-server/src/modules/relation_manager/manager.rs b/moqt-server/src/modules/pubsub_relation_manager/manager.rs similarity index 99% rename from moqt-server/src/modules/relation_manager/manager.rs rename to moqt-server/src/modules/pubsub_relation_manager/manager.rs index a911f7d1..ea080ff5 100644 --- a/moqt-server/src/modules/relation_manager/manager.rs +++ b/moqt-server/src/modules/pubsub_relation_manager/manager.rs @@ -1,4 +1,4 @@ -use crate::modules::relation_manager::{ +use crate::modules::pubsub_relation_manager::{ commands::{PubSubRelationCommand, PubSubRelationCommand::*}, relation::PubSubRelation, }; diff --git a/moqt-server/src/modules/relation_manager/relation.rs b/moqt-server/src/modules/pubsub_relation_manager/relation.rs similarity index 100% rename from moqt-server/src/modules/relation_manager/relation.rs rename to moqt-server/src/modules/pubsub_relation_manager/relation.rs From 7185caa81f6d84f72d6d4282cbd1326b6f14c53b Mon Sep 17 00:00:00 2001 From: tetta maeda Date: Wed, 9 Oct 2024 21:14:53 +0900 Subject: [PATCH 15/17] impl: support client role pubsub --- .../modules/handlers/server_setup_handler.rs | 40 +++++++++++++------ 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/moqt-server/src/modules/handlers/server_setup_handler.rs b/moqt-server/src/modules/handlers/server_setup_handler.rs index 3cbdf6d7..bdb237ac 100644 --- a/moqt-server/src/modules/handlers/server_setup_handler.rs +++ b/moqt-server/src/modules/handlers/server_setup_handler.rs @@ -57,19 +57,33 @@ pub(crate) async fn setup_handler( } } - if client.role() == Some(RoleCase::Subscriber) { - // Generate producer that manages namespaces and subscriptions with subscribers. - // FIXME: max_subscribe_id for subscriber is fixed at 100 for now. - pubsub_relation_manager_repository - .setup_subscriber(100, client.id) - .await?; - } else if client.role() == Some(RoleCase::Publisher) { - // Generate consumer that manages namespaces and subscriptions with producers. - pubsub_relation_manager_repository - .setup_publisher(max_subscribe_id, client.id) - .await?; - } else if client.role().is_none() { - bail!("Role parameter is required in SETUP parameter from client."); + match client.role() { + Some(RoleCase::Publisher) => { + // Generate consumer that manages namespaces and subscriptions with producers. + pubsub_relation_manager_repository + .setup_publisher(max_subscribe_id, client.id) + .await?; + } + Some(RoleCase::Subscriber) => { + // Generate producer that manages namespaces and subscriptions with subscribers. + // FIXME: max_subscribe_id for subscriber is fixed at 100 for now. + pubsub_relation_manager_repository + .setup_subscriber(100, client.id) + .await?; + } + Some(RoleCase::PubSub) => { + // Generate producer and consumer that manages namespaces and subscriptions with publishers and subscribers. + pubsub_relation_manager_repository + .setup_publisher(max_subscribe_id, client.id) + .await?; + // FIXME: max_subscribe_id for subscriber is fixed at 100 for now. + pubsub_relation_manager_repository + .setup_subscriber(100, client.id) + .await?; + } + None => { + bail!("Role parameter is required in SETUP parameter from client."); + } } // Create a setup parameter with role set to 3 and assign it. From 5e32a3e01e7dc7e5ee5065f744a72dae3d2b6039 Mon Sep 17 00:00:00 2001 From: tetta maeda Date: Wed, 9 Oct 2024 21:26:35 +0900 Subject: [PATCH 16/17] refactor: rename variable --- .../src/modules/handlers/subscribe_handler.rs | 326 +++++++++--------- .../modules/handlers/subscribe_ok_handler.rs | 208 +++++------ 2 files changed, 266 insertions(+), 268 deletions(-) diff --git a/moqt-server/src/modules/handlers/subscribe_handler.rs b/moqt-server/src/modules/handlers/subscribe_handler.rs index 419fe9d9..9f442c22 100644 --- a/moqt-server/src/modules/handlers/subscribe_handler.rs +++ b/moqt-server/src/modules/handlers/subscribe_handler.rs @@ -74,13 +74,13 @@ pub(crate) async fn subscribe_handler( // Since only the track_namespace is recorded in ANNOUNCE, use track_namespace to determine the publisher // TODO: multiple publishers for the same track_namespace - let publisher_session_id = pubsub_relation_manager_repository + let upstream_session_id = pubsub_relation_manager_repository .get_publisher_session_id(subscribe_message.track_namespace().clone()) .await .unwrap(); - match publisher_session_id { + match upstream_session_id { Some(session_id) => { - let (publisher_subscribe_id, publisher_track_alias) = + let (upstream_subscribe_id, upstream_track_alias) = match set_subscriber_and_publisher_subscription( pubsub_relation_manager_repository, &subscribe_message, @@ -89,8 +89,8 @@ pub(crate) async fn subscribe_handler( ) .await { - Ok((publisher_subscribe_id, publisher_track_alias)) => { - (publisher_subscribe_id, publisher_track_alias) + Ok((upstream_subscribe_id, upstream_track_alias)) => { + (upstream_subscribe_id, upstream_track_alias) } Err(e) => { bail!("cannot register publisher and subscriber: {:?}", e); @@ -100,10 +100,8 @@ pub(crate) async fn subscribe_handler( let mut relaying_subscribe_message = subscribe_message.clone(); // Replace the subscribe_id and track_alias in the SUBSCRIBE message to request to the upstream publisher - relaying_subscribe_message.replace_subscribe_id_and_track_alias( - publisher_subscribe_id, - publisher_track_alias, - ); + relaying_subscribe_message + .replace_subscribe_id_and_track_alias(upstream_subscribe_id, upstream_track_alias); let message: Box = Box::new(relaying_subscribe_message.clone()); tracing::debug!( @@ -150,74 +148,74 @@ async fn set_only_subscriber_subscription( subscribe_message: &Subscribe, client: &MOQTClient, ) -> Result<()> { - let subscriber_client_id = client.id; - let subscriber_subscribe_id = subscribe_message.subscribe_id(); - let subscriber_track_alias = subscribe_message.track_alias(); - let subscriber_track_namespace = subscribe_message.track_namespace().to_vec(); - let subscriber_track_name = subscribe_message.track_name().to_string(); - let subscriber_priority = subscribe_message.subscriber_priority(); - let subscriber_group_order = subscribe_message.group_order(); - let subscriber_filter_type = subscribe_message.filter_type(); - let subscriber_start_group = subscribe_message.start_group(); - let subscriber_start_object = subscribe_message.start_object(); - let subscriber_end_group = subscribe_message.end_group(); - let subscriber_end_object = subscribe_message.end_object(); + let downstream_client_id = client.id; + let downstream_subscribe_id = subscribe_message.subscribe_id(); + let downstream_track_alias = subscribe_message.track_alias(); + let downstream_track_namespace = subscribe_message.track_namespace().to_vec(); + let downstream_track_name = subscribe_message.track_name().to_string(); + let downstream_priority = subscribe_message.subscriber_priority(); + let downstream_group_order = subscribe_message.group_order(); + let downstream_filter_type = subscribe_message.filter_type(); + let downstream_start_group = subscribe_message.start_group(); + let downstream_start_object = subscribe_message.start_object(); + let downstream_end_group = subscribe_message.end_group(); + let downstream_end_object = subscribe_message.end_object(); // Get publisher subscription already exists - let publisher_subscription = pubsub_relation_manager_repository + let upstream_subscription = pubsub_relation_manager_repository .get_publisher_subscription_by_full_track_name( - subscriber_track_namespace.clone(), - subscriber_track_name.clone(), + downstream_track_namespace.clone(), + downstream_track_name.clone(), ) .await? .unwrap(); pubsub_relation_manager_repository .set_subscriber_subscription( - subscriber_client_id, - subscriber_subscribe_id, - subscriber_track_alias, - subscriber_track_namespace.clone(), - subscriber_track_name.clone(), - subscriber_priority, - subscriber_group_order, - subscriber_filter_type, - subscriber_start_group, - subscriber_start_object, - subscriber_end_group, - subscriber_end_object, + downstream_client_id, + downstream_subscribe_id, + downstream_track_alias, + downstream_track_namespace.clone(), + downstream_track_name.clone(), + downstream_priority, + downstream_group_order, + downstream_filter_type, + downstream_start_group, + downstream_start_object, + downstream_end_group, + downstream_end_object, ) .await?; - let publisher_session_id = pubsub_relation_manager_repository - .get_publisher_session_id(subscriber_track_namespace) + let upstream_session_id = pubsub_relation_manager_repository + .get_publisher_session_id(downstream_track_namespace) .await? .unwrap(); - let (publisher_track_namespace, publisher_track_name) = - publisher_subscription.get_track_namespace_and_name(); + let (upstream_track_namespace, upstream_track_name) = + upstream_subscription.get_track_namespace_and_name(); // Get publisher subscribe id to register pubsup relation - let publisher_subscribe_id = pubsub_relation_manager_repository + let upstream_subscribe_id = pubsub_relation_manager_repository .get_publisher_subscribe_id( - publisher_track_namespace, - publisher_track_name, - publisher_session_id, + upstream_track_namespace, + upstream_track_name, + upstream_session_id, ) .await? .unwrap(); pubsub_relation_manager_repository .register_pubsup_relation( - publisher_session_id, - publisher_subscribe_id, - subscriber_client_id, - subscriber_subscribe_id, + upstream_session_id, + upstream_subscribe_id, + downstream_client_id, + downstream_subscribe_id, ) .await?; pubsub_relation_manager_repository - .activate_subscriber_subscription(subscriber_client_id, subscriber_subscribe_id) + .activate_subscriber_subscription(downstream_client_id, downstream_subscribe_id) .await?; Ok(()) @@ -227,63 +225,63 @@ async fn set_subscriber_and_publisher_subscription( pubsub_relation_manager_repository: &mut dyn PubSubRelationManagerRepository, subscribe_message: &Subscribe, client: &MOQTClient, - publisher_session_id: usize, + upstream_session_id: usize, ) -> Result<(u64, u64)> { - let subscriber_client_id = client.id; - let subscriber_subscribe_id = subscribe_message.subscribe_id(); - let subscriber_track_alias = subscribe_message.track_alias(); - let subscriber_track_namespace = subscribe_message.track_namespace().to_vec(); - let subscriber_track_name = subscribe_message.track_name().to_string(); - let subscriber_priority = subscribe_message.subscriber_priority(); - let subscriber_group_order = subscribe_message.group_order(); - let subscriber_filter_type = subscribe_message.filter_type(); - let subscriber_start_group = subscribe_message.start_group(); - let subscriber_start_object = subscribe_message.start_object(); - let subscriber_end_group = subscribe_message.end_group(); - let subscriber_end_object = subscribe_message.end_object(); + let downstream_client_id = client.id; + let downstream_subscribe_id = subscribe_message.subscribe_id(); + let downstream_track_alias = subscribe_message.track_alias(); + let downstream_track_namespace = subscribe_message.track_namespace().to_vec(); + let downstream_track_name = subscribe_message.track_name().to_string(); + let downstream_priority = subscribe_message.subscriber_priority(); + let downstream_group_order = subscribe_message.group_order(); + let downstream_filter_type = subscribe_message.filter_type(); + let downstream_start_group = subscribe_message.start_group(); + let downstream_start_object = subscribe_message.start_object(); + let downstream_end_group = subscribe_message.end_group(); + let downstream_end_object = subscribe_message.end_object(); pubsub_relation_manager_repository .set_subscriber_subscription( - subscriber_client_id, - subscriber_subscribe_id, - subscriber_track_alias, - subscriber_track_namespace.clone(), - subscriber_track_name.clone(), - subscriber_priority, - subscriber_group_order, - subscriber_filter_type, - subscriber_start_group, - subscriber_start_object, - subscriber_end_group, - subscriber_end_object, + downstream_client_id, + downstream_subscribe_id, + downstream_track_alias, + downstream_track_namespace.clone(), + downstream_track_name.clone(), + downstream_priority, + downstream_group_order, + downstream_filter_type, + downstream_start_group, + downstream_start_object, + downstream_end_group, + downstream_end_object, ) .await?; - let (publisher_subscribe_id, publisher_track_alias) = pubsub_relation_manager_repository + let (upstream_subscribe_id, upstream_track_alias) = pubsub_relation_manager_repository .set_publisher_subscription( - publisher_session_id, - subscriber_track_namespace.clone(), - subscriber_track_name.clone(), - subscriber_priority, - subscriber_group_order, - subscriber_filter_type, - subscriber_start_group, - subscriber_start_object, - subscriber_end_group, - subscriber_end_object, + upstream_session_id, + downstream_track_namespace.clone(), + downstream_track_name.clone(), + downstream_priority, + downstream_group_order, + downstream_filter_type, + downstream_start_group, + downstream_start_object, + downstream_end_group, + downstream_end_object, ) .await?; pubsub_relation_manager_repository .register_pubsup_relation( - publisher_session_id, - publisher_subscribe_id, - subscriber_client_id, - subscriber_subscribe_id, + upstream_session_id, + upstream_subscribe_id, + downstream_client_id, + downstream_subscribe_id, ) .await?; - Ok((publisher_subscribe_id, publisher_track_alias)) + Ok((upstream_subscribe_id, upstream_track_alias)) } #[cfg(test)] @@ -313,12 +311,12 @@ mod success { #[tokio::test] async fn normal_case_track_not_exists() { // Generate SUBSCRIBE message - let expected_publisher_subscribe_id = 0; + let expected_upstream_subscribe_id = 0; let subscribe_id = 0; let track_alias = 0; let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); let track_name = "track_name".to_string(); - let subscriber_priority = 0; + let downstream_priority = 0; let group_order = GroupOrder::Ascending; let filter_type = FilterType::LatestGroup; let start_group = None; @@ -334,7 +332,7 @@ mod success { track_alias, track_namespace.clone(), track_name.clone(), - subscriber_priority, + downstream_priority, group_order, filter_type, start_group, @@ -346,8 +344,8 @@ mod success { .unwrap(); // Generate client - let subscriber_session_id = 0; - let mut client = MOQTClient::new(subscriber_session_id); + let downstream_session_id = 0; + let mut client = MOQTClient::new(downstream_session_id); // Generate PubSubRelationManagerInterface let (track_namespace_tx, mut track_namespace_rx) = @@ -356,19 +354,19 @@ mod success { let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); - let publisher_session_id = 1; + let upstream_session_id = 1; let max_subscribe_id = 10; // Register the publisher track in advance let _ = pubsub_relation_manager - .setup_publisher(max_subscribe_id, publisher_session_id) + .setup_publisher(max_subscribe_id, upstream_session_id) .await; let _ = pubsub_relation_manager - .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) + .set_publisher_announced_namespace(track_namespace.clone(), upstream_session_id) .await; let _ = pubsub_relation_manager - .setup_subscriber(max_subscribe_id, subscriber_session_id) + .setup_subscriber(max_subscribe_id, downstream_session_id) .await; // Generate SendStreamDispacher @@ -381,7 +379,7 @@ mod success { let (uni_relay_tx, _) = mpsc::channel::>>(1024); let _ = send_stream_tx .send(SendStreamDispatchCommand::Set { - session_id: publisher_session_id, + session_id: upstream_session_id, stream_direction: StreamDirection::Bi, sender: uni_relay_tx, }) @@ -410,13 +408,13 @@ mod success { println!("{:?}", pubsub_relation); let subscribers = pubsub_relation - .get_subscribers(publisher_session_id, expected_publisher_subscribe_id) + .get_subscribers(upstream_session_id, expected_upstream_subscribe_id) .unwrap(); - let (subscriber_session_id, subscriber_subscribe_id) = subscribers.first().unwrap(); + let (downstream_session_id, downstream_subscribe_id) = subscribers.first().unwrap(); - assert_eq!(subscriber_session_id, subscriber_session_id); - assert_eq!(subscriber_subscribe_id, subscriber_subscribe_id); + assert_eq!(downstream_session_id, downstream_session_id); + assert_eq!(downstream_subscribe_id, downstream_subscribe_id); } #[tokio::test] @@ -426,7 +424,7 @@ mod success { let track_alias = 0; let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); let track_name = "track_name".to_string(); - let subscriber_priority = 0; + let downstream_priority = 0; let group_order = GroupOrder::Ascending; let filter_type = FilterType::LatestGroup; let start_group = None; @@ -442,7 +440,7 @@ mod success { track_alias, track_namespace.clone(), track_name.clone(), - subscriber_priority, + downstream_priority, group_order, filter_type, start_group, @@ -454,8 +452,8 @@ mod success { .unwrap(); // Generate client - let subscriber_session_id = 0; - let mut client = MOQTClient::new(subscriber_session_id); + let downstream_session_id = 0; + let mut client = MOQTClient::new(downstream_session_id); // Generate PubSubRelationManagerInterface let (track_namespace_tx, mut track_namespace_rx) = @@ -464,22 +462,22 @@ mod success { let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); - let publisher_session_id = 1; + let upstream_session_id = 1; let max_subscribe_id = 10; // Register the publisher track in advance let _ = pubsub_relation_manager - .setup_publisher(max_subscribe_id, publisher_session_id) + .setup_publisher(max_subscribe_id, upstream_session_id) .await; let _ = pubsub_relation_manager - .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) + .set_publisher_announced_namespace(track_namespace.clone(), upstream_session_id) .await; - let (publisher_subscribe_id, _) = pubsub_relation_manager + let (upstream_subscribe_id, _) = pubsub_relation_manager .set_publisher_subscription( - publisher_session_id, + upstream_session_id, track_namespace, track_name, - subscriber_priority, + downstream_priority, group_order, filter_type, start_group, @@ -491,7 +489,7 @@ mod success { .unwrap(); let _ = pubsub_relation_manager - .setup_subscriber(max_subscribe_id, subscriber_session_id) + .setup_subscriber(max_subscribe_id, downstream_session_id) .await; // Generate SendStreamDispacher @@ -504,7 +502,7 @@ mod success { let (uni_relay_tx, _) = mpsc::channel::>>(1024); let _ = send_stream_tx .send(SendStreamDispatchCommand::Set { - session_id: publisher_session_id, + session_id: upstream_session_id, stream_direction: StreamDirection::Bi, sender: uni_relay_tx, }) @@ -531,13 +529,13 @@ mod success { assert_eq!(producers.len(), 1); let subscribers = pubsub_relation - .get_subscribers(publisher_session_id, publisher_subscribe_id) + .get_subscribers(upstream_session_id, upstream_subscribe_id) .unwrap(); - let (subscriber_session_id, subscriber_subscribe_id) = subscribers.first().unwrap(); + let (downstream_session_id, downstream_subscribe_id) = subscribers.first().unwrap(); - assert_eq!(subscriber_session_id, subscriber_session_id); - assert_eq!(subscriber_subscribe_id, subscriber_subscribe_id); + assert_eq!(downstream_session_id, downstream_session_id); + assert_eq!(downstream_subscribe_id, downstream_subscribe_id); } } @@ -571,7 +569,7 @@ mod failure { let track_alias = 0; let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); let track_name = "track_name".to_string(); - let subscriber_priority = 0; + let downstream_priority = 0; let group_order = GroupOrder::Ascending; let filter_type = FilterType::LatestGroup; let start_group = None; @@ -587,7 +585,7 @@ mod failure { track_alias, track_namespace.clone(), track_name.clone(), - subscriber_priority, + downstream_priority, group_order, filter_type, start_group, @@ -599,8 +597,8 @@ mod failure { .unwrap(); // Generate client - let subscriber_session_id = 0; - let mut client = MOQTClient::new(subscriber_session_id); + let downstream_session_id = 0; + let mut client = MOQTClient::new(downstream_session_id); // Generate PubSubRelationManagerInterface (register subscriber in advance) let (track_namespace_tx, mut track_namespace_rx) = @@ -609,27 +607,27 @@ mod failure { let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); - let publisher_session_id = 1; + let upstream_session_id = 1; let max_subscribe_id = 10; let _ = pubsub_relation_manager - .setup_publisher(max_subscribe_id, publisher_session_id) + .setup_publisher(max_subscribe_id, upstream_session_id) .await; let _ = pubsub_relation_manager - .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) + .set_publisher_announced_namespace(track_namespace.clone(), upstream_session_id) .await; let _ = pubsub_relation_manager - .setup_subscriber(max_subscribe_id, subscriber_session_id) + .setup_subscriber(max_subscribe_id, downstream_session_id) .await; let _ = pubsub_relation_manager .set_subscriber_subscription( - subscriber_session_id, + downstream_session_id, subscribe_id, track_alias, track_namespace, track_name, - subscriber_priority, + downstream_priority, group_order, filter_type, start_group, @@ -649,7 +647,7 @@ mod failure { let (uni_relay_tx, _) = mpsc::channel::>>(1024); let _ = send_stream_tx .send(SendStreamDispatchCommand::Set { - session_id: publisher_session_id, + session_id: upstream_session_id, stream_direction: StreamDirection::Bi, sender: uni_relay_tx, }) @@ -674,7 +672,7 @@ mod failure { let track_alias = 0; let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); let track_name = "track_name".to_string(); - let subscriber_priority = 0; + let downstream_priority = 0; let group_order = GroupOrder::Ascending; let filter_type = FilterType::LatestGroup; let start_group = None; @@ -690,7 +688,7 @@ mod failure { track_alias, track_namespace.clone(), track_name.clone(), - subscriber_priority, + downstream_priority, group_order, filter_type, start_group, @@ -702,8 +700,8 @@ mod failure { .unwrap(); // Generate client - let subscriber_session_id = 0; - let mut client = MOQTClient::new(subscriber_session_id); + let downstream_session_id = 0; + let mut client = MOQTClient::new(downstream_session_id); // Generate PubSubRelationManagerInterface let (track_namespace_tx, mut track_namespace_rx) = @@ -712,18 +710,18 @@ mod failure { let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); - let publisher_session_id = 1; + let upstream_session_id = 1; let max_subscribe_id = 10; let _ = pubsub_relation_manager - .setup_publisher(max_subscribe_id, publisher_session_id) + .setup_publisher(max_subscribe_id, upstream_session_id) .await; let _ = pubsub_relation_manager - .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) + .set_publisher_announced_namespace(track_namespace.clone(), upstream_session_id) .await; let _ = pubsub_relation_manager - .setup_subscriber(max_subscribe_id, subscriber_session_id) + .setup_subscriber(max_subscribe_id, downstream_session_id) .await; // Generate SendStreamDispacher (without set sender) @@ -752,7 +750,7 @@ mod failure { let track_alias = 0; let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); let track_name = "track_name"; - let subscriber_priority = 0; + let downstream_priority = 0; let group_order = GroupOrder::Ascending; let filter_type = FilterType::LatestGroup; let start_group = None; @@ -768,7 +766,7 @@ mod failure { track_alias, track_namespace, track_name.to_string(), - subscriber_priority, + downstream_priority, group_order, filter_type, start_group, @@ -780,8 +778,8 @@ mod failure { .unwrap(); // Generate client - let subscriber_session_id = 0; - let mut client = MOQTClient::new(subscriber_session_id); + let downstream_session_id = 0; + let mut client = MOQTClient::new(downstream_session_id); // Generate PubSubRelationManagerInterface (without set publisher) let (track_namespace_tx, mut track_namespace_rx) = @@ -790,11 +788,11 @@ mod failure { let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); - let publisher_session_id = 1; + let upstream_session_id = 1; let max_subscribe_id = 10; let _ = pubsub_relation_manager - .setup_subscriber(max_subscribe_id, subscriber_session_id) + .setup_subscriber(max_subscribe_id, downstream_session_id) .await; // Generate SendStreamDispacher @@ -807,7 +805,7 @@ mod failure { let (uni_relay_tx, _) = mpsc::channel::>>(1024); let _ = send_stream_tx .send(SendStreamDispatchCommand::Set { - session_id: publisher_session_id, + session_id: upstream_session_id, stream_direction: StreamDirection::Bi, sender: uni_relay_tx, }) @@ -832,7 +830,7 @@ mod failure { let track_aliases = [0, 1]; let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); let track_name = "track_name".to_string(); - let subscriber_priority = 0; + let downstream_priority = 0; let group_order = GroupOrder::Ascending; let filter_type = FilterType::LatestGroup; let start_group = None; @@ -851,7 +849,7 @@ mod failure { track_aliases[i], track_namespace.clone(), track_name.clone(), - subscriber_priority, + downstream_priority, group_order, filter_type, start_group, @@ -866,8 +864,8 @@ mod failure { } // Generate client - let subscriber_session_id = 0; - let mut client = MOQTClient::new(subscriber_session_id); + let downstream_session_id = 0; + let mut client = MOQTClient::new(downstream_session_id); // Generate PubSubRelationManagerInterface let (track_namespace_tx, mut track_namespace_rx) = @@ -876,19 +874,19 @@ mod failure { let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); - let publisher_session_id = 1; + let upstream_session_id = 1; let max_subscribe_id = 0; // Register the publisher track in advance let _ = pubsub_relation_manager - .setup_publisher(max_subscribe_id, publisher_session_id) + .setup_publisher(max_subscribe_id, upstream_session_id) .await; let _ = pubsub_relation_manager - .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) + .set_publisher_announced_namespace(track_namespace.clone(), upstream_session_id) .await; let _ = pubsub_relation_manager - .setup_subscriber(max_subscribe_id, subscriber_session_id) + .setup_subscriber(max_subscribe_id, downstream_session_id) .await; // Generate SendStreamDispacher @@ -901,7 +899,7 @@ mod failure { let (uni_relay_tx, _) = mpsc::channel::>>(1024); let _ = send_stream_tx .send(SendStreamDispatchCommand::Set { - session_id: publisher_session_id, + session_id: upstream_session_id, stream_direction: StreamDirection::Bi, sender: uni_relay_tx, }) @@ -934,7 +932,7 @@ mod failure { let track_alias = 0; let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); let track_name = "track_name".to_string(); - let subscriber_priority = 0; + let downstream_priority = 0; let group_order = GroupOrder::Ascending; let filter_type = FilterType::LatestGroup; let start_group = None; @@ -953,7 +951,7 @@ mod failure { track_alias, track_namespace.clone(), track_name.clone(), - subscriber_priority, + downstream_priority, group_order, filter_type, start_group, @@ -968,8 +966,8 @@ mod failure { } // Generate client - let subscriber_session_id = 0; - let mut client = MOQTClient::new(subscriber_session_id); + let downstream_session_id = 0; + let mut client = MOQTClient::new(downstream_session_id); // Generate PubSubRelationManagerInterface let (track_namespace_tx, mut track_namespace_rx) = @@ -978,19 +976,19 @@ mod failure { let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); - let publisher_session_id = 1; + let upstream_session_id = 1; let max_subscribe_id = 10; // Register the publisher track in advance let _ = pubsub_relation_manager - .setup_publisher(max_subscribe_id, publisher_session_id) + .setup_publisher(max_subscribe_id, upstream_session_id) .await; let _ = pubsub_relation_manager - .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) + .set_publisher_announced_namespace(track_namespace.clone(), upstream_session_id) .await; let _ = pubsub_relation_manager - .setup_subscriber(max_subscribe_id, subscriber_session_id) + .setup_subscriber(max_subscribe_id, downstream_session_id) .await; // Generate SendStreamDispacher @@ -1003,7 +1001,7 @@ mod failure { let (uni_relay_tx, _) = mpsc::channel::>>(1024); let _ = send_stream_tx .send(SendStreamDispatchCommand::Set { - session_id: publisher_session_id, + session_id: upstream_session_id, stream_direction: StreamDirection::Bi, sender: uni_relay_tx, }) diff --git a/moqt-server/src/modules/handlers/subscribe_ok_handler.rs b/moqt-server/src/modules/handlers/subscribe_ok_handler.rs index 3caefa51..045bd883 100644 --- a/moqt-server/src/modules/handlers/subscribe_ok_handler.rs +++ b/moqt-server/src/modules/handlers/subscribe_ok_handler.rs @@ -16,37 +16,37 @@ pub(crate) async fn subscribe_ok_handler( tracing::debug!("subscribe_ok_message: {:#?}", subscribe_ok_message); - let publisher_session_id = client.id; - let publisher_subscribe_id = subscribe_ok_message.subscribe_id(); + let upstream_session_id = client.id; + let upstream_subscribe_id = subscribe_ok_message.subscribe_id(); // Determine the SUBSCRIBER who sent the SUBSCRIBE using the track_namespace and track_name let ids = pubsub_relation_manager_repository .get_requesting_subscriber_session_ids_and_subscribe_ids( - publisher_subscribe_id, - publisher_session_id, + upstream_subscribe_id, + upstream_session_id, ) .await?; match ids { Some(ids) => { let _ = pubsub_relation_manager_repository - .activate_publisher_subscription(publisher_session_id, publisher_subscribe_id) + .activate_publisher_subscription(upstream_session_id, upstream_subscribe_id) .await; // Notify all waiting subscribers with the SUBSCRIBE_OK message - for (subscriber_session_id, subscriber_subscribe_id) in ids.iter() { + for (downstream_session_id, downstream_subscribe_id) in ids.iter() { let mut relaying_subscribe_ok_message = subscribe_ok_message.clone(); - relaying_subscribe_ok_message.replace_subscribe_id(*subscriber_subscribe_id); + relaying_subscribe_ok_message.replace_subscribe_id(*downstream_subscribe_id); let message: Box = Box::new(relaying_subscribe_ok_message.clone()); tracing::debug!( "message: {:#?} is sent to relay handler for client {:?}", relaying_subscribe_ok_message, - subscriber_session_id + downstream_session_id ); send_stream_dispatcher_repository .send_message_to_send_stream_thread( - *subscriber_session_id, + *downstream_session_id, message, StreamDirection::Bi, ) @@ -54,8 +54,8 @@ pub(crate) async fn subscribe_ok_handler( pubsub_relation_manager_repository .activate_subscriber_subscription( - *subscriber_session_id, - *subscriber_subscribe_id, + *downstream_session_id, + *downstream_subscribe_id, ) .await?; @@ -67,7 +67,7 @@ pub(crate) async fn subscribe_ok_handler( // Activate the publisher because it is in the Requesting state let _ = pubsub_relation_manager_repository - .activate_publisher_subscription(publisher_session_id, publisher_subscribe_id) + .activate_publisher_subscription(upstream_session_id, upstream_subscribe_id) .await; } } @@ -101,11 +101,11 @@ mod success { #[tokio::test] async fn normal_case() { // Generate SUBSCRIBE_OK message - let subscriber_subscribe_id = 0; + let downstream_subscribe_id = 0; let track_alias = 0; let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); let track_name = "track_name".to_string(); - let subscriber_priority = 0; + let downstream_priority = 0; let expires = 1; let group_order = GroupOrder::Ascending; let content_exists = false; @@ -121,7 +121,7 @@ mod success { let subscribe_parameters = vec![version_specific_parameter]; let subscribe_ok = SubscribeOk::new( - subscriber_subscribe_id, + downstream_subscribe_id, expires, group_order, content_exists, @@ -133,8 +133,8 @@ mod success { subscribe_ok.packetize(&mut buf); // Generate client - let publisher_session_id = 1; - let mut client = MOQTClient::new(publisher_session_id); + let upstream_session_id = 1; + let mut client = MOQTClient::new(upstream_session_id); // Generate PubSubRelationManagerInterface let (track_namespace_tx, mut track_namespace_rx) = @@ -143,21 +143,21 @@ mod success { let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); - let subscriber_session_id = 2; + let downstream_session_id = 2; let max_subscribe_id = 10; let _ = pubsub_relation_manager - .setup_publisher(max_subscribe_id, publisher_session_id) + .setup_publisher(max_subscribe_id, upstream_session_id) .await; let _ = pubsub_relation_manager - .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) + .set_publisher_announced_namespace(track_namespace.clone(), upstream_session_id) .await; - let (publisher_subscribe_id, _) = pubsub_relation_manager + let (upstream_subscribe_id, _) = pubsub_relation_manager .set_publisher_subscription( - publisher_session_id, + upstream_session_id, track_namespace.clone(), track_name.clone(), - subscriber_priority, + downstream_priority, group_order, filter_type, start_group, @@ -169,16 +169,16 @@ mod success { .unwrap(); let _ = pubsub_relation_manager - .setup_subscriber(max_subscribe_id, subscriber_session_id) + .setup_subscriber(max_subscribe_id, downstream_session_id) .await; let _ = pubsub_relation_manager .set_subscriber_subscription( - subscriber_session_id, - subscriber_subscribe_id, + downstream_session_id, + downstream_subscribe_id, track_alias, track_namespace, track_name, - subscriber_priority, + downstream_priority, group_order, filter_type, start_group, @@ -189,10 +189,10 @@ mod success { .await; let _ = pubsub_relation_manager .register_pubsup_relation( - publisher_session_id, - publisher_subscribe_id, - subscriber_session_id, - subscriber_subscribe_id, + upstream_session_id, + upstream_subscribe_id, + downstream_session_id, + downstream_subscribe_id, ) .await; @@ -206,7 +206,7 @@ mod success { let (uni_relay_tx, _) = mpsc::channel::>>(1024); let _ = send_stream_tx .send(SendStreamDispatchCommand::Set { - session_id: subscriber_session_id, + session_id: downstream_session_id, stream_direction: StreamDirection::Bi, sender: uni_relay_tx, }) @@ -251,11 +251,11 @@ mod failure { #[tokio::test] async fn relay_fail() { // Generate SUBSCRIBE_OK message - let subscriber_subscribe_id = 0; + let downstream_subscribe_id = 0; let track_alias = 0; let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); let track_name = "track_name".to_string(); - let subscriber_priority = 0; + let downstream_priority = 0; let expires = 1; let group_order = GroupOrder::Ascending; let content_exists = false; @@ -271,7 +271,7 @@ mod failure { let subscribe_parameters = vec![version_specific_parameter]; let subscribe_ok = SubscribeOk::new( - subscriber_subscribe_id, + downstream_subscribe_id, expires, group_order, content_exists, @@ -283,8 +283,8 @@ mod failure { subscribe_ok.packetize(&mut buf); // Generate client - let publisher_session_id = 1; - let mut client = MOQTClient::new(publisher_session_id); + let upstream_session_id = 1; + let mut client = MOQTClient::new(upstream_session_id); // Generate PubSubRelationManagerInterface let (track_namespace_tx, mut track_namespace_rx) = @@ -293,21 +293,21 @@ mod failure { let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); - let subscriber_session_id = 2; + let downstream_session_id = 2; let max_subscribe_id = 10; let _ = pubsub_relation_manager - .setup_publisher(max_subscribe_id, publisher_session_id) + .setup_publisher(max_subscribe_id, upstream_session_id) .await; let _ = pubsub_relation_manager - .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) + .set_publisher_announced_namespace(track_namespace.clone(), upstream_session_id) .await; - let (publisher_subscribe_id, _) = pubsub_relation_manager + let (upstream_subscribe_id, _) = pubsub_relation_manager .set_publisher_subscription( - publisher_session_id, + upstream_session_id, track_namespace.clone(), track_name.clone(), - subscriber_priority, + downstream_priority, group_order, filter_type, start_group, @@ -319,16 +319,16 @@ mod failure { .unwrap(); let _ = pubsub_relation_manager - .setup_subscriber(max_subscribe_id, subscriber_session_id) + .setup_subscriber(max_subscribe_id, downstream_session_id) .await; let _ = pubsub_relation_manager .set_subscriber_subscription( - subscriber_session_id, - subscriber_subscribe_id, + downstream_session_id, + downstream_subscribe_id, track_alias, track_namespace, track_name, - subscriber_priority, + downstream_priority, group_order, filter_type, start_group, @@ -339,10 +339,10 @@ mod failure { .await; let _ = pubsub_relation_manager .register_pubsup_relation( - publisher_session_id, - publisher_subscribe_id, - subscriber_session_id, - subscriber_subscribe_id, + upstream_session_id, + upstream_subscribe_id, + downstream_session_id, + downstream_subscribe_id, ) .await; @@ -368,10 +368,10 @@ mod failure { #[tokio::test] async fn subscriber_not_found() { // Generate SUBSCRIBE_OK message - let subscriber_subscribe_id = 0; + let downstream_subscribe_id = 0; let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); let track_name = "track_name".to_string(); - let subscriber_priority = 0; + let downstream_priority = 0; let expires = 1; let group_order = GroupOrder::Ascending; let content_exists = false; @@ -387,7 +387,7 @@ mod failure { let subscribe_parameters = vec![version_specific_parameter]; let subscribe_ok = SubscribeOk::new( - subscriber_subscribe_id, + downstream_subscribe_id, expires, group_order, content_exists, @@ -399,8 +399,8 @@ mod failure { subscribe_ok.packetize(&mut buf); // Generate client - let publisher_session_id = 1; - let mut client = MOQTClient::new(publisher_session_id); + let upstream_session_id = 1; + let mut client = MOQTClient::new(upstream_session_id); // Generate PubSubRelationManagerInterface let (track_namespace_tx, mut track_namespace_rx) = @@ -409,21 +409,21 @@ mod failure { let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); - let subscriber_session_id = 2; + let downstream_session_id = 2; let max_subscribe_id = 10; let _ = pubsub_relation_manager - .setup_publisher(max_subscribe_id, publisher_session_id) + .setup_publisher(max_subscribe_id, upstream_session_id) .await; let _ = pubsub_relation_manager - .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) + .set_publisher_announced_namespace(track_namespace.clone(), upstream_session_id) .await; let _ = pubsub_relation_manager .set_publisher_subscription( - publisher_session_id, + upstream_session_id, track_namespace.clone(), track_name.clone(), - subscriber_priority, + downstream_priority, group_order, filter_type, start_group, @@ -444,7 +444,7 @@ mod failure { let (uni_relay_tx, _) = mpsc::channel::>>(1024); let _ = send_stream_tx .send(SendStreamDispatchCommand::Set { - session_id: subscriber_session_id, + session_id: downstream_session_id, stream_direction: StreamDirection::Bi, sender: uni_relay_tx, }) @@ -465,11 +465,11 @@ mod failure { #[tokio::test] async fn subscriber_already_activated() { // Generate SUBSCRIBE_OK message - let subscriber_subscribe_id = 0; + let downstream_subscribe_id = 0; let track_alias = 0; let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); let track_name = "track_name".to_string(); - let subscriber_priority = 0; + let downstream_priority = 0; let expires = 1; let group_order = GroupOrder::Ascending; let content_exists = false; @@ -485,7 +485,7 @@ mod failure { let subscribe_parameters = vec![version_specific_parameter]; let subscribe_ok = SubscribeOk::new( - subscriber_subscribe_id, + downstream_subscribe_id, expires, group_order, content_exists, @@ -497,8 +497,8 @@ mod failure { subscribe_ok.packetize(&mut buf); // Generate client - let publisher_session_id = 1; - let mut client = MOQTClient::new(publisher_session_id); + let upstream_session_id = 1; + let mut client = MOQTClient::new(upstream_session_id); // Generate PubSubRelationManagerInterface let (track_namespace_tx, mut track_namespace_rx) = @@ -507,21 +507,21 @@ mod failure { let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); - let subscriber_session_id = 2; + let downstream_session_id = 2; let max_subscribe_id = 10; let _ = pubsub_relation_manager - .setup_publisher(max_subscribe_id, publisher_session_id) + .setup_publisher(max_subscribe_id, upstream_session_id) .await; let _ = pubsub_relation_manager - .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) + .set_publisher_announced_namespace(track_namespace.clone(), upstream_session_id) .await; - let (publisher_subscribe_id, _) = pubsub_relation_manager + let (upstream_subscribe_id, _) = pubsub_relation_manager .set_publisher_subscription( - publisher_session_id, + upstream_session_id, track_namespace.clone(), track_name.clone(), - subscriber_priority, + downstream_priority, group_order, filter_type, start_group, @@ -533,16 +533,16 @@ mod failure { .unwrap(); let _ = pubsub_relation_manager - .setup_subscriber(max_subscribe_id, subscriber_session_id) + .setup_subscriber(max_subscribe_id, downstream_session_id) .await; let _ = pubsub_relation_manager .set_subscriber_subscription( - subscriber_session_id, - subscriber_subscribe_id, + downstream_session_id, + downstream_subscribe_id, track_alias, track_namespace, track_name, - subscriber_priority, + downstream_priority, group_order, filter_type, start_group, @@ -553,15 +553,15 @@ mod failure { .await; let _ = pubsub_relation_manager .register_pubsup_relation( - publisher_session_id, - publisher_subscribe_id, - subscriber_session_id, - subscriber_subscribe_id, + upstream_session_id, + upstream_subscribe_id, + downstream_session_id, + downstream_subscribe_id, ) .await; let _ = pubsub_relation_manager - .activate_subscriber_subscription(subscriber_session_id, subscriber_subscribe_id) + .activate_subscriber_subscription(downstream_session_id, downstream_subscribe_id) .await; // Generate SendStreamDispacher @@ -574,7 +574,7 @@ mod failure { let (uni_relay_tx, _) = mpsc::channel::>>(1024); let _ = send_stream_tx .send(SendStreamDispatchCommand::Set { - session_id: subscriber_session_id, + session_id: downstream_session_id, stream_direction: StreamDirection::Bi, sender: uni_relay_tx, }) @@ -595,11 +595,11 @@ mod failure { #[tokio::test] async fn publisher_already_activated() { // Generate SUBSCRIBE_OK message - let subscriber_subscribe_id = 0; + let downstream_subscribe_id = 0; let track_alias = 0; let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); let track_name = "track_name".to_string(); - let subscriber_priority = 0; + let downstream_priority = 0; let expires = 1; let group_order = GroupOrder::Ascending; let content_exists = false; @@ -615,7 +615,7 @@ mod failure { let subscribe_parameters = vec![version_specific_parameter]; let subscribe_ok = SubscribeOk::new( - subscriber_subscribe_id, + downstream_subscribe_id, expires, group_order, content_exists, @@ -627,8 +627,8 @@ mod failure { subscribe_ok.packetize(&mut buf); // Generate client - let publisher_session_id = 1; - let mut client = MOQTClient::new(publisher_session_id); + let upstream_session_id = 1; + let mut client = MOQTClient::new(upstream_session_id); // Generate PubSubRelationManagerInterface let (track_namespace_tx, mut track_namespace_rx) = @@ -637,21 +637,21 @@ mod failure { let mut pubsub_relation_manager: PubSubRelationManagerInterface = PubSubRelationManagerInterface::new(track_namespace_tx); - let subscriber_session_id = 2; + let downstream_session_id = 2; let max_subscribe_id = 10; let _ = pubsub_relation_manager - .setup_publisher(max_subscribe_id, publisher_session_id) + .setup_publisher(max_subscribe_id, upstream_session_id) .await; let _ = pubsub_relation_manager - .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) + .set_publisher_announced_namespace(track_namespace.clone(), upstream_session_id) .await; - let (publisher_subscribe_id, _) = pubsub_relation_manager + let (upstream_subscribe_id, _) = pubsub_relation_manager .set_publisher_subscription( - publisher_session_id, + upstream_session_id, track_namespace.clone(), track_name.clone(), - subscriber_priority, + downstream_priority, group_order, filter_type, start_group, @@ -663,16 +663,16 @@ mod failure { .unwrap(); let _ = pubsub_relation_manager - .setup_subscriber(max_subscribe_id, subscriber_session_id) + .setup_subscriber(max_subscribe_id, downstream_session_id) .await; let _ = pubsub_relation_manager .set_subscriber_subscription( - subscriber_session_id, - subscriber_subscribe_id, + downstream_session_id, + downstream_subscribe_id, track_alias, track_namespace, track_name, - subscriber_priority, + downstream_priority, group_order, filter_type, start_group, @@ -683,15 +683,15 @@ mod failure { .await; let _ = pubsub_relation_manager .register_pubsup_relation( - publisher_session_id, - publisher_subscribe_id, - subscriber_session_id, - subscriber_subscribe_id, + upstream_session_id, + upstream_subscribe_id, + downstream_session_id, + downstream_subscribe_id, ) .await; let _ = pubsub_relation_manager - .activate_publisher_subscription(publisher_session_id, publisher_subscribe_id) + .activate_publisher_subscription(upstream_session_id, upstream_subscribe_id) .await; // Generate SendStreamDispacher @@ -704,7 +704,7 @@ mod failure { let (uni_relay_tx, _) = mpsc::channel::>>(1024); let _ = send_stream_tx .send(SendStreamDispatchCommand::Set { - session_id: subscriber_session_id, + session_id: downstream_session_id, stream_direction: StreamDirection::Bi, sender: uni_relay_tx, }) From 01e87a8ad6ced0b0dce158de4f245b5d4db0eea7 Mon Sep 17 00:00:00 2001 From: tetta maeda Date: Thu, 10 Oct 2024 13:20:25 +0900 Subject: [PATCH 17/17] refactor: variable name and behavior --- moqt-core/src/modules.rs | 2 +- .../messages/control_messages/subscribe.rs | 5 +- .../messages/control_messages/subscribe_ok.rs | 2 +- .../{subscription_models.rs => models.rs} | 1 - .../subscriptions.rs | 80 +- .../src/modules/models/subscriptions/nodes.rs | 3 + .../subscriptions/nodes/consumers.rs} | 157 ++-- .../subscriptions/nodes/producers.rs} | 135 ++-- .../subscriptions/nodes/registry.rs} | 11 +- .../{subscription_models => models}/tracks.rs | 0 .../pubsub_relation_manager_repository.rs | 63 +- .../src/modules/subscription_models/nodes.rs | 3 - moqt-server/src/lib.rs | 4 +- .../src/modules/control_message_handler.rs | 24 +- .../src/modules/handlers/announce_handler.rs | 39 +- .../modules/handlers/server_setup_handler.rs | 50 +- .../src/modules/handlers/subscribe_handler.rs | 178 ++--- .../modules/handlers/subscribe_ok_handler.rs | 124 +-- .../modules/handlers/unannounce_handler.rs | 2 +- .../src/modules/pubsub_relation_manager.rs | 2 +- .../pubsub_relation_manager/commands.rs | 62 +- .../pubsub_relation_manager/manager.rs | 138 ++-- .../pubsub_relation_manager/relation.rs | 18 +- .../{interface.rs => wrapper.rs} | 748 +++++++++--------- 24 files changed, 915 insertions(+), 936 deletions(-) rename moqt-core/src/modules/{subscription_models.rs => models.rs} (72%) rename moqt-core/src/modules/{subscription_models => models}/subscriptions.rs (80%) create mode 100644 moqt-core/src/modules/models/subscriptions/nodes.rs rename moqt-core/src/modules/{subscription_models/nodes/consumer_node.rs => models/subscriptions/nodes/consumers.rs} (82%) rename moqt-core/src/modules/{subscription_models/nodes/producer_node.rs => models/subscriptions/nodes/producers.rs} (83%) rename moqt-core/src/modules/{subscription_models/nodes/node_registory.rs => models/subscriptions/nodes/registry.rs} (82%) rename moqt-core/src/modules/{subscription_models => models}/tracks.rs (100%) delete mode 100644 moqt-core/src/modules/subscription_models/nodes.rs rename moqt-server/src/modules/pubsub_relation_manager/{interface.rs => wrapper.rs} (67%) diff --git a/moqt-core/src/modules.rs b/moqt-core/src/modules.rs index 5e301015..c9e763a7 100644 --- a/moqt-core/src/modules.rs +++ b/moqt-core/src/modules.rs @@ -1,9 +1,9 @@ pub mod constants; pub mod control_message_type; pub mod messages; +pub mod models; pub mod moqt_client; pub mod pubsub_relation_manager_repository; pub mod send_stream_dispatcher_repository; -pub mod subscription_models; pub mod variable_bytes; pub mod variable_integer; diff --git a/moqt-core/src/modules/messages/control_messages/subscribe.rs b/moqt-core/src/modules/messages/control_messages/subscribe.rs index 0ae1d55e..e77b683a 100644 --- a/moqt-core/src/modules/messages/control_messages/subscribe.rs +++ b/moqt-core/src/modules/messages/control_messages/subscribe.rs @@ -152,8 +152,11 @@ impl Subscribe { self.end_object } - pub fn replace_subscribe_id_and_track_alias(&mut self, subscribe_id: u64, track_alias: u64) { + pub fn set_subscribe_id(&mut self, subscribe_id: u64) { self.subscribe_id = subscribe_id; + } + + pub fn set_track_alias(&mut self, track_alias: u64) { self.track_alias = track_alias; } } diff --git a/moqt-core/src/modules/messages/control_messages/subscribe_ok.rs b/moqt-core/src/modules/messages/control_messages/subscribe_ok.rs index 6ca2bf36..17f8613b 100644 --- a/moqt-core/src/modules/messages/control_messages/subscribe_ok.rs +++ b/moqt-core/src/modules/messages/control_messages/subscribe_ok.rs @@ -48,7 +48,7 @@ impl SubscribeOk { self.subscribe_id } - pub fn replace_subscribe_id(&mut self, subscribe_id: u64) { + pub fn set_subscribe_id(&mut self, subscribe_id: u64) { self.subscribe_id = subscribe_id; } } diff --git a/moqt-core/src/modules/subscription_models.rs b/moqt-core/src/modules/models.rs similarity index 72% rename from moqt-core/src/modules/subscription_models.rs rename to moqt-core/src/modules/models.rs index 39e1df2a..5a741dbb 100644 --- a/moqt-core/src/modules/subscription_models.rs +++ b/moqt-core/src/modules/models.rs @@ -1,3 +1,2 @@ -pub mod nodes; pub mod subscriptions; pub mod tracks; diff --git a/moqt-core/src/modules/subscription_models/subscriptions.rs b/moqt-core/src/modules/models/subscriptions.rs similarity index 80% rename from moqt-core/src/modules/subscription_models/subscriptions.rs rename to moqt-core/src/modules/models/subscriptions.rs index 4ca86c7d..970e0848 100644 --- a/moqt-core/src/modules/subscription_models/subscriptions.rs +++ b/moqt-core/src/modules/models/subscriptions.rs @@ -1,6 +1,8 @@ +pub mod nodes; + use crate::messages::control_messages::subscribe::{FilterType, GroupOrder}; -use crate::subscription_models::tracks::ForwardingPreference; -use crate::subscription_models::tracks::Track; +use crate::models::tracks::ForwardingPreference; +use crate::models::tracks::Track; #[derive(Debug, PartialEq, Eq, Clone, Copy)] enum Status { @@ -18,7 +20,7 @@ pub struct Subscription { start_object: Option, end_group: Option, end_object: Option, - subscription_status: Status, + status: Status, } impl Subscription { @@ -52,7 +54,7 @@ impl Subscription { start_object, end_group, end_object, - subscription_status: Status::Requesting, + status: Status::Requesting, } } @@ -60,17 +62,17 @@ impl Subscription { if self.is_active() { false } else { - self.subscription_status = Status::Active; + self.status = Status::Active; true } } pub fn is_active(&self) -> bool { - self.subscription_status == Status::Active + self.status == Status::Active } pub fn is_requesting(&self) -> bool { - self.subscription_status == Status::Requesting + self.status == Status::Requesting } pub fn set_forwarding_preference(&mut self, forwarding_preference: ForwardingPreference) { @@ -87,11 +89,11 @@ impl Subscription { } #[cfg(test)] -pub(crate) mod test_utils { +pub(crate) mod test_helper_fn { use crate::messages::control_messages::subscribe::{FilterType, GroupOrder}; #[derive(Debug, Clone)] - pub(crate) struct SubscriptionUtils { + pub(crate) struct SubscriptionVariables { pub(crate) track_alias: u64, pub(crate) track_namespace: Vec, pub(crate) track_name: String, @@ -104,31 +106,29 @@ pub(crate) mod test_utils { pub(crate) end_object: Option, } - impl SubscriptionUtils { - pub(crate) fn normal_variable() -> Self { - let track_alias = 0; - let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); - let track_name = "track_name".to_string(); - let subscriber_priority = 0; - let group_order = GroupOrder::Ascending; - let filter_type = FilterType::AbsoluteStart; - let start_group = Some(0); - let start_object = Some(0); - let end_group = None; - let end_object = None; - - SubscriptionUtils { - track_alias, - track_namespace, - track_name, - subscriber_priority, - group_order, - filter_type, - start_group, - start_object, - end_group, - end_object, - } + pub(crate) fn common_subscription_variable() -> SubscriptionVariables { + let track_alias = 0; + let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); + let track_name = "track_name".to_string(); + let subscriber_priority = 0; + let group_order = GroupOrder::Ascending; + let filter_type = FilterType::AbsoluteStart; + let start_group = Some(0); + let start_object = Some(0); + let end_group = None; + let end_object = None; + + SubscriptionVariables { + track_alias, + track_namespace, + track_name, + subscriber_priority, + group_order, + filter_type, + start_group, + start_object, + end_group, + end_object, } } } @@ -136,7 +136,7 @@ pub(crate) mod test_utils { #[cfg(test)] mod success { use super::*; - use crate::subscription_models::tracks::ForwardingPreference; + use crate::models::tracks::ForwardingPreference; #[test] fn new() { @@ -182,7 +182,7 @@ mod success { #[test] fn activate() { - let variable = test_utils::SubscriptionUtils::normal_variable(); + let variable = test_helper_fn::common_subscription_variable(); let mut subscription = Subscription::new( variable.track_alias, @@ -207,7 +207,7 @@ mod success { #[test] fn is_active() { - let variable = test_utils::SubscriptionUtils::normal_variable(); + let variable = test_helper_fn::common_subscription_variable(); let mut subscription = Subscription::new( variable.track_alias, @@ -231,7 +231,7 @@ mod success { #[test] fn is_requesting() { - let variable = test_utils::SubscriptionUtils::normal_variable(); + let variable = test_helper_fn::common_subscription_variable(); let mut subscription = Subscription::new( variable.track_alias, @@ -255,7 +255,7 @@ mod success { #[test] fn get_track_namespace_and_name() { - let variable = test_utils::SubscriptionUtils::normal_variable(); + let variable = test_helper_fn::common_subscription_variable(); let subscription = Subscription::new( variable.track_alias, @@ -279,7 +279,7 @@ mod success { #[test] fn get_track_alias() { - let variable = test_utils::SubscriptionUtils::normal_variable(); + let variable = test_helper_fn::common_subscription_variable(); let subscription = Subscription::new( variable.track_alias, diff --git a/moqt-core/src/modules/models/subscriptions/nodes.rs b/moqt-core/src/modules/models/subscriptions/nodes.rs new file mode 100644 index 00000000..107cb521 --- /dev/null +++ b/moqt-core/src/modules/models/subscriptions/nodes.rs @@ -0,0 +1,3 @@ +pub mod consumers; +pub mod producers; +pub mod registry; diff --git a/moqt-core/src/modules/subscription_models/nodes/consumer_node.rs b/moqt-core/src/modules/models/subscriptions/nodes/consumers.rs similarity index 82% rename from moqt-core/src/modules/subscription_models/nodes/consumer_node.rs rename to moqt-core/src/modules/models/subscriptions/nodes/consumers.rs index be508cde..0671b309 100644 --- a/moqt-core/src/modules/subscription_models/nodes/consumer_node.rs +++ b/moqt-core/src/modules/models/subscriptions/nodes/consumers.rs @@ -1,6 +1,5 @@ use crate::messages::control_messages::subscribe::{FilterType, GroupOrder}; -use crate::subscription_models::nodes::node_registory::SubscriptionNodeRegistory; -use crate::subscription_models::subscriptions::Subscription; +use crate::models::subscriptions::{nodes::registry::SubscriptionNodeRegistry, Subscription}; use anyhow::{bail, Result}; use std::collections::HashMap; @@ -14,6 +13,7 @@ pub struct Consumer { announced_namespaces: Vec, subscribing_namespace_prefixes: Vec, subscriptions: HashMap, + latest_subscribe_id: u64, } impl Consumer { @@ -23,11 +23,12 @@ impl Consumer { announced_namespaces: Vec::new(), subscribing_namespace_prefixes: Vec::new(), subscriptions: HashMap::new(), + latest_subscribe_id: 0, } } } -impl SubscriptionNodeRegistory for Consumer { +impl SubscriptionNodeRegistry for Consumer { fn set_subscription( &mut self, subscribe_id: SubscribeId, @@ -57,6 +58,7 @@ impl SubscriptionNodeRegistory for Consumer { None, ); + self.latest_subscribe_id = subscribe_id + 1; self.subscriptions.insert(subscribe_id, subscription); Ok(()) @@ -105,9 +107,9 @@ impl SubscriptionNodeRegistory for Consumer { fn activate_subscription(&mut self, subscribe_id: SubscribeId) -> Result { let subscription = self.subscriptions.get_mut(&subscribe_id).unwrap(); - let activate = subscription.activate(); + let is_activated = subscription.activate(); - Ok(activate) + Ok(is_activated) } fn is_requesting(&self, subscribe_id: SubscribeId) -> bool { @@ -122,23 +124,29 @@ impl SubscriptionNodeRegistory for Consumer { Ok(()) } - fn is_within_max_subscribe_id(&self, subscribe_id: SubscribeId) -> bool { - subscribe_id <= self.max_subscriber_id - } + fn is_subscribe_id_valid(&self, subscribe_id: SubscribeId) -> bool { + let is_less_than_max_subscribe_id = subscribe_id < self.max_subscriber_id; + let is_unique = !self.subscriptions.contains_key(&subscribe_id); - fn is_subscribe_id_unique(&self, subscribe_id: SubscribeId) -> bool { - !self.subscriptions.contains_key(&subscribe_id) + is_less_than_max_subscribe_id && is_unique } - fn is_track_alias_unique(&self, track_alias: TrackAlias) -> bool { - !self + fn is_track_alias_valid(&self, track_alias: TrackAlias) -> bool { + let is_unique = !self .subscriptions .values() - .any(|subscription| subscription.get_track_alias() == track_alias) + .any(|subscription| subscription.get_track_alias() == track_alias); + + is_unique } - fn find_unused_subscribe_id_and_track_alias(&self) -> Result<(SubscribeId, TrackAlias)> { - for subscribe_id in 0..=self.max_subscriber_id { - if !self.subscriptions.contains_key(&subscribe_id) { + + fn create_latest_subscribe_id_and_track_alias(&self) -> Result<(SubscribeId, TrackAlias)> { + let subscribe_id = self.latest_subscribe_id; + match self.is_subscribe_id_valid(subscribe_id) { + false => { + bail!("No available subscribe_id."); + } + true => { for track_alias in 0.. { if !self .subscriptions @@ -148,10 +156,10 @@ impl SubscriptionNodeRegistory for Consumer { return Ok((subscribe_id, track_alias)); } } + + bail!("No available track_alias."); } } - - bail!("No available subscribe_id and track_alias."); } fn set_namespace(&mut self, namespace: TrackNamespace) -> Result<()> { @@ -214,12 +222,12 @@ impl SubscriptionNodeRegistory for Consumer { } #[cfg(test)] -pub(crate) mod test_utils { +pub(crate) mod test_helper_fn { use super::{Consumer, TrackNamespace}; - use crate::subscription_models::nodes::consumer_node::{FilterType, GroupOrder}; + use crate::models::subscriptions::nodes::consumers::{FilterType, GroupOrder}; #[derive(Debug, Clone)] - pub(crate) struct SubscriptionUtils { + pub(crate) struct SubscriptionVariables { pub(crate) consumer: Consumer, pub(crate) subscribe_id: u64, pub(crate) track_alias: u64, @@ -234,34 +242,32 @@ pub(crate) mod test_utils { pub(crate) end_object: Option, } - impl SubscriptionUtils { - pub(crate) fn normal_variable(subscribe_id: u64) -> Self { - let consumer = Consumer::new(10); - let track_alias = 0; - let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); - let track_name = "track_name".to_string(); - let subscriber_priority = 0; - let group_order = GroupOrder::Ascending; - let filter_type = FilterType::AbsoluteStart; - let start_group = Some(0); - let start_object = Some(0); - let end_group = None; - let end_object = None; - - SubscriptionUtils { - consumer, - subscribe_id, - track_alias, - track_namespace, - track_name, - subscriber_priority, - group_order, - filter_type, - start_group, - start_object, - end_group, - end_object, - } + pub(crate) fn common_subscription_variable(subscribe_id: u64) -> SubscriptionVariables { + let consumer = Consumer::new(10); + let track_alias = 0; + let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); + let track_name = "track_name".to_string(); + let subscriber_priority = 0; + let group_order = GroupOrder::Ascending; + let filter_type = FilterType::AbsoluteStart; + let start_group = Some(0); + let start_object = Some(0); + let end_group = None; + let end_object = None; + + SubscriptionVariables { + consumer, + subscribe_id, + track_alias, + track_namespace, + track_name, + subscriber_priority, + group_order, + filter_type, + start_group, + start_object, + end_group, + end_object, } } } @@ -269,13 +275,13 @@ pub(crate) mod test_utils { #[cfg(test)] mod success { use super::*; - use crate::subscription_models::nodes::consumer_node::test_utils::SubscriptionUtils; - use crate::subscription_models::subscriptions::Subscription; + use crate::models::subscriptions::nodes::consumers::test_helper_fn; + use crate::models::subscriptions::Subscription; #[test] fn set_subscription() { let subscribe_id = 0; - let mut variables = SubscriptionUtils::normal_variable(subscribe_id); + let mut variables = test_helper_fn::common_subscription_variable(subscribe_id); let result = variables.consumer.set_subscription( variables.subscribe_id, @@ -297,7 +303,7 @@ mod success { #[test] fn get_subscription() { let subscribe_id = 0; - let variables = SubscriptionUtils::normal_variable(subscribe_id); + let variables = test_helper_fn::common_subscription_variable(subscribe_id); let mut variables_clone = variables.clone(); let _ = variables_clone.consumer.set_subscription( @@ -339,7 +345,7 @@ mod success { #[test] fn get_subscription_by_full_track_name() { let subscribe_id = 0; - let variables = SubscriptionUtils::normal_variable(subscribe_id); + let variables = test_helper_fn::common_subscription_variable(subscribe_id); let mut variables_clone = variables.clone(); let _ = variables_clone.consumer.set_subscription( @@ -384,7 +390,7 @@ mod success { #[test] fn get_subscribe_id() { let subscribe_id = 0; - let mut variables = SubscriptionUtils::normal_variable(subscribe_id); + let mut variables = test_helper_fn::common_subscription_variable(subscribe_id); let _ = variables.consumer.set_subscription( variables.subscribe_id, @@ -414,7 +420,7 @@ mod success { #[test] fn has_track() { let subscribe_id = 0; - let mut variables = SubscriptionUtils::normal_variable(subscribe_id); + let mut variables = test_helper_fn::common_subscription_variable(subscribe_id); let _ = variables.consumer.set_subscription( variables.subscribe_id, @@ -440,7 +446,7 @@ mod success { #[test] fn activate_subscription() { let subscribe_id = 0; - let mut variables = SubscriptionUtils::normal_variable(subscribe_id); + let mut variables = test_helper_fn::common_subscription_variable(subscribe_id); let _ = variables.consumer.set_subscription( variables.subscribe_id, @@ -467,7 +473,7 @@ mod success { #[test] fn is_requesting() { let subscribe_id = 0; - let mut variables = SubscriptionUtils::normal_variable(subscribe_id); + let mut variables = test_helper_fn::common_subscription_variable(subscribe_id); let _ = variables.consumer.set_subscription( variables.subscribe_id, @@ -491,7 +497,7 @@ mod success { #[test] fn delete_subscription() { let subscribe_id = 0; - let mut variables = SubscriptionUtils::normal_variable(subscribe_id); + let mut variables = test_helper_fn::common_subscription_variable(subscribe_id); let _ = variables.consumer.set_subscription( variables.subscribe_id, @@ -515,20 +521,9 @@ mod success { } #[test] - fn is_within_max_subscribe_id() { - let max_subscribe_id = 10; - let subscribe_id = 5; - - let consumer = Consumer::new(max_subscribe_id); - let result = consumer.is_within_max_subscribe_id(subscribe_id); - - assert!(result); - } - - #[test] - fn is_subscribe_id_unique() { + fn is_subscribe_id_valid() { let subscribe_id = 0; - let mut variables = SubscriptionUtils::normal_variable(subscribe_id); + let mut variables = test_helper_fn::common_subscription_variable(subscribe_id); let _ = variables.consumer.set_subscription( variables.subscribe_id, @@ -546,15 +541,15 @@ mod success { let result = variables .consumer - .is_subscribe_id_unique(variables.subscribe_id); + .is_subscribe_id_valid(variables.subscribe_id); assert!(!result); } #[test] - fn is_track_alias_unique() { + fn is_track_alias_valid() { let track_alias = 100; - let mut variables = SubscriptionUtils::normal_variable(0); + let mut variables = test_helper_fn::common_subscription_variable(0); let _ = variables.consumer.set_subscription( variables.subscribe_id, @@ -570,15 +565,15 @@ mod success { variables.end_object, ); - let result = variables.consumer.is_track_alias_unique(track_alias); + let result = variables.consumer.is_track_alias_valid(track_alias); assert!(result); } #[test] - fn find_unused_subscribe_id_and_track_alias() { + fn create_latest_subscribe_id_and_track_alias() { let subscribe_id = 0; - let mut variables = SubscriptionUtils::normal_variable(subscribe_id); + let mut variables = test_helper_fn::common_subscription_variable(subscribe_id); let _ = variables.consumer.set_subscription( variables.subscribe_id, @@ -596,9 +591,13 @@ mod success { let result = variables .consumer - .find_unused_subscribe_id_and_track_alias(); + .create_latest_subscribe_id_and_track_alias(); assert!(result.is_ok()); + + let expected_id_and_alias = (variables.subscribe_id + 1, variables.track_alias + 1); + + assert_eq!(result.unwrap(), expected_id_and_alias); } #[test] diff --git a/moqt-core/src/modules/subscription_models/nodes/producer_node.rs b/moqt-core/src/modules/models/subscriptions/nodes/producers.rs similarity index 83% rename from moqt-core/src/modules/subscription_models/nodes/producer_node.rs rename to moqt-core/src/modules/models/subscriptions/nodes/producers.rs index f4a49ace..1616a4cc 100644 --- a/moqt-core/src/modules/subscription_models/nodes/producer_node.rs +++ b/moqt-core/src/modules/models/subscriptions/nodes/producers.rs @@ -1,6 +1,6 @@ use crate::messages::control_messages::subscribe::{FilterType, GroupOrder}; -use crate::subscription_models::nodes::node_registory::SubscriptionNodeRegistory; -use crate::subscription_models::subscriptions::Subscription; + +use crate::models::subscriptions::{nodes::registry::SubscriptionNodeRegistry, Subscription}; use anyhow::{bail, Result}; use std::collections::HashMap; @@ -27,7 +27,7 @@ impl Producer { } } -impl SubscriptionNodeRegistory for Producer { +impl SubscriptionNodeRegistry for Producer { fn set_subscription( &mut self, subscribe_id: SubscribeId, @@ -105,9 +105,9 @@ impl SubscriptionNodeRegistory for Producer { fn activate_subscription(&mut self, subscribe_id: SubscribeId) -> Result { let subscription = self.subscriptions.get_mut(&subscribe_id).unwrap(); - let activate = subscription.activate(); + let is_activated = subscription.activate(); - Ok(activate) + Ok(is_activated) } fn is_requesting(&self, subscribe_id: SubscribeId) -> bool { @@ -122,22 +122,23 @@ impl SubscriptionNodeRegistory for Producer { Ok(()) } - fn is_within_max_subscribe_id(&self, subscribe_id: SubscribeId) -> bool { - subscribe_id <= self.max_subscriber_id - } + fn is_subscribe_id_valid(&self, subscribe_id: SubscribeId) -> bool { + let is_less_than_max_subscribe_id = subscribe_id < self.max_subscriber_id; + let is_unique = !self.subscriptions.contains_key(&subscribe_id); - fn is_subscribe_id_unique(&self, subscribe_id: SubscribeId) -> bool { - !self.subscriptions.contains_key(&subscribe_id) + is_less_than_max_subscribe_id && is_unique } - fn is_track_alias_unique(&self, track_alias: TrackAlias) -> bool { - !self + fn is_track_alias_valid(&self, track_alias: TrackAlias) -> bool { + let is_unique = !self .subscriptions .values() - .any(|subscription| subscription.get_track_alias() == track_alias) + .any(|subscription| subscription.get_track_alias() == track_alias); + + is_unique } - fn find_unused_subscribe_id_and_track_alias(&self) -> Result<(SubscribeId, TrackAlias)> { + fn create_latest_subscribe_id_and_track_alias(&self) -> Result<(SubscribeId, TrackAlias)> { unimplemented!() } @@ -202,12 +203,12 @@ impl SubscriptionNodeRegistory for Producer { } #[cfg(test)] -pub(crate) mod test_utils { +pub(crate) mod test_helper_fn { use super::{Producer, TrackNamespace}; - use crate::subscription_models::nodes::producer_node::{FilterType, GroupOrder}; + use crate::models::subscriptions::nodes::producers::{FilterType, GroupOrder}; #[derive(Debug, Clone)] - pub(crate) struct SubscriptionUtils { + pub(crate) struct SubscriptionVariables { pub(crate) producer: Producer, pub(crate) subscribe_id: u64, pub(crate) track_alias: u64, @@ -222,34 +223,32 @@ pub(crate) mod test_utils { pub(crate) end_object: Option, } - impl SubscriptionUtils { - pub(crate) fn normal_variable(subscribe_id: u64) -> Self { - let producer = Producer::new(10); - let track_alias = 0; - let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); - let track_name = "track_name".to_string(); - let subscriber_priority = 0; - let group_order = GroupOrder::Ascending; - let filter_type = FilterType::AbsoluteStart; - let start_group = Some(0); - let start_object = Some(0); - let end_group = None; - let end_object = None; - - SubscriptionUtils { - producer, - subscribe_id, - track_alias, - track_namespace, - track_name, - subscriber_priority, - group_order, - filter_type, - start_group, - start_object, - end_group, - end_object, - } + pub(crate) fn common_subscription_variable(subscribe_id: u64) -> SubscriptionVariables { + let producer = Producer::new(10); + let track_alias = 0; + let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); + let track_name = "track_name".to_string(); + let subscriber_priority = 0; + let group_order = GroupOrder::Ascending; + let filter_type = FilterType::AbsoluteStart; + let start_group = Some(0); + let start_object = Some(0); + let end_group = None; + let end_object = None; + + SubscriptionVariables { + producer, + subscribe_id, + track_alias, + track_namespace, + track_name, + subscriber_priority, + group_order, + filter_type, + start_group, + start_object, + end_group, + end_object, } } } @@ -257,13 +256,12 @@ pub(crate) mod test_utils { #[cfg(test)] mod success { use super::*; - use crate::subscription_models::nodes::producer_node::test_utils::SubscriptionUtils; - use crate::subscription_models::subscriptions::Subscription; + use crate::models::subscriptions::Subscription; #[test] fn set_subscription() { let subscribe_id = 0; - let mut variables = SubscriptionUtils::normal_variable(subscribe_id); + let mut variables = test_helper_fn::common_subscription_variable(subscribe_id); let result = variables.producer.set_subscription( variables.subscribe_id, @@ -285,7 +283,7 @@ mod success { #[test] fn get_subscription() { let subscribe_id = 0; - let variables = SubscriptionUtils::normal_variable(subscribe_id); + let variables = test_helper_fn::common_subscription_variable(subscribe_id); let mut variables_clone = variables.clone(); let _ = variables_clone.producer.set_subscription( @@ -327,7 +325,7 @@ mod success { #[test] fn get_subscription_by_full_track_name() { let subscribe_id = 0; - let variables = SubscriptionUtils::normal_variable(subscribe_id); + let variables = test_helper_fn::common_subscription_variable(subscribe_id); let mut variables_clone = variables.clone(); let _ = variables_clone.producer.set_subscription( @@ -372,7 +370,7 @@ mod success { #[test] fn get_subscribe_id() { let subscribe_id = 0; - let mut variables = SubscriptionUtils::normal_variable(subscribe_id); + let mut variables = test_helper_fn::common_subscription_variable(subscribe_id); let _ = variables.producer.set_subscription( variables.subscribe_id, @@ -402,7 +400,7 @@ mod success { #[test] fn has_track() { let subscribe_id = 0; - let mut variables = SubscriptionUtils::normal_variable(subscribe_id); + let mut variables = test_helper_fn::common_subscription_variable(subscribe_id); let _ = variables.producer.set_subscription( variables.subscribe_id, @@ -428,7 +426,7 @@ mod success { #[test] fn activate_subscription() { let subscribe_id = 0; - let mut variables = SubscriptionUtils::normal_variable(subscribe_id); + let mut variables = test_helper_fn::common_subscription_variable(subscribe_id); let _ = variables.producer.set_subscription( variables.subscribe_id, @@ -455,7 +453,7 @@ mod success { #[test] fn is_requesting() { let subscribe_id = 0; - let mut variables = SubscriptionUtils::normal_variable(subscribe_id); + let mut variables = test_helper_fn::common_subscription_variable(subscribe_id); let _ = variables.producer.set_subscription( variables.subscribe_id, @@ -479,7 +477,7 @@ mod success { #[test] fn delete_subscription() { let subscribe_id = 0; - let mut variables = SubscriptionUtils::normal_variable(subscribe_id); + let mut variables = test_helper_fn::common_subscription_variable(subscribe_id); let _ = variables.producer.set_subscription( variables.subscribe_id, @@ -503,20 +501,9 @@ mod success { } #[test] - fn is_within_max_subscribe_id() { - let max_subscribe_id = 10; - let subscribe_id = 5; - - let producer = Producer::new(max_subscribe_id); - let result = producer.is_within_max_subscribe_id(subscribe_id); - - assert!(result); - } - - #[test] - fn is_subscribe_id_unique() { + fn is_subscribe_id_valid() { let subscribe_id = 0; - let mut variables = SubscriptionUtils::normal_variable(subscribe_id); + let mut variables = test_helper_fn::common_subscription_variable(subscribe_id); let _ = variables.producer.set_subscription( variables.subscribe_id, @@ -534,15 +521,15 @@ mod success { let result = variables .producer - .is_subscribe_id_unique(variables.subscribe_id); + .is_subscribe_id_valid(variables.subscribe_id); assert!(!result); } #[test] - fn is_track_alias_unique() { + fn is_track_alias_valid() { let track_alias = 100; - let mut variables = SubscriptionUtils::normal_variable(0); + let mut variables = test_helper_fn::common_subscription_variable(0); let _ = variables.producer.set_subscription( variables.subscribe_id, @@ -558,17 +545,17 @@ mod success { variables.end_object, ); - let result = variables.producer.is_track_alias_unique(track_alias); + let result = variables.producer.is_track_alias_valid(track_alias); assert!(result); } #[test] #[should_panic] - fn find_unused_subscribe_id_and_track_alias() { + fn create_latest_subscribe_id_and_track_alias() { let producer = Producer::new(10); - let _ = producer.find_unused_subscribe_id_and_track_alias(); + let _ = producer.create_latest_subscribe_id_and_track_alias(); } #[test] diff --git a/moqt-core/src/modules/subscription_models/nodes/node_registory.rs b/moqt-core/src/modules/models/subscriptions/nodes/registry.rs similarity index 82% rename from moqt-core/src/modules/subscription_models/nodes/node_registory.rs rename to moqt-core/src/modules/models/subscriptions/nodes/registry.rs index 5cc59861..02e70785 100644 --- a/moqt-core/src/modules/subscription_models/nodes/node_registory.rs +++ b/moqt-core/src/modules/models/subscriptions/nodes/registry.rs @@ -1,12 +1,12 @@ use crate::messages::control_messages::subscribe::{FilterType, GroupOrder}; -use crate::subscription_models::subscriptions::Subscription; +use crate::models::subscriptions::Subscription; use anyhow::Result; type SubscribeId = u64; type TrackNamespace = Vec; type TrackAlias = u64; -pub trait SubscriptionNodeRegistory { +pub trait SubscriptionNodeRegistry { #[allow(clippy::too_many_arguments)] fn set_subscription( &mut self, @@ -38,10 +38,9 @@ pub trait SubscriptionNodeRegistory { fn is_requesting(&self, subscribe_id: SubscribeId) -> bool; fn delete_subscription(&mut self, subscribe_id: SubscribeId) -> Result<()>; - fn is_within_max_subscribe_id(&self, subscribe_id: SubscribeId) -> bool; - fn is_subscribe_id_unique(&self, subscribe_id: SubscribeId) -> bool; - fn is_track_alias_unique(&self, track_alias: TrackAlias) -> bool; - fn find_unused_subscribe_id_and_track_alias(&self) -> Result<(SubscribeId, TrackAlias)>; + fn is_subscribe_id_valid(&self, subscribe_id: SubscribeId) -> bool; + fn is_track_alias_valid(&self, track_alias: TrackAlias) -> bool; + fn create_latest_subscribe_id_and_track_alias(&self) -> Result<(SubscribeId, TrackAlias)>; fn set_namespace(&mut self, namespace: TrackNamespace) -> Result<()>; fn get_namespaces(&self) -> Result<&Vec>; diff --git a/moqt-core/src/modules/subscription_models/tracks.rs b/moqt-core/src/modules/models/tracks.rs similarity index 100% rename from moqt-core/src/modules/subscription_models/tracks.rs rename to moqt-core/src/modules/models/tracks.rs diff --git a/moqt-core/src/modules/pubsub_relation_manager_repository.rs b/moqt-core/src/modules/pubsub_relation_manager_repository.rs index 5aff5ade..2a52478f 100644 --- a/moqt-core/src/modules/pubsub_relation_manager_repository.rs +++ b/moqt-core/src/modules/pubsub_relation_manager_repository.rs @@ -1,5 +1,5 @@ use crate::messages::control_messages::subscribe::{FilterType, GroupOrder}; -use crate::subscription_models::subscriptions::Subscription; +use crate::models::subscriptions::Subscription; use anyhow::Result; use async_trait::async_trait; @@ -9,55 +9,54 @@ pub trait PubSubRelationManagerRepository: Send + Sync { async fn setup_publisher( &self, max_subscribe_id: u64, - publisher_session_id: usize, + upstream_session_id: usize, ) -> Result<()>; - async fn set_publisher_announced_namespace( + async fn set_upstream_announced_namespace( &self, track_namespace: Vec, - publisher_session_id: usize, + upstream_session_id: usize, ) -> Result<()>; async fn setup_subscriber( &self, max_subscribe_id: u64, - subscriber_session_id: usize, + downstream_session_id: usize, ) -> Result<()>; - async fn is_valid_subscriber_subscribe_id( + async fn is_valid_downstream_subscribe_id( &self, subscribe_id: u64, - subscriber_session_id: usize, + downstream_session_id: usize, ) -> Result; - async fn is_valid_subscriber_track_alias( + async fn is_valid_downstream_track_alias( &self, track_alias: u64, - subscriber_session_id: usize, + downstream_session_id: usize, ) -> Result; async fn is_track_existing( &self, track_namespace: Vec, track_name: String, ) -> Result; - async fn get_publisher_subscription_by_full_track_name( + async fn get_upstream_subscription_by_full_track_name( &self, track_namespace: Vec, track_name: String, ) -> Result>; - async fn get_publisher_session_id(&self, track_namespace: Vec) - -> Result>; - async fn get_requesting_subscriber_session_ids_and_subscribe_ids( + async fn get_upstream_session_id(&self, track_namespace: Vec) -> Result>; + async fn get_requesting_downstream_session_ids_and_subscribe_ids( &self, - publisher_subscribe_id: u64, - publisher_session_id: usize, + upstream_subscribe_id: u64, + upstream_session_id: usize, ) -> Result>>; - async fn get_publisher_subscribe_id( + async fn get_upstream_subscribe_id( &self, track_namespace: Vec, track_name: String, - publisher_session_id: usize, + upstream_session_id: usize, ) -> Result>; #[allow(clippy::too_many_arguments)] - async fn set_subscriber_subscription( + async fn set_downstream_subscription( &self, - subscriber_session_id: usize, + downstream_session_id: usize, subscribe_id: u64, track_alias: u64, track_namespace: Vec, @@ -71,9 +70,9 @@ pub trait PubSubRelationManagerRepository: Send + Sync { end_object: Option, ) -> Result<()>; #[allow(clippy::too_many_arguments)] - async fn set_publisher_subscription( + async fn set_upstream_subscription( &self, - publisher_session_id: usize, + upstream_session_id: usize, track_namespace: Vec, track_name: String, subscriber_priority: u8, @@ -84,27 +83,27 @@ pub trait PubSubRelationManagerRepository: Send + Sync { end_group: Option, end_object: Option, ) -> Result<(u64, u64)>; - async fn register_pubsup_relation( + async fn set_pubsub_relation( &self, - publisher_session_id: usize, - publisher_subscribe_id: u64, - subscriber_session_id: usize, - subscriber_subscribe_id: u64, + upstream_session_id: usize, + upstream_subscribe_id: u64, + downstream_session_id: usize, + downstream_subscribe_id: u64, ) -> Result<()>; - async fn activate_subscriber_subscription( + async fn activate_downstream_subscription( &self, - subscriber_session_id: usize, + downstream_session_id: usize, subscribe_id: u64, ) -> Result; - async fn activate_publisher_subscription( + async fn activate_upstream_subscription( &self, - publisher_session_id: usize, + upstream_session_id: usize, subscribe_id: u64, ) -> Result; - async fn delete_publisher_announced_namespace( + async fn delete_upstream_announced_namespace( &self, track_namespace: Vec, - publisher_session_id: usize, + upstream_session_id: usize, ) -> Result; async fn delete_client(&self, session_id: usize) -> Result; } diff --git a/moqt-core/src/modules/subscription_models/nodes.rs b/moqt-core/src/modules/subscription_models/nodes.rs deleted file mode 100644 index f8f51b62..00000000 --- a/moqt-core/src/modules/subscription_models/nodes.rs +++ /dev/null @@ -1,3 +0,0 @@ -pub mod consumer_node; -pub mod node_registory; -pub mod producer_node; diff --git a/moqt-server/src/lib.rs b/moqt-server/src/lib.rs index cbc2b0a0..b48e15b1 100644 --- a/moqt-server/src/lib.rs +++ b/moqt-server/src/lib.rs @@ -282,7 +282,7 @@ async fn handle_connection( // Delete pub/sub information related to the client let pubsub_relation_manager = - modules::pubsub_relation_manager::interface::PubSubRelationManagerInterface::new( + modules::pubsub_relation_manager::wrapper::PubSubRelationManagerWrapper::new( track_namespace_tx.clone(), ); let _ = pubsub_relation_manager.delete_client(stable_id).await; @@ -329,7 +329,7 @@ async fn handle_incoming_bi_stream( let shread_send_stream = &mut stream.shread_send_stream; let mut pubsub_relation_manager = - modules::pubsub_relation_manager::interface::PubSubRelationManagerInterface::new( + modules::pubsub_relation_manager::wrapper::PubSubRelationManagerWrapper::new( track_namespace_tx.clone(), ); let mut send_stream_dispatcher = diff --git a/moqt-server/src/modules/control_message_handler.rs b/moqt-server/src/modules/control_message_handler.rs index 74759806..3be3014e 100644 --- a/moqt-server/src/modules/control_message_handler.rs +++ b/moqt-server/src/modules/control_message_handler.rs @@ -309,13 +309,13 @@ pub async fn control_message_handler( } #[cfg(test)] -pub(crate) mod test_utils { +pub(crate) mod test_helper_fn { use crate::modules::control_message_handler::control_message_handler; use crate::modules::control_message_handler::MessageProcessResult; use crate::modules::pubsub_relation_manager::{ - commands::PubSubRelationCommand, interface::PubSubRelationManagerInterface, - manager::pubsub_relation_manager, + commands::PubSubRelationCommand, manager::pubsub_relation_manager, + wrapper::PubSubRelationManagerWrapper, }; use crate::modules::send_stream_dispatcher::{ send_stream_dispatcher, SendStreamDispatchCommand, SendStreamDispatcher, @@ -340,12 +340,12 @@ pub(crate) mod test_utils { let mut client = MOQTClient::new(subscriber_sessin_id); client.update_status(client_status); - // Generate PubSubRelationManagerInterface + // Generate PubSubRelationManagerWrapper let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); - let mut pubsub_relation_manager: PubSubRelationManagerInterface = - PubSubRelationManagerInterface::new(track_namespace_tx); + let mut pubsub_relation_manager: PubSubRelationManagerWrapper = + PubSubRelationManagerWrapper::new(track_namespace_tx); // Generate SendStreamDispacher let (send_stream_tx, mut send_stream_rx) = mpsc::channel::(1024); @@ -372,14 +372,14 @@ mod success { use moqt_core::control_message_type::ControlMessageType; use moqt_core::moqt_client::MOQTClientStatus; - use crate::modules::control_message_handler::test_utils; + use crate::modules::control_message_handler::test_helper_fn; async fn assert_success( message_type_u8: u8, bytes_array: &[u8], client_status: MOQTClientStatus, ) { - let result = test_utils::packetize_buf_and_execute_control_message_handler( + let result = test_helper_fn::packetize_buf_and_execute_control_message_handler( message_type_u8, bytes_array, client_status, @@ -435,14 +435,14 @@ mod failure { use moqt_core::control_message_type::ControlMessageType; use moqt_core::moqt_client::MOQTClientStatus; - use crate::modules::control_message_handler::test_utils; + use crate::modules::control_message_handler::test_helper_fn; async fn assert_protocol_violation( message_type_u8: u8, bytes_array: &[u8], client_status: MOQTClientStatus, ) { - let result = test_utils::packetize_buf_and_execute_control_message_handler( + let result = test_helper_fn::packetize_buf_and_execute_control_message_handler( message_type_u8, bytes_array, client_status, @@ -462,7 +462,7 @@ mod failure { bytes_array: &[u8], client_status: MOQTClientStatus, ) { - let result = test_utils::packetize_buf_and_execute_control_message_handler( + let result = test_helper_fn::packetize_buf_and_execute_control_message_handler( message_type_u8, bytes_array, client_status, @@ -482,7 +482,7 @@ mod failure { bytes_array: &[u8], client_status: MOQTClientStatus, ) { - let result = test_utils::packetize_buf_and_execute_control_message_handler( + let result = test_helper_fn::packetize_buf_and_execute_control_message_handler( message_type_u8, bytes_array, client_status, diff --git a/moqt-server/src/modules/handlers/announce_handler.rs b/moqt-server/src/modules/handlers/announce_handler.rs index 0542cdf2..96701835 100644 --- a/moqt-server/src/modules/handlers/announce_handler.rs +++ b/moqt-server/src/modules/handlers/announce_handler.rs @@ -23,7 +23,7 @@ pub(crate) async fn announce_handler( // Record the announced Track Namespace let set_result = pubsub_relation_manager_repository - .set_publisher_announced_namespace(announce_message.track_namespace().clone(), client.id) + .set_upstream_announced_namespace(announce_message.track_namespace().clone(), client.id) .await; match set_result { @@ -54,8 +54,8 @@ pub(crate) async fn announce_handler( mod success { use crate::modules::handlers::announce_handler::{announce_handler, AnnounceResponse}; use crate::modules::pubsub_relation_manager::{ - commands::PubSubRelationCommand, interface::PubSubRelationManagerInterface, - manager::pubsub_relation_manager, + commands::PubSubRelationCommand, manager::pubsub_relation_manager, + wrapper::PubSubRelationManagerWrapper, }; use moqt_core::messages::moqt_payload::MOQTPayload; use moqt_core::pubsub_relation_manager_repository::PubSubRelationManagerRepository; @@ -85,20 +85,20 @@ mod success { announce_message.packetize(&mut buf); // Generate client - let publisher_session_id = 0; - let mut client = MOQTClient::new(publisher_session_id); + let upstream_session_id = 0; + let mut client = MOQTClient::new(upstream_session_id); - // Generate PubSubRelationManagerInterface + // Generate PubSubRelationManagerWrapper let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); - let mut pubsub_relation_manager: PubSubRelationManagerInterface = - PubSubRelationManagerInterface::new(track_namespace_tx); + let mut pubsub_relation_manager: PubSubRelationManagerWrapper = + PubSubRelationManagerWrapper::new(track_namespace_tx); let max_subscribe_id = 10; let _ = pubsub_relation_manager - .setup_publisher(max_subscribe_id, publisher_session_id) + .setup_publisher(max_subscribe_id, upstream_session_id) .await; // Execute announce_handler and get result @@ -116,8 +116,8 @@ mod success { mod failure { use crate::modules::handlers::announce_handler::{announce_handler, AnnounceResponse}; use crate::modules::pubsub_relation_manager::{ - commands::PubSubRelationCommand, interface::PubSubRelationManagerInterface, - manager::pubsub_relation_manager, + commands::PubSubRelationCommand, manager::pubsub_relation_manager, + wrapper::PubSubRelationManagerWrapper, }; use moqt_core::messages::moqt_payload::MOQTPayload; use moqt_core::pubsub_relation_manager_repository::PubSubRelationManagerRepository; @@ -147,28 +147,25 @@ mod failure { announce_message.packetize(&mut buf); // Generate client - let publisher_session_id = 0; - let mut client = MOQTClient::new(publisher_session_id); + let upstream_session_id = 0; + let mut client = MOQTClient::new(upstream_session_id); - // Generate PubSubRelationManagerInterface + // Generate PubSubRelationManagerWrapper let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); - let mut pubsub_relation_manager: PubSubRelationManagerInterface = - PubSubRelationManagerInterface::new(track_namespace_tx); + let mut pubsub_relation_manager: PubSubRelationManagerWrapper = + PubSubRelationManagerWrapper::new(track_namespace_tx); let max_subscribe_id = 10; let _ = pubsub_relation_manager - .setup_publisher(max_subscribe_id, publisher_session_id) + .setup_publisher(max_subscribe_id, upstream_session_id) .await; // Set the duplicated publisher in advance let _ = pubsub_relation_manager - .set_publisher_announced_namespace( - announce_message.track_namespace().clone(), - client.id, - ) + .set_upstream_announced_namespace(announce_message.track_namespace().clone(), client.id) .await; // Execute announce_handler and get result diff --git a/moqt-server/src/modules/handlers/server_setup_handler.rs b/moqt-server/src/modules/handlers/server_setup_handler.rs index bdb237ac..3b5b5494 100644 --- a/moqt-server/src/modules/handlers/server_setup_handler.rs +++ b/moqt-server/src/modules/handlers/server_setup_handler.rs @@ -102,8 +102,8 @@ pub(crate) async fn setup_handler( #[cfg(test)] mod success { use crate::modules::pubsub_relation_manager::{ - commands::PubSubRelationCommand, interface::PubSubRelationManagerInterface, - manager::pubsub_relation_manager, + commands::PubSubRelationCommand, manager::pubsub_relation_manager, + wrapper::PubSubRelationManagerWrapper, }; use crate::{constants, modules::handlers::server_setup_handler::setup_handler}; use moqt_core::messages::control_messages::{ @@ -122,12 +122,12 @@ mod success { ClientSetup::new(vec![constants::MOQ_TRANSPORT_VERSION], setup_parameters); let underlay_type = crate::constants::UnderlayType::WebTransport; - // Generate PubSubRelationManagerInterface + // Generate PubSubRelationManagerWrapper let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); - let mut pubsub_relation_manager: PubSubRelationManagerInterface = - PubSubRelationManagerInterface::new(track_namespace_tx); + let mut pubsub_relation_manager: PubSubRelationManagerWrapper = + PubSubRelationManagerWrapper::new(track_namespace_tx); let server_setup_message = setup_handler( client_setup_message, @@ -152,12 +152,12 @@ mod success { ClientSetup::new(vec![constants::MOQ_TRANSPORT_VERSION], setup_parameters); let underlay_type = crate::constants::UnderlayType::QUIC; - // Generate PubSubRelationManagerInterface + // Generate PubSubRelationManagerWrapper let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); - let mut pubsub_relation_manager: PubSubRelationManagerInterface = - PubSubRelationManagerInterface::new(track_namespace_tx); + let mut pubsub_relation_manager: PubSubRelationManagerWrapper = + PubSubRelationManagerWrapper::new(track_namespace_tx); let server_setup_message = setup_handler( client_setup_message, @@ -175,8 +175,8 @@ mod success { #[cfg(test)] mod failure { use crate::modules::pubsub_relation_manager::{ - commands::PubSubRelationCommand, interface::PubSubRelationManagerInterface, - manager::pubsub_relation_manager, + commands::PubSubRelationCommand, manager::pubsub_relation_manager, + wrapper::PubSubRelationManagerWrapper, }; use crate::{constants, modules::handlers::server_setup_handler::setup_handler}; use moqt_core::messages::control_messages::{ @@ -195,12 +195,12 @@ mod failure { ClientSetup::new(vec![constants::MOQ_TRANSPORT_VERSION], setup_parameters); let underlay_type = crate::constants::UnderlayType::WebTransport; - // Generate PubSubRelationManagerInterface + // Generate PubSubRelationManagerWrapper let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); - let mut pubsub_relation_manager: PubSubRelationManagerInterface = - PubSubRelationManagerInterface::new(track_namespace_tx); + let mut pubsub_relation_manager: PubSubRelationManagerWrapper = + PubSubRelationManagerWrapper::new(track_namespace_tx); let server_setup_message = setup_handler( client_setup_message, @@ -221,12 +221,12 @@ mod failure { ClientSetup::new(vec![constants::MOQ_TRANSPORT_VERSION], setup_parameters); let underlay_type = crate::constants::UnderlayType::WebTransport; - // Generate PubSubRelationManagerInterface + // Generate PubSubRelationManagerWrapper let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); - let mut pubsub_relation_manager: PubSubRelationManagerInterface = - PubSubRelationManagerInterface::new(track_namespace_tx); + let mut pubsub_relation_manager: PubSubRelationManagerWrapper = + PubSubRelationManagerWrapper::new(track_namespace_tx); let server_setup_message = setup_handler( client_setup_message, @@ -247,12 +247,12 @@ mod failure { ClientSetup::new(vec![constants::MOQ_TRANSPORT_VERSION], setup_parameters); let underlay_type = crate::constants::UnderlayType::QUIC; - // Generate PubSubRelationManagerInterface + // Generate PubSubRelationManagerWrapper let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); - let mut pubsub_relation_manager: PubSubRelationManagerInterface = - PubSubRelationManagerInterface::new(track_namespace_tx); + let mut pubsub_relation_manager: PubSubRelationManagerWrapper = + PubSubRelationManagerWrapper::new(track_namespace_tx); let server_setup_message = setup_handler( client_setup_message, @@ -274,12 +274,12 @@ mod failure { let client_setup_message = ClientSetup::new(vec![unsupported_version], setup_parameters); let underlay_type = crate::constants::UnderlayType::WebTransport; - // Generate PubSubRelationManagerInterface + // Generate PubSubRelationManagerWrapper let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); - let mut pubsub_relation_manager: PubSubRelationManagerInterface = - PubSubRelationManagerInterface::new(track_namespace_tx); + let mut pubsub_relation_manager: PubSubRelationManagerWrapper = + PubSubRelationManagerWrapper::new(track_namespace_tx); let server_setup_message = setup_handler( client_setup_message, @@ -304,12 +304,12 @@ mod failure { ClientSetup::new(vec![constants::MOQ_TRANSPORT_VERSION], setup_parameters); let underlay_type = crate::constants::UnderlayType::WebTransport; - // Generate PubSubRelationManagerInterface + // Generate PubSubRelationManagerWrapper let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); - let mut pubsub_relation_manager: PubSubRelationManagerInterface = - PubSubRelationManagerInterface::new(track_namespace_tx); + let mut pubsub_relation_manager: PubSubRelationManagerWrapper = + PubSubRelationManagerWrapper::new(track_namespace_tx); let server_setup_message = setup_handler( client_setup_message, diff --git a/moqt-server/src/modules/handlers/subscribe_handler.rs b/moqt-server/src/modules/handlers/subscribe_handler.rs index 9f442c22..5bc653a2 100644 --- a/moqt-server/src/modules/handlers/subscribe_handler.rs +++ b/moqt-server/src/modules/handlers/subscribe_handler.rs @@ -20,14 +20,14 @@ pub(crate) async fn subscribe_handler( tracing::debug!("subscribe_message: {:#?}", subscribe_message); if !pubsub_relation_manager_repository - .is_valid_subscriber_subscribe_id(subscribe_message.subscribe_id(), client.id) + .is_valid_downstream_subscribe_id(subscribe_message.subscribe_id(), client.id) .await? { // TODO: return TerminationErrorCode bail!("TooManySubscribers"); } if !pubsub_relation_manager_repository - .is_valid_subscriber_track_alias(subscribe_message.track_alias(), client.id) + .is_valid_downstream_track_alias(subscribe_message.track_alias(), client.id) .await? { // TODO: return TerminationErrorCode @@ -43,7 +43,7 @@ pub(crate) async fn subscribe_handler( .await .unwrap() { - let _ = set_only_subscriber_subscription( + let _ = set_downstream_subscription( pubsub_relation_manager_repository, &subscribe_message, client, @@ -75,33 +75,33 @@ pub(crate) async fn subscribe_handler( // Since only the track_namespace is recorded in ANNOUNCE, use track_namespace to determine the publisher // TODO: multiple publishers for the same track_namespace let upstream_session_id = pubsub_relation_manager_repository - .get_publisher_session_id(subscribe_message.track_namespace().clone()) + .get_upstream_session_id(subscribe_message.track_namespace().clone()) .await .unwrap(); match upstream_session_id { Some(session_id) => { - let (upstream_subscribe_id, upstream_track_alias) = - match set_subscriber_and_publisher_subscription( - pubsub_relation_manager_repository, - &subscribe_message, - client, - session_id, - ) - .await - { - Ok((upstream_subscribe_id, upstream_track_alias)) => { - (upstream_subscribe_id, upstream_track_alias) - } - Err(e) => { - bail!("cannot register publisher and subscriber: {:?}", e); - } - }; + let (upstream_subscribe_id, _) = match set_downstream_and_upstream_subscription( + pubsub_relation_manager_repository, + &subscribe_message, + client, + session_id, + ) + .await + { + Ok((upstream_subscribe_id, upstream_track_alias)) => { + (upstream_subscribe_id, upstream_track_alias) + } + Err(e) => { + bail!("cannot register publisher and subscriber: {:?}", e); + } + }; let mut relaying_subscribe_message = subscribe_message.clone(); // Replace the subscribe_id and track_alias in the SUBSCRIBE message to request to the upstream publisher - relaying_subscribe_message - .replace_subscribe_id_and_track_alias(upstream_subscribe_id, upstream_track_alias); + relaying_subscribe_message.set_subscribe_id(upstream_subscribe_id); + relaying_subscribe_message.set_track_alias(upstream_subscribe_id); + let message: Box = Box::new(relaying_subscribe_message.clone()); tracing::debug!( @@ -143,7 +143,7 @@ pub(crate) async fn subscribe_handler( } } -async fn set_only_subscriber_subscription( +async fn set_downstream_subscription( pubsub_relation_manager_repository: &mut dyn PubSubRelationManagerRepository, subscribe_message: &Subscribe, client: &MOQTClient, @@ -153,7 +153,7 @@ async fn set_only_subscriber_subscription( let downstream_track_alias = subscribe_message.track_alias(); let downstream_track_namespace = subscribe_message.track_namespace().to_vec(); let downstream_track_name = subscribe_message.track_name().to_string(); - let downstream_priority = subscribe_message.subscriber_priority(); + let subscriber_priority = subscribe_message.subscriber_priority(); let downstream_group_order = subscribe_message.group_order(); let downstream_filter_type = subscribe_message.filter_type(); let downstream_start_group = subscribe_message.start_group(); @@ -163,7 +163,7 @@ async fn set_only_subscriber_subscription( // Get publisher subscription already exists let upstream_subscription = pubsub_relation_manager_repository - .get_publisher_subscription_by_full_track_name( + .get_upstream_subscription_by_full_track_name( downstream_track_namespace.clone(), downstream_track_name.clone(), ) @@ -171,13 +171,13 @@ async fn set_only_subscriber_subscription( .unwrap(); pubsub_relation_manager_repository - .set_subscriber_subscription( + .set_downstream_subscription( downstream_client_id, downstream_subscribe_id, downstream_track_alias, downstream_track_namespace.clone(), downstream_track_name.clone(), - downstream_priority, + subscriber_priority, downstream_group_order, downstream_filter_type, downstream_start_group, @@ -188,16 +188,16 @@ async fn set_only_subscriber_subscription( .await?; let upstream_session_id = pubsub_relation_manager_repository - .get_publisher_session_id(downstream_track_namespace) + .get_upstream_session_id(downstream_track_namespace) .await? .unwrap(); let (upstream_track_namespace, upstream_track_name) = upstream_subscription.get_track_namespace_and_name(); - // Get publisher subscribe id to register pubsup relation + // Get publisher subscribe id to register pubsub relation let upstream_subscribe_id = pubsub_relation_manager_repository - .get_publisher_subscribe_id( + .get_upstream_subscribe_id( upstream_track_namespace, upstream_track_name, upstream_session_id, @@ -206,7 +206,7 @@ async fn set_only_subscriber_subscription( .unwrap(); pubsub_relation_manager_repository - .register_pubsup_relation( + .set_pubsub_relation( upstream_session_id, upstream_subscribe_id, downstream_client_id, @@ -215,13 +215,13 @@ async fn set_only_subscriber_subscription( .await?; pubsub_relation_manager_repository - .activate_subscriber_subscription(downstream_client_id, downstream_subscribe_id) + .activate_downstream_subscription(downstream_client_id, downstream_subscribe_id) .await?; Ok(()) } -async fn set_subscriber_and_publisher_subscription( +async fn set_downstream_and_upstream_subscription( pubsub_relation_manager_repository: &mut dyn PubSubRelationManagerRepository, subscribe_message: &Subscribe, client: &MOQTClient, @@ -232,7 +232,7 @@ async fn set_subscriber_and_publisher_subscription( let downstream_track_alias = subscribe_message.track_alias(); let downstream_track_namespace = subscribe_message.track_namespace().to_vec(); let downstream_track_name = subscribe_message.track_name().to_string(); - let downstream_priority = subscribe_message.subscriber_priority(); + let subscriber_priority = subscribe_message.subscriber_priority(); let downstream_group_order = subscribe_message.group_order(); let downstream_filter_type = subscribe_message.filter_type(); let downstream_start_group = subscribe_message.start_group(); @@ -241,13 +241,13 @@ async fn set_subscriber_and_publisher_subscription( let downstream_end_object = subscribe_message.end_object(); pubsub_relation_manager_repository - .set_subscriber_subscription( + .set_downstream_subscription( downstream_client_id, downstream_subscribe_id, downstream_track_alias, downstream_track_namespace.clone(), downstream_track_name.clone(), - downstream_priority, + subscriber_priority, downstream_group_order, downstream_filter_type, downstream_start_group, @@ -258,11 +258,11 @@ async fn set_subscriber_and_publisher_subscription( .await?; let (upstream_subscribe_id, upstream_track_alias) = pubsub_relation_manager_repository - .set_publisher_subscription( + .set_upstream_subscription( upstream_session_id, downstream_track_namespace.clone(), downstream_track_name.clone(), - downstream_priority, + subscriber_priority, downstream_group_order, downstream_filter_type, downstream_start_group, @@ -273,7 +273,7 @@ async fn set_subscriber_and_publisher_subscription( .await?; pubsub_relation_manager_repository - .register_pubsup_relation( + .set_pubsub_relation( upstream_session_id, upstream_subscribe_id, downstream_client_id, @@ -289,8 +289,8 @@ mod success { use crate::modules::handlers::subscribe_handler::subscribe_handler; use crate::modules::pubsub_relation_manager::{ commands::PubSubRelationCommand, - interface::{test_utils, PubSubRelationManagerInterface}, manager::pubsub_relation_manager, + wrapper::{test_helper_fn, PubSubRelationManagerWrapper}, }; use crate::modules::send_stream_dispatcher::{ send_stream_dispatcher, SendStreamDispatchCommand, SendStreamDispatcher, @@ -316,7 +316,7 @@ mod success { let track_alias = 0; let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); let track_name = "track_name".to_string(); - let downstream_priority = 0; + let subscriber_priority = 0; let group_order = GroupOrder::Ascending; let filter_type = FilterType::LatestGroup; let start_group = None; @@ -332,7 +332,7 @@ mod success { track_alias, track_namespace.clone(), track_name.clone(), - downstream_priority, + subscriber_priority, group_order, filter_type, start_group, @@ -347,12 +347,12 @@ mod success { let downstream_session_id = 0; let mut client = MOQTClient::new(downstream_session_id); - // Generate PubSubRelationManagerInterface + // Generate PubSubRelationManagerWrapper let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); - let mut pubsub_relation_manager: PubSubRelationManagerInterface = - PubSubRelationManagerInterface::new(track_namespace_tx); + let mut pubsub_relation_manager: PubSubRelationManagerWrapper = + PubSubRelationManagerWrapper::new(track_namespace_tx); let upstream_session_id = 1; let max_subscribe_id = 10; @@ -362,7 +362,7 @@ mod success { .setup_publisher(max_subscribe_id, upstream_session_id) .await; let _ = pubsub_relation_manager - .set_publisher_announced_namespace(track_namespace.clone(), upstream_session_id) + .set_upstream_announced_namespace(track_namespace.clone(), upstream_session_id) .await; let _ = pubsub_relation_manager @@ -401,7 +401,7 @@ mod success { // Check the subscriber is registered let (_, producers, pubsub_relation) = - test_utils::get_node_and_relation_clone(&pubsub_relation_manager).await; + test_helper_fn::get_node_and_relation_clone(&pubsub_relation_manager).await; assert_eq!(producers.len(), 1); @@ -424,7 +424,7 @@ mod success { let track_alias = 0; let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); let track_name = "track_name".to_string(); - let downstream_priority = 0; + let subscriber_priority = 0; let group_order = GroupOrder::Ascending; let filter_type = FilterType::LatestGroup; let start_group = None; @@ -440,7 +440,7 @@ mod success { track_alias, track_namespace.clone(), track_name.clone(), - downstream_priority, + subscriber_priority, group_order, filter_type, start_group, @@ -455,12 +455,12 @@ mod success { let downstream_session_id = 0; let mut client = MOQTClient::new(downstream_session_id); - // Generate PubSubRelationManagerInterface + // Generate PubSubRelationManagerWrapper let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); - let mut pubsub_relation_manager: PubSubRelationManagerInterface = - PubSubRelationManagerInterface::new(track_namespace_tx); + let mut pubsub_relation_manager: PubSubRelationManagerWrapper = + PubSubRelationManagerWrapper::new(track_namespace_tx); let upstream_session_id = 1; let max_subscribe_id = 10; @@ -470,14 +470,14 @@ mod success { .setup_publisher(max_subscribe_id, upstream_session_id) .await; let _ = pubsub_relation_manager - .set_publisher_announced_namespace(track_namespace.clone(), upstream_session_id) + .set_upstream_announced_namespace(track_namespace.clone(), upstream_session_id) .await; let (upstream_subscribe_id, _) = pubsub_relation_manager - .set_publisher_subscription( + .set_upstream_subscription( upstream_session_id, track_namespace, track_name, - downstream_priority, + subscriber_priority, group_order, filter_type, start_group, @@ -524,7 +524,7 @@ mod success { // Check the subscriber is registered let (_, producers, pubsub_relation) = - test_utils::get_node_and_relation_clone(&pubsub_relation_manager).await; + test_helper_fn::get_node_and_relation_clone(&pubsub_relation_manager).await; assert_eq!(producers.len(), 1); @@ -543,8 +543,8 @@ mod success { mod failure { use crate::modules::handlers::subscribe_handler::subscribe_handler; use crate::modules::pubsub_relation_manager::{ - commands::PubSubRelationCommand, interface::PubSubRelationManagerInterface, - manager::pubsub_relation_manager, + commands::PubSubRelationCommand, manager::pubsub_relation_manager, + wrapper::PubSubRelationManagerWrapper, }; use crate::modules::send_stream_dispatcher::{ send_stream_dispatcher, SendStreamDispatchCommand, SendStreamDispatcher, @@ -569,7 +569,7 @@ mod failure { let track_alias = 0; let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); let track_name = "track_name".to_string(); - let downstream_priority = 0; + let subscriber_priority = 0; let group_order = GroupOrder::Ascending; let filter_type = FilterType::LatestGroup; let start_group = None; @@ -585,7 +585,7 @@ mod failure { track_alias, track_namespace.clone(), track_name.clone(), - downstream_priority, + subscriber_priority, group_order, filter_type, start_group, @@ -600,12 +600,12 @@ mod failure { let downstream_session_id = 0; let mut client = MOQTClient::new(downstream_session_id); - // Generate PubSubRelationManagerInterface (register subscriber in advance) + // Generate PubSubRelationManagerWrapper (register subscriber in advance) let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); - let mut pubsub_relation_manager: PubSubRelationManagerInterface = - PubSubRelationManagerInterface::new(track_namespace_tx); + let mut pubsub_relation_manager: PubSubRelationManagerWrapper = + PubSubRelationManagerWrapper::new(track_namespace_tx); let upstream_session_id = 1; let max_subscribe_id = 10; @@ -614,20 +614,20 @@ mod failure { .setup_publisher(max_subscribe_id, upstream_session_id) .await; let _ = pubsub_relation_manager - .set_publisher_announced_namespace(track_namespace.clone(), upstream_session_id) + .set_upstream_announced_namespace(track_namespace.clone(), upstream_session_id) .await; let _ = pubsub_relation_manager .setup_subscriber(max_subscribe_id, downstream_session_id) .await; let _ = pubsub_relation_manager - .set_subscriber_subscription( + .set_downstream_subscription( downstream_session_id, subscribe_id, track_alias, track_namespace, track_name, - downstream_priority, + subscriber_priority, group_order, filter_type, start_group, @@ -672,7 +672,7 @@ mod failure { let track_alias = 0; let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); let track_name = "track_name".to_string(); - let downstream_priority = 0; + let subscriber_priority = 0; let group_order = GroupOrder::Ascending; let filter_type = FilterType::LatestGroup; let start_group = None; @@ -688,7 +688,7 @@ mod failure { track_alias, track_namespace.clone(), track_name.clone(), - downstream_priority, + subscriber_priority, group_order, filter_type, start_group, @@ -703,12 +703,12 @@ mod failure { let downstream_session_id = 0; let mut client = MOQTClient::new(downstream_session_id); - // Generate PubSubRelationManagerInterface + // Generate PubSubRelationManagerWrapper let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); - let mut pubsub_relation_manager: PubSubRelationManagerInterface = - PubSubRelationManagerInterface::new(track_namespace_tx); + let mut pubsub_relation_manager: PubSubRelationManagerWrapper = + PubSubRelationManagerWrapper::new(track_namespace_tx); let upstream_session_id = 1; let max_subscribe_id = 10; @@ -717,7 +717,7 @@ mod failure { .setup_publisher(max_subscribe_id, upstream_session_id) .await; let _ = pubsub_relation_manager - .set_publisher_announced_namespace(track_namespace.clone(), upstream_session_id) + .set_upstream_announced_namespace(track_namespace.clone(), upstream_session_id) .await; let _ = pubsub_relation_manager @@ -750,7 +750,7 @@ mod failure { let track_alias = 0; let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); let track_name = "track_name"; - let downstream_priority = 0; + let subscriber_priority = 0; let group_order = GroupOrder::Ascending; let filter_type = FilterType::LatestGroup; let start_group = None; @@ -766,7 +766,7 @@ mod failure { track_alias, track_namespace, track_name.to_string(), - downstream_priority, + subscriber_priority, group_order, filter_type, start_group, @@ -781,12 +781,12 @@ mod failure { let downstream_session_id = 0; let mut client = MOQTClient::new(downstream_session_id); - // Generate PubSubRelationManagerInterface (without set publisher) + // Generate PubSubRelationManagerWrapper (without set publisher) let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); - let mut pubsub_relation_manager: PubSubRelationManagerInterface = - PubSubRelationManagerInterface::new(track_namespace_tx); + let mut pubsub_relation_manager: PubSubRelationManagerWrapper = + PubSubRelationManagerWrapper::new(track_namespace_tx); let upstream_session_id = 1; let max_subscribe_id = 10; @@ -830,7 +830,7 @@ mod failure { let track_aliases = [0, 1]; let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); let track_name = "track_name".to_string(); - let downstream_priority = 0; + let subscriber_priority = 0; let group_order = GroupOrder::Ascending; let filter_type = FilterType::LatestGroup; let start_group = None; @@ -849,7 +849,7 @@ mod failure { track_aliases[i], track_namespace.clone(), track_name.clone(), - downstream_priority, + subscriber_priority, group_order, filter_type, start_group, @@ -867,12 +867,12 @@ mod failure { let downstream_session_id = 0; let mut client = MOQTClient::new(downstream_session_id); - // Generate PubSubRelationManagerInterface + // Generate PubSubRelationManagerWrapper let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); - let mut pubsub_relation_manager: PubSubRelationManagerInterface = - PubSubRelationManagerInterface::new(track_namespace_tx); + let mut pubsub_relation_manager: PubSubRelationManagerWrapper = + PubSubRelationManagerWrapper::new(track_namespace_tx); let upstream_session_id = 1; let max_subscribe_id = 0; @@ -882,7 +882,7 @@ mod failure { .setup_publisher(max_subscribe_id, upstream_session_id) .await; let _ = pubsub_relation_manager - .set_publisher_announced_namespace(track_namespace.clone(), upstream_session_id) + .set_upstream_announced_namespace(track_namespace.clone(), upstream_session_id) .await; let _ = pubsub_relation_manager @@ -932,7 +932,7 @@ mod failure { let track_alias = 0; let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); let track_name = "track_name".to_string(); - let downstream_priority = 0; + let subscriber_priority = 0; let group_order = GroupOrder::Ascending; let filter_type = FilterType::LatestGroup; let start_group = None; @@ -951,7 +951,7 @@ mod failure { track_alias, track_namespace.clone(), track_name.clone(), - downstream_priority, + subscriber_priority, group_order, filter_type, start_group, @@ -969,12 +969,12 @@ mod failure { let downstream_session_id = 0; let mut client = MOQTClient::new(downstream_session_id); - // Generate PubSubRelationManagerInterface + // Generate PubSubRelationManagerWrapper let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); - let mut pubsub_relation_manager: PubSubRelationManagerInterface = - PubSubRelationManagerInterface::new(track_namespace_tx); + let mut pubsub_relation_manager: PubSubRelationManagerWrapper = + PubSubRelationManagerWrapper::new(track_namespace_tx); let upstream_session_id = 1; let max_subscribe_id = 10; @@ -984,7 +984,7 @@ mod failure { .setup_publisher(max_subscribe_id, upstream_session_id) .await; let _ = pubsub_relation_manager - .set_publisher_announced_namespace(track_namespace.clone(), upstream_session_id) + .set_upstream_announced_namespace(track_namespace.clone(), upstream_session_id) .await; let _ = pubsub_relation_manager diff --git a/moqt-server/src/modules/handlers/subscribe_ok_handler.rs b/moqt-server/src/modules/handlers/subscribe_ok_handler.rs index 045bd883..f8688445 100644 --- a/moqt-server/src/modules/handlers/subscribe_ok_handler.rs +++ b/moqt-server/src/modules/handlers/subscribe_ok_handler.rs @@ -20,22 +20,22 @@ pub(crate) async fn subscribe_ok_handler( let upstream_subscribe_id = subscribe_ok_message.subscribe_id(); // Determine the SUBSCRIBER who sent the SUBSCRIBE using the track_namespace and track_name - let ids = pubsub_relation_manager_repository - .get_requesting_subscriber_session_ids_and_subscribe_ids( + let ids_and_aliases = pubsub_relation_manager_repository + .get_requesting_downstream_session_ids_and_subscribe_ids( upstream_subscribe_id, upstream_session_id, ) .await?; - match ids { - Some(ids) => { + match ids_and_aliases { + Some(ids_and_aliases) => { let _ = pubsub_relation_manager_repository - .activate_publisher_subscription(upstream_session_id, upstream_subscribe_id) + .activate_upstream_subscription(upstream_session_id, upstream_subscribe_id) .await; // Notify all waiting subscribers with the SUBSCRIBE_OK message - for (downstream_session_id, downstream_subscribe_id) in ids.iter() { + for (downstream_session_id, downstream_subscribe_id) in ids_and_aliases.iter() { let mut relaying_subscribe_ok_message = subscribe_ok_message.clone(); - relaying_subscribe_ok_message.replace_subscribe_id(*downstream_subscribe_id); + relaying_subscribe_ok_message.set_subscribe_id(*downstream_subscribe_id); let message: Box = Box::new(relaying_subscribe_ok_message.clone()); tracing::debug!( @@ -53,7 +53,7 @@ pub(crate) async fn subscribe_ok_handler( .await?; pubsub_relation_manager_repository - .activate_subscriber_subscription( + .activate_downstream_subscription( *downstream_session_id, *downstream_subscribe_id, ) @@ -67,7 +67,7 @@ pub(crate) async fn subscribe_ok_handler( // Activate the publisher because it is in the Requesting state let _ = pubsub_relation_manager_repository - .activate_publisher_subscription(upstream_session_id, upstream_subscribe_id) + .activate_upstream_subscription(upstream_session_id, upstream_subscribe_id) .await; } } @@ -79,8 +79,8 @@ pub(crate) async fn subscribe_ok_handler( mod success { use crate::modules::handlers::subscribe_ok_handler::subscribe_ok_handler; use crate::modules::pubsub_relation_manager::{ - commands::PubSubRelationCommand, interface::PubSubRelationManagerInterface, - manager::pubsub_relation_manager, + commands::PubSubRelationCommand, manager::pubsub_relation_manager, + wrapper::PubSubRelationManagerWrapper, }; use crate::modules::send_stream_dispatcher::{ send_stream_dispatcher, SendStreamDispatchCommand, SendStreamDispatcher, @@ -105,7 +105,7 @@ mod success { let track_alias = 0; let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); let track_name = "track_name".to_string(); - let downstream_priority = 0; + let subscriber_priority = 0; let expires = 1; let group_order = GroupOrder::Ascending; let content_exists = false; @@ -136,12 +136,12 @@ mod success { let upstream_session_id = 1; let mut client = MOQTClient::new(upstream_session_id); - // Generate PubSubRelationManagerInterface + // Generate PubSubRelationManagerWrapper let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); - let mut pubsub_relation_manager: PubSubRelationManagerInterface = - PubSubRelationManagerInterface::new(track_namespace_tx); + let mut pubsub_relation_manager: PubSubRelationManagerWrapper = + PubSubRelationManagerWrapper::new(track_namespace_tx); let downstream_session_id = 2; let max_subscribe_id = 10; @@ -150,14 +150,14 @@ mod success { .setup_publisher(max_subscribe_id, upstream_session_id) .await; let _ = pubsub_relation_manager - .set_publisher_announced_namespace(track_namespace.clone(), upstream_session_id) + .set_upstream_announced_namespace(track_namespace.clone(), upstream_session_id) .await; let (upstream_subscribe_id, _) = pubsub_relation_manager - .set_publisher_subscription( + .set_upstream_subscription( upstream_session_id, track_namespace.clone(), track_name.clone(), - downstream_priority, + subscriber_priority, group_order, filter_type, start_group, @@ -172,13 +172,13 @@ mod success { .setup_subscriber(max_subscribe_id, downstream_session_id) .await; let _ = pubsub_relation_manager - .set_subscriber_subscription( + .set_downstream_subscription( downstream_session_id, downstream_subscribe_id, track_alias, track_namespace, track_name, - downstream_priority, + subscriber_priority, group_order, filter_type, start_group, @@ -188,7 +188,7 @@ mod success { ) .await; let _ = pubsub_relation_manager - .register_pubsup_relation( + .set_pubsub_relation( upstream_session_id, upstream_subscribe_id, downstream_session_id, @@ -229,8 +229,8 @@ mod success { mod failure { use crate::modules::handlers::subscribe_ok_handler::subscribe_ok_handler; use crate::modules::pubsub_relation_manager::{ - commands::PubSubRelationCommand, interface::PubSubRelationManagerInterface, - manager::pubsub_relation_manager, + commands::PubSubRelationCommand, manager::pubsub_relation_manager, + wrapper::PubSubRelationManagerWrapper, }; use crate::modules::send_stream_dispatcher::{ send_stream_dispatcher, SendStreamDispatchCommand, SendStreamDispatcher, @@ -255,7 +255,7 @@ mod failure { let track_alias = 0; let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); let track_name = "track_name".to_string(); - let downstream_priority = 0; + let subscriber_priority = 0; let expires = 1; let group_order = GroupOrder::Ascending; let content_exists = false; @@ -286,12 +286,12 @@ mod failure { let upstream_session_id = 1; let mut client = MOQTClient::new(upstream_session_id); - // Generate PubSubRelationManagerInterface + // Generate PubSubRelationManagerWrapper let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); - let mut pubsub_relation_manager: PubSubRelationManagerInterface = - PubSubRelationManagerInterface::new(track_namespace_tx); + let mut pubsub_relation_manager: PubSubRelationManagerWrapper = + PubSubRelationManagerWrapper::new(track_namespace_tx); let downstream_session_id = 2; let max_subscribe_id = 10; @@ -300,14 +300,14 @@ mod failure { .setup_publisher(max_subscribe_id, upstream_session_id) .await; let _ = pubsub_relation_manager - .set_publisher_announced_namespace(track_namespace.clone(), upstream_session_id) + .set_upstream_announced_namespace(track_namespace.clone(), upstream_session_id) .await; let (upstream_subscribe_id, _) = pubsub_relation_manager - .set_publisher_subscription( + .set_upstream_subscription( upstream_session_id, track_namespace.clone(), track_name.clone(), - downstream_priority, + subscriber_priority, group_order, filter_type, start_group, @@ -322,13 +322,13 @@ mod failure { .setup_subscriber(max_subscribe_id, downstream_session_id) .await; let _ = pubsub_relation_manager - .set_subscriber_subscription( + .set_downstream_subscription( downstream_session_id, downstream_subscribe_id, track_alias, track_namespace, track_name, - downstream_priority, + subscriber_priority, group_order, filter_type, start_group, @@ -338,7 +338,7 @@ mod failure { ) .await; let _ = pubsub_relation_manager - .register_pubsup_relation( + .set_pubsub_relation( upstream_session_id, upstream_subscribe_id, downstream_session_id, @@ -371,7 +371,7 @@ mod failure { let downstream_subscribe_id = 0; let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); let track_name = "track_name".to_string(); - let downstream_priority = 0; + let subscriber_priority = 0; let expires = 1; let group_order = GroupOrder::Ascending; let content_exists = false; @@ -402,12 +402,12 @@ mod failure { let upstream_session_id = 1; let mut client = MOQTClient::new(upstream_session_id); - // Generate PubSubRelationManagerInterface + // Generate PubSubRelationManagerWrapper let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); - let mut pubsub_relation_manager: PubSubRelationManagerInterface = - PubSubRelationManagerInterface::new(track_namespace_tx); + let mut pubsub_relation_manager: PubSubRelationManagerWrapper = + PubSubRelationManagerWrapper::new(track_namespace_tx); let downstream_session_id = 2; let max_subscribe_id = 10; @@ -416,14 +416,14 @@ mod failure { .setup_publisher(max_subscribe_id, upstream_session_id) .await; let _ = pubsub_relation_manager - .set_publisher_announced_namespace(track_namespace.clone(), upstream_session_id) + .set_upstream_announced_namespace(track_namespace.clone(), upstream_session_id) .await; let _ = pubsub_relation_manager - .set_publisher_subscription( + .set_upstream_subscription( upstream_session_id, track_namespace.clone(), track_name.clone(), - downstream_priority, + subscriber_priority, group_order, filter_type, start_group, @@ -469,7 +469,7 @@ mod failure { let track_alias = 0; let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); let track_name = "track_name".to_string(); - let downstream_priority = 0; + let subscriber_priority = 0; let expires = 1; let group_order = GroupOrder::Ascending; let content_exists = false; @@ -500,12 +500,12 @@ mod failure { let upstream_session_id = 1; let mut client = MOQTClient::new(upstream_session_id); - // Generate PubSubRelationManagerInterface + // Generate PubSubRelationManagerWrapper let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); - let mut pubsub_relation_manager: PubSubRelationManagerInterface = - PubSubRelationManagerInterface::new(track_namespace_tx); + let mut pubsub_relation_manager: PubSubRelationManagerWrapper = + PubSubRelationManagerWrapper::new(track_namespace_tx); let downstream_session_id = 2; let max_subscribe_id = 10; @@ -514,14 +514,14 @@ mod failure { .setup_publisher(max_subscribe_id, upstream_session_id) .await; let _ = pubsub_relation_manager - .set_publisher_announced_namespace(track_namespace.clone(), upstream_session_id) + .set_upstream_announced_namespace(track_namespace.clone(), upstream_session_id) .await; let (upstream_subscribe_id, _) = pubsub_relation_manager - .set_publisher_subscription( + .set_upstream_subscription( upstream_session_id, track_namespace.clone(), track_name.clone(), - downstream_priority, + subscriber_priority, group_order, filter_type, start_group, @@ -536,13 +536,13 @@ mod failure { .setup_subscriber(max_subscribe_id, downstream_session_id) .await; let _ = pubsub_relation_manager - .set_subscriber_subscription( + .set_downstream_subscription( downstream_session_id, downstream_subscribe_id, track_alias, track_namespace, track_name, - downstream_priority, + subscriber_priority, group_order, filter_type, start_group, @@ -552,7 +552,7 @@ mod failure { ) .await; let _ = pubsub_relation_manager - .register_pubsup_relation( + .set_pubsub_relation( upstream_session_id, upstream_subscribe_id, downstream_session_id, @@ -561,7 +561,7 @@ mod failure { .await; let _ = pubsub_relation_manager - .activate_subscriber_subscription(downstream_session_id, downstream_subscribe_id) + .activate_downstream_subscription(downstream_session_id, downstream_subscribe_id) .await; // Generate SendStreamDispacher @@ -599,7 +599,7 @@ mod failure { let track_alias = 0; let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); let track_name = "track_name".to_string(); - let downstream_priority = 0; + let subscriber_priority = 0; let expires = 1; let group_order = GroupOrder::Ascending; let content_exists = false; @@ -630,12 +630,12 @@ mod failure { let upstream_session_id = 1; let mut client = MOQTClient::new(upstream_session_id); - // Generate PubSubRelationManagerInterface + // Generate PubSubRelationManagerWrapper let (track_namespace_tx, mut track_namespace_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_namespace_rx).await }); - let mut pubsub_relation_manager: PubSubRelationManagerInterface = - PubSubRelationManagerInterface::new(track_namespace_tx); + let mut pubsub_relation_manager: PubSubRelationManagerWrapper = + PubSubRelationManagerWrapper::new(track_namespace_tx); let downstream_session_id = 2; let max_subscribe_id = 10; @@ -644,14 +644,14 @@ mod failure { .setup_publisher(max_subscribe_id, upstream_session_id) .await; let _ = pubsub_relation_manager - .set_publisher_announced_namespace(track_namespace.clone(), upstream_session_id) + .set_upstream_announced_namespace(track_namespace.clone(), upstream_session_id) .await; let (upstream_subscribe_id, _) = pubsub_relation_manager - .set_publisher_subscription( + .set_upstream_subscription( upstream_session_id, track_namespace.clone(), track_name.clone(), - downstream_priority, + subscriber_priority, group_order, filter_type, start_group, @@ -666,13 +666,13 @@ mod failure { .setup_subscriber(max_subscribe_id, downstream_session_id) .await; let _ = pubsub_relation_manager - .set_subscriber_subscription( + .set_downstream_subscription( downstream_session_id, downstream_subscribe_id, track_alias, track_namespace, track_name, - downstream_priority, + subscriber_priority, group_order, filter_type, start_group, @@ -682,7 +682,7 @@ mod failure { ) .await; let _ = pubsub_relation_manager - .register_pubsup_relation( + .set_pubsub_relation( upstream_session_id, upstream_subscribe_id, downstream_session_id, @@ -691,7 +691,7 @@ mod failure { .await; let _ = pubsub_relation_manager - .activate_publisher_subscription(upstream_session_id, upstream_subscribe_id) + .activate_upstream_subscription(upstream_session_id, upstream_subscribe_id) .await; // Generate SendStreamDispacher diff --git a/moqt-server/src/modules/handlers/unannounce_handler.rs b/moqt-server/src/modules/handlers/unannounce_handler.rs index 4371163e..a779e846 100644 --- a/moqt-server/src/modules/handlers/unannounce_handler.rs +++ b/moqt-server/src/modules/handlers/unannounce_handler.rs @@ -13,7 +13,7 @@ pub(crate) async fn unannounce_handler( // Remove the announced Track Namespace let delete_result = pubsub_relation_manager_repository - .delete_publisher_announced_namespace( + .delete_upstream_announced_namespace( unannounce_message.track_namespace().clone(), _client.id, ) diff --git a/moqt-server/src/modules/pubsub_relation_manager.rs b/moqt-server/src/modules/pubsub_relation_manager.rs index 77fc0a54..94d37213 100644 --- a/moqt-server/src/modules/pubsub_relation_manager.rs +++ b/moqt-server/src/modules/pubsub_relation_manager.rs @@ -1,4 +1,4 @@ pub mod commands; -pub mod interface; pub mod manager; pub mod relation; +pub mod wrapper; diff --git a/moqt-server/src/modules/pubsub_relation_manager/commands.rs b/moqt-server/src/modules/pubsub_relation_manager/commands.rs index 9878469c..2778b37a 100644 --- a/moqt-server/src/modules/pubsub_relation_manager/commands.rs +++ b/moqt-server/src/modules/pubsub_relation_manager/commands.rs @@ -1,6 +1,6 @@ use anyhow::Result; use moqt_core::messages::control_messages::subscribe::{FilterType, GroupOrder}; -use moqt_core::subscription_models::subscriptions::Subscription; +use moqt_core::models::subscriptions::Subscription; use tokio::sync::oneshot; #[cfg(test)] @@ -13,27 +13,27 @@ use crate::modules::pubsub_relation_manager::{ pub(crate) enum PubSubRelationCommand { SetupPublisher { max_subscribe_id: u64, - publisher_session_id: usize, + upstream_session_id: usize, resp: oneshot::Sender>, }, - SetPublisherAnnouncedNamespace { + SetUpstreamAnnouncedNamespace { track_namespace: Vec, - publisher_session_id: usize, + upstream_session_id: usize, resp: oneshot::Sender>, }, SetupSubscriber { max_subscribe_id: u64, - subscriber_session_id: usize, + downstream_session_id: usize, resp: oneshot::Sender>, }, - IsValidSubscriberSubscribeId { + IsValidDownstreamSubscribeId { subscribe_id: u64, - subscriber_session_id: usize, + downstream_session_id: usize, resp: oneshot::Sender>, }, - IsValidSubscriberTrackAlias { + IsValidDownstreamTrackAlias { track_alias: u64, - subscriber_session_id: usize, + downstream_session_id: usize, resp: oneshot::Sender>, }, IsTrackExisting { @@ -41,29 +41,29 @@ pub(crate) enum PubSubRelationCommand { track_name: String, resp: oneshot::Sender>, }, - GetPublisherSubscription { + GetUpstreamSubscription { track_namespace: Vec, track_name: String, resp: oneshot::Sender>>, }, - GetPublisherSessionId { + GetUpstreamSessionId { track_namespace: Vec, resp: oneshot::Sender>>, }, - GetRequestingSubscriberSessionIdsAndSubscribeIds { - publisher_subscribe_id: u64, - publisher_session_id: usize, + GetRequestingDownstreamSessionIdsAndSubscribeIds { + upstream_subscribe_id: u64, + upstream_session_id: usize, #[allow(clippy::type_complexity)] resp: oneshot::Sender>>>, }, - GetPublisherSubscribeId { + GetUpstreamSubscribeId { track_namespace: Vec, track_name: String, - publisher_session_id: usize, + upstream_session_id: usize, resp: oneshot::Sender>>, }, - SetSubscriberSubscription { - subscriber_session_id: usize, + SetDownstreamSubscription { + downstream_session_id: usize, subscribe_id: u64, track_alias: u64, track_namespace: Vec, @@ -77,8 +77,8 @@ pub(crate) enum PubSubRelationCommand { end_object: Option, resp: oneshot::Sender>, }, - SetPublisherSubscription { - publisher_session_id: usize, + SetUpstreamSubscription { + upstream_session_id: usize, track_namespace: Vec, track_name: String, subscriber_priority: u8, @@ -90,26 +90,26 @@ pub(crate) enum PubSubRelationCommand { end_object: Option, resp: oneshot::Sender>, }, - RegisterPubSubRelation { - publisher_session_id: usize, - publisher_subscribe_id: u64, - subscriber_session_id: usize, - subscriber_subscribe_id: u64, + SetPubSubRelation { + upstream_session_id: usize, + upstream_subscribe_id: u64, + downstream_session_id: usize, + downstream_subscribe_id: u64, resp: oneshot::Sender>, }, - ActivateSubscriberSubscription { - subscriber_session_id: usize, + ActivateDownstreamSubscription { + downstream_session_id: usize, subscribe_id: u64, resp: oneshot::Sender>, }, - ActivatePublisherSubscription { - publisher_session_id: usize, + ActivateUpstreamSubscription { + upstream_session_id: usize, subscribe_id: u64, resp: oneshot::Sender>, }, - DeletePublisherAnnouncedNamespace { + DeleteUpstreamAnnouncedNamespace { track_namespace: Vec, - publisher_session_id: usize, + upstream_session_id: usize, resp: oneshot::Sender>, }, DeleteClient { diff --git a/moqt-server/src/modules/pubsub_relation_manager/manager.rs b/moqt-server/src/modules/pubsub_relation_manager/manager.rs index ea080ff5..065c6c1c 100644 --- a/moqt-server/src/modules/pubsub_relation_manager/manager.rs +++ b/moqt-server/src/modules/pubsub_relation_manager/manager.rs @@ -3,17 +3,17 @@ use crate::modules::pubsub_relation_manager::{ relation::PubSubRelation, }; use anyhow::anyhow; -use moqt_core::subscription_models::nodes::{ - consumer_node::Consumer, node_registory::SubscriptionNodeRegistory, producer_node::Producer, +use moqt_core::models::subscriptions::nodes::{ + consumers::Consumer, producers::Producer, registry::SubscriptionNodeRegistry, }; use std::collections::HashMap; use tokio::sync::mpsc; -type SubscriberSessionId = usize; -type PublisherSessionId = usize; +type DownstreamSessionId = usize; +type UpstreamSessionId = usize; -pub(crate) type Consumers = HashMap; -pub(crate) type Producers = HashMap; +pub(crate) type Consumers = HashMap; +pub(crate) type Producers = HashMap; // [Original Publisher: (Producer) ] -> [Relay: (Consumer) - - (Producer) ] -> [End Subscriber: (Consumer)] @@ -30,25 +30,25 @@ pub(crate) async fn pubsub_relation_manager(rx: &mut mpsc::Receiver { // Return an error if it already exists - if consumers.contains_key(&publisher_session_id) { + if consumers.contains_key(&upstream_session_id) { let msg = "publisher already exists"; tracing::error!(msg); resp.send(Err(anyhow!(msg))).unwrap(); continue; } - consumers.insert(publisher_session_id, Consumer::new(max_subscribe_id)); + consumers.insert(upstream_session_id, Consumer::new(max_subscribe_id)); resp.send(Ok(())).unwrap(); } - SetPublisherAnnouncedNamespace { + SetUpstreamAnnouncedNamespace { track_namespace, - publisher_session_id, + upstream_session_id, resp, } => { - let consumer = consumers.get_mut(&publisher_session_id).unwrap(); + let consumer = consumers.get_mut(&upstream_session_id).unwrap(); match consumer.set_namespace(track_namespace) { Ok(_) => { resp.send(Ok(())).unwrap(); @@ -62,27 +62,27 @@ pub(crate) async fn pubsub_relation_manager(rx: &mut mpsc::Receiver { // Return an error if it already exists - if producers.contains_key(&subscriber_session_id) { + if producers.contains_key(&downstream_session_id) { let msg = "subscriber already exists"; tracing::error!(msg); resp.send(Err(anyhow!(msg))).unwrap(); continue; } - producers.insert(subscriber_session_id, Producer::new(max_subscribe_id)); + producers.insert(downstream_session_id, Producer::new(max_subscribe_id)); resp.send(Ok(())).unwrap(); } - IsValidSubscriberSubscribeId { + IsValidDownstreamSubscribeId { subscribe_id, - subscriber_session_id, + downstream_session_id, resp, } => { // Return an error if the subscriber does not exist - let producer = match producers.get(&subscriber_session_id) { + let producer = match producers.get(&downstream_session_id) { Some(producer) => producer, None => { let msg = "subscriber not found"; @@ -92,18 +92,16 @@ pub(crate) async fn pubsub_relation_manager(rx: &mut mpsc::Receiver { // Return an error if the subscriber does not exist - let producer = match producers.get(&subscriber_session_id) { + let producer = match producers.get(&downstream_session_id) { Some(producer) => producer, None => { let msg = "subscriber not found"; @@ -113,28 +111,28 @@ pub(crate) async fn pubsub_relation_manager(rx: &mut mpsc::Receiver { // Find the publisher that has the track namespace from all consumers - let publisher_session_id = consumers + let upstream_session_id = consumers .iter() .find(|(_, consumer)| consumer.has_namespace(track_namespace.clone())) .map(|(session_id, _)| *session_id); - resp.send(Ok(publisher_session_id)).unwrap(); + resp.send(Ok(upstream_session_id)).unwrap(); } - GetRequestingSubscriberSessionIdsAndSubscribeIds { - publisher_subscribe_id, - publisher_session_id, + GetRequestingDownstreamSessionIdsAndSubscribeIds { + upstream_subscribe_id, + upstream_session_id, resp, } => { if !pubsub_relation .records - .contains_key(&(publisher_session_id, publisher_subscribe_id)) + .contains_key(&(upstream_session_id, upstream_subscribe_id)) { let msg = "publisher not found in pubsub relation"; tracing::error!(msg); @@ -143,18 +141,18 @@ pub(crate) async fn pubsub_relation_manager(rx: &mut mpsc::Receiver> = match subscribers { Some(subscribers) => { let mut requesting_subscribers = vec![]; - for (subscriber_session_id, subscriber_subscribe_id) in subscribers { - let producer = producers.get(subscriber_session_id).unwrap(); - if producer.is_requesting(*subscriber_subscribe_id) { + for (downstream_session_id, downstream_subscribe_id) in subscribers { + let producer = producers.get(downstream_session_id).unwrap(); + if producer.is_requesting(*downstream_subscribe_id) { requesting_subscribers - .push((*subscriber_session_id, *subscriber_subscribe_id)); + .push((*downstream_session_id, *downstream_subscribe_id)); } } @@ -165,14 +163,14 @@ pub(crate) async fn pubsub_relation_manager(rx: &mut mpsc::Receiver { // Return an error if the publisher does not exist - let consumer = match consumers.get(&publisher_session_id) { + let consumer = match consumers.get(&upstream_session_id) { Some(consumer) => consumer, None => { let msg = "publisher not found"; @@ -197,7 +195,7 @@ pub(crate) async fn pubsub_relation_manager(rx: &mut mpsc::Receiver { // Return an error if the subscriber does not exist - let producer = match producers.get_mut(&subscriber_session_id) { + let producer = match producers.get_mut(&downstream_session_id) { Some(producer) => producer, None => { let msg = "subscriber not found"; @@ -254,14 +252,14 @@ pub(crate) async fn pubsub_relation_manager(rx: &mut mpsc::Receiver resp.send(Ok(())).unwrap(), Err(err) => { - tracing::error!("set_subscriber_subscription: err: {:?}", err.to_string()); + tracing::error!("set_downstream_subscription: err: {:?}", err.to_string()); resp.send(Err(anyhow!(err))).unwrap(); continue; } } } - SetPublisherSubscription { - publisher_session_id, + SetUpstreamSubscription { + upstream_session_id, track_namespace, track_name, subscriber_priority, @@ -274,7 +272,7 @@ pub(crate) async fn pubsub_relation_manager(rx: &mut mpsc::Receiver { // Return an error if the publisher does not exist - let consumer = match consumers.get_mut(&publisher_session_id) { + let consumer = match consumers.get_mut(&upstream_session_id) { Some(consumer) => consumer, None => { let msg = "publisher not found"; @@ -285,11 +283,11 @@ pub(crate) async fn pubsub_relation_manager(rx: &mut mpsc::Receiver result, Err(err) => { tracing::error!( - "find_unused_subscribe_id_and_track_alias: err: {:?}", + "create_latest_subscribe_id_and_track_alias: err: {:?}", err.to_string() ); resp.send(Err(anyhow!(err))).unwrap(); @@ -317,18 +315,18 @@ pub(crate) async fn pubsub_relation_manager(rx: &mut mpsc::Receiver { let result = pubsub_relation.add_relation( - publisher_session_id, - publisher_subscribe_id, - subscriber_session_id, - subscriber_subscribe_id, + upstream_session_id, + upstream_subscribe_id, + downstream_session_id, + downstream_subscribe_id, ); match result { @@ -339,13 +337,13 @@ pub(crate) async fn pubsub_relation_manager(rx: &mut mpsc::Receiver { // Return an error if the subscriber does not exist - let producer = match producers.get_mut(&subscriber_session_id) { + let producer = match producers.get_mut(&downstream_session_id) { Some(producer) => producer, None => { let msg = "subscriber not found"; @@ -366,13 +364,13 @@ pub(crate) async fn pubsub_relation_manager(rx: &mut mpsc::Receiver { // Return an error if the publisher does not exist - let consumer = match consumers.get_mut(&publisher_session_id) { + let consumer = match consumers.get_mut(&upstream_session_id) { Some(consumer) => consumer, None => { let msg = "publisher not found"; @@ -393,13 +391,13 @@ pub(crate) async fn pubsub_relation_manager(rx: &mut mpsc::Receiver { // Return an error if the publisher does not exist - let consumer = match consumers.get_mut(&publisher_session_id) { + let consumer = match consumers.get_mut(&upstream_session_id) { Some(consumer) => consumer, None => { let msg = "publisher not found"; @@ -453,8 +451,8 @@ pub(crate) async fn pubsub_relation_manager(rx: &mut mpsc::Receiver Result<()> { - let key = (publisher_session_id, publisher_subscribe_id); - let value = (subscriber_session_id, subscriber_subscribe_id); + let key = (upstream_session_id, upstream_subscribe_id); + let value = (downstream_session_id, downstream_subscribe_id); match self.records.get_mut(&key) { // If the key exists, add the value to the existing vector @@ -47,10 +47,10 @@ impl PubSubRelation { pub(crate) fn get_subscribers( &self, - publisher_session_id: PublisherSessionId, - publisher_subscribe_id: PublisherSubscribeId, + upstream_session_id: PublisherSessionId, + upstream_subscribe_id: PublisherSubscribeId, ) -> Option<&Vec<(SubscriberSessionId, SubscriberSubscribeId)>> { - let key = (publisher_session_id, publisher_subscribe_id); + let key = (upstream_session_id, upstream_subscribe_id); self.records.get(&key) } diff --git a/moqt-server/src/modules/pubsub_relation_manager/interface.rs b/moqt-server/src/modules/pubsub_relation_manager/wrapper.rs similarity index 67% rename from moqt-server/src/modules/pubsub_relation_manager/interface.rs rename to moqt-server/src/modules/pubsub_relation_manager/wrapper.rs index a83104d3..515deb21 100644 --- a/moqt-server/src/modules/pubsub_relation_manager/interface.rs +++ b/moqt-server/src/modules/pubsub_relation_manager/wrapper.rs @@ -4,33 +4,33 @@ use crate::modules::pubsub_relation_manager::commands::{ use anyhow::{bail, Result}; use async_trait::async_trait; use moqt_core::messages::control_messages::subscribe::{FilterType, GroupOrder}; +use moqt_core::models::subscriptions::Subscription; use moqt_core::pubsub_relation_manager_repository::PubSubRelationManagerRepository; -use moqt_core::subscription_models::subscriptions::Subscription; use tokio::sync::{mpsc, oneshot}; // Wrapper to encapsulate channel-related operations -pub(crate) struct PubSubRelationManagerInterface { +pub(crate) struct PubSubRelationManagerWrapper { tx: mpsc::Sender, } -impl PubSubRelationManagerInterface { +impl PubSubRelationManagerWrapper { pub fn new(tx: mpsc::Sender) -> Self { Self { tx } } } #[async_trait] -impl PubSubRelationManagerRepository for PubSubRelationManagerInterface { +impl PubSubRelationManagerRepository for PubSubRelationManagerWrapper { async fn setup_publisher( &self, max_subscribe_id: u64, - publisher_session_id: usize, + upstream_session_id: usize, ) -> Result<()> { let (resp_tx, resp_rx) = oneshot::channel::>(); let cmd = PubSubRelationCommand::SetupPublisher { max_subscribe_id, - publisher_session_id, + upstream_session_id, resp: resp_tx, }; self.tx.send(cmd).await.unwrap(); @@ -42,16 +42,16 @@ impl PubSubRelationManagerRepository for PubSubRelationManagerInterface { Err(err) => bail!(err), } } - async fn set_publisher_announced_namespace( + async fn set_upstream_announced_namespace( &self, track_namespace: Vec, - publisher_session_id: usize, + upstream_session_id: usize, ) -> Result<()> { let (resp_tx, resp_rx) = oneshot::channel::>(); - let cmd = PubSubRelationCommand::SetPublisherAnnouncedNamespace { + let cmd = PubSubRelationCommand::SetUpstreamAnnouncedNamespace { track_namespace, - publisher_session_id, + upstream_session_id, resp: resp_tx, }; self.tx.send(cmd).await.unwrap(); @@ -66,13 +66,13 @@ impl PubSubRelationManagerRepository for PubSubRelationManagerInterface { async fn setup_subscriber( &self, max_subscribe_id: u64, - subscriber_session_id: usize, + downstream_session_id: usize, ) -> Result<()> { let (resp_tx, resp_rx) = oneshot::channel::>(); let cmd = PubSubRelationCommand::SetupSubscriber { max_subscribe_id, - subscriber_session_id, + downstream_session_id, resp: resp_tx, }; self.tx.send(cmd).await.unwrap(); @@ -84,15 +84,15 @@ impl PubSubRelationManagerRepository for PubSubRelationManagerInterface { Err(err) => bail!(err), } } - async fn is_valid_subscriber_subscribe_id( + async fn is_valid_downstream_subscribe_id( &self, subscribe_id: u64, - subscriber_session_id: usize, + downstream_session_id: usize, ) -> Result { let (resp_tx, resp_rx) = oneshot::channel::>(); - let cmd = PubSubRelationCommand::IsValidSubscriberSubscribeId { + let cmd = PubSubRelationCommand::IsValidDownstreamSubscribeId { subscribe_id, - subscriber_session_id, + downstream_session_id, resp: resp_tx, }; self.tx.send(cmd).await.unwrap(); @@ -104,15 +104,15 @@ impl PubSubRelationManagerRepository for PubSubRelationManagerInterface { Err(err) => bail!(err), } } - async fn is_valid_subscriber_track_alias( + async fn is_valid_downstream_track_alias( &self, track_alias: u64, - subscriber_session_id: usize, + downstream_session_id: usize, ) -> Result { let (resp_tx, resp_rx) = oneshot::channel::>(); - let cmd = PubSubRelationCommand::IsValidSubscriberTrackAlias { + let cmd = PubSubRelationCommand::IsValidDownstreamTrackAlias { track_alias, - subscriber_session_id, + downstream_session_id, resp: resp_tx, }; self.tx.send(cmd).await.unwrap(); @@ -144,13 +144,13 @@ impl PubSubRelationManagerRepository for PubSubRelationManagerInterface { Err(err) => bail!(err), } } - async fn get_publisher_subscription_by_full_track_name( + async fn get_upstream_subscription_by_full_track_name( &self, track_namespace: Vec, track_name: String, ) -> Result> { let (resp_tx, resp_rx) = oneshot::channel::>>(); - let cmd = PubSubRelationCommand::GetPublisherSubscription { + let cmd = PubSubRelationCommand::GetUpstreamSubscription { track_namespace, track_name, resp: resp_tx, @@ -164,12 +164,9 @@ impl PubSubRelationManagerRepository for PubSubRelationManagerInterface { Err(err) => bail!(err), } } - async fn get_publisher_session_id( - &self, - track_namespace: Vec, - ) -> Result> { + async fn get_upstream_session_id(&self, track_namespace: Vec) -> Result> { let (resp_tx, resp_rx) = oneshot::channel::>>(); - let cmd = PubSubRelationCommand::GetPublisherSessionId { + let cmd = PubSubRelationCommand::GetUpstreamSessionId { track_namespace, resp: resp_tx, }; @@ -178,19 +175,19 @@ impl PubSubRelationManagerRepository for PubSubRelationManagerInterface { let result = resp_rx.await.unwrap(); match result { - Ok(publisher_session_id) => Ok(publisher_session_id), + Ok(upstream_session_id) => Ok(upstream_session_id), Err(err) => bail!(err), } } - async fn get_requesting_subscriber_session_ids_and_subscribe_ids( + async fn get_requesting_downstream_session_ids_and_subscribe_ids( &self, - publisher_subscribe_id: u64, - publisher_session_id: usize, + upstream_subscribe_id: u64, + upstream_session_id: usize, ) -> Result>> { let (resp_tx, resp_rx) = oneshot::channel::>>>(); - let cmd = PubSubRelationCommand::GetRequestingSubscriberSessionIdsAndSubscribeIds { - publisher_subscribe_id, - publisher_session_id, + let cmd = PubSubRelationCommand::GetRequestingDownstreamSessionIdsAndSubscribeIds { + upstream_subscribe_id, + upstream_session_id, resp: resp_tx, }; self.tx.send(cmd).await.unwrap(); @@ -202,17 +199,17 @@ impl PubSubRelationManagerRepository for PubSubRelationManagerInterface { Err(err) => bail!(err), } } - async fn get_publisher_subscribe_id( + async fn get_upstream_subscribe_id( &self, track_namespace: Vec, track_name: String, - publisher_session_id: usize, + upstream_session_id: usize, ) -> Result> { let (resp_tx, resp_rx) = oneshot::channel::>>(); - let cmd = PubSubRelationCommand::GetPublisherSubscribeId { + let cmd = PubSubRelationCommand::GetUpstreamSubscribeId { track_namespace, track_name, - publisher_session_id, + upstream_session_id, resp: resp_tx, }; self.tx.send(cmd).await.unwrap(); @@ -224,9 +221,9 @@ impl PubSubRelationManagerRepository for PubSubRelationManagerInterface { Err(err) => bail!(err), } } - async fn set_subscriber_subscription( + async fn set_downstream_subscription( &self, - subscriber_session_id: usize, + downstream_session_id: usize, subscribe_id: u64, track_alias: u64, track_namespace: Vec, @@ -240,8 +237,8 @@ impl PubSubRelationManagerRepository for PubSubRelationManagerInterface { end_object: Option, ) -> Result<()> { let (resp_tx, resp_rx) = oneshot::channel::>(); - let cmd = PubSubRelationCommand::SetSubscriberSubscription { - subscriber_session_id, + let cmd = PubSubRelationCommand::SetDownstreamSubscription { + downstream_session_id, subscribe_id, track_alias, track_namespace, @@ -265,9 +262,9 @@ impl PubSubRelationManagerRepository for PubSubRelationManagerInterface { } } #[allow(clippy::too_many_arguments)] - async fn set_publisher_subscription( + async fn set_upstream_subscription( &self, - publisher_session_id: usize, + upstream_session_id: usize, track_namespace: Vec, track_name: String, subscriber_priority: u8, @@ -279,8 +276,8 @@ impl PubSubRelationManagerRepository for PubSubRelationManagerInterface { end_object: Option, ) -> Result<(u64, u64)> { let (resp_tx, resp_rx) = oneshot::channel::>(); - let cmd = PubSubRelationCommand::SetPublisherSubscription { - publisher_session_id, + let cmd = PubSubRelationCommand::SetUpstreamSubscription { + upstream_session_id, track_namespace, track_name, subscriber_priority, @@ -301,19 +298,19 @@ impl PubSubRelationManagerRepository for PubSubRelationManagerInterface { Err(err) => bail!(err), } } - async fn register_pubsup_relation( + async fn set_pubsub_relation( &self, - publisher_session_id: usize, - publisher_subscribe_id: u64, - subscriber_session_id: usize, - subscriber_subscribe_id: u64, + upstream_session_id: usize, + upstream_subscribe_id: u64, + downstream_session_id: usize, + downstream_subscribe_id: u64, ) -> Result<()> { let (resp_tx, resp_rx) = oneshot::channel::>(); - let cmd = RegisterPubSubRelation { - publisher_session_id, - publisher_subscribe_id, - subscriber_session_id, - subscriber_subscribe_id, + let cmd = SetPubSubRelation { + upstream_session_id, + upstream_subscribe_id, + downstream_session_id, + downstream_subscribe_id, resp: resp_tx, }; @@ -325,14 +322,14 @@ impl PubSubRelationManagerRepository for PubSubRelationManagerInterface { Err(err) => bail!(err), } } - async fn activate_subscriber_subscription( + async fn activate_downstream_subscription( &self, - subscriber_session_id: usize, + downstream_session_id: usize, subscribe_id: u64, ) -> Result { let (resp_tx, resp_rx) = oneshot::channel::>(); - let cmd = ActivateSubscriberSubscription { - subscriber_session_id, + let cmd = ActivateDownstreamSubscription { + downstream_session_id, subscribe_id, resp: resp_tx, }; @@ -344,14 +341,14 @@ impl PubSubRelationManagerRepository for PubSubRelationManagerInterface { Err(err) => bail!(err), } } - async fn activate_publisher_subscription( + async fn activate_upstream_subscription( &self, - publisher_session_id: usize, + upstream_session_id: usize, subscribe_id: u64, ) -> Result { let (resp_tx, resp_rx) = oneshot::channel::>(); - let cmd = ActivatePublisherSubscription { - publisher_session_id, + let cmd = ActivateUpstreamSubscription { + upstream_session_id, subscribe_id, resp: resp_tx, }; @@ -363,15 +360,15 @@ impl PubSubRelationManagerRepository for PubSubRelationManagerInterface { Err(err) => bail!(err), } } - async fn delete_publisher_announced_namespace( + async fn delete_upstream_announced_namespace( &self, track_namespace: Vec, - publisher_session_id: usize, + upstream_session_id: usize, ) -> Result { let (resp_tx, resp_rx) = oneshot::channel::>(); - let cmd = DeletePublisherAnnouncedNamespace { + let cmd = DeleteUpstreamAnnouncedNamespace { track_namespace, - publisher_session_id, + upstream_session_id, resp: resp_tx, }; self.tx.send(cmd).await.unwrap(); @@ -399,19 +396,19 @@ impl PubSubRelationManagerRepository for PubSubRelationManagerInterface { } #[cfg(test)] -pub(crate) mod test_utils { +pub(crate) mod test_helper_fn { use crate::modules::pubsub_relation_manager::{ commands::PubSubRelationCommand, - interface::PubSubRelationManagerInterface, manager::{Consumers, Producers}, relation::PubSubRelation, + wrapper::PubSubRelationManagerWrapper, }; use anyhow::Result; use tokio::sync::oneshot; pub(crate) async fn get_node_and_relation_clone( - pubsub_relation_manager: &PubSubRelationManagerInterface, + pubsub_relation_manager: &PubSubRelationManagerWrapper, ) -> (Consumers, Producers, PubSubRelation) { let (resp_tx, resp_rx) = oneshot::channel::>(); let cmd = PubSubRelationCommand::GetNodeAndRelationClone { resp: resp_tx }; @@ -424,62 +421,63 @@ pub(crate) mod test_utils { #[cfg(test)] mod success { use crate::modules::pubsub_relation_manager::{ - commands::PubSubRelationCommand, interface::test_utils, - interface::PubSubRelationManagerInterface, manager::pubsub_relation_manager, + commands::PubSubRelationCommand, manager::pubsub_relation_manager, wrapper::test_helper_fn, + wrapper::PubSubRelationManagerWrapper, }; use moqt_core::messages::control_messages::subscribe::{FilterType, GroupOrder}; + use moqt_core::models::subscriptions::{ + nodes::registry::SubscriptionNodeRegistry, Subscription, + }; use moqt_core::pubsub_relation_manager_repository::PubSubRelationManagerRepository; - use moqt_core::subscription_models::nodes::node_registory::SubscriptionNodeRegistory; - use moqt_core::subscription_models::subscriptions::Subscription; use tokio::sync::mpsc; #[tokio::test] async fn setup_publisher() { let max_subscribe_id = 10; - let publisher_session_id = 1; + let upstream_session_id = 1; // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let pubsub_relation_manager = PubSubRelationManagerWrapper::new(track_tx.clone()); let result = pubsub_relation_manager - .setup_publisher(max_subscribe_id, publisher_session_id) + .setup_publisher(max_subscribe_id, upstream_session_id) .await; assert!(result.is_ok()); // Check if the publisher is created let (consumers, _, _) = - test_utils::get_node_and_relation_clone(&pubsub_relation_manager).await; + test_helper_fn::get_node_and_relation_clone(&pubsub_relation_manager).await; let length = consumers.len(); assert_eq!(length, 1); } #[tokio::test] - async fn set_publisher_announced_namespace() { + async fn set_upstream_announced_namespace() { let max_subscribe_id = 10; let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); - let publisher_session_id = 1; + let upstream_session_id = 1; // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let pubsub_relation_manager = PubSubRelationManagerWrapper::new(track_tx.clone()); let _ = pubsub_relation_manager - .setup_publisher(max_subscribe_id, publisher_session_id) + .setup_publisher(max_subscribe_id, upstream_session_id) .await; let result = pubsub_relation_manager - .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) + .set_upstream_announced_namespace(track_namespace.clone(), upstream_session_id) .await; assert!(result.is_ok()); // Check if the track_namespace is set let (consumers, _, _) = - test_utils::get_node_and_relation_clone(&pubsub_relation_manager).await; + test_helper_fn::get_node_and_relation_clone(&pubsub_relation_manager).await; - let consumer = consumers.get(&publisher_session_id).unwrap(); + let consumer = consumers.get(&upstream_session_id).unwrap(); let announced_namespaces = consumer.get_namespaces().unwrap(); let announced_namespace = announced_namespaces.first().unwrap().to_vec(); @@ -489,43 +487,43 @@ mod success { #[tokio::test] async fn setup_subscriber() { let max_subscribe_id = 10; - let subscriber_session_id = 1; + let downstream_session_id = 1; // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let pubsub_relation_manager = PubSubRelationManagerWrapper::new(track_tx.clone()); let result = pubsub_relation_manager - .setup_subscriber(max_subscribe_id, subscriber_session_id) + .setup_subscriber(max_subscribe_id, downstream_session_id) .await; assert!(result.is_ok()); // Check if the subscriber is created let (_, producers, _) = - test_utils::get_node_and_relation_clone(&pubsub_relation_manager).await; + test_helper_fn::get_node_and_relation_clone(&pubsub_relation_manager).await; let length = producers.len(); assert_eq!(length, 1); } #[tokio::test] - async fn is_valid_subscriber_subscribe_id_valid() { + async fn is_valid_downstream_subscribe_id_valid() { let max_subscribe_id = 10; let subscribe_id = 1; - let subscriber_session_id = 1; + let downstream_session_id = 1; // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let pubsub_relation_manager = PubSubRelationManagerWrapper::new(track_tx.clone()); let _ = pubsub_relation_manager - .setup_subscriber(max_subscribe_id, subscriber_session_id) + .setup_subscriber(max_subscribe_id, downstream_session_id) .await; let result = pubsub_relation_manager - .is_valid_subscriber_subscribe_id(subscribe_id, subscriber_session_id) + .is_valid_downstream_subscribe_id(subscribe_id, downstream_session_id) .await; let is_valid = result.unwrap(); @@ -533,22 +531,22 @@ mod success { } #[tokio::test] - async fn is_valid_subscriber_subscribe_id_invalid() { + async fn is_valid_downstream_subscribe_id_invalid() { let max_subscribe_id = 10; let subscribe_id = 11; - let subscriber_session_id = 1; + let downstream_session_id = 1; // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let pubsub_relation_manager = PubSubRelationManagerWrapper::new(track_tx.clone()); let _ = pubsub_relation_manager - .setup_subscriber(max_subscribe_id, subscriber_session_id) + .setup_subscriber(max_subscribe_id, downstream_session_id) .await; let result = pubsub_relation_manager - .is_valid_subscriber_subscribe_id(subscribe_id, subscriber_session_id) + .is_valid_downstream_subscribe_id(subscribe_id, downstream_session_id) .await; let is_valid = result.unwrap(); @@ -556,22 +554,22 @@ mod success { } #[tokio::test] - async fn is_valid_subscriber_track_alias_valid() { + async fn is_valid_downstream_track_alias_valid() { let max_subscribe_id = 10; let track_alias = 1; - let subscriber_session_id = 1; + let downstream_session_id = 1; // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let pubsub_relation_manager = PubSubRelationManagerWrapper::new(track_tx.clone()); let _ = pubsub_relation_manager - .setup_subscriber(max_subscribe_id, subscriber_session_id) + .setup_subscriber(max_subscribe_id, downstream_session_id) .await; let result = pubsub_relation_manager - .is_valid_subscriber_track_alias(track_alias, subscriber_session_id) + .is_valid_downstream_track_alias(track_alias, downstream_session_id) .await; let is_valid = result.unwrap(); @@ -579,9 +577,9 @@ mod success { } #[tokio::test] - async fn is_valid_subscriber_track_alias_invalid() { + async fn is_valid_downstream_track_alias_invalid() { let max_subscribe_id = 10; - let subscriber_session_id = 1; + let downstream_session_id = 1; let subscribe_id = 0; let track_alias = 0; let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); @@ -598,13 +596,13 @@ mod success { let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let pubsub_relation_manager = PubSubRelationManagerWrapper::new(track_tx.clone()); let _ = pubsub_relation_manager - .setup_subscriber(max_subscribe_id, subscriber_session_id) + .setup_subscriber(max_subscribe_id, downstream_session_id) .await; let _ = pubsub_relation_manager - .set_subscriber_subscription( - subscriber_session_id, + .set_downstream_subscription( + downstream_session_id, subscribe_id, track_alias, track_namespace, @@ -620,7 +618,7 @@ mod success { .await; let result = pubsub_relation_manager - .is_valid_subscriber_track_alias(track_alias, subscriber_session_id) + .is_valid_downstream_track_alias(track_alias, downstream_session_id) .await; assert!(result.is_ok()); @@ -631,7 +629,7 @@ mod success { #[tokio::test] async fn is_track_existing_exists() { let max_subscribe_id = 10; - let publisher_session_id = 1; + let upstream_session_id = 1; let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); let track_name = "track_name".to_string(); let subscriber_priority = 0; @@ -646,19 +644,19 @@ mod success { let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let pubsub_relation_manager = PubSubRelationManagerWrapper::new(track_tx.clone()); let _ = pubsub_relation_manager - .setup_publisher(max_subscribe_id, publisher_session_id) + .setup_publisher(max_subscribe_id, upstream_session_id) .await; let _ = pubsub_relation_manager - .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) + .set_upstream_announced_namespace(track_namespace.clone(), upstream_session_id) .await; let _ = pubsub_relation_manager - .set_publisher_subscription( - publisher_session_id, + .set_upstream_subscription( + upstream_session_id, track_namespace.clone(), track_name.clone(), subscriber_priority, @@ -689,7 +687,7 @@ mod success { let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let pubsub_relation_manager = PubSubRelationManagerWrapper::new(track_tx.clone()); let result = pubsub_relation_manager .is_track_existing(track_namespace, track_name) .await; @@ -700,9 +698,9 @@ mod success { } #[tokio::test] - async fn get_publisher_subscription_by_full_track_name() { + async fn get_upstream_subscription_by_full_track_name() { let max_subscribe_id = 10; - let publisher_session_id = 1; + let upstream_session_id = 1; let track_alias = 0; let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); let track_name = "track_name".to_string(); @@ -718,13 +716,13 @@ mod success { let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let pubsub_relation_manager = PubSubRelationManagerWrapper::new(track_tx.clone()); let _ = pubsub_relation_manager - .setup_publisher(max_subscribe_id, publisher_session_id) + .setup_publisher(max_subscribe_id, upstream_session_id) .await; let _ = pubsub_relation_manager - .set_publisher_subscription( - publisher_session_id, + .set_upstream_subscription( + upstream_session_id, track_namespace.clone(), track_name.clone(), subscriber_priority, @@ -738,7 +736,7 @@ mod success { .await; let subscription = pubsub_relation_manager - .get_publisher_subscription_by_full_track_name( + .get_upstream_subscription_by_full_track_name( track_namespace.clone(), track_name.clone(), ) @@ -764,40 +762,40 @@ mod success { } #[tokio::test] - async fn get_publisher_session_id() { + async fn get_upstream_session_id() { let max_subscribe_id = 10; let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); - let publisher_session_id = 1; + let upstream_session_id = 1; // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let pubsub_relation_manager = PubSubRelationManagerWrapper::new(track_tx.clone()); let _ = pubsub_relation_manager - .setup_publisher(max_subscribe_id, publisher_session_id) + .setup_publisher(max_subscribe_id, upstream_session_id) .await; let _ = pubsub_relation_manager - .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) + .set_upstream_announced_namespace(track_namespace.clone(), upstream_session_id) .await; let session_id = pubsub_relation_manager - .get_publisher_session_id(track_namespace.clone()) + .get_upstream_session_id(track_namespace.clone()) .await .unwrap() .unwrap(); - assert_eq!(session_id, publisher_session_id); + assert_eq!(session_id, upstream_session_id); } #[tokio::test] - async fn get_requesting_subscriber_session_ids_and_subscribe_ids() { + async fn get_requesting_downstream_session_ids_and_subscribe_ids() { let max_subscribe_id = 10; - let publisher_session_id = 1; - let subscriber_session_ids = [2, 3]; - let subscriber_subscribe_ids = [4, 5]; - let subscriber_track_aliases = [6, 7]; + let upstream_session_id = 1; + let downstream_session_ids = [2, 3]; + let downstream_subscribe_ids = [4, 5]; + let downstream_track_aliases = [6, 7]; let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); let track_name = "track_name".to_string(); let subscriber_priority = 0; @@ -812,17 +810,17 @@ mod success { let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let pubsub_relation_manager = PubSubRelationManagerWrapper::new(track_tx.clone()); let _ = pubsub_relation_manager - .setup_publisher(max_subscribe_id, publisher_session_id) + .setup_publisher(max_subscribe_id, upstream_session_id) .await; let _ = pubsub_relation_manager - .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) + .set_upstream_announced_namespace(track_namespace.clone(), upstream_session_id) .await; - let (publisher_subscribe_id, _) = pubsub_relation_manager - .set_publisher_subscription( - publisher_session_id, + let (upstream_subscribe_id, _) = pubsub_relation_manager + .set_upstream_subscription( + upstream_session_id, track_namespace.clone(), track_name.clone(), subscriber_priority, @@ -838,13 +836,13 @@ mod success { for i in [0, 1] { let _ = pubsub_relation_manager - .setup_subscriber(max_subscribe_id, subscriber_session_ids[i]) + .setup_subscriber(max_subscribe_id, downstream_session_ids[i]) .await; let _ = pubsub_relation_manager - .set_subscriber_subscription( - subscriber_session_ids[i], - subscriber_subscribe_ids[i], - subscriber_track_aliases[i], + .set_downstream_subscription( + downstream_session_ids[i], + downstream_subscribe_ids[i], + downstream_track_aliases[i], track_namespace.clone(), track_name.clone(), subscriber_priority, @@ -857,36 +855,36 @@ mod success { ) .await; let _ = pubsub_relation_manager - .register_pubsup_relation( - publisher_session_id, - publisher_subscribe_id, - subscriber_session_ids[i], - subscriber_subscribe_ids[i], + .set_pubsub_relation( + upstream_session_id, + upstream_subscribe_id, + downstream_session_ids[i], + downstream_subscribe_ids[i], ) .await; } let list = pubsub_relation_manager - .get_requesting_subscriber_session_ids_and_subscribe_ids( - publisher_subscribe_id, - publisher_session_id, + .get_requesting_downstream_session_ids_and_subscribe_ids( + upstream_subscribe_id, + upstream_session_id, ) .await .unwrap() .unwrap(); let expected_list = vec![ - (subscriber_session_ids[0], subscriber_subscribe_ids[0]), - (subscriber_session_ids[1], subscriber_subscribe_ids[1]), + (downstream_session_ids[0], downstream_subscribe_ids[0]), + (downstream_session_ids[1], downstream_subscribe_ids[1]), ]; assert_eq!(list, expected_list); } #[tokio::test] - async fn get_publisher_subscribe_id() { + async fn get_upstream_subscribe_id() { let max_subscribe_id = 10; - let publisher_session_id = 1; + let upstream_session_id = 1; let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); let track_name = "track_name".to_string(); let subscriber_priority = 0; @@ -901,17 +899,17 @@ mod success { let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let pubsub_relation_manager = PubSubRelationManagerWrapper::new(track_tx.clone()); let _ = pubsub_relation_manager - .setup_publisher(max_subscribe_id, publisher_session_id) + .setup_publisher(max_subscribe_id, upstream_session_id) .await; let _ = pubsub_relation_manager - .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) + .set_upstream_announced_namespace(track_namespace.clone(), upstream_session_id) .await; - let (expected_publisher_subscribe_id, _) = pubsub_relation_manager - .set_publisher_subscription( - publisher_session_id, + let (expected_upstream_subscribe_id, _) = pubsub_relation_manager + .set_upstream_subscription( + upstream_session_id, track_namespace.clone(), track_name.clone(), subscriber_priority, @@ -925,19 +923,19 @@ mod success { .await .unwrap(); - let publisher_subscribe_id = pubsub_relation_manager - .get_publisher_subscribe_id(track_namespace, track_name, publisher_session_id) + let upstream_subscribe_id = pubsub_relation_manager + .get_upstream_subscribe_id(track_namespace, track_name, upstream_session_id) .await .unwrap() .unwrap(); - assert_eq!(publisher_subscribe_id, expected_publisher_subscribe_id); + assert_eq!(upstream_subscribe_id, expected_upstream_subscribe_id); } #[tokio::test] - async fn set_subscriber_subscription() { + async fn set_downstream_subscription() { let max_subscribe_id = 10; - let subscriber_session_id = 1; + let downstream_session_id = 1; let subscribe_id = 0; let track_alias = 0; let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); @@ -954,13 +952,13 @@ mod success { let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let pubsub_relation_manager = PubSubRelationManagerWrapper::new(track_tx.clone()); let _ = pubsub_relation_manager - .setup_subscriber(max_subscribe_id, subscriber_session_id) + .setup_subscriber(max_subscribe_id, downstream_session_id) .await; let result = pubsub_relation_manager - .set_subscriber_subscription( - subscriber_session_id, + .set_downstream_subscription( + downstream_session_id, subscribe_id, track_alias, track_namespace.clone(), @@ -979,8 +977,8 @@ mod success { // Assert that the subscription is set let (_, producers, _) = - test_utils::get_node_and_relation_clone(&pubsub_relation_manager).await; - let producer = producers.get(&subscriber_session_id).unwrap(); + test_helper_fn::get_node_and_relation_clone(&pubsub_relation_manager).await; + let producer = producers.get(&downstream_session_id).unwrap(); let subscription = producer.get_subscription(subscribe_id).unwrap().unwrap(); let expected_subscription = Subscription::new( @@ -1001,9 +999,9 @@ mod success { } #[tokio::test] - async fn set_publisher_subscription() { + async fn set_upstream_subscription() { let max_subscribe_id = 10; - let publisher_session_id = 1; + let upstream_session_id = 1; let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); let track_name = "track_name".to_string(); let subscriber_priority = 0; @@ -1018,13 +1016,13 @@ mod success { let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let pubsub_relation_manager = PubSubRelationManagerWrapper::new(track_tx.clone()); let _ = pubsub_relation_manager - .setup_publisher(max_subscribe_id, publisher_session_id) + .setup_publisher(max_subscribe_id, upstream_session_id) .await; let result = pubsub_relation_manager - .set_publisher_subscription( - publisher_session_id, + .set_upstream_subscription( + upstream_session_id, track_namespace.clone(), track_name.clone(), subscriber_priority, @@ -1039,19 +1037,19 @@ mod success { assert!(result.is_ok()); - let (publisher_subscribe_id, publisher_track_alias) = result.unwrap(); + let (upstream_subscribe_id, upstream_track_alias) = result.unwrap(); // Assert that the subscription is set let (consumers, _, _) = - test_utils::get_node_and_relation_clone(&pubsub_relation_manager).await; - let consumer = consumers.get(&publisher_session_id).unwrap(); + test_helper_fn::get_node_and_relation_clone(&pubsub_relation_manager).await; + let consumer = consumers.get(&upstream_session_id).unwrap(); let subscription = consumer - .get_subscription(publisher_subscribe_id) + .get_subscription(upstream_subscribe_id) .unwrap() .unwrap(); let expected_subscription = Subscription::new( - publisher_track_alias, + upstream_track_alias, track_namespace, track_name, subscriber_priority, @@ -1068,12 +1066,12 @@ mod success { } #[tokio::test] - async fn register_pubsup_relation() { + async fn set_pubsub_relation() { let max_subscribe_id = 10; - let publisher_session_id = 1; - let subscriber_session_ids = [2, 3]; - let subscriber_subscribe_ids = [4, 5]; - let subscriber_track_aliases = [6, 7]; + let upstream_session_id = 1; + let downstream_session_ids = [2, 3]; + let downstream_subscribe_ids = [4, 5]; + let downstream_track_aliases = [6, 7]; let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); let track_name = "track_name".to_string(); let subscriber_priority = 0; @@ -1088,18 +1086,18 @@ mod success { let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let pubsub_relation_manager = PubSubRelationManagerWrapper::new(track_tx.clone()); // pub 1 <- sub 2, 3 let _ = pubsub_relation_manager - .setup_publisher(max_subscribe_id, publisher_session_id) + .setup_publisher(max_subscribe_id, upstream_session_id) .await; let _ = pubsub_relation_manager - .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) + .set_upstream_announced_namespace(track_namespace.clone(), upstream_session_id) .await; - let (publisher_subscribe_id, _) = pubsub_relation_manager - .set_publisher_subscription( - publisher_session_id, + let (upstream_subscribe_id, _) = pubsub_relation_manager + .set_upstream_subscription( + upstream_session_id, track_namespace.clone(), track_name.clone(), subscriber_priority, @@ -1115,13 +1113,13 @@ mod success { for i in [0, 1] { let _ = pubsub_relation_manager - .setup_subscriber(max_subscribe_id, subscriber_session_ids[i]) + .setup_subscriber(max_subscribe_id, downstream_session_ids[i]) .await; let _ = pubsub_relation_manager - .set_subscriber_subscription( - subscriber_session_ids[i], - subscriber_subscribe_ids[i], - subscriber_track_aliases[i], + .set_downstream_subscription( + downstream_session_ids[i], + downstream_subscribe_ids[i], + downstream_track_aliases[i], track_namespace.clone(), track_name.clone(), subscriber_priority, @@ -1134,11 +1132,11 @@ mod success { ) .await; let result = pubsub_relation_manager - .register_pubsup_relation( - publisher_session_id, - publisher_subscribe_id, - subscriber_session_ids[i], - subscriber_subscribe_ids[i], + .set_pubsub_relation( + upstream_session_id, + upstream_subscribe_id, + downstream_session_ids[i], + downstream_subscribe_ids[i], ) .await; @@ -1147,25 +1145,25 @@ mod success { // Assert that the relation is registered let (_, _, pubsub_relation) = - test_utils::get_node_and_relation_clone(&pubsub_relation_manager).await; + test_helper_fn::get_node_and_relation_clone(&pubsub_relation_manager).await; let subscriber = pubsub_relation - .get_subscribers(publisher_session_id, publisher_subscribe_id) + .get_subscribers(upstream_session_id, upstream_subscribe_id) .unwrap() .to_vec(); let expected_subscriber = vec![ - (subscriber_session_ids[0], subscriber_subscribe_ids[0]), - (subscriber_session_ids[1], subscriber_subscribe_ids[1]), + (downstream_session_ids[0], downstream_subscribe_ids[0]), + (downstream_session_ids[1], downstream_subscribe_ids[1]), ]; assert_eq!(subscriber, expected_subscriber); } #[tokio::test] - async fn activate_subscriber_subscription() { + async fn activate_downstream_subscription() { let max_subscribe_id = 10; - let subscriber_session_id = 1; + let downstream_session_id = 1; let subscribe_id = 0; let track_alias = 0; let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); @@ -1182,13 +1180,13 @@ mod success { let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let pubsub_relation_manager = PubSubRelationManagerWrapper::new(track_tx.clone()); let _ = pubsub_relation_manager - .setup_subscriber(max_subscribe_id, subscriber_session_id) + .setup_subscriber(max_subscribe_id, downstream_session_id) .await; let _ = pubsub_relation_manager - .set_subscriber_subscription( - subscriber_session_id, + .set_downstream_subscription( + downstream_session_id, subscribe_id, track_alias, track_namespace, @@ -1204,7 +1202,7 @@ mod success { .await; let activate_occured = pubsub_relation_manager - .activate_subscriber_subscription(subscriber_session_id, subscribe_id) + .activate_downstream_subscription(downstream_session_id, subscribe_id) .await .unwrap(); @@ -1212,17 +1210,17 @@ mod success { // Assert that the subscription is active let (_, producers, _) = - test_utils::get_node_and_relation_clone(&pubsub_relation_manager).await; - let producer = producers.get(&subscriber_session_id).unwrap(); + test_helper_fn::get_node_and_relation_clone(&pubsub_relation_manager).await; + let producer = producers.get(&downstream_session_id).unwrap(); let subscription = producer.get_subscription(subscribe_id).unwrap().unwrap(); assert!(subscription.is_active()); } #[tokio::test] - async fn activate_publisher_subscription() { + async fn activate_upstream_subscription() { let max_subscribe_id = 10; - let publisher_session_id = 1; + let upstream_session_id = 1; let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); let track_name = "track_name".to_string(); let subscriber_priority = 0; @@ -1237,13 +1235,13 @@ mod success { let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let pubsub_relation_manager = PubSubRelationManagerWrapper::new(track_tx.clone()); let _ = pubsub_relation_manager - .setup_publisher(max_subscribe_id, publisher_session_id) + .setup_publisher(max_subscribe_id, upstream_session_id) .await; - let (publisher_subscribe_id, _) = pubsub_relation_manager - .set_publisher_subscription( - publisher_session_id, + let (upstream_subscribe_id, _) = pubsub_relation_manager + .set_upstream_subscription( + upstream_session_id, track_namespace.clone(), track_name.clone(), subscriber_priority, @@ -1258,7 +1256,7 @@ mod success { .unwrap(); let activate_occured = pubsub_relation_manager - .activate_publisher_subscription(publisher_session_id, publisher_subscribe_id) + .activate_upstream_subscription(upstream_session_id, upstream_subscribe_id) .await .unwrap(); @@ -1266,10 +1264,10 @@ mod success { // Assert that the subscription is active let (consumers, _, _) = - test_utils::get_node_and_relation_clone(&pubsub_relation_manager).await; - let consumer = consumers.get(&publisher_session_id).unwrap(); + test_helper_fn::get_node_and_relation_clone(&pubsub_relation_manager).await; + let consumer = consumers.get(&upstream_session_id).unwrap(); let subscription = consumer - .get_subscription(publisher_subscribe_id) + .get_subscription(upstream_subscribe_id) .unwrap() .unwrap(); @@ -1277,25 +1275,25 @@ mod success { } #[tokio::test] - async fn delete_publisher_announced_namespace() { + async fn delete_upstream_announced_namespace() { let max_subscribe_id = 10; let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); - let publisher_session_id = 1; + let upstream_session_id = 1; // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let pubsub_relation_manager = PubSubRelationManagerWrapper::new(track_tx.clone()); let _ = pubsub_relation_manager - .setup_publisher(max_subscribe_id, publisher_session_id) + .setup_publisher(max_subscribe_id, upstream_session_id) .await; let _ = pubsub_relation_manager - .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) + .set_upstream_announced_namespace(track_namespace.clone(), upstream_session_id) .await; let result = pubsub_relation_manager - .delete_publisher_announced_namespace(track_namespace, publisher_session_id) + .delete_upstream_announced_namespace(track_namespace, upstream_session_id) .await; assert!(result.is_ok()); @@ -1304,30 +1302,30 @@ mod success { // Assert that the announced namespace is deleted let (consumers, _, _) = - test_utils::get_node_and_relation_clone(&pubsub_relation_manager).await; - let consumer = consumers.get(&publisher_session_id).unwrap(); + test_helper_fn::get_node_and_relation_clone(&pubsub_relation_manager).await; + let consumer = consumers.get(&upstream_session_id).unwrap(); let announced_namespaces = consumer.get_namespaces().unwrap().to_vec(); assert!(announced_namespaces.is_empty()); } #[tokio::test] - async fn delete_publisher_announced_namespace_not_exists() { + async fn delete_upstream_announced_namespace_not_exists() { let max_subscribe_id = 10; let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); - let publisher_session_id = 1; + let upstream_session_id = 1; // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let pubsub_relation_manager = PubSubRelationManagerWrapper::new(track_tx.clone()); let _ = pubsub_relation_manager - .setup_publisher(max_subscribe_id, publisher_session_id) + .setup_publisher(max_subscribe_id, upstream_session_id) .await; let result = pubsub_relation_manager - .delete_publisher_announced_namespace(track_namespace, publisher_session_id) + .delete_upstream_announced_namespace(track_namespace, upstream_session_id) .await; assert!(result.is_ok()); @@ -1342,11 +1340,11 @@ mod success { Vec::from(["test1".to_string(), "test1".to_string()]), Vec::from(["test2".to_string(), "test2".to_string()]), ]; - let publisher_session_ids = [1, 2]; - let mut publisher_subscribe_ids = vec![]; - let subscriber_session_ids = [2, 3, 4]; - let subscriber_subscribe_ids = [2, 3, 4]; - let subscriber_track_aliases = [2, 3, 4]; + let upstream_session_ids = [1, 2]; + let mut upstream_subscribe_ids = vec![]; + let downstream_session_ids = [2, 3, 4]; + let downstream_subscribe_ids = [2, 3, 4]; + let downstream_track_aliases = [2, 3, 4]; let track_name = "test_name".to_string(); let subscriber_priority = 0; let group_order = GroupOrder::Ascending; @@ -1360,7 +1358,7 @@ mod success { let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let pubsub_relation_manager = PubSubRelationManagerWrapper::new(track_tx.clone()); // Register: // pub 1 <- sub 2, 3, 4 @@ -1368,17 +1366,17 @@ mod success { for i in [0, 1] { // for pub 1, 2 let _ = pubsub_relation_manager - .setup_publisher(max_subscribe_id, publisher_session_ids[i]) + .setup_publisher(max_subscribe_id, upstream_session_ids[i]) .await; let _ = pubsub_relation_manager - .set_publisher_announced_namespace( + .set_upstream_announced_namespace( track_namespaces[i].clone(), - publisher_session_ids[i], + upstream_session_ids[i], ) .await; - let (publisher_subscribe_id, _) = pubsub_relation_manager - .set_publisher_subscription( - publisher_session_ids[i], + let (upstream_subscribe_id, _) = pubsub_relation_manager + .set_upstream_subscription( + upstream_session_ids[i], track_namespaces[i].clone(), track_name.clone(), subscriber_priority, @@ -1391,22 +1389,22 @@ mod success { ) .await .unwrap(); - publisher_subscribe_ids.push(publisher_subscribe_id); + upstream_subscribe_ids.push(upstream_subscribe_id); } for j in [0, 1, 2] { // for sub 2, 3, 4 let _ = pubsub_relation_manager - .setup_subscriber(max_subscribe_id, subscriber_session_ids[j]) + .setup_subscriber(max_subscribe_id, downstream_session_ids[j]) .await; } // for sub 2 let _ = pubsub_relation_manager - .set_subscriber_subscription( - subscriber_session_ids[0], - subscriber_subscribe_ids[0], - subscriber_track_aliases[0], + .set_downstream_subscription( + downstream_session_ids[0], + downstream_subscribe_ids[0], + downstream_track_aliases[0], track_namespaces[0].clone(), track_name.clone(), subscriber_priority, @@ -1424,10 +1422,10 @@ mod success { for j in [1, 2] { // for sub 3, 4 let _ = pubsub_relation_manager - .set_subscriber_subscription( - subscriber_session_ids[j], - subscriber_subscribe_ids[j], - subscriber_track_aliases[j], + .set_downstream_subscription( + downstream_session_ids[j], + downstream_subscribe_ids[j], + downstream_track_aliases[j], track_namespaces[i].clone(), track_name.clone(), subscriber_priority, @@ -1441,24 +1439,24 @@ mod success { .await; let _ = pubsub_relation_manager - .register_pubsup_relation( - publisher_session_ids[i], - publisher_subscribe_ids[i], - subscriber_session_ids[j], - subscriber_subscribe_ids[j], + .set_pubsub_relation( + upstream_session_ids[i], + upstream_subscribe_ids[i], + downstream_session_ids[j], + downstream_subscribe_ids[j], ) .await; let _ = pubsub_relation_manager - .activate_subscriber_subscription( - subscriber_session_ids[j], - subscriber_subscribe_ids[j], + .activate_downstream_subscription( + downstream_session_ids[j], + downstream_subscribe_ids[j], ) .await; let _ = pubsub_relation_manager - .activate_publisher_subscription( - publisher_session_ids[i], - publisher_subscribe_ids[i], + .activate_upstream_subscription( + upstream_session_ids[i], + upstream_subscribe_ids[i], ) .await; } @@ -1466,28 +1464,28 @@ mod success { // for pub 1 and sub 2 let _ = pubsub_relation_manager - .register_pubsup_relation( - publisher_session_ids[0], - publisher_subscribe_ids[0], - subscriber_session_ids[0], - subscriber_subscribe_ids[0], + .set_pubsub_relation( + upstream_session_ids[0], + upstream_subscribe_ids[0], + downstream_session_ids[0], + downstream_subscribe_ids[0], ) .await; let _ = pubsub_relation_manager - .activate_subscriber_subscription( - subscriber_session_ids[0], - subscriber_subscribe_ids[0], + .activate_downstream_subscription( + downstream_session_ids[0], + downstream_subscribe_ids[0], ) .await; let _ = pubsub_relation_manager - .activate_publisher_subscription(publisher_session_ids[0], publisher_subscribe_ids[0]) + .activate_upstream_subscription(upstream_session_ids[0], upstream_subscribe_ids[0]) .await; // Delete: pub 2, sub 2 // Remain: pub 1 <- sub 3, 4 let result = pubsub_relation_manager - .delete_client(subscriber_session_ids[0]) + .delete_client(downstream_session_ids[0]) .await; assert!(result.is_ok()); @@ -1495,35 +1493,35 @@ mod success { assert!(delete_occured); let (consumers, producers, pubsub_relation) = - test_utils::get_node_and_relation_clone(&pubsub_relation_manager).await; + test_helper_fn::get_node_and_relation_clone(&pubsub_relation_manager).await; // Assert that sub 2 is deleted // Remain: sub 3, 4 - let sub2 = producers.get(&subscriber_session_ids[0]); + let sub2 = producers.get(&downstream_session_ids[0]); assert!(sub2.is_none()); - let sub3 = producers.get(&subscriber_session_ids[1]); + let sub3 = producers.get(&downstream_session_ids[1]); assert!(sub3.is_some()); - let sub4 = producers.get(&subscriber_session_ids[2]); + let sub4 = producers.get(&downstream_session_ids[2]); assert!(sub4.is_some()); // Assert that pub 2 is deleted // Remain: pub 1 - let pub1 = consumers.get(&publisher_session_ids[1]); + let pub1 = consumers.get(&upstream_session_ids[1]); assert!(pub1.is_none()); - let pub2 = consumers.get(&publisher_session_ids[0]); + let pub2 = consumers.get(&upstream_session_ids[0]); assert!(pub2.is_some()); // Assert that the relation is deleted // Remain: pub 1 <- sub 3, 4 let pub1_relation = - pubsub_relation.get_subscribers(publisher_session_ids[0], publisher_subscribe_ids[0]); + pubsub_relation.get_subscribers(upstream_session_ids[0], upstream_subscribe_ids[0]); assert!(pub1_relation.is_some()); let pub2_relation = - pubsub_relation.get_subscribers(publisher_session_ids[1], publisher_subscribe_ids[1]); + pubsub_relation.get_subscribers(upstream_session_ids[1], upstream_subscribe_ids[1]); assert!(pub2_relation.is_none()); } @@ -1535,7 +1533,7 @@ mod success { let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let pubsub_relation_manager = PubSubRelationManagerWrapper::new(track_tx.clone()); let result = pubsub_relation_manager.delete_client(session_id).await; assert!(result.is_ok()); @@ -1548,8 +1546,8 @@ mod success { #[cfg(test)] mod failure { use crate::modules::pubsub_relation_manager::{ - commands::PubSubRelationCommand, interface::PubSubRelationManagerInterface, - manager::pubsub_relation_manager, + commands::PubSubRelationCommand, manager::pubsub_relation_manager, + wrapper::PubSubRelationManagerWrapper, }; use moqt_core::messages::control_messages::subscribe::{FilterType, GroupOrder}; use moqt_core::pubsub_relation_manager_repository::PubSubRelationManagerRepository; @@ -1558,46 +1556,46 @@ mod failure { #[tokio::test] async fn setup_publisher_already_exist() { let max_subscribe_id = 10; - let publisher_session_id = 1; + let upstream_session_id = 1; // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let pubsub_relation_manager = PubSubRelationManagerWrapper::new(track_tx.clone()); let _ = pubsub_relation_manager - .setup_publisher(max_subscribe_id, publisher_session_id) + .setup_publisher(max_subscribe_id, upstream_session_id) .await; // Register the same publisher let result = pubsub_relation_manager - .setup_publisher(max_subscribe_id, publisher_session_id) + .setup_publisher(max_subscribe_id, upstream_session_id) .await; assert!(result.is_err()); } #[tokio::test] - async fn set_publisher_announced_namespace_already_exist() { + async fn set_upstream_announced_namespace_already_exist() { let max_subscribe_id = 10; - let publisher_session_id = 1; + let upstream_session_id = 1; let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let pubsub_relation_manager = PubSubRelationManagerWrapper::new(track_tx.clone()); let _ = pubsub_relation_manager - .setup_publisher(max_subscribe_id, publisher_session_id) + .setup_publisher(max_subscribe_id, upstream_session_id) .await; let _ = pubsub_relation_manager - .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) + .set_upstream_announced_namespace(track_namespace.clone(), upstream_session_id) .await; // Register the same track namespace let result = pubsub_relation_manager - .set_publisher_announced_namespace(track_namespace.clone(), publisher_session_id) + .set_upstream_announced_namespace(track_namespace.clone(), upstream_session_id) .await; assert!(result.is_err()); @@ -1606,45 +1604,45 @@ mod failure { #[tokio::test] async fn setup_subscriber_already_exist() { let max_subscribe_id = 10; - let subscriber_session_id = 1; + let downstream_session_id = 1; // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let pubsub_relation_manager = PubSubRelationManagerWrapper::new(track_tx.clone()); let _ = pubsub_relation_manager - .setup_subscriber(max_subscribe_id, subscriber_session_id) + .setup_subscriber(max_subscribe_id, downstream_session_id) .await; // Register the same subscriber let result = pubsub_relation_manager - .setup_subscriber(max_subscribe_id, subscriber_session_id) + .setup_subscriber(max_subscribe_id, downstream_session_id) .await; assert!(result.is_err()); } #[tokio::test] - async fn is_valid_subscriber_subscribe_id_subscriber_not_found() { + async fn is_valid_downstream_subscribe_id_subscriber_not_found() { let max_subscribe_id = 10; - let subscriber_session_id = 1; - let subscriber_subscribe_id = 0; - let invalid_subscriber_session_id = 2; + let downstream_session_id = 1; + let downstream_subscribe_id = 0; + let invalid_downstream_session_id = 2; // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let pubsub_relation_manager = PubSubRelationManagerWrapper::new(track_tx.clone()); let _ = pubsub_relation_manager - .setup_subscriber(max_subscribe_id, subscriber_session_id) + .setup_subscriber(max_subscribe_id, downstream_session_id) .await; let result = pubsub_relation_manager - .is_valid_subscriber_subscribe_id( - subscriber_subscribe_id, - invalid_subscriber_session_id, + .is_valid_downstream_subscribe_id( + downstream_subscribe_id, + invalid_downstream_session_id, ) .await; @@ -1652,42 +1650,42 @@ mod failure { } #[tokio::test] - async fn is_valid_subscriber_track_alias_subscriber_not_found() { + async fn is_valid_downstream_track_alias_subscriber_not_found() { let max_subscribe_id = 10; - let subscriber_session_id = 1; - let subscriber_track_alias = 0; - let invalid_subscriber_session_id = 2; + let downstream_session_id = 1; + let downstream_track_alias = 0; + let invalid_downstream_session_id = 2; // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let pubsub_relation_manager = PubSubRelationManagerWrapper::new(track_tx.clone()); let _ = pubsub_relation_manager - .setup_subscriber(max_subscribe_id, subscriber_session_id) + .setup_subscriber(max_subscribe_id, downstream_session_id) .await; let result = pubsub_relation_manager - .is_valid_subscriber_track_alias(subscriber_track_alias, invalid_subscriber_session_id) + .is_valid_downstream_track_alias(downstream_track_alias, invalid_downstream_session_id) .await; assert!(result.is_err()); } #[tokio::test] - async fn get_requesting_subscriber_session_ids_and_subscribe_ids_publisher_not_found() { - let publisher_session_id = 0; - let publisher_subscribe_id = 0; + async fn get_requesting_downstream_session_ids_and_subscribe_ids_publisher_not_found() { + let upstream_session_id = 0; + let upstream_subscribe_id = 0; // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let pubsub_relation_manager = PubSubRelationManagerWrapper::new(track_tx.clone()); let result = pubsub_relation_manager - .get_requesting_subscriber_session_ids_and_subscribe_ids( - publisher_subscribe_id, - publisher_session_id, + .get_requesting_downstream_session_ids_and_subscribe_ids( + upstream_subscribe_id, + upstream_session_id, ) .await; @@ -1695,28 +1693,28 @@ mod failure { } #[tokio::test] - async fn get_publisher_subscribe_id_publisher_not_found() { + async fn get_upstream_subscribe_id_publisher_not_found() { let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); let track_name = "track_name".to_string(); - let invalid_publisher_session_id = 1; + let invalid_upstream_session_id = 1; // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let pubsub_relation_manager = PubSubRelationManagerWrapper::new(track_tx.clone()); let result = pubsub_relation_manager - .get_publisher_subscribe_id(track_namespace, track_name, invalid_publisher_session_id) + .get_upstream_subscribe_id(track_namespace, track_name, invalid_upstream_session_id) .await; assert!(result.is_err()); } #[tokio::test] - async fn set_subscriber_subscription_subscriber_not_found() { + async fn set_downstream_subscription_subscriber_not_found() { let max_subscribe_id = 10; - let subscriber_session_id = 1; + let downstream_session_id = 1; let subscribe_id = 0; let track_alias = 0; let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); @@ -1728,21 +1726,21 @@ mod failure { let start_object = Some(0); let end_group = None; let end_object = None; - let invalid_subscriber_session_id = 2; + let invalid_downstream_session_id = 2; // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let pubsub_relation_manager = PubSubRelationManagerWrapper::new(track_tx.clone()); let _ = pubsub_relation_manager - .setup_subscriber(max_subscribe_id, subscriber_session_id) + .setup_subscriber(max_subscribe_id, downstream_session_id) .await; let result = pubsub_relation_manager - .set_subscriber_subscription( - invalid_subscriber_session_id, + .set_downstream_subscription( + invalid_downstream_session_id, subscribe_id, track_alias, track_namespace, @@ -1761,9 +1759,9 @@ mod failure { } #[tokio::test] - async fn set_publisher_subscription_publisher_not_found() { + async fn set_upstream_subscription_publisher_not_found() { let max_subscribe_id = 10; - let publisher_session_id = 1; + let upstream_session_id = 1; let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); let track_name = "track_name".to_string(); let subscriber_priority = 0; @@ -1773,21 +1771,21 @@ mod failure { let start_object = Some(0); let end_group = None; let end_object = None; - let invalid_publisher_session_id = 2; + let invalid_upstream_session_id = 2; // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let pubsub_relation_manager = PubSubRelationManagerWrapper::new(track_tx.clone()); let _ = pubsub_relation_manager - .setup_publisher(max_subscribe_id, publisher_session_id) + .setup_publisher(max_subscribe_id, upstream_session_id) .await; let result = pubsub_relation_manager - .set_publisher_subscription( - invalid_publisher_session_id, + .set_upstream_subscription( + invalid_upstream_session_id, track_namespace, track_name, subscriber_priority, @@ -1804,69 +1802,69 @@ mod failure { } #[tokio::test] - async fn activate_subscriber_subscription_subscriber_not_found() { + async fn activate_downstream_subscription_subscriber_not_found() { let max_subscribe_id = 10; - let subscriber_session_id = 1; + let downstream_session_id = 1; let subscribe_id = 0; - let invalid_subscriber_session_id = 2; + let invalid_downstream_session_id = 2; // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let pubsub_relation_manager = PubSubRelationManagerWrapper::new(track_tx.clone()); let _ = pubsub_relation_manager - .setup_subscriber(max_subscribe_id, subscriber_session_id) + .setup_subscriber(max_subscribe_id, downstream_session_id) .await; let result = pubsub_relation_manager - .activate_subscriber_subscription(invalid_subscriber_session_id, subscribe_id) + .activate_downstream_subscription(invalid_downstream_session_id, subscribe_id) .await; assert!(result.is_err()); } #[tokio::test] - async fn activate_publisher_subscription_publisher_not_found() { + async fn activate_upstream_subscription_publisher_not_found() { let max_subscribe_id = 10; - let publisher_session_id = 1; - let publisher_subscribe_id = 0; - let invalid_publisher_session_id = 2; + let upstream_session_id = 1; + let upstream_subscribe_id = 0; + let invalid_upstream_session_id = 2; // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let pubsub_relation_manager = PubSubRelationManagerWrapper::new(track_tx.clone()); let _ = pubsub_relation_manager - .setup_publisher(max_subscribe_id, publisher_session_id) + .setup_publisher(max_subscribe_id, upstream_session_id) .await; let result = pubsub_relation_manager - .activate_publisher_subscription(invalid_publisher_session_id, publisher_subscribe_id) + .activate_upstream_subscription(invalid_upstream_session_id, upstream_subscribe_id) .await; assert!(result.is_err()); } #[tokio::test] - async fn delete_publisher_announced_namespace_publisher_not_found() { + async fn delete_upstream_announced_namespace_publisher_not_found() { let max_subscribe_id = 10; let track_namespace = Vec::from(["test".to_string(), "test".to_string()]); - let publisher_session_id = 1; - let invalid_publisher_session_id = 2; + let upstream_session_id = 1; + let invalid_upstream_session_id = 2; // Start track management thread let (track_tx, mut track_rx) = mpsc::channel::(1024); tokio::spawn(async move { pubsub_relation_manager(&mut track_rx).await }); - let pubsub_relation_manager = PubSubRelationManagerInterface::new(track_tx.clone()); + let pubsub_relation_manager = PubSubRelationManagerWrapper::new(track_tx.clone()); let _ = pubsub_relation_manager - .setup_publisher(max_subscribe_id, publisher_session_id) + .setup_publisher(max_subscribe_id, upstream_session_id) .await; let result = pubsub_relation_manager - .delete_publisher_announced_namespace(track_namespace, invalid_publisher_session_id) + .delete_upstream_announced_namespace(track_namespace, invalid_upstream_session_id) .await; assert!(result.is_err());