sheave_core/messages/
peer_bandwidth.rs

1mod limit_type;
2
3use std::{
4    cmp::Ordering,
5    io::Result as IOResult,
6    ops::Div
7};
8use crate::{
9    ByteBuffer,
10    Decoder,
11    Encoder,
12    messages::{
13        Channel,
14        ChunkData,
15        headers::MessageType
16    }
17};
18pub use self::limit_type::*;
19
20/// The message to tell the client-side bandwidth.
21///
22/// This has 2 ways of comparision by which field you specifies.
23///
24/// # Examples
25///
26/// ```rust
27/// use sheave_core::messages::{
28///     LimitType,
29///     PeerBandwidth,
30/// };
31///
32/// let peer_bandwidth = PeerBandwidth::default();
33///
34/// // When you compare this message with a bandwidth number.
35/// assert!(2500000u32 == peer_bandwidth);
36/// assert!(0 <= peer_bandwidth);
37/// // When you compare this message with a limit type.
38/// assert!(LimitType::default() == peer_bandwidth)
39/// ```
40#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
41pub struct PeerBandwidth(u32, LimitType);
42
43impl PeerBandwidth {
44    const DEFAULT: u32 = 2500000;
45
46    /// Constructs a PeerBandwidth message.
47    pub fn new(peer_bandwidth: u32, limit_type: LimitType) -> Self {
48        Self(peer_bandwidth, limit_type)
49    }
50
51    pub fn get_inner_bandwidth(&self) -> u32 {
52        self.0
53    }
54
55    pub fn get_inner_limit_type(&self) -> LimitType {
56        self.1
57    }
58}
59
60impl Default for PeerBandwidth {
61    /// Constructs a PeerBandwidth message with the default bandwidth and the default limit type (2500000 in bits, 2 (Dynamic)).
62    ///
63    /// # Examples
64    ///
65    /// ```rust
66    /// use sheave_core::messages::{
67    ///     LimitType,
68    ///     PeerBandwidth
69    /// };
70    ///
71    /// let peer_bandwidth = PeerBandwidth::default();
72    /// assert_eq!(2500000u32, peer_bandwidth);
73    /// assert_eq!(LimitType::default(), peer_bandwidth)
74    /// ```
75    fn default() -> Self {
76        Self(Self::DEFAULT, LimitType::default())
77    }
78}
79
80impl PartialEq<u32> for PeerBandwidth {
81    fn eq(&self, other: &u32) -> bool {
82        self.0.eq(other)
83    }
84}
85
86impl PartialEq<PeerBandwidth> for u32 {
87    fn eq(&self, other: &PeerBandwidth) -> bool {
88        self.eq(&other.0)
89    }
90}
91
92impl PartialOrd<u32> for PeerBandwidth {
93    fn partial_cmp(&self, other: &u32) -> Option<Ordering> {
94        self.0.partial_cmp(other)
95    }
96}
97
98impl PartialOrd<PeerBandwidth> for u32 {
99    fn partial_cmp(&self, other: &PeerBandwidth) -> Option<Ordering> {
100        self.partial_cmp(&other.0)
101    }
102}
103
104impl PartialEq<LimitType> for PeerBandwidth {
105    fn eq(&self, other: &LimitType) -> bool {
106        self.1.eq(other)
107    }
108}
109
110impl PartialEq<PeerBandwidth> for LimitType {
111    fn eq(&self, other: &PeerBandwidth) -> bool {
112        self.eq(&other.1)
113    }
114}
115
116impl Div<u32> for PeerBandwidth {
117    type Output = Self;
118
119    fn div(self, rhs: u32) -> Self::Output {
120        Self(self.0 / rhs, self.1)
121    }
122}
123
124impl ChunkData for PeerBandwidth {
125    const CHANNEL: Channel = Channel::Network;
126    const MESSAGE_TYPE: MessageType = MessageType::PeerBandwidth;
127}
128
129impl Decoder<PeerBandwidth> for ByteBuffer {
130    /// Decodes bytes into a PeerBandwidth message.
131    ///
132    /// # Errors
133    ///
134    /// * [`InsufficientBufferLength`]
135    ///
136    /// When this buffer didn't remain at least 5 bytes.
137    ///
138    /// # Examples
139    ///
140    /// ```rust
141    /// use sheave_core::{
142    ///     ByteBuffer,
143    ///     Decoder,
144    ///     messages::PeerBandwidth
145    /// };
146    ///
147    /// let mut buffer = ByteBuffer::default();
148    /// buffer.put_u32_be(u32::default());
149    /// buffer.put_u8(u8::default());
150    /// assert!(Decoder::<PeerBandwidth>::decode(&mut buffer).is_ok())
151    /// ```
152    ///
153    /// [`InsufficientBufferLength`]: crate::byte_buffer::InsufficientBufferLength
154    fn decode(&mut self) -> IOResult<PeerBandwidth> {
155        let bandwidth = self.get_u32_be()?;
156        let limit_type = self.get_u8()?;
157        Ok(PeerBandwidth(bandwidth, limit_type.into()))
158    }
159}
160
161impl Encoder<PeerBandwidth> for ByteBuffer {
162    /// Encodes a PeerBandwidth message into bytes.
163    fn encode(&mut self, peer_bandwidth: &PeerBandwidth) {
164        self.put_u32_be(peer_bandwidth.0);
165        self.put_u8(peer_bandwidth.1.into());
166    }
167}
168
169#[cfg(test)]
170mod tests {
171    use super::*;
172
173    #[test]
174    fn decode_peer_bandwidth() {
175        let mut buffer = ByteBuffer::default();
176        buffer.put_u32_be(2500000);
177        buffer.put_u8(2);
178        let result: IOResult<PeerBandwidth> = buffer.decode();
179        assert!(result.is_ok());
180        let actual = result.unwrap();
181        let expected = PeerBandwidth::default();
182        assert_eq!(expected, actual)
183    }
184
185    #[test]
186    fn encode_peer_bandwidth() {
187        let mut buffer = ByteBuffer::default();
188        let expected_bandwidth = 2500000u32;
189        let expected_limit_type = 2u8;
190        buffer.encode(&PeerBandwidth::default());
191        let actual_bandwidth = buffer.get_u32_be().unwrap();
192        assert_eq!(expected_bandwidth, actual_bandwidth);
193        let actual_limit_type = buffer.get_u8().unwrap();
194        assert_eq!(expected_limit_type, actual_limit_type)
195    }
196}