use serde::{Deserialize, Serialize};
use std::net::{IpAddr, Ipv4Addr};
use std::time::Duration;
use thiserror::Error;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct NetworkAddress {
pub ip: IpAddr,
pub port: u16,
}
impl NetworkAddress {
pub fn new(ip_parts: [u8; 4], port: u16) -> Self {
Self {
ip: IpAddr::V4(Ipv4Addr::new(
ip_parts[0],
ip_parts[1],
ip_parts[2],
ip_parts[3],
)),
port,
}
}
pub fn from_ip_port(ip: IpAddr, port: u16) -> Self {
Self { ip, port }
}
pub fn to_socket_addr(&self) -> String {
format!("{}:{}", self.ip, self.port)
}
}
#[derive(Debug, Error)]
pub enum NetworkError {
#[error("Connection failed: {0}")]
ConnectionError(String),
#[error("Message handling failed: {0}")]
MessageError(String),
#[error("Routing failed: {0}")]
RoutingError(String),
#[error("Encryption failed: {0}")]
EncryptionError(String),
#[error("Bootstrap failed")]
BootstrapFailed,
#[error("Content too large")]
ContentTooLarge,
#[error("Internal error: {0}")]
Internal(String),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum MessagePriority {
High,
Normal,
Low,
}
#[derive(Debug, Clone)]
pub enum RoutingStrategy {
Direct(Vec<u8>),
Flood,
RandomSubset(usize),
Anonymous {
hops: usize,
},
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RoutingLayer {
pub next_hop: Vec<u8>,
pub payload: Vec<u8>,
pub metadata: LayerMetadata,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LayerMetadata {
pub ttl: Duration,
pub flags: u32,
pub id: String,
}
#[derive(Debug, Clone, Default)]
pub struct NetworkMetrics {
pub messages_per_second: f64,
pub connections: usize,
pub active_connections: usize,
pub avg_latency: Duration,
pub memory_usage: usize,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum MessageType {
Handshake {
version: u32,
node_id: Vec<u8>,
},
Data {
id: String,
payload: Vec<u8>,
priority: MessagePriority,
},
Control {
command: String,
params: Vec<String>,
},
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NetworkMessage {
pub id: String,
pub source: Vec<u8>,
pub destination: Vec<u8>,
pub payload: Vec<u8>,
pub priority: MessagePriority,
pub ttl: Duration,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct PeerId([u8; 32]);
impl PeerId {
pub fn random() -> Self {
use rand::RngCore;
let mut id = [0u8; 32];
rand::thread_rng().fill_bytes(&mut id);
Self(id)
}
pub fn from_bytes(bytes: [u8; 32]) -> Self {
Self(bytes)
}
pub fn to_bytes(&self) -> [u8; 32] {
self.0
}
pub fn as_bytes(&self) -> &[u8] {
&self.0
}
}
impl std::fmt::Display for PeerId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for byte in &self.0[..8] {
write!(f, "{:02x}", byte)?;
}
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ConnectionStatus {
Connecting,
Connected,
Disconnecting,
Disconnected,
Failed(String),
}
#[derive(Debug, Clone, Default)]
pub struct QueueMetrics {
pub current_size: usize,
pub max_size: usize,
pub utilization: f64,
pub high_water_mark: usize,
pub messages_per_second: f64,
}
#[derive(Debug, Clone, Default)]
pub struct LatencyMetrics {
pub avg_latency: Duration,
pub peak_latency: Duration,
pub p95_latency: Duration,
pub p99_latency: Duration,
}
#[derive(Debug, Clone, Default)]
pub struct ThroughputMetrics {
pub messages_per_second: f64,
pub bytes_per_second: f64,
pub peak_throughput: f64,
pub avg_throughput: f64,
pub total_messages: u64,
}