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 id: Option<u32>,
69 address: Address,
70 hash_code: u64,
71}
72
73impl BackendState {
74 pub fn new(id: Option<u32>, addr: Address) -> BackendState {
75 let hash_code = addr.hash_code();
76 BackendState {
77 id,
78 address: addr,
79 hash_code,
80 }
81 }
82
83 pub fn id(&self) -> Option<u32> {
84 self.id.clone()
85 }
86
87 pub fn hash_code(&self) -> u64 {
88 self.hash_code
89 }
90
91 pub fn get_address(&self) -> &Address {
92 &self.address
93 }
94}
95
96impl Hash for BackendState {
97 fn hash<H: Hasher>(&self, state: &mut H) {
98 state.write_u64(self.hash_code)
99 }
100}
101
102impl Eq for BackendState {}
103
104impl PartialEq for BackendState {
105 fn eq(&self, other: &Self) -> bool {
106 self.hash_code.eq(&other.hash_code)
107 }
108}
109
110impl PartialOrd for BackendState {
111 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
112 self.hash_code.partial_cmp(&other.hash_code)
113 }
114}
115
116impl Ord for BackendState {
117 fn cmp(&self, other: &Self) -> Ordering {
118 self.hash_code.cmp(&other.hash_code)
119 }
120}
121
122impl From<Address> for BackendState {
123 fn from(addr: Address) -> Self {
124 BackendState::new(None, addr)
125 }
126}
127