1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
use std::io::{Read, Write}; use control::{FixedHeader, PacketType, ControlType}; use control::variable_header::PacketIdentifier; use packet::{Packet, PacketError}; use topic_name::TopicName; use {Encodable, Decodable}; #[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Copy, Clone)] pub enum QoSWithPacketIdentifier { Level0, Level1(u16), Level2(u16), } #[derive(Debug, Eq, PartialEq)] pub struct PublishPacket { fixed_header: FixedHeader, topic_name: TopicName, packet_identifier: Option<PacketIdentifier>, payload: Vec<u8>, } impl PublishPacket { pub fn new(topic_name: TopicName, qos: QoSWithPacketIdentifier, payload: Vec<u8>) -> PublishPacket { let (qos, pkid) = match qos { QoSWithPacketIdentifier::Level0 => (0, None), QoSWithPacketIdentifier::Level1(pkid) => (1, Some(PacketIdentifier(pkid))), QoSWithPacketIdentifier::Level2(pkid) => (2, Some(PacketIdentifier(pkid))), }; let mut pk = PublishPacket { fixed_header: FixedHeader::new(PacketType::with_default(ControlType::Publish), 0), topic_name: topic_name, packet_identifier: pkid, payload: payload, }; pk.fixed_header.packet_type.flags |= qos << 1; pk.fixed_header.remaining_length = pk.calculate_remaining_length(); pk } #[inline] fn calculate_remaining_length(&self) -> u32 { self.encoded_variable_headers_length() + self.payload().encoded_length() } pub fn set_dup(&mut self, dup: bool) { self.fixed_header.packet_type.flags |= (dup as u8) << 3; } pub fn dup(&self) -> bool { self.fixed_header.packet_type.flags & 0x80 != 0 } pub fn set_qos(&mut self, qos: QoSWithPacketIdentifier) { let (qos, pkid) = match qos { QoSWithPacketIdentifier::Level0 => (0, None), QoSWithPacketIdentifier::Level1(pkid) => (1, Some(PacketIdentifier(pkid))), QoSWithPacketIdentifier::Level2(pkid) => (2, Some(PacketIdentifier(pkid))), }; self.fixed_header.packet_type.flags |= qos << 1; self.packet_identifier = pkid; } pub fn qos(&self) -> QoSWithPacketIdentifier { match self.packet_identifier { None => QoSWithPacketIdentifier::Level0, Some(pkid) => { let qos_val = (self.fixed_header.packet_type.flags & 0x06) >> 1; match qos_val { 1 => QoSWithPacketIdentifier::Level1(pkid.0), 2 => QoSWithPacketIdentifier::Level2(pkid.0), _ => unreachable!(), } } } } pub fn set_retain(&mut self, ret: bool) { self.fixed_header.packet_type.flags |= ret as u8; } pub fn retain(&self) -> bool { self.fixed_header.packet_type.flags & 0x01 != 0 } pub fn set_topic_name(&mut self, topic_name: TopicName) { self.topic_name = topic_name; self.fixed_header.remaining_length = self.calculate_remaining_length(); } pub fn topic_name(&self) -> &str { &self.topic_name[..] } } impl<'a> Packet<'a> for PublishPacket { type Payload = Vec<u8>; fn fixed_header(&self) -> &FixedHeader { &self.fixed_header } fn payload(&self) -> &Self::Payload { &self.payload } fn encode_variable_headers<W: Write>(&self, writer: &mut W) -> Result<(), PacketError<'a, Self>> { try!(self.topic_name.encode(writer)); if let Some(pkid) = self.packet_identifier.as_ref() { try!(pkid.encode(writer)); } Ok(()) } fn encoded_variable_headers_length(&self) -> u32 { self.topic_name.encoded_length() + self.packet_identifier.as_ref().map(|x| x.encoded_length()).unwrap_or(0) } fn decode_packet<R: Read>(reader: &mut R, fixed_header: FixedHeader) -> Result<Self, PacketError<'a, Self>> { let topic_name: TopicName = try!(TopicName::decode(reader)); let packet_identifier = if fixed_header.packet_type.flags & 0x06 != 0 { Some(try!(PacketIdentifier::decode(reader))) } else { None }; let vhead_len = topic_name.encoded_length() + packet_identifier.as_ref().map(|x| x.encoded_length()).unwrap_or(0); let payload_len = fixed_header.remaining_length - vhead_len; let payload: Vec<u8> = try!(Decodable::decode_with(reader, Some(payload_len))); Ok(PublishPacket { fixed_header: fixed_header, topic_name: topic_name, packet_identifier: packet_identifier, payload: payload, }) } } #[cfg(test)] mod test { use super::*; use std::io::Cursor; use topic_name::TopicName; use {Encodable, Decodable}; #[test] fn test_publish_packet_basic() { let packet = PublishPacket::new(TopicName::new("a/b".to_owned()).unwrap(), QoSWithPacketIdentifier::Level2(10), b"Hello world!".to_vec()); let mut buf = Vec::new(); packet.encode(&mut buf).unwrap(); let mut decode_buf = Cursor::new(buf); let decoded = PublishPacket::decode(&mut decode_buf).unwrap(); assert_eq!(packet, decoded); } }