Skip to content

Commit

Permalink
cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul-weqe committed Jul 17, 2024
1 parent fa62019 commit a76a0b3
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 49 deletions.
39 changes: 18 additions & 21 deletions holo-vrrp/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
use std::net::IpAddr;

use crate::error::Error;
use crate::instance::Instance;
use crate::instance::{Instance, State};
use crate::interface::Interface;
use crate::packet::{DecodeResult, VrrpPacket};
use crate::instance::State;
use crate::tasks;

// ===== Network packet receipt =====
Expand All @@ -25,41 +24,39 @@ pub(crate) fn process_packet(
Ok(())
}


pub(crate) fn process_vrrp_packet(
interface: &mut Interface,
packet: DecodeResult<VrrpPacket>
) -> Result<(), Error>{
packet: DecodeResult<VrrpPacket>,
) -> Result<(), Error> {
let pkt = packet.unwrap();
let mut instance = interface.instances.get_mut(&pkt.vrid).unwrap();
let mut instance = interface.instances.get_mut(&pkt.vrid).unwrap();

// errors will be modified to occupy the other statistics
instance.state.statistics.adv_rcvd += 1;

match instance.state.state {
State::Initialize => {

}
State::Initialize => {}
State::Backup => {
if pkt.priority == 0 {
tasks::set_master_down_timer(instance, instance.state.skew_time as u64);
}

else if !instance.config.preempt || pkt.priority >= instance.config.priority {
tasks::set_master_down_timer(
instance,
instance.state.skew_time as u64,
);
} else if !instance.config.preempt
|| pkt.priority >= instance.config.priority
{
instance.reset_timer();
}

}
State::Master => {
if pkt.priority == 0 {
instance.reset_timer();
}


else if (pkt.priority > instance.config.priority) || (
pkt.priority == instance.config.priority
// && check if primary IP of sender is greater than the local primary IP
){
} else if (pkt.priority > instance.config.priority)
|| (
pkt.priority == instance.config.priority
// && check if primary IP of sender is greater than the local primary IP
)
{
instance.transition_state(State::Backup);
}
}
Expand Down
4 changes: 1 addition & 3 deletions holo-vrrp/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,8 @@ impl Instance {
}

pub(crate) fn reset_timer(&mut self) {
tasks::reset_timer(self);
tasks::reset_timer(self);
}

}

// ===== impl InstanceState =====
Expand Down Expand Up @@ -158,4 +157,3 @@ impl Default for Statistics {
}
}
}

8 changes: 3 additions & 5 deletions holo-vrrp/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ pub struct ProtocolInputChannelsRx {
impl Interface {
fn send_advert(&self, vrid: u8) {
if let Some(instance) = self.instances.get(&vrid) {

// send advertisement then reset the timer.
let mut ip_addresses: Vec<Ipv4Addr> = vec![];
for addr in &instance.config.virtual_addresses {
Expand All @@ -102,13 +101,12 @@ impl Interface {
checksum: 0,
ip_addresses,
auth_data: 0,
auth_data2: 0
auth_data2: 0,
};
packet.generate_checksum();
//network::send_packet_vrrp(socket, src, dst, packet);

}
}
}
}

#[async_trait]
Expand Down Expand Up @@ -148,7 +146,7 @@ impl ProtocolInstance for Interface {
if let Err(error) = match msg {
// Received network packet.
ProtocolInputMsg::NetRxPacket(msg) => {
events::process_vrrp_packet(self, msg.packet)
events::process_vrrp_packet(self, msg.packet)
}
} {
error.log();
Expand Down
1 change: 0 additions & 1 deletion holo-vrrp/src/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,6 @@ impl VrrpPacket {
return Err(DecodeError::PacketLengthError);
}


let checksum = buf.get_u16();

// confirm checksum. checksum position is the third item in 16 bit words
Expand Down
36 changes: 17 additions & 19 deletions holo-vrrp/src/tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,10 @@ pub(crate) fn set_timer(instance: &mut Instance) {
instance.timer = VrrpTimer::Null;
}
crate::instance::State::Backup => {
set_master_down_timer(instance, instance.state.master_down_interval as u64);
set_master_down_timer(
instance,
instance.state.master_down_interval as u64,
);
}
crate::instance::State::Master => {
set_adver_timer(instance, instance.config.advertise_interval as u64)
Expand All @@ -176,37 +179,32 @@ pub(crate) fn set_timer(instance: &mut Instance) {
}

pub(crate) fn set_master_down_timer(instance: &mut Instance, period: u64) {
let timer = TimeoutTask::new(
Duration::from_secs(period),
move || async move {}
);
let timer =
TimeoutTask::new(Duration::from_secs(period), move || async move {});
instance.timer = VrrpTimer::MasterDownTimer(timer);
}

fn set_adver_timer(instance: &mut Instance, period: u64){
fn set_adver_timer(instance: &mut Instance, period: u64) {
let timer = IntervalTask::new(
Duration::from_secs(period),
true,
move || async move {}
move || async move {},
);
instance.timer = VrrpTimer::AdverTimer(timer);
}


pub(crate) fn reset_timer(instance: &mut Instance) {
match instance.timer {
VrrpTimer::AdverTimer(ref mut t) => {
t.reset(
Some(Duration::from_secs(instance.config.advertise_interval as u64)),
);

},
VrrpTimer::MasterDownTimer(ref mut t) => {
t.reset(
Some(Duration::from_secs(instance.state.master_down_interval as u64))
);
},
t.reset(Some(Duration::from_secs(
instance.config.advertise_interval as u64,
)));
}
VrrpTimer::MasterDownTimer(ref mut t) => {
t.reset(Some(Duration::from_secs(
instance.state.master_down_interval as u64,
)));
}
_ => {}
}
}

0 comments on commit a76a0b3

Please sign in to comment.