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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
use std::io::{self, Read, Write}; use std::string::FromUtf8Error; use std::error::Error; use std::fmt; use std::convert::From; use byteorder::{self, WriteBytesExt, ReadBytesExt}; use control::{FixedHeader, PacketType, ControlType}; use control::variable_header::PacketIdentifier; use packet::{Packet, PacketError}; use {Encodable, Decodable, QualityOfService}; use encodable::StringEncodeError; use topic_filter::{TopicFilter, TopicFilterError}; #[derive(Debug, Eq, PartialEq)] pub struct SubscribePacket { fixed_header: FixedHeader, packet_identifier: PacketIdentifier, payload: SubscribePacketPayload, } impl SubscribePacket { pub fn new(pkid: u16, subscribes: Vec<(TopicFilter, QualityOfService)>) -> SubscribePacket { let mut pk = SubscribePacket { fixed_header: FixedHeader::new(PacketType::with_default(ControlType::Subscribe), 0), packet_identifier: PacketIdentifier(pkid), payload: SubscribePacketPayload::new(subscribes), }; pk.fixed_header.remaining_length = pk.encoded_variable_headers_length() + pk.payload.encoded_length(); pk } pub fn packet_identifier(&self) -> u16 { self.packet_identifier.0 } pub fn set_packet_identifier(&mut self, pkid: u16) { self.packet_identifier.0 = pkid; } } impl<'a> Packet<'a> for SubscribePacket { type Payload = SubscribePacketPayload; 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.packet_identifier.encode(writer)); Ok(()) } fn encoded_variable_headers_length(&self) -> u32 { self.packet_identifier.encoded_length() } fn decode_packet<R: Read>(reader: &mut R, fixed_header: FixedHeader) -> Result<Self, PacketError<'a, Self>> { let packet_identifier: PacketIdentifier = try!(PacketIdentifier::decode(reader)); let payload: SubscribePacketPayload = try!(SubscribePacketPayload::decode_with(reader, Some(fixed_header.remaining_length - packet_identifier.encoded_length())) .map_err(PacketError::PayloadError)); Ok(SubscribePacket { fixed_header: fixed_header, packet_identifier: packet_identifier, payload: payload, }) } } #[derive(Debug, Eq, PartialEq)] pub struct SubscribePacketPayload { subscribes: Vec<(TopicFilter, QualityOfService)>, } impl SubscribePacketPayload { pub fn new(subs: Vec<(TopicFilter, QualityOfService)>) -> SubscribePacketPayload { SubscribePacketPayload { subscribes: subs, } } pub fn subscribes(&self) -> &[(TopicFilter, QualityOfService)] { &self.subscribes[..] } } impl<'a> Encodable<'a> for SubscribePacketPayload { type Err = SubscribePacketPayloadError; fn encode<W: Write>(&self, writer: &mut W) -> Result<(), Self::Err> { for &(ref filter, ref qos) in self.subscribes.iter() { try!(filter.encode(writer)); try!(writer.write_u8(*qos as u8)); } Ok(()) } fn encoded_length(&self) -> u32 { self.subscribes.iter() .fold(0, |b, a| b + a.0.encoded_length() + 1) } } impl<'a> Decodable<'a> for SubscribePacketPayload { type Err = SubscribePacketPayloadError; type Cond = u32; fn decode_with<R: Read>(reader: &mut R, payload_len: Option<u32>) -> Result<SubscribePacketPayload, SubscribePacketPayloadError> { let mut payload_len = payload_len.expect("Must provide payload length"); let mut subs = Vec::new(); while payload_len > 0 { let filter = try!(TopicFilter::decode(reader)); let qos = match try!(reader.read_u8()) { 0 => QualityOfService::Level0, 1 => QualityOfService::Level1, 2 => QualityOfService::Level2, _ => return Err(SubscribePacketPayloadError::InvalidQualityOfService), }; payload_len -= filter.encoded_length() + 1; subs.push((filter, qos)); } Ok(SubscribePacketPayload::new(subs)) } } #[derive(Debug)] pub enum SubscribePacketPayloadError { IoError(io::Error), FromUtf8Error(FromUtf8Error), StringEncodeError(StringEncodeError), InvalidQualityOfService, TopicFilterError(TopicFilterError), } impl fmt::Display for SubscribePacketPayloadError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { &SubscribePacketPayloadError::IoError(ref err) => err.fmt(f), &SubscribePacketPayloadError::FromUtf8Error(ref err) => err.fmt(f), &SubscribePacketPayloadError::StringEncodeError(ref err) => err.fmt(f), &SubscribePacketPayloadError::InvalidQualityOfService => write!(f, "Invalid quality of service"), &SubscribePacketPayloadError::TopicFilterError(ref err) => err.fmt(f), } } } impl Error for SubscribePacketPayloadError { fn description(&self) -> &str { match self { &SubscribePacketPayloadError::IoError(ref err) => err.description(), &SubscribePacketPayloadError::FromUtf8Error(ref err) => err.description(), &SubscribePacketPayloadError::StringEncodeError(ref err) => err.description(), &SubscribePacketPayloadError::InvalidQualityOfService => "Invalid quality of service", &SubscribePacketPayloadError::TopicFilterError(ref err) => err.description(), } } fn cause(&self) -> Option<&Error> { match self { &SubscribePacketPayloadError::IoError(ref err) => Some(err), &SubscribePacketPayloadError::FromUtf8Error(ref err) => Some(err), &SubscribePacketPayloadError::StringEncodeError(ref err) => Some(err), &SubscribePacketPayloadError::InvalidQualityOfService => None, &SubscribePacketPayloadError::TopicFilterError(ref err) => Some(err), } } } impl From<TopicFilterError> for SubscribePacketPayloadError { fn from(err: TopicFilterError) -> SubscribePacketPayloadError { SubscribePacketPayloadError::TopicFilterError(err) } } impl From<StringEncodeError> for SubscribePacketPayloadError { fn from(err: StringEncodeError) -> SubscribePacketPayloadError { SubscribePacketPayloadError::StringEncodeError(err) } } impl From<byteorder::Error> for SubscribePacketPayloadError { fn from(err: byteorder::Error) -> SubscribePacketPayloadError { SubscribePacketPayloadError::IoError(From::from(err)) } }