dscale 0.7.1

A fast & deterministic simulation framework for benchmarking and testing distributed systems
Documentation
// DScale: deterministic distributed systems simulator
// Copyright (C) 2026  Konstantin Shprenger

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

use crate::Pid;

#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) enum LinkState {
    Broken,
    Alive,
}

pub(crate) struct FaultState {
    link_matrix: Vec<Vec<LinkState>>,
}

impl FaultState {
    pub(crate) fn new(size: usize) -> Self {
        Self {
            link_matrix: vec![vec![LinkState::Alive; size]; size],
        }
    }

    pub(crate) fn break_link(&mut self, pid1: Pid, pid2: Pid) {
        self.sym_change_state(pid1, pid2, LinkState::Broken);
    }

    pub(crate) fn isolate(&mut self, pid: Pid) {
        for other in 0..self.link_matrix.len() {
            self.break_link(pid, other);
        }
    }

    pub(crate) fn restore(&mut self, pid: Pid) {
        for other in 0..self.link_matrix.len() {
            self.restore_link(pid, other);
        }
    }

    pub(crate) fn restore_link(&mut self, pid1: Pid, pid2: Pid) {
        self.sym_change_state(pid1, pid2, LinkState::Alive);
    }

    pub(crate) fn is_link_broken(&self, pid1: Pid, pid2: Pid) -> bool {
        self.link_matrix[pid1][pid2] == LinkState::Broken
    }

    fn sym_change_state(&mut self, pid1: Pid, pid2: Pid, state: LinkState) {
        self.link_matrix[pid1][pid2] = state;
        self.link_matrix[pid2][pid1] = state;
    }
}