Module sync

Module sync 

Source
Expand description

HIVE-Lite Sync Protocol

Efficient CRDT synchronization over BLE GATT characteristics.

§Overview

This module provides the sync layer for HIVE-Lite nodes, enabling efficient state synchronization over bandwidth-constrained BLE links.

§Key Components

  • CRDTs: Conflict-free replicated data types (LWW-Register, G-Counter)
  • Batching: Accumulates changes to reduce radio activity
  • Delta Encoding: Only sends changes since last sync
  • Chunking: Splits large messages across MTU boundaries

§Architecture

┌────────────────────────────────────────────────────┐
│                  Application                        │
│    (position updates, health status, alerts)       │
└─────────────────────┬──────────────────────────────┘
                      │
                      ▼
┌────────────────────────────────────────────────────┐
│              GattSyncProtocol                       │
│  ┌──────────────┐  ┌────────────┐  ┌────────────┐  │
│  │    Batch     │  │   Delta    │  │  Chunked   │  │
│  │ Accumulator  │─▶│  Encoder   │─▶│  Transfer  │  │
│  └──────────────┘  └────────────┘  └────────────┘  │
└─────────────────────┬──────────────────────────────┘
                      │
                      ▼
┌────────────────────────────────────────────────────┐
│              GATT Characteristics                   │
│           (Sync Data, Sync State)                  │
└────────────────────────────────────────────────────┘

§Usage

use hive_btle::sync::{GattSyncProtocol, SyncConfig, CrdtOperation, Position};
use hive_btle::NodeId;

// Create sync protocol
let mut sync = GattSyncProtocol::new(
    NodeId::new(0x12345678),
    SyncConfig::default(),
);

// Add a peer
sync.add_peer(&peer_id);

// Queue position update
sync.queue_operation(CrdtOperation::UpdatePosition {
    node_id: my_node_id,
    position: Position::new(37.7749, -122.4194),
    timestamp: current_time_ms,
});

// Check if time to sync
if sync.should_sync() {
    let chunks = sync.prepare_sync(&peer_id);
    for chunk in chunks {
        // Write chunk to GATT characteristic
        gatt.write_sync_data(&chunk.encode());
    }
}

// Process received data
if let Some(ops) = sync.process_received(chunk, &peer_id) {
    for op in ops {
        // Apply CRDT operation to local state
        apply_operation(op);
    }
}

§Power Efficiency

The sync protocol is designed for constrained devices:

FeatureBenefit
BatchingReduces sync frequency (less radio time)
Delta EncodingSends only changes (less bytes)
Configurable IntervalsTrade freshness for battery
Compact CRDT EncodingMinimal overhead

§Sync Profiles

// For smartwatch (battery critical)
let config = SyncConfig::low_power();

// For tablet (responsiveness preferred)
let config = SyncConfig::responsive();

Re-exports§

pub use batch::BatchAccumulator;
pub use batch::BatchConfig;
pub use batch::OperationBatch;
pub use crdt::ChatCRDT;
pub use crdt::ChatMessage;
pub use crdt::CrdtOperation;
pub use crdt::EventType;
pub use crdt::GCounter;
pub use crdt::HealthStatus;
pub use crdt::LwwRegister;
pub use crdt::Peripheral;
pub use crdt::PeripheralEvent;
pub use crdt::PeripheralType;
pub use crdt::Position;
pub use crdt::Timestamp;
pub use crdt::CHAT_MAX_MESSAGES;
pub use crdt::CHAT_MAX_SENDER_LEN;
pub use crdt::CHAT_MAX_TEXT_LEN;
pub use delta::DeltaEncoder;
pub use delta::DeltaStats;
pub use delta::PeerSyncState;
pub use delta::VectorClock;
pub use delta_document::DeltaDocument;
pub use delta_document::DeltaFlags;
pub use delta_document::Operation;
pub use delta_document::DELTA_DOCUMENT_MARKER;
pub use protocol::chunk_data;
pub use protocol::ChunkHeader;
pub use protocol::ChunkReassembler;
pub use protocol::GattSyncProtocol;
pub use protocol::SyncChunk;
pub use protocol::SyncConfig;
pub use protocol::SyncState;
pub use protocol::SyncStats;
pub use protocol::CHUNK_HEADER_SIZE;
pub use protocol::DEFAULT_MTU;
pub use protocol::MAX_MTU;

Modules§

batch
Batch Accumulator for HIVE-Lite Sync
crdt
CRDT (Conflict-free Replicated Data Types) for HIVE-Lite
delta
Delta Encoder for HIVE-Lite Sync
delta_document
Delta Document wire format for bandwidth-efficient sync
protocol
GATT Sync Protocol for HIVE-Lite