.. _rust_actor_architecture: =========================================== Rust Actor Architecture & Concurrency =========================================== The Rust implementation (``rustflexstack``) is built upon the **Actor Model** of concurrent computation. Each protocol layer executes within its own dedicated operating system thread, communicating exclusively via message passing across asynchronous ``std::sync::mpsc`` (Multiple Producer, Single Consumer) channels. Concurrency Model Overview ========================== .. mermaid:: flowchart TD FAC["FACILITIES LAYER ACTORS
LdmFacility / CaService / DenService
std::thread with mpsc channels"] BTP["BTP ROUTER ACTOR
BTPRouter::spawn()
mpsc::channel<BTPRouterInput>"] GN["GEONETWORKING ROUTER ACTOR
Router::spawn()
mpsc::channel<RouterInput>"] LL["LINK LAYER DRIVER ACTORS
RawLinkLayer: rx_thread (pnet) & tx_thread (pnet)
or Cv2xLinkLayer (Qualcomm PC5 C-FFI)"] FAC -->|"BTPDataRequest (mpsc)"| BTP BTP -->|"BTPDataIndication (mpsc)"| FAC BTP -->|"GNDataRequest (mpsc)"| GN GN -->|"GNDataIndication (mpsc)"| BTP GN -->|"raw payload tx (mpsc)"| LL LL -->|"raw payload rx (mpsc)"| GN style FAC fill:#dcfce7,stroke:#16a34a,stroke-width:2px,color:#14532d style BTP fill:#dbeafe,stroke:#2563eb,stroke-width:2px,color:#1e3a8a style GN fill:#fee2e2,stroke:#dc2626,stroke-width:2px,color:#7f1d1d style LL fill:#fef3c7,stroke:#d97706,stroke-width:2px,color:#78350f Key Architectural Advantages ============================ 1. **Little Shared Mutable State**: Layers communicate over channels, and each actor owns its protocol state. The few structures that several threads use (for example the security context and the channel busy ratio) sit behind ``Mutex`` locks, and safe Rust rules out data races at compile time. 2. **Move Semantics & Zero Reallocation**: Packet byte buffers (``Vec``) and parsed ASN.1 data structures are moved across channels rather than copied. 3. **Low Processing Latency**: There are no garbage-collection pauses, and the code is compiled ahead of time. Measured processing latencies are reported on the :ref:`performance` page. 4. **Autonomous Beaconing**: If the GeoNetworking MIB defines ``itsGnBeaconServiceRetransmitTimer > 0``, the GeoNetworking router automatically spawns a dedicated background beacon timer thread that injects periodic Beacon packets without application polling. Router Handles & Channel Wrapping ================================= Callers interact with running actors using lightweight, thread-safe handle structs that wrap a channel ``Sender``: .. list-table:: :widths: 30 70 :header-rows: 1 * - Handle Type - Role & Method Primitives * - ``RouterHandle`` - Wraps GeoNetworking input channel. Methods: ``send_gn_data_request()``, ``update_position()``, ``shutdown()``. Cloneable across threads. * - ``BTPRouterHandle`` - Wraps BTP input channel. Methods: ``send_btp_data_request()``, ``register_port(port, tx)``, ``unregister_port(port)``. * - ``IfLdm3`` / ``IfLdm4`` - Interfaces for LDM data providers (registration, additions) and consumers (queries, subscriptions). Thread Lifecycle & Graceful Teardown ==================================== The entire pipeline shuts down gracefully: - When a handle or sender is dropped, the corresponding receiver's ``rx.recv()`` returns ``Err(RecvError)``, causing the actor's event loop to cleanly exit. - Sockets and telematics flows are closed deterministically when their owner structs drop.