samod_core/network/
connection_info.rs

1use std::collections::HashMap;
2
3use crate::{ConnectionId, DocumentId, PeerId, UnixTimestamp};
4
5/// Information about each live connection
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub struct ConnectionInfo {
8    pub id: ConnectionId,
9    pub last_received: Option<UnixTimestamp>,
10    pub last_sent: Option<UnixTimestamp>,
11    /// The state of each document we are synchronizing with this peer
12    pub docs: HashMap<DocumentId, PeerDocState>,
13    /// Whether we are handshaking or connected with this peer
14    pub state: ConnectionState,
15}
16
17#[derive(Debug, Clone, PartialEq, Eq)]
18pub enum ConnectionState {
19    /// We're still exchanging peer IDs
20    Handshaking,
21    /// We have exchanged peer IDs and we're now synchronizing documents
22    Connected { their_peer_id: PeerId },
23}
24
25/// The state of synchronization for one (peer, document) pair
26#[derive(Debug, Clone, PartialEq, Eq, PartialOrd)]
27pub struct PeerDocState {
28    /// When we last received a message from this peer
29    pub last_received: Option<UnixTimestamp>,
30    /// When we last sent a message to this peer
31    pub last_sent: Option<UnixTimestamp>,
32    /// The heads of the document when we last sent a message
33    pub last_sent_heads: Option<Vec<automerge::ChangeHash>>,
34    /// The last heads of the document that the peer said they had
35    pub last_acked_heads: Option<Vec<automerge::ChangeHash>>,
36    /// The minimum heads which we share with the other end
37    pub shared_heads: Option<Vec<automerge::ChangeHash>>,
38    /// The last heads they said they had
39    pub their_heads: Option<Vec<automerge::ChangeHash>>,
40}
41
42impl PeerDocState {
43    pub(crate) fn empty() -> Self {
44        Self {
45            last_received: None,
46            last_sent: None,
47            last_sent_heads: None,
48            last_acked_heads: None,
49            shared_heads: None,
50            their_heads: None,
51        }
52    }
53}