DocumentSync

Struct DocumentSync 

Source
pub struct DocumentSync { /* private fields */ }
Expand description

Document synchronization manager for HIVE-Lite nodes

Manages the local CRDT state and handles document serialization/merging. Thread-safe for use from multiple BLE callbacks.

§Integration with Full HIVE

This implementation uses a simple GCounter suitable for embedded devices. For integration with the larger HIVE project using AutomergeIroh:

  • The build_document() output can be wrapped in an Automerge-compatible format
  • The observer events (Emergency, Ack, DocumentSynced) work with any backend
  • The BLE transport layer is document-format agnostic

Implementations§

Source§

impl DocumentSync

Source

pub fn new(node_id: NodeId, callsign: &str) -> Self

Create a new document sync manager

Source

pub fn with_peripheral_type( node_id: NodeId, callsign: &str, ptype: PeripheralType, ) -> Self

Create with a specific peripheral type

Source

pub fn node_id(&self) -> NodeId

Get our node ID

Source

pub fn version(&self) -> u32

Get the current document version

Source

pub fn total_count(&self) -> u64

Get the total counter value

Source

pub fn local_count(&self) -> u64

Get our counter contribution

Source

pub fn current_event(&self) -> Option<EventType>

Get current event type (if any)

Source

pub fn is_emergency_active(&self) -> bool

Check if we’re in emergency state

Source

pub fn is_ack_active(&self) -> bool

Check if we’ve sent an ACK

Source

pub fn callsign(&self) -> String

Get the callsign

Source

pub fn send_emergency(&self, timestamp: u64) -> Vec<u8>

Send an emergency - returns the document bytes to broadcast

Source

pub fn send_ack(&self, timestamp: u64) -> Vec<u8>

Send an ACK - returns the document bytes to broadcast

Source

pub fn clear_event(&self)

Clear the current event

Source

pub fn increment_counter(&self)

Increment the counter (for periodic sync)

Source

pub fn update_health(&self, battery_percent: u8)

Update health status (battery percentage)

Source

pub fn update_activity(&self, activity: u8)

Update activity level (0=still, 1=walking, 2=running, 3=fall)

Source

pub fn update_health_full(&self, battery_percent: u8, activity: u8)

Update full health status (battery and activity)

Source

pub fn start_emergency(&self, timestamp: u64, known_peers: &[u32]) -> Vec<u8>

Start a new emergency event

Creates an emergency event that tracks ACKs from all known peers. Returns the document bytes to broadcast.

Source

pub fn ack_emergency(&self, timestamp: u64) -> Option<Vec<u8>>

Record our ACK for the current emergency

Returns the document bytes to broadcast, or None if no emergency is active.

Source

pub fn clear_emergency(&self)

Clear the current emergency event

Source

pub fn has_active_emergency(&self) -> bool

Check if there’s an active emergency

Source

pub fn get_emergency_status(&self) -> Option<(u32, u64, usize, usize)>

Get emergency status info

Returns (source_node, timestamp, acked_count, pending_count) if emergency is active.

Source

pub fn has_peer_acked(&self, peer_id: u32) -> bool

Check if a specific peer has ACKed the current emergency

Source

pub fn all_peers_acked(&self) -> bool

Check if all peers have ACKed the current emergency

Source

pub fn add_chat_message(&self, sender: &str, text: &str, timestamp: u64) -> bool

Add a chat message to the local CRDT

Returns true if the message was new (not a duplicate).

Source

pub fn add_chat_reply( &self, sender: &str, text: &str, reply_to_node: u32, reply_to_timestamp: u64, timestamp: u64, ) -> bool

Add a chat reply to the local CRDT

Returns true if the message was new.

Source

pub fn chat_count(&self) -> usize

Get the number of chat messages

Source

pub fn chat_messages_since( &self, since_timestamp: u64, ) -> Vec<(u32, u64, String, String, u32, u64)>

Get chat messages newer than a timestamp

Returns a vector of (origin_node, timestamp, sender, text, reply_to_node, reply_to_timestamp) tuples.

Source

pub fn all_chat_messages(&self) -> Vec<(u32, u64, String, String, u32, u64)>

Get all chat messages

Returns a vector of (origin_node, timestamp, sender, text, reply_to_node, reply_to_timestamp) tuples.

Source

pub fn chat_snapshot(&self) -> Option<ChatCRDT>

Get a snapshot of the chat CRDT

Source

pub fn counter_entries(&self) -> Vec<(u32, u64)>

Get all counter entries for delta document building

Returns a vector of (node_id, count) pairs for all nodes that have contributed to the counter.

Source

pub fn peripheral_snapshot(&self) -> Peripheral

Get a clone of the peripheral state

Used for building delta documents with peripheral updates.

Source

pub fn emergency_snapshot(&self) -> Option<EmergencyEvent>

Get a clone of the emergency state

Used for building delta documents with emergency data.

Source

pub fn build_document(&self) -> Vec<u8>

Build the document for transmission

Returns the encoded bytes ready for BLE GATT write.

Source

pub fn merge_document(&self, data: &[u8]) -> Option<MergeResult>

Merge a received document

Returns Some(MergeResult) if the document was valid, None otherwise. The result contains information about what changed and any events.

Source

pub fn decode_document(data: &[u8]) -> Option<HiveDocument>

Create a document from raw bytes (for inspection without merging)

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, 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> 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.