pub struct NetworkLayer {
pub(crate) swarm: Swarm<P2PBehaviour>,
pub(crate) command_receiver: UnboundedReceiver<NetworkCommand>,
pub(crate) pending_requests: HashMap<OutboundRequestId, Sender<NetworkResponse>>,
pub(crate) sync_event_tx: Option<UnboundedSender<SyncEvent>>,
pub(crate) ui_notify_tx: Option<UnboundedSender<UiNotification>>,
pub(crate) mailbox_storage: Option<Arc<SledMailboxStore>>,
pub(crate) blocked_peers: HashMap<PeerId, Instant>,
}Expand description
The state of the network layer.
This struct holds all the necessary components for the network layer to
function, such as the libp2p Swarm, channels for communication with
other parts of the application, and storage.
Fields§
§swarm: Swarm<P2PBehaviour>The libp2p Swarm.
command_receiver: UnboundedReceiver<NetworkCommand>The receiver for network commands.
pending_requests: HashMap<OutboundRequestId, Sender<NetworkResponse>>A map of pending outbound requests.
sync_event_tx: Option<UnboundedSender<SyncEvent>>The sender for synchronization events.
ui_notify_tx: Option<UnboundedSender<UiNotification>>The sender for UI notifications.
mailbox_storage: Option<Arc<SledMailboxStore>>The storage for the mailbox.
blocked_peers: HashMap<PeerId, Instant>A map of peers that are currently blocked.
Implementations§
Source§impl NetworkLayer
impl NetworkLayer
Sourcepub(super) async fn handle_command(
&mut self,
command: NetworkCommand,
) -> Result<()>
pub(super) async fn handle_command( &mut self, command: NetworkCommand, ) -> Result<()>
Source§impl NetworkLayer
impl NetworkLayer
Sourcepub(super) async fn handle_chat_event(
&mut self,
event: Event<ChatRequest, ChatResponse>,
incoming_messages: &UnboundedSender<Message>,
) -> Result<()>
pub(super) async fn handle_chat_event( &mut self, event: Event<ChatRequest, ChatResponse>, incoming_messages: &UnboundedSender<Message>, ) -> Result<()>
Handles an event from the ChatBehaviour.
This function is called when an event is received from the ChatBehaviour.
It dispatches the event to the appropriate handler.
§Arguments
event- Therequest_response::Event<ChatRequest, ChatResponse>to handle.incoming_messages- The sender for incoming chat messages.
§Errors
This function will return an error if handling the event fails.
Sourceasync fn handle_chat_request(
&mut self,
request: ChatRequest,
channel: ResponseChannel<ChatResponse>,
incoming_messages: &UnboundedSender<Message>,
) -> Result<()>
async fn handle_chat_request( &mut self, request: ChatRequest, channel: ResponseChannel<ChatResponse>, incoming_messages: &UnboundedSender<Message>, ) -> Result<()>
Handles an inbound chat request.
Sourceasync fn handle_chat_response(
&mut self,
request_id: OutboundRequestId,
response: ChatResponse,
) -> Result<()>
async fn handle_chat_response( &mut self, request_id: OutboundRequestId, response: ChatResponse, ) -> Result<()>
Handles an outbound chat response.
Source§impl NetworkLayer
impl NetworkLayer
Sourcepub(super) async fn handle_discovery_event(
&mut self,
event: DiscoveryBehaviourEvent,
) -> Result<()>
pub(super) async fn handle_discovery_event( &mut self, event: DiscoveryBehaviourEvent, ) -> Result<()>
Handles an event from the DiscoveryBehaviour.
This function is called when an event is received from the DiscoveryBehaviour.
It dispatches the event to the appropriate handler.
§Arguments
event- TheDiscoveryBehaviourEventto handle.
§Errors
This function will return an error if handling the event fails.
Sourceasync fn handle_kademlia_event(&mut self, event: Event) -> Result<()>
async fn handle_kademlia_event(&mut self, event: Event) -> Result<()>
Handles a Kademlia event.
Source§impl NetworkLayer
impl NetworkLayer
Sourcepub(super) async fn handle_mailbox_event(
&mut self,
event: Event<MailboxRequest, MailboxResponse>,
) -> Result<()>
pub(super) async fn handle_mailbox_event( &mut self, event: Event<MailboxRequest, MailboxResponse>, ) -> Result<()>
Handles an event from the MailboxBehaviour.
This function is called when an event is received from the MailboxBehaviour.
It dispatches the event to the appropriate handler.
§Arguments
event- Therequest_response::Event<MailboxRequest, MailboxResponse>to handle.
§Errors
This function will return an error if handling the event fails.
Sourceasync fn handle_mailbox_request(
&mut self,
request: MailboxRequest,
channel: ResponseChannel<MailboxResponse>,
) -> Result<()>
async fn handle_mailbox_request( &mut self, request: MailboxRequest, channel: ResponseChannel<MailboxResponse>, ) -> Result<()>
Handles an inbound mailbox request.
Sourceasync fn handle_mailbox_response(
&mut self,
request_id: OutboundRequestId,
response: MailboxResponse,
) -> Result<()>
async fn handle_mailbox_response( &mut self, request_id: OutboundRequestId, response: MailboxResponse, ) -> Result<()>
Handles an outbound mailbox response.
Source§impl NetworkLayer
impl NetworkLayer
Sourcepub(crate) async fn handle_swarm_event(
&mut self,
event: SwarmEvent<P2PBehaviourEvent>,
incoming_messages: &UnboundedSender<Message>,
) -> Result<()>
pub(crate) async fn handle_swarm_event( &mut self, event: SwarmEvent<P2PBehaviourEvent>, incoming_messages: &UnboundedSender<Message>, ) -> Result<()>
Handles a SwarmEvent.
This function is the main entry point for handling events from the libp2p Swarm.
It dispatches the event to the appropriate handler based on its type.
§Arguments
event- TheSwarmEvent<P2PBehaviourEvent>to handle.incoming_messages- The sender for incoming chat messages.
§Errors
This function will return an error if handling the event fails.
Source§impl NetworkLayer
impl NetworkLayer
Sourcepub fn new(
identity: Arc<Identity>,
listen_addr: Multiaddr,
is_mailbox: bool,
bootstrap_nodes: Vec<&str>,
) -> Result<(Self, NetworkHandle)>
pub fn new( identity: Arc<Identity>, listen_addr: Multiaddr, is_mailbox: bool, bootstrap_nodes: Vec<&str>, ) -> Result<(Self, NetworkHandle)>
Creates a new NetworkLayer and NetworkHandle.
§Arguments
identity- The identity of the local node.listen_addr- The address to listen on for incoming connections.is_mailbox- Whether the node is a mailbox node.bootstrap_nodes- A list of bootstrap nodes to connect to.
§Errors
This function will return an error if the network layer cannot be created.
Sourcepub fn new_with_mailbox_storage(
identity: Arc<Identity>,
listen_addr: Multiaddr,
is_mailbox: bool,
mailbox_storage: Option<Arc<SledMailboxStore>>,
bootstrap_nodes: Vec<&str>,
) -> Result<(Self, NetworkHandle)>
pub fn new_with_mailbox_storage( identity: Arc<Identity>, listen_addr: Multiaddr, is_mailbox: bool, mailbox_storage: Option<Arc<SledMailboxStore>>, bootstrap_nodes: Vec<&str>, ) -> Result<(Self, NetworkHandle)>
Creates a new NetworkLayer and NetworkHandle with optional mailbox storage.
§Arguments
identity- The identity of the local node.listen_addr- The address to listen on for incoming connections.is_mailbox- Whether the node is a mailbox node.mailbox_storage- The storage for the mailbox, if this is a mailbox node.bootstrap_nodes- A list of bootstrap nodes to connect to.
§Errors
This function will return an error if the network layer cannot be created.
Source§impl NetworkLayer
impl NetworkLayer
Sourcepub fn set_sync_event_sender(&mut self, sender: UnboundedSender<SyncEvent>)
pub fn set_sync_event_sender(&mut self, sender: UnboundedSender<SyncEvent>)
Sets the sender for synchronization events.
Sourcepub fn set_ui_notify_sender(&mut self, sender: UnboundedSender<UiNotification>)
pub fn set_ui_notify_sender(&mut self, sender: UnboundedSender<UiNotification>)
Sets the sender for UI notifications.
Sourcepub fn bootstrap_dht(&mut self) -> Result<()>
pub fn bootstrap_dht(&mut self) -> Result<()>
Bootstraps the Kademlia DHT.
§Errors
This function will return an error if the bootstrap process fails.
Sourcepub fn start_providing_mailbox(&mut self) -> Result<()>
pub fn start_providing_mailbox(&mut self) -> Result<()>
Starts providing the general mailbox provider key in the Kademlia DHT.
§Errors
This function will return an error if the providing process fails to start.
Source§impl NetworkLayer
impl NetworkLayer
Sourcefn cleanup_blocked_peers(&mut self)
fn cleanup_blocked_peers(&mut self)
Periodically cleans up the list of blocked peers.
Sourcepub async fn run(
&mut self,
incoming_messages: UnboundedSender<Message>,
) -> Result<()>
pub async fn run( &mut self, incoming_messages: UnboundedSender<Message>, ) -> Result<()>
Runs the main event loop for the NetworkLayer.
This function listens for events from the libp2p Swarm and for
commands from other parts of the application. It also periodically
cleans up the list of blocked peers.
§Arguments
incoming_messages- The sender for incoming chat messages.
§Errors
This function will return an error if the event loop fails.
Auto Trait Implementations§
impl !Freeze for NetworkLayer
impl !RefUnwindSafe for NetworkLayer
impl Send for NetworkLayer
impl !Sync for NetworkLayer
impl Unpin for NetworkLayer
impl !UnwindSafe for NetworkLayer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more