p2p_chat/network/message.rs
1//! This module defines the messages that are sent to and from the `NetworkLayer`.
2use crate::types::{ChatRequest, EncryptedMessage, Message};
3use anyhow::Result;
4use libp2p::{kad, PeerId};
5use tokio::sync::oneshot;
6
7/// A response from the `NetworkLayer`.
8#[derive(Debug)]
9pub enum NetworkResponse {
10 /// A message was successfully sent.
11 MessageSent,
12 /// A list of connected peers.
13 ConnectedPeers {
14 /// The list of connected peers.
15 peers: Vec<PeerId>,
16 },
17 /// An error occurred.
18 Error(String),
19 /// The result of a mailbox `put` operation.
20 MailboxPutResult {
21 /// Whether the operation was successful.
22 success: bool,
23 },
24 /// A list of messages fetched from a mailbox.
25 MailboxMessages {
26 /// The list of fetched messages.
27 messages: Vec<EncryptedMessage>,
28 },
29 /// The result of a mailbox `ack` operation.
30 MailboxAckResult {
31 /// The number of messages that were deleted.
32 deleted: usize,
33 },
34}
35
36/// A command to be sent to the `NetworkLayer`.
37#[derive(Debug)]
38pub enum NetworkCommand {
39 /// Send a chat message to a peer.
40 SendMessage {
41 /// The `PeerId` of the recipient.
42 peer_id: PeerId,
43 /// The message to send.
44 message: Message,
45 /// The channel to send the response on.
46 response: oneshot::Sender<NetworkResponse>,
47 },
48 /// Send a chat request to a peer.
49 SendChatRequest {
50 /// The `PeerId` of the recipient.
51 peer_id: PeerId,
52 /// The request to send.
53 request: ChatRequest,
54 /// The channel to send the response on.
55 response: oneshot::Sender<NetworkResponse>,
56 },
57 /// Get the list of connected peers.
58 GetConnectedPeers {
59 /// The channel to send the response on.
60 response: oneshot::Sender<NetworkResponse>,
61 },
62 /// Put a message into a mailbox.
63 MailboxPut {
64 /// The `PeerId` of the mailbox node.
65 peer_id: PeerId,
66 /// The hash of the recipient's public key.
67 recipient: [u8; 32],
68 /// The encrypted message to store.
69 message: EncryptedMessage,
70 /// The channel to send the response on.
71 response: oneshot::Sender<NetworkResponse>,
72 },
73 /// Fetch messages from a mailbox.
74 MailboxFetch {
75 /// The `PeerId` of the mailbox node.
76 peer_id: PeerId,
77 /// The hash of the recipient's public key.
78 recipient: [u8; 32],
79 /// The maximum number of messages to fetch.
80 limit: usize,
81 /// The channel to send the response on.
82 response: oneshot::Sender<NetworkResponse>,
83 },
84 /// Acknowledge the receipt of messages from a mailbox.
85 MailboxAck {
86 /// The `PeerId` of the mailbox node.
87 peer_id: PeerId,
88 /// The hash of the recipient's public key.
89 recipient: [u8; 32],
90 /// The IDs of the messages to acknowledge.
91 msg_ids: Vec<uuid::Uuid>,
92 /// The channel to send the response on.
93 response: oneshot::Sender<NetworkResponse>,
94 },
95 /// Start a Kademlia DHT query to find providers for a key.
96 StartDhtProviderQuery {
97 /// The key to find providers for.
98 key: kad::RecordKey,
99 /// The channel to send the response on.
100 response: oneshot::Sender<Result<kad::QueryId>>,
101 },
102}