control_loop

Function control_loop 

Source
pub async fn control_loop(state: &mut AppState) -> String
Expand description

Main control loop for event processing.

Connects to Tor, authenticates, initializes state, and processes events in a continuous loop until the connection is lost or an error occurs.

§Flow

  1. Connect to Tor’s control port (socket or TCP)
  2. Authenticate using available methods
  3. Get Tor version for feature detection
  4. Initialize vanguard state from consensus
  5. Initialize optional components (logguard, pathverify)
  6. Subscribe to configured event types
  7. Process events until connection closes

§Arguments

  • state - The application state containing all protection components

§Returns

Returns a status string:

  • "closed" - Connection was closed normally
  • "failed: <reason>" - Connection or operation failed

§Event Processing

The loop dispatches events to appropriate handlers:

Event TypeHandlers
CIRCRendGuard, BandGuards, CBTVerify, PathVerify, LogGuard
CIRC_BWBandGuards
CIRC_MINORBandGuards, PathVerify
ORCONNBandGuards, PathVerify
BWBandGuards (connectivity check)
NEWCONSENSUSVanguardState update
SIGNALConfiguration reload (SIGHUP)

§Example

use vanguards_rs::control::{AppState, control_loop};
use vanguards_rs::vanguards::VanguardState;
use vanguards_rs::config::Config;

let state = VanguardState::new("/tmp/vanguards.state");
let config = Config::default();
let mut app_state = AppState::new(state, config);

let result = control_loop(&mut app_state).await;
println!("Control loop exited: {}", result);

§See Also