1use std::cmp::Ordering;
2use std::hash::{Hash, Hasher};
3use std::net::{AddrParseError, IpAddr, SocketAddr, SocketAddrV4, SocketAddrV6};
4use std::str::FromStr;
5
6#[derive(Clone, Debug)]
7pub enum Address {
8 Ori(String),
9 Addr(SocketAddr),
10}
11
12impl Address {
13 pub fn hash_code(&self) -> u64 {
14 match self {
15 Address::Ori(ori) => crate::utils::bytes_to_hash_code(ori.as_bytes()),
16 Address::Addr(addr) => crate::utils::socketaddr_to_hash_code(addr),
17 }
18 }
19}
20
21impl From<String> for Address {
22 fn from(ori: String) -> Self {
23 Address::Ori(ori)
24 }
25}
26
27impl From<&str> for Address {
28 fn from(ori: &str) -> Self {
29 Address::Ori(ori.to_string())
30 }
31}
32
33impl From<SocketAddr> for Address {
34 fn from(addr: SocketAddr) -> Self {
35 Address::Addr(addr)
36 }
37}
38
39impl<I: Into<IpAddr>> From<(I, u16)> for Address {
40 fn from(value: (I, u16)) -> Self {
41 Address::from(SocketAddr::from(value))
42 }
43}
44
45impl From<SocketAddrV4> for Address {
46 fn from(value: SocketAddrV4) -> Self {
47 Address::from(SocketAddr::from(value))
48 }
49}
50
51impl From<SocketAddrV6> for Address {
52 fn from(value: SocketAddrV6) -> Self {
53 Address::from(SocketAddr::from(value))
54 }
55}
56
57impl FromStr for Address {
58 type Err = AddrParseError;
59
60 fn from_str(s: &str) -> Result<Self, Self::Err> {
61 let a = SocketAddr::from_str(s)?;
62 Ok(Address::from(a))
63 }
64}
65
66#[derive(Clone, Debug)]
67pub struct BackendState {
68 hash_code: u64,
69 address: Address,
70}
71
72impl BackendState {
73 pub fn new(addr: Address) -> BackendState {
74 let hash_code = addr.hash_code();
75 BackendState {
76 hash_code,
77 address: addr,
78 }
79 }
80
81 pub fn hash_code(&self) -> u64 {
82 self.hash_code
83 }
84
85 pub fn get_address(&self) -> &Address {
86 &self.address
87 }
88}
89
90impl Hash for BackendState {
91 fn hash<H: Hasher>(&self, state: &mut H) {
92 state.write_u64(self.hash_code)
93 }
94}
95
96impl Eq for BackendState {}
97
98impl PartialEq for BackendState {
99 fn eq(&self, other: &Self) -> bool {
100 self.hash_code.eq(&other.hash_code)
101 }
102}
103
104impl PartialOrd for BackendState {
105 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
106 self.hash_code.partial_cmp(&other.hash_code)
107 }
108}
109
110impl Ord for BackendState {
111 fn cmp(&self, other: &Self) -> Ordering {
112 self.hash_code.cmp(&other.hash_code)
113 }
114}
115
116impl From<Address> for BackendState {
117 fn from(addr: Address) -> Self {
118 BackendState::new(addr)
119 }
120}
121
122#[derive(Default)]
123pub(crate) struct BackendStates(Vec<BackendState>);
124
125impl BackendStates {
126 pub fn new() -> Self {
127 BackendStates(Vec::new())
128 }
129
130 pub fn add_backend(&mut self, addr: Address) {
131 let hash_code = addr.hash_code();
132 if self.0.iter().any(|bs| bs.hash_code == hash_code) {
133 return;
134 }
135
136 self.0.push(BackendState::new(addr));
137 self.sort_backends();
138 }
139
140 pub fn remove_backend(&mut self, addr: &Address) -> bool {
141 let c = self.0.len();
142 let hash_code = addr.hash_code();
143 self.0.retain(|bs| bs.hash_code != hash_code);
144 self.0.len() != c
145 }
146
147 pub fn get_backend(&self, addr: &Address) -> Option<&BackendState> {
148 let hash_code = addr.hash_code();
149 self.0.iter().find(|bs| bs.hash_code == hash_code)
150 }
151
152 #[allow(dead_code)]
153 pub fn get_backend_mut(&mut self, addr: &Address) -> Option<&mut BackendState> {
154 let hash_code = addr.hash_code();
155 self.0.iter_mut().find(|bs| bs.hash_code == hash_code)
156 }
157
158 pub fn get_backends(&self) -> &[BackendState] {
159 self.0.as_slice()
160 }
161
162 #[allow(dead_code)]
163 pub fn get_backends_mut(&mut self) -> &mut [BackendState] {
164 self.0.as_mut_slice()
165 }
166
167 fn sort_backends(&mut self) {
168 self.0.sort_by(|bs1, bs2| bs1.hash_code.cmp(&bs2.hash_code));
169 }
170}