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