1pub mod ant_quic_adapter;
22
23pub mod dht_handler;
25
26use crate::validation::{Validate, ValidationContext, validate_message_size, validate_peer_id};
27use crate::{P2PError, PeerId, Result};
28use serde::{Deserialize, Serialize};
29use std::collections::HashMap;
30use std::fmt;
31use std::time::{Duration, Instant};
32
33#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
35pub enum TransportType {
36 QUIC,
38}
39
40#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
42pub enum TransportSelection {
43 #[default]
45 QUIC,
46}
47
48#[derive(Debug, Clone)]
50pub struct ConnectionQuality {
51 pub latency: Duration,
53 pub throughput_mbps: f64,
55 pub packet_loss: f64,
57 pub jitter: Duration,
59 pub connect_time: Duration,
61}
62
63#[derive(Debug, Clone)]
65pub struct ConnectionInfo {
66 pub transport_type: TransportType,
68 pub local_addr: crate::NetworkAddress,
70 pub remote_addr: crate::NetworkAddress,
72 pub is_encrypted: bool,
74 pub cipher_suite: String,
76 pub used_0rtt: bool,
78 pub established_at: Instant,
80 pub last_activity: Instant,
82}
83
84#[derive(Debug, Clone)]
86pub struct ConnectionPoolInfo {
87 pub active_connections: usize,
89 pub total_connections: usize,
91 pub bytes_sent: u64,
93 pub bytes_received: u64,
95}
96
97#[derive(Debug, Clone)]
99pub struct ConnectionPoolStats {
100 pub messages_per_connection: HashMap<String, usize>,
102 pub bytes_per_connection: HashMap<String, u64>,
104 pub latency_per_connection: HashMap<String, Duration>,
106}
107
108#[derive(Debug, Clone)]
110pub struct TransportMessage {
111 pub sender: PeerId,
113 pub data: Vec<u8>,
115 pub protocol: String,
117 pub received_at: Instant,
119}
120
121impl Validate for TransportMessage {
122 fn validate(&self, ctx: &ValidationContext) -> Result<()> {
123 validate_peer_id(&self.sender)?;
125
126 validate_message_size(self.data.len(), ctx.max_message_size)?;
128
129 if self.protocol.is_empty() || self.protocol.len() > 64 {
131 return Err(P2PError::validation("Invalid protocol identifier"));
132 }
133
134 Ok(())
135 }
136}
137
138#[derive(Debug, Clone)]
140pub struct TransportOptions {
141 pub enable_0rtt: bool,
143 pub require_encryption: bool,
145 pub connect_timeout: Duration,
147 pub keep_alive: Duration,
149 pub max_message_size: usize,
151}
152
153impl fmt::Display for TransportType {
154 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
155 match self {
156 TransportType::QUIC => write!(f, "quic"),
157 }
158 }
159}
160
161impl Default for TransportOptions {
162 fn default() -> Self {
163 Self {
164 enable_0rtt: true,
165 require_encryption: true,
166 connect_timeout: Duration::from_secs(30),
167 keep_alive: Duration::from_secs(60),
168 max_message_size: 64 * 1024 * 1024, }
170 }
171}
172
173impl Default for ConnectionQuality {
174 fn default() -> Self {
175 Self {
176 latency: Duration::from_millis(50),
177 throughput_mbps: 100.0,
178 packet_loss: 0.0,
179 jitter: Duration::from_millis(5),
180 connect_time: Duration::from_millis(100),
181 }
182 }
183}
184
185pub mod transport_types {
187 pub use super::TransportType;
188}
189
190#[cfg(test)]
191mod tests {
192 use super::*;
193
194 #[test]
195 fn test_transport_type_display() {
196 assert_eq!(format!("{}", TransportType::QUIC), "quic");
197 }
198
199 #[test]
200 fn test_transport_type_serialization() {
201 let quic_type = TransportType::QUIC;
202 assert_eq!(quic_type, TransportType::QUIC);
203 }
204
205 #[test]
206 fn test_transport_selection_variants() {
207 let quic_selection = TransportSelection::QUIC;
208 assert!(matches!(quic_selection, TransportSelection::QUIC));
209 }
210
211 #[test]
212 fn test_transport_selection_default() {
213 let default = TransportSelection::default();
214 assert!(matches!(default, TransportSelection::QUIC));
215 }
216
217 #[test]
218 fn test_transport_options_default() {
219 let options = TransportOptions::default();
220
221 assert!(options.enable_0rtt);
222 assert!(options.require_encryption);
223 assert_eq!(options.connect_timeout, Duration::from_secs(30));
224 assert_eq!(options.keep_alive, Duration::from_secs(60));
225 assert_eq!(options.max_message_size, 64 * 1024 * 1024);
226 }
227
228 #[test]
229 fn test_connection_quality_default() {
230 let quality = ConnectionQuality::default();
231
232 assert_eq!(quality.latency, Duration::from_millis(50));
233 assert_eq!(quality.throughput_mbps, 100.0);
234 assert_eq!(quality.packet_loss, 0.0);
235 assert_eq!(quality.jitter, Duration::from_millis(5));
236 assert_eq!(quality.connect_time, Duration::from_millis(100));
237 }
238
239 #[test]
240 fn test_transport_options_configuration() {
241 let options = TransportOptions {
242 enable_0rtt: false,
243 require_encryption: false,
244 connect_timeout: Duration::from_secs(10),
245 keep_alive: Duration::from_secs(30),
246 max_message_size: 1024,
247 };
248
249 assert!(!options.enable_0rtt);
250 assert!(!options.require_encryption);
251 assert_eq!(options.connect_timeout, Duration::from_secs(10));
252 assert_eq!(options.keep_alive, Duration::from_secs(30));
253 assert_eq!(options.max_message_size, 1024);
254 }
255
256 #[test]
257 fn test_transport_message_structure() {
258 let message = TransportMessage {
259 sender: "test_peer".to_string(),
260 data: vec![1, 2, 3, 4],
261 protocol: "/p2p/test/1.0.0".to_string(),
262 received_at: Instant::now(),
263 };
264
265 assert_eq!(message.sender, "test_peer");
266 assert_eq!(message.data, vec![1, 2, 3, 4]);
267 assert_eq!(message.protocol, "/p2p/test/1.0.0");
268 }
269}