diff options
author | Sunil Nimmagadda <sunil@nimmagadda.net> | 2023-01-01 12:00:44 +0530 |
---|---|---|
committer | Sunil Nimmagadda <sunil@nimmagadda.net> | 2023-01-01 12:00:44 +0530 |
commit | 5a93c9d76366f3699f15af9ee74ab1f387c94d23 (patch) | |
tree | b166135cc53ab4ce533f8c3e2a53ee46162eaa6a /src | |
parent | 26b293760b4cdee55ae689a6f719b3f83a0d3cde (diff) |
A skeletal state machine, wip.
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 44 |
1 files changed, 29 insertions, 15 deletions
diff --git a/src/main.rs b/src/main.rs index d390b39..6421bf1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,15 +1,33 @@ use tokio::signal::unix; use tokio::time; -const ADV_INTERVAL: u64 = 3; -const SKEW_INTERVAL: u64 = 1; +const ADV_INTERVAL: u64 = 1; -async fn advert_handler() { - println!("3 second timer fired"); +#[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 skew_handler() { - println!("1 second timer fired"); +async fn advert_handler() { + println!("advert timer fired"); } async fn sighup_handler() { @@ -18,20 +36,13 @@ async fn sighup_handler() { #[tokio::main(flavor = "current_thread")] async fn main() { - let advert_handle = tokio::spawn(async move { + 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 skew_handle = tokio::spawn(async move { - let mut skew_interval = time::interval(time::Duration::from_secs(SKEW_INTERVAL)); - loop { - skew_interval.tick().await; - skew_handler().await; - } - }); let sighup_handle = tokio::spawn(async move { let mut stream = unix::signal(unix::SignalKind::hangup()).expect("Should be a signal stream"); @@ -40,5 +51,8 @@ async fn main() { sighup_handler().await; } }); - tokio::try_join!(advert_handle, skew_handle, sighup_handle).expect("task(s) erred"); + 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"); } |