summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 6421bf12bac6408074b77ef0ce679d0a5e3eee42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use tokio::signal::unix;
use tokio::time;

const ADV_INTERVAL: u64 = 1;

#[derive(Debug)]
enum VRRPv2State {
    Initialize,
    Backup,
    Master,
}

struct StateMachine {
    state: VRRPv2State,
}

impl StateMachine {
    fn new() -> Self {
        StateMachine {
            state: VRRPv2State::Initialize,
        }
    }
    fn transit_to(&mut self, new_state: VRRPv2State) {
        std::println!("vvrpd: {:?} -> {:?}", self.state, new_state);
        self.state = new_state;
    }
}

async fn advert_handler() {
    println!("advert timer fired");
}

async fn sighup_handler() {
    println!("got SIGHUP");
}

#[tokio::main(flavor = "current_thread")]
async fn main() {
    let advert_timer_handle = tokio::spawn(async move {
        let mut advert_interval = time::interval(time::Duration::from_secs(ADV_INTERVAL));
        loop {
            advert_interval.tick().await;
            advert_handler().await;
        }
    });
    let sighup_handle = tokio::spawn(async move {
        let mut stream =
            unix::signal(unix::SignalKind::hangup()).expect("Should be a signal stream");
        loop {
            stream.recv().await;
            sighup_handler().await;
        }
    });
    let mut sm = StateMachine::new();
    sm.transit_to(VRRPv2State::Master);
    sm.transit_to(VRRPv2State::Backup);
    tokio::try_join!(advert_timer_handle, sighup_handle).expect("task(s) erred");
}