p2p_chat/network/layer/
state.rs

1//! This module defines the state of the `NetworkLayer`.
2use std::collections::HashMap;
3use std::sync::Arc;
4
5use libp2p::{request_response::OutboundRequestId, swarm::Swarm, PeerId};
6use tokio::sync::{mpsc, oneshot};
7
8use crate::cli::commands::UiNotification;
9use crate::storage::SledMailboxStore;
10use crate::sync::SyncEvent;
11
12use super::super::behaviour::P2PBehaviour;
13use super::super::message::{NetworkCommand, NetworkResponse};
14
15/// The state of the network layer.
16///
17/// This struct holds all the necessary components for the network layer to
18/// function, such as the `libp2p` `Swarm`, channels for communication with
19/// other parts of the application, and storage.
20pub struct NetworkLayer {
21    /// The `libp2p` `Swarm`.
22    pub(crate) swarm: Swarm<P2PBehaviour>,
23    /// The receiver for network commands.
24    pub(crate) command_receiver: mpsc::UnboundedReceiver<NetworkCommand>,
25    /// A map of pending outbound requests.
26    pub(crate) pending_requests: HashMap<OutboundRequestId, oneshot::Sender<NetworkResponse>>,
27    /// The sender for synchronization events.
28    pub(crate) sync_event_tx: Option<mpsc::UnboundedSender<SyncEvent>>,
29    /// The sender for UI notifications.
30    pub(crate) ui_notify_tx: Option<mpsc::UnboundedSender<UiNotification>>,
31    /// The storage for the mailbox.
32    pub(crate) mailbox_storage: Option<Arc<SledMailboxStore>>,
33    /// A map of peers that are currently blocked.
34    pub(crate) blocked_peers: HashMap<PeerId, std::time::Instant>,
35}