1use serde::{Deserialize, Serialize};
2use std::net::{IpAddr, Ipv4Addr};
3use std::time::Duration;
4use thiserror::Error;
5
6#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
26pub struct NetworkAddress {
27 pub ip: IpAddr,
29 pub port: u16,
31}
32
33impl NetworkAddress {
34 pub fn new(ip_parts: [u8; 4], port: u16) -> Self {
36 Self {
37 ip: IpAddr::V4(Ipv4Addr::new(
38 ip_parts[0],
39 ip_parts[1],
40 ip_parts[2],
41 ip_parts[3],
42 )),
43 port,
44 }
45 }
46
47 pub fn from_ip_port(ip: IpAddr, port: u16) -> Self {
49 Self { ip, port }
50 }
51
52 pub fn to_socket_addr(&self) -> String {
54 format!("{}:{}", self.ip, self.port)
55 }
56}
57
58#[derive(Debug, Error)]
60pub enum NetworkError {
61 #[error("Connection failed: {0}")]
62 ConnectionError(String),
63
64 #[error("Message handling failed: {0}")]
65 MessageError(String),
66
67 #[error("Routing failed: {0}")]
68 RoutingError(String),
69
70 #[error("Encryption failed: {0}")]
71 EncryptionError(String),
72
73 #[error("Bootstrap failed")]
74 BootstrapFailed,
75
76 #[error("Content too large")]
77 ContentTooLarge,
78
79 #[error("Internal error: {0}")]
80 Internal(String),
81}
82
83#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
85pub enum MessagePriority {
86 High,
88 Normal,
90 Low,
92}
93
94#[derive(Debug, Clone)]
96pub enum RoutingStrategy {
97 Direct(Vec<u8>),
99 Flood,
101 RandomSubset(usize),
103 Anonymous {
105 hops: usize,
107 },
108}
109
110#[derive(Debug, Clone, Serialize, Deserialize)]
112pub struct RoutingLayer {
113 pub next_hop: Vec<u8>,
115 pub payload: Vec<u8>,
117 pub metadata: LayerMetadata,
119}
120
121#[derive(Debug, Clone, Serialize, Deserialize)]
123pub struct LayerMetadata {
124 pub ttl: Duration,
126 pub flags: u32,
128 pub id: String,
130}
131
132#[derive(Debug, Clone, Default)]
134pub struct NetworkMetrics {
135 pub messages_per_second: f64,
137 pub connections: usize,
139 pub active_connections: usize,
141 pub avg_latency: Duration,
143 pub memory_usage: usize,
145}
146
147#[derive(Debug, Clone, Serialize, Deserialize)]
149pub enum MessageType {
150 Handshake {
152 version: u32,
154 node_id: Vec<u8>,
156 },
157 Data {
159 id: String,
161 payload: Vec<u8>,
163 priority: MessagePriority,
165 },
166 Control {
168 command: String,
170 params: Vec<String>,
172 },
173}
174
175#[derive(Debug, Clone, Serialize, Deserialize)]
177pub struct NetworkMessage {
178 pub id: String,
180 pub source: Vec<u8>,
182 pub destination: Vec<u8>,
184 pub payload: Vec<u8>,
186 pub priority: MessagePriority,
188 pub ttl: Duration,
190}
191
192#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
194pub struct PeerId([u8; 32]);
195
196impl PeerId {
197 pub fn random() -> Self {
199 use rand::RngCore;
200 let mut id = [0u8; 32];
201 rand::thread_rng().fill_bytes(&mut id);
202 Self(id)
203 }
204
205 pub fn from_bytes(bytes: [u8; 32]) -> Self {
207 Self(bytes)
208 }
209
210 pub fn to_bytes(&self) -> [u8; 32] {
212 self.0
213 }
214
215 pub fn as_bytes(&self) -> &[u8] {
217 &self.0
218 }
219}
220
221impl std::fmt::Display for PeerId {
222 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
223 for byte in &self.0[..8] {
225 write!(f, "{:02x}", byte)?;
226 }
227 Ok(())
228 }
229}
230
231#[derive(Debug, Clone, PartialEq, Eq)]
233pub enum ConnectionStatus {
234 Connecting,
236 Connected,
238 Disconnecting,
240 Disconnected,
242 Failed(String),
244}
245
246#[derive(Debug, Clone, Default)]
248pub struct QueueMetrics {
249 pub current_size: usize,
251 pub max_size: usize,
253 pub utilization: f64,
255 pub high_water_mark: usize,
257 pub messages_per_second: f64,
259}
260
261#[derive(Debug, Clone, Default)]
263pub struct LatencyMetrics {
264 pub avg_latency: Duration,
266 pub peak_latency: Duration,
268 pub p95_latency: Duration,
270 pub p99_latency: Duration,
272}
273
274#[derive(Debug, Clone, Default)]
276pub struct ThroughputMetrics {
277 pub messages_per_second: f64,
279 pub bytes_per_second: f64,
281 pub peak_throughput: f64,
283 pub avg_throughput: f64,
285 pub total_messages: u64,
287}