ant_quic/api/
mod.rs

1//! High-Level NAT Traversal API
2//!
3//! This module provides a clean, intuitive API for developers to use the
4//! ant-quic library for NAT traversal and P2P networking.
5
6use std::time::Duration;
7use std::collections::HashMap;
8use thiserror::Error;
9
10use crate::nat_traversal::{
11    NatTraversalEndpoint,
12    PeerId,
13    NatTraversalError,
14};
15
16
17// Re-export configuration module
18pub mod config;
19pub use config::P2PConfig;
20pub use config::P2PConfigBuilder;
21pub use config::ConfigError;
22
23/// High-level P2P node implementation
24pub struct P2PNode {
25    // Internal NAT traversal endpoint
26    endpoint: NatTraversalEndpoint,
27    // Node configuration
28    config: P2PConfig,
29    // Active connections
30    connections: HashMap<PeerId, P2PConnection>,
31    // Event queue
32    events: Vec<P2PEvent>,
33}
34
35/// High-level P2P connection
36pub struct P2PConnection {
37    // Peer ID
38    peer_id: PeerId,
39    // Connection state
40    state: ConnectionState,
41    // Statistics
42    stats: ConnectionStats,
43}
44
45/// Connection state
46enum ConnectionState {
47    Connecting,
48    Connected,
49    Disconnecting,
50    Disconnected,
51}
52
53/// Connection statistics
54pub struct ConnectionStats {
55    // Round-trip time
56    rtt: Duration,
57    // Bytes sent
58    bytes_sent: u64,
59    // Bytes received
60    bytes_received: u64,
61    // Packets sent
62    packets_sent: u64,
63    // Packets received
64    packets_received: u64,
65}
66
67/// High-level P2P events
68pub enum P2PEvent {
69    /// Connected to a peer
70    Connected {
71        peer_id: PeerId,
72    },
73    /// Disconnected from a peer
74    Disconnected {
75        peer_id: PeerId,
76        reason: Option<String>,
77    },
78    /// Received data from a peer
79    Data {
80        peer_id: PeerId,
81        data: Vec<u8>,
82    },
83    /// Error occurred
84    Error {
85        peer_id: Option<PeerId>,
86        error: P2PError,
87    },
88}
89
90/// Errors that can occur in the P2P API
91#[derive(Debug, Error)]
92pub enum P2PError {
93    #[error("Connection error: {0}")]
94    Connection(String),
95    
96    #[error("Authentication error: {0}")]
97    Authentication(String),
98    
99    #[error("NAT traversal error: {0}")]
100    NatTraversal(#[from] NatTraversalError),
101    
102    #[error("Configuration error: {0}")]
103    Configuration(String),
104    
105    #[error("Timeout: {0}")]
106    Timeout(String),
107}
108
109// Implementation of the P2P API
110// (Placeholder - actual implementation would go here)