1use serde::{Deserialize, Serialize};
2use std::fmt;
3use std::future::Future;
4
5#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
11pub struct PeerId(pub String);
12
13impl PeerId {
14 pub fn as_str(&self) -> &str {
15 &self.0
16 }
17}
18
19impl fmt::Display for PeerId {
20 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21 self.0.fmt(f)
22 }
23}
24
25impl AsRef<str> for PeerId {
26 fn as_ref(&self) -> &str {
27 &self.0
28 }
29}
30
31impl From<String> for PeerId {
32 fn from(s: String) -> Self {
33 PeerId(s)
34 }
35}
36
37impl From<&str> for PeerId {
38 fn from(s: &str) -> Self {
39 PeerId(s.to_string())
40 }
41}
42
43#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
45pub struct Multiaddr(pub String);
46
47impl Multiaddr {
48 pub fn as_str(&self) -> &str {
49 &self.0
50 }
51}
52
53impl fmt::Display for Multiaddr {
54 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55 self.0.fmt(f)
56 }
57}
58
59impl AsRef<str> for Multiaddr {
60 fn as_ref(&self) -> &str {
61 &self.0
62 }
63}
64
65impl From<String> for Multiaddr {
66 fn from(s: String) -> Self {
67 Multiaddr(s)
68 }
69}
70
71impl From<&str> for Multiaddr {
72 fn from(s: &str) -> Self {
73 Multiaddr(s.to_string())
74 }
75}
76
77#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
79pub struct RequestId(pub String);
80
81impl RequestId {
82 pub fn as_str(&self) -> &str {
83 &self.0
84 }
85}
86
87impl fmt::Display for RequestId {
88 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
89 self.0.fmt(f)
90 }
91}
92
93impl AsRef<str> for RequestId {
94 fn as_ref(&self) -> &str {
95 &self.0
96 }
97}
98
99impl From<String> for RequestId {
100 fn from(s: String) -> Self {
101 RequestId(s)
102 }
103}
104
105impl From<&str> for RequestId {
106 fn from(s: &str) -> Self {
107 RequestId(s.to_string())
108 }
109}
110
111#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
118pub struct RoomId(pub String);
119
120impl RoomId {
121 pub fn as_str(&self) -> &str {
122 &self.0
123 }
124}
125
126impl fmt::Display for RoomId {
127 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
128 self.0.fmt(f)
129 }
130}
131
132impl AsRef<str> for RoomId {
133 fn as_ref(&self) -> &str {
134 &self.0
135 }
136}
137
138impl From<String> for RoomId {
139 fn from(s: String) -> Self {
140 RoomId(s)
141 }
142}
143
144impl From<&str> for RoomId {
145 fn from(s: &str) -> Self {
146 RoomId(s.to_string())
147 }
148}
149
150#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
157pub struct HelloMessage {
158 pub peer_id: String,
159 pub circuit_address: Option<String>,
160 pub web_rtc_address: Option<String>,
161}
162
163pub mod handlers;
164
165#[cfg(test)]
166pub mod mock;
167
168pub type NetworkResult<T> = crate::error::ChatResult<T>;
170
171pub trait NetworkNode {
173 fn local_id(&self) -> PeerId;
175
176 fn dial(&mut self, addr: &Multiaddr) -> NetworkResult<()>;
178
179 fn join_room(&mut self, room: &RoomId) -> NetworkResult<()>;
181
182 fn publish_message(&mut self, data: Vec<u8>) -> NetworkResult<()>;
185
186 fn send_message(&mut self, peer_id: &PeerId, data: Vec<u8>) -> NetworkResult<RequestId>;
188
189 fn send_response(&mut self, request_id: &RequestId, data: Vec<u8>) -> NetworkResult<()>;
191
192 fn dial_addrs(&self) -> Vec<Multiaddr>;
194
195 fn bootstrap(&mut self) -> NetworkResult<()>;
197
198 fn raw_stats(&self) -> crate::stats::RawStats;
200
201 fn connected_peers(&self) -> Vec<PeerId>;
203
204 fn is_dialing(&self, peer_id: &PeerId) -> bool;
206
207 fn next_event(&mut self) -> impl Future<Output = Option<NetworkEvent>>;
209}
210
211#[derive(Debug, Clone)]
213pub enum NetworkEvent {
214 PeerConnected { peer_id: PeerId },
215 PeerDisconnected { peer_id: PeerId },
216 MessageReceived { peer_id: PeerId, request_id: Option<RequestId>, data: Vec<u8> },
220 BroadcastReceived { peer_id: PeerId, data: Vec<u8> },
222 NewListenAddr { addr: Multiaddr },
223 ExternalAddrConfirmed { addr: Multiaddr },
224 RoomPeerJoined { peer_id: PeerId },
226 RoomPeerLeft { peer_id: PeerId },
228 StatsUpdated { total_peers: usize },
229}