Skip to content

Commit 199dca1

Browse files
authored
Merge pull request #55 from smaeul/up/fix-topic-alias
Fix auto_map_topic_alias_send logic
2 parents 5350491 + 2328d15 commit 199dca1

File tree

3 files changed

+46
-7
lines changed

3 files changed

+46
-7
lines changed

src/mqtt/connection/core.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1630,7 +1630,7 @@ where
16301630
} else {
16311631
let lru_ta = topic_alias_send.get_lru_alias();
16321632
topic_alias_send.insert_or_update(packet.topic_name(), lru_ta);
1633-
packet = packet.remove_topic_add_topic_alias(lru_ta);
1633+
packet = packet.add_topic_alias(lru_ta);
16341634
}
16351635
}
16361636
} else if self.auto_replace_topic_alias_send {

src/mqtt/packet/v5_0/publish.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,45 @@ where
688688
// Set topic name to empty string
689689
self.topic_name_buf = MqttString::new("").unwrap();
690690

691+
self.add_topic_alias(topic_alias)
692+
}
693+
694+
/// Add TopicAlias property
695+
///
696+
/// This method adds a TopicAlias property to the packet properties. This
697+
/// is required to associate a topic alias with a topic name on the server.
698+
/// Future messages can use only the topic alias and omit the topic name.
699+
/// This is useful for reducing packet size when sending multiple messages
700+
/// to the same topic.
701+
///
702+
/// The method removes any existing TopicAlias property before adding the new one
703+
/// to ensure only one TopicAlias property exists. It automatically recalculates
704+
/// the property_length and remaining_length to reflect these changes.
705+
///
706+
/// # Parameters
707+
///
708+
/// - `topic_alias`: The numeric topic alias to associate with the topic name
709+
///
710+
/// # Returns
711+
///
712+
/// The modified `GenericPublish` instance with the TopicAlias property
713+
///
714+
/// # Examples
715+
///
716+
/// ```ignore
717+
/// use mqtt_protocol_core::mqtt;
718+
///
719+
/// let publish_with_topic = mqtt::packet::v5_0::Publish::builder()
720+
/// .topic_name("sensors/temperature/room1")
721+
/// .unwrap()
722+
/// .payload(b"23.5")
723+
/// .build()
724+
/// .unwrap();
725+
///
726+
/// let publish_with_alias = publish_with_topic.add_topic_alias(42);
727+
/// // props() now contains TopicAlias property with value 42
728+
/// ```
729+
pub fn add_topic_alias(mut self, topic_alias: TopicAliasType) -> Self {
691730
// Add TopicAlias property to the end of properties
692731
let topic_alias_property =
693732
Property::TopicAlias(crate::mqtt::packet::TopicAlias::new(topic_alias).unwrap());

tests/connection-core-topic-alias.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ fn auto_map_topic_alias_send() {
7171
..
7272
} = event
7373
{
74-
// Verify topic name is empty and topic alias is 1
75-
if p.topic_name().is_empty() {
74+
// Verify topic name is present and topic alias is 1
75+
if p.topic_name() == "topic/a" {
7676
// Check for TopicAlias property with value 1
7777
for prop in p.props().iter() {
7878
if let mqtt::packet::Property::TopicAlias(ta) = prop {
@@ -87,7 +87,7 @@ fn auto_map_topic_alias_send() {
8787
}
8888
assert!(
8989
publish_a_mapped,
90-
"PUBLISH A should have empty topic name and TopicAlias=1"
90+
"PUBLISH A should have valid topic name and TopicAlias=1"
9191
);
9292

9393
// Send QoS0 PUBLISH B
@@ -109,8 +109,8 @@ fn auto_map_topic_alias_send() {
109109
..
110110
} = event
111111
{
112-
// Verify topic name is empty and topic alias is 2
113-
if p.topic_name().is_empty() {
112+
// Verify topic name is present and topic alias is 2
113+
if p.topic_name() == "topic/b" {
114114
// Check for TopicAlias property with value 2
115115
for prop in p.props().iter() {
116116
if let mqtt::packet::Property::TopicAlias(ta) = prop {
@@ -125,7 +125,7 @@ fn auto_map_topic_alias_send() {
125125
}
126126
assert!(
127127
publish_b_mapped,
128-
"PUBLISH B should have empty topic name and TopicAlias=2"
128+
"PUBLISH B should have valid topic name and TopicAlias=2"
129129
);
130130

131131
{

0 commit comments

Comments
 (0)