pub struct TopicRouter<T> { /* private fields */ }Expand description
Routes MQTT topics to subscription payloads.
Each subscription pattern is associated with a payload T and a unique
SubscriptionId. Delivered topics are matched against all stored patterns
(including +/# wildcards); the router also bookkeeps which distinct
broker subscriptions are active and at what QoS.
Implementations§
Source§impl<T> TopicRouter<T>
impl<T> TopicRouter<T>
Sourcepub fn add_subscription(
&mut self,
topic: TopicPatternPath,
qos: impl Into<QoS>,
subscription: T,
) -> (bool, SubscriptionId)
pub fn add_subscription( &mut self, topic: TopicPatternPath, qos: impl Into<QoS>, subscription: T, ) -> (bool, SubscriptionId)
Registers a subscription for topic with the given qos and payload.
Returns (needs_subscribe, id): needs_subscribe is true when this is
the first subscription for the pattern or it raises the effective QoS, so
the caller must (re)subscribe on the broker; id identifies the new
subscription for later unsubscribe.
Sourcepub fn unsubscribe(
&mut self,
id: &SubscriptionId,
) -> Result<(bool, TopicPatternPath), TopicRouterError>
pub fn unsubscribe( &mut self, id: &SubscriptionId, ) -> Result<(bool, TopicPatternPath), TopicRouterError>
Removes the subscription identified by id.
Returns (topic_now_empty, pattern): topic_now_empty is true when no
other subscription remains for the pattern, so the caller should
unsubscribe it on the broker. Fails with
TopicRouterError::SubscriptionNotFound if id is unknown.
Sourcepub fn get_subscribers<'a>(
&'a self,
topic: &TopicPath,
) -> Vec<(&'a SubscriptionId, &'a (TopicPatternPath, QoS), &'a T)>
pub fn get_subscribers<'a>( &'a self, topic: &TopicPath, ) -> Vec<(&'a SubscriptionId, &'a (TopicPatternPath, QoS), &'a T)>
Returns every subscription whose pattern matches topic.
Each entry is (id, (pattern, qos), payload) for a matching subscription
(one delivered topic may match several patterns via +/# wildcards).
Sourcepub fn get_active_subscriptions(
&self,
) -> impl Iterator<Item = &(TopicPatternPath, QoS)>
pub fn get_active_subscriptions( &self, ) -> impl Iterator<Item = &(TopicPatternPath, QoS)>
Iterates over every active subscription as (pattern, qos).
Patterns are not deduplicated; multiple subscriptions may share one.
Sourcepub fn get_topics_for_unsubscribe(&self) -> HashSet<ArcStr>
pub fn get_topics_for_unsubscribe(&self) -> HashSet<ArcStr>
Get all unique active topic patterns
Sourcepub fn get_topics_for_resubscribe(&self) -> HashMap<ArcStr, QoS>
pub fn get_topics_for_resubscribe(&self) -> HashMap<ArcStr, QoS>
Get all active topic patterns with their maximum QoS Returns unique topics (grouped by pattern) with the highest QoS among all subscribers
Sourcepub fn cleanup(&mut self)
pub fn cleanup(&mut self)
Cleanup all internal data structures and close subscriber channels This method is called during shutdown to ensure proper resource cleanup
Sourcepub fn get_topic_by_id(
&self,
id: &SubscriptionId,
) -> Result<&(TopicPatternPath, QoS), TopicRouterError>
pub fn get_topic_by_id( &self, id: &SubscriptionId, ) -> Result<&(TopicPatternPath, QoS), TopicRouterError>
Looks up the (pattern, qos) registered for a subscription id.
Fails with TopicRouterError::SubscriptionNotFound if id is unknown.