Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor main loop #436

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod network;
mod os;
#[cfg(test)]
mod tests;
mod threading;

use std::{
collections::HashMap,
Expand Down Expand Up @@ -35,7 +36,7 @@ use simplelog::WriteLogger;
use crate::cli::Opt;
use crate::os::ProcessInfo;

const DISPLAY_DELTA: Duration = Duration::from_millis(1000);
const UPDATE_RATE: Duration = Duration::from_millis(1000);

fn main() -> anyhow::Result<()> {
let opts = Opt::parse();
Expand Down Expand Up @@ -155,8 +156,8 @@ where
}
}
let render_duration = render_start_time.elapsed();
if render_duration < DISPLAY_DELTA {
park_timeout(DISPLAY_DELTA - render_duration);
if render_duration < UPDATE_RATE {
park_timeout(UPDATE_RATE - render_duration);
}
}
if !raw_mode {
Expand Down
50 changes: 50 additions & 0 deletions src/threading/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use std::{io, sync::mpsc::SendError};

use crate::threading::messages::{ClockCmd, DisplayCmd, SnifferCmd, TrackerCmd};

/// Fatal errors that can be encountered by worker threads.
#[derive(Debug, thiserror::Error)]
pub enum ThreadError {
#[error("terminal events iterator terminated unexpectedly")]
TerminalEventsTerminated,
#[error("terminal stop did not complete successfully")]
TerminalStopFail(io::Error),

#[error("all receivers of a ClockCmd channel have hung up")]
ClockCmdSend,
#[error("all receivers of a DisplayCmd channel have hung up")]
DisplayCmdSend,
#[error("all receivers of a TrackerCmd channel have hung up")]
TrackerCmdSend,
#[error("all receivers of a SnifferCmd channel have hung up")]
SnifferCmdSend,

#[error("all senders of a ClockCmd channel have hung up")]
ClockCmdRecv,
#[error("all senders of a DisplayCmd channel have hung up")]
DisplayCmdRecv,
#[error("all senders of a TrackerCmd channel have hung up")]
TrackerCmdRecv,
#[error("all senders of a SnifferCmd channel have hung up")]
SnifferCmdRecv,
}
impl From<SendError<ClockCmd>> for ThreadError {
fn from(_: SendError<ClockCmd>) -> Self {
Self::ClockCmdSend
}
}
impl From<SendError<DisplayCmd>> for ThreadError {
fn from(_: SendError<DisplayCmd>) -> Self {
Self::DisplayCmdSend
}
}
impl From<SendError<TrackerCmd>> for ThreadError {
fn from(_: SendError<TrackerCmd>) -> Self {
Self::TrackerCmdSend
}
}
impl From<SendError<SnifferCmd>> for ThreadError {
fn from(_: SendError<SnifferCmd>) -> Self {
Self::SnifferCmdSend
}
}
45 changes: 45 additions & 0 deletions src/threading/messages.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/// A command sent to the update clock thread.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum ClockCmd {
/// Pause the update clock.
Pause,
/// Unpause the update clock.
Unpause,
/// Stop the thread.
Stop,
}

/// A command sent to the display handler thread.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum DisplayCmd {
/// Rerender the display without updating data.
Refresh,
/// Consume the utilisation data generated since last update.
///
/// Note that this only changes the handler state, but does not trigger a display refresh.
Update,
/// Cycle the order of the tables.
///
/// Note that this only changes the handler state, but does not trigger a display refresh.
CycleTables,
/// Cleanup the display, then stop the thread.
Stop,
}

/// A command sent to the utilisation tracker thread.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum TrackerCmd {
/// Pause data collection.
Pause,
/// Unpause data collection.
Unpause,
/// Stop the thread.
Stop,
}

/// A command sent to sniffer threads.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum SnifferCmd {
/// Stop the thread.
Stop,
}
Loading
Loading