1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use crate::{
codec, PropertiesDecoder, Property,
ReasonCode::{self, ProtocolError},
Result as SageResult,
};
use std::{convert::TryInto, marker::Unpin};
use tokio::io::{AsyncRead, AsyncWrite, AsyncWriteExt};
#[derive(Debug, PartialEq, Clone)]
pub struct PubAck {
pub packet_identifier: u16,
pub reason_code: ReasonCode,
pub reason_string: Option<String>,
pub user_properties: Vec<(String, String)>,
}
impl Default for PubAck {
fn default() -> Self {
PubAck {
packet_identifier: 0,
reason_code: ReasonCode::Success,
reason_string: None,
user_properties: Default::default(),
}
}
}
impl PubAck {
pub(crate) async fn write<W: AsyncWrite + Unpin>(self, writer: &mut W) -> SageResult<usize> {
let mut n_bytes = codec::write_two_byte_integer(self.packet_identifier, writer).await?;
let mut properties = Vec::new();
if let Some(v) = self.reason_string {
n_bytes += Property::ReasonString(v).encode(&mut properties).await?;
}
for (k, v) in self.user_properties {
n_bytes += Property::UserProperty(k, v).encode(&mut properties).await?;
}
if n_bytes == 2 && self.reason_code != ReasonCode::Success {
Ok(2)
} else {
n_bytes += codec::write_reason_code(self.reason_code, writer).await?;
n_bytes += codec::write_variable_byte_integer(properties.len() as u32, writer).await?;
writer.write_all(&properties).await?;
Ok(n_bytes)
}
}
pub(crate) async fn read<R: AsyncRead + Unpin>(
reader: &mut R,
shortened: bool,
) -> SageResult<Self> {
let packet_identifier = codec::read_two_byte_integer(reader).await?;
let mut puback = PubAck {
packet_identifier,
..Default::default()
};
if shortened {
puback.reason_code = ReasonCode::Success;
} else {
puback.reason_code = codec::read_byte(reader).await?.try_into()?;
let mut properties = PropertiesDecoder::take(reader).await?;
while properties.has_properties() {
match properties.read().await? {
Property::ReasonString(v) => puback.reason_string = Some(v),
Property::UserProperty(k, v) => puback.user_properties.push((k, v)),
_ => return Err(ProtocolError.into()),
}
}
}
Ok(puback)
}
}
#[cfg(test)]
mod unit {
use super::*;
use std::io::Cursor;
fn encoded() -> Vec<u8> {
vec![
5, 57, 151, 29, 31, 0, 11, 66, 108, 97, 99, 107, 32, 66, 101, 116, 116, 121, 38, 0, 7,
77, 111, 103, 119, 97, 195, 175, 0, 3, 67, 97, 116,
]
}
fn decoded() -> PubAck {
PubAck {
packet_identifier: 1337,
reason_code: ReasonCode::QuotaExceeded,
reason_string: Some("Black Betty".into()),
user_properties: vec![("Mogwaï".into(), "Cat".into())],
}
}
#[tokio::test]
async fn encode() {
let test_data = decoded();
let mut tested_result = Vec::new();
let n_bytes = test_data.write(&mut tested_result).await.unwrap();
assert_eq!(tested_result, encoded());
assert_eq!(n_bytes, 33);
}
#[tokio::test]
async fn decode() {
let mut test_data = Cursor::new(encoded());
let tested_result = PubAck::read(&mut test_data, false).await.unwrap();
assert_eq!(tested_result, decoded());
}
}