NetworkLayer

Struct NetworkLayer 

Source
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

Source

pub(super) async fn handle_command( &mut self, command: NetworkCommand, ) -> Result<()>

Handles an incoming NetworkCommand.

This function processes commands sent to the NetworkLayer from other parts of the application.

§Arguments
  • command - The NetworkCommand to handle.
§Errors

This function will return an error if handling the command fails.

Source§

impl NetworkLayer

Source

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 - The request_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.

Source

async fn handle_chat_request( &mut self, request: ChatRequest, channel: ResponseChannel<ChatResponse>, incoming_messages: &UnboundedSender<Message>, ) -> Result<()>

Handles an inbound chat request.

Source

async fn handle_chat_response( &mut self, request_id: OutboundRequestId, response: ChatResponse, ) -> Result<()>

Handles an outbound chat response.

Source§

impl NetworkLayer

Source

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 - The DiscoveryBehaviourEvent to handle.
§Errors

This function will return an error if handling the event fails.

Source

async fn handle_kademlia_event(&mut self, event: Event) -> Result<()>

Handles a Kademlia event.

Source§

impl NetworkLayer

Source

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 - The request_response::Event<MailboxRequest, MailboxResponse> to handle.
§Errors

This function will return an error if handling the event fails.

Source

async fn handle_mailbox_request( &mut self, request: MailboxRequest, channel: ResponseChannel<MailboxResponse>, ) -> Result<()>

Handles an inbound mailbox request.

Source

async fn handle_mailbox_response( &mut self, request_id: OutboundRequestId, response: MailboxResponse, ) -> Result<()>

Handles an outbound mailbox response.

Source§

impl NetworkLayer

Source

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 - The SwarmEvent<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

Source

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.

Source

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

Source

pub fn set_sync_event_sender(&mut self, sender: UnboundedSender<SyncEvent>)

Sets the sender for synchronization events.

Source

pub fn set_ui_notify_sender(&mut self, sender: UnboundedSender<UiNotification>)

Sets the sender for UI notifications.

Source

pub fn bootstrap_dht(&mut self) -> Result<()>

Bootstraps the Kademlia DHT.

§Errors

This function will return an error if the bootstrap process fails.

Source

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

pub fn start_providing_for_recipient( &mut self, recipient_hash: [u8; 32], ) -> Result<()>

Starts providing a key for a specific recipient in the Kademlia DHT.

§Arguments
  • recipient_hash - The hash of the recipient’s public key.
§Errors

This function will return an error if the providing process fails to start.

Source§

impl NetworkLayer

Source

fn cleanup_blocked_peers(&mut self)

Periodically cleans up the list of blocked peers.

Source

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§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,