summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/lib.rs1
-rw-r--r--src/main.rs39
-rw-r--r--src/statemachine.rs45
3 files changed, 46 insertions, 39 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 4c1bcff..ffaaa18 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,2 +1,3 @@
#![feature(slice_as_chunks)]
+pub mod statemachine;
pub mod vrrpv2;
diff --git a/src/main.rs b/src/main.rs
index ed61942..99c5c0c 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,45 +1,6 @@
use socket2::{Domain, Protocol, Socket, Type};
use std::net::Ipv4Addr;
-struct Init;
-struct Master;
-struct Backup;
-
-impl Init {
- fn new() -> Init {
- Init {}
- }
-}
-
-trait VRRPv2StateMachine {
- fn next(&self) -> impl VRRPv2StateMachine;
-}
-
-impl VRRPv2StateMachine for Init {
- fn next(&self) -> impl VRRPv2StateMachine {
- Master {}
- }
-}
-
-impl VRRPv2StateMachine for Master {
- fn next(&self) -> impl VRRPv2StateMachine {
- Backup {}
- }
-}
-
-impl VRRPv2StateMachine for Backup {
- fn next(&self) -> impl VRRPv2StateMachine {
- Init {}
- }
-}
-
-fn run() {
- let sm = Init::new();
- loop {
- let _ = sm.next();
- }
-}
-
fn main() {
let res = Socket::new(Domain::IPV4, Type::RAW, Some(Protocol::from(112)));
let Ok(sock) = res else {
diff --git a/src/statemachine.rs b/src/statemachine.rs
new file mode 100644
index 0000000..a8b471d
--- /dev/null
+++ b/src/statemachine.rs
@@ -0,0 +1,45 @@
+struct Init;
+struct Master;
+struct Backup;
+
+enum Event {
+ Startup,
+ Shutdown,
+ MasterDownTimer,
+ AdvertTimer,
+}
+
+impl Init {
+ fn new() -> Init {
+ Init {}
+ }
+}
+
+trait VRRPv2StateMachine {
+ fn next(&self, event: Event) -> impl VRRPv2StateMachine;
+}
+
+impl VRRPv2StateMachine for Init {
+ fn next(&self, _event: Event) -> impl VRRPv2StateMachine {
+ Master {}
+ }
+}
+
+impl VRRPv2StateMachine for Master {
+ fn next(&self, _event: Event) -> impl VRRPv2StateMachine {
+ Backup {}
+ }
+}
+
+impl VRRPv2StateMachine for Backup {
+ fn next(&self, _event: Event) -> impl VRRPv2StateMachine {
+ Init {}
+ }
+}
+
+pub fn run() {
+ let sm = Init::new();
+ loop {
+ let _ = sm.next(Event::Startup);
+ }
+}