sheave_core/messages/
peer_bandwidth.rs1mod 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#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
41pub struct PeerBandwidth(u32, LimitType);
42
43impl PeerBandwidth {
44 const DEFAULT: u32 = 2500000;
45
46 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 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 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 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}