Skip to content

Commit

Permalink
Simplify by storing cancel/stop handles in the StopState.
Browse files Browse the repository at this point in the history
  • Loading branch information
martinling committed Aug 29, 2024
1 parent 86761fc commit 31a1759
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 40 deletions.
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ use ui::{
activate,
display_error,
open,
stop_cynthion
stop_operation
};
use version::{version, version_info};

Expand Down Expand Up @@ -112,6 +112,6 @@ fn main() {
}
});
application.run();
display_error(stop_cynthion());
display_error(stop_operation());
}
}
59 changes: 21 additions & 38 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,10 @@ enum FileAction {
Save,
}

#[derive(Copy, Clone, PartialEq)]
enum StopState {
Disabled,
Pcap,
Cynthion,
Pcap(Cancellable),
Cynthion(CynthionStop),
}

struct DeviceSelector {
Expand Down Expand Up @@ -317,7 +316,6 @@ pub struct UserInterface {
pub capture: CaptureReader,
selector: DeviceSelector,
file_name: Option<String>,
stop_handle: Option<CynthionStop>,
stop_state: StopState,
traffic_window: ScrolledWindow,
device_window: ScrolledWindow,
Expand All @@ -326,7 +324,6 @@ pub struct UserInterface {
detail_text: TextBuffer,
endpoint_count: u16,
show_progress: Option<FileAction>,
cancel_handle: Cancellable,
progress_bar: ProgressBar,
separator: Separator,
vbox: gtk::Box,
Expand Down Expand Up @@ -382,17 +379,8 @@ pub fn activate(application: &Application) -> Result<(), Error> {
let save_action = button_action!("save", save_button, choose_file(Save));
let scan_action = button_action!("scan", scan_button, detect_hardware());
let capture_action = button_action!("capture", capture_button, start_cynthion());
let stop_action = ActionEntry::builder("stop")
.activate(|_: &ApplicationWindow, _, _| {
let mut state = StopState::Disabled;
display_error(with_ui(|ui| { state = ui.stop_state; Ok(()) }));
display_error(match state {
StopState::Pcap => stop_pcap(),
StopState::Cynthion => stop_cynthion(),
StopState::Disabled => Ok(()),
});
})
.build();
let stop_action = button_action!("stop", stop_button, stop_operation());

window.add_action_entries([open_action, save_action, scan_action, capture_action, stop_action]);

#[cfg(not(target_os="macos"))]
Expand Down Expand Up @@ -566,7 +554,6 @@ pub fn activate(application: &Application) -> Result<(), Error> {
capture,
selector,
file_name: None,
stop_handle: None,
stop_state: StopState::Disabled,
traffic_window,
device_window,
Expand All @@ -575,7 +562,6 @@ pub fn activate(application: &Application) -> Result<(), Error> {
detail_text,
endpoint_count: 2,
show_progress: None,
cancel_handle: Cancellable::new(),
progress_bar,
separator,
vbox,
Expand Down Expand Up @@ -942,6 +928,7 @@ fn start_pcap(action: FileAction, file: gio::File) -> Result<(), Error> {
None
};
with_ui(|ui| {
let cancel_handle = Cancellable::new();
#[cfg(feature="record-ui-test")]
ui.recording.borrow_mut().log_open_file(
&file.path().context("Cannot record UI test for non-local path")?,
Expand All @@ -952,15 +939,14 @@ fn start_pcap(action: FileAction, file: gio::File) -> Result<(), Error> {
ui.selector.set_sensitive(false);
ui.capture_button.set_sensitive(false);
ui.stop_button.set_sensitive(true);
ui.stop_state = StopState::Pcap;
ui.stop_state = StopState::Pcap(cancel_handle.clone());
ui.vbox.insert_child_after(&ui.separator, Some(&ui.vertical_panes));
ui.vbox.insert_child_after(&ui.progress_bar, Some(&ui.separator));
ui.show_progress = Some(action);
ui.file_name = file
.basename()
.map(|path| path.to_string_lossy().to_string());
let capture = ui.capture.clone();
let cancel_handle = ui.cancel_handle.clone();
let packet_count = capture.packet_index.len();
CURRENT.store(0, Ordering::Relaxed);
TOTAL.store(match action {
Expand Down Expand Up @@ -988,7 +974,6 @@ fn start_pcap(action: FileAction, file: gio::File) -> Result<(), Error> {
STOP.store(false, Ordering::Relaxed);
display_error(
with_ui(|ui| {
ui.cancel_handle = Cancellable::new();
ui.show_progress = None;
ui.vbox.remove(&ui.separator);
ui.vbox.remove(&ui.progress_bar);
Expand Down Expand Up @@ -1072,12 +1057,22 @@ fn save_pcap(file: gio::File,
Ok(())
}

pub fn stop_pcap() -> Result<(), Error> {
STOP.store(true, Ordering::Relaxed);
pub fn stop_operation() -> Result<(), Error> {
with_ui(|ui| {
ui.cancel_handle.cancel();
ui.scan_button.set_sensitive(true);
match std::mem::replace(&mut ui.stop_state, StopState::Disabled) {
StopState::Disabled => {},
StopState::Pcap(cancel_handle) => {
STOP.store(true, Ordering::Relaxed);
cancel_handle.cancel();
},
StopState::Cynthion(stop_handle) => {
stop_handle.stop()?;
ui.scan_button.set_sensitive(true);
}
};
ui.stop_button.set_sensitive(false);
ui.scan_button.set_sensitive(true);
ui.save_button.set_sensitive(true);
Ok(())
})
}
Expand Down Expand Up @@ -1106,13 +1101,12 @@ pub fn start_cynthion() -> Result<(), Error> {
let (cynthion, speed) = ui.selector.open()?;
let (stream_handle, stop_handle) =
cynthion.start(speed, display_error)?;
ui.stop_handle.replace(stop_handle);
ui.open_button.set_sensitive(false);
ui.scan_button.set_sensitive(false);
ui.selector.set_sensitive(false);
ui.capture_button.set_sensitive(false);
ui.stop_button.set_sensitive(true);
ui.stop_state = StopState::Cynthion;
ui.stop_state = StopState::Cynthion(stop_handle);
let read_cynthion = move || {
let mut decoder = Decoder::new(writer)?;
for packet in stream_handle {
Expand Down Expand Up @@ -1143,17 +1137,6 @@ pub fn start_cynthion() -> Result<(), Error> {
})
}

pub fn stop_cynthion() -> Result<(), Error> {
with_ui(|ui| {
if let Some(stop_handle) = ui.stop_handle.take() {
stop_handle.stop()?;
}
ui.scan_button.set_sensitive(true);
ui.save_button.set_sensitive(true);
Ok(())
})
}

fn show_about() -> Result<(), Error> {
const LICENSE: &str = include_str!("../LICENSE");
let about = AboutDialog::builder()
Expand Down

0 comments on commit 31a1759

Please sign in to comment.