From 57697080a5b2e52b0fff87dc590179609832d97a Mon Sep 17 00:00:00 2001 From: edouard Date: Thu, 16 Dec 2021 16:39:00 +0100 Subject: [PATCH] dummysigner: add low level exit handling - do not rely on iced close_on_exit - listen to window event and ctrl-c signal to exit - drop any connection on exit close #278 --- contrib/tools/dummysigner/Cargo.lock | 11 ++++++++ contrib/tools/dummysigner/Cargo.toml | 2 +- contrib/tools/dummysigner/src/app.rs | 38 ++++++++++++++++++++++++---- 3 files changed, 45 insertions(+), 6 deletions(-) diff --git a/contrib/tools/dummysigner/Cargo.lock b/contrib/tools/dummysigner/Cargo.lock index b527d274..a9351c0a 100644 --- a/contrib/tools/dummysigner/Cargo.lock +++ b/contrib/tools/dummysigner/Cargo.lock @@ -2046,6 +2046,15 @@ dependencies = [ "serde", ] +[[package]] +name = "signal-hook-registry" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e51e73328dc4ac0c7ccbda3a494dfa03df1de2f46018127f60c693f2648455b0" +dependencies = [ + "libc", +] + [[package]] name = "slab" version = "0.4.3" @@ -2228,7 +2237,9 @@ dependencies = [ "memchr", "mio 0.7.13", "num_cpus", + "once_cell", "pin-project-lite", + "signal-hook-registry", "tokio-macros", "winapi 0.3.9", ] diff --git a/contrib/tools/dummysigner/Cargo.toml b/contrib/tools/dummysigner/Cargo.toml index 593a15ba..2782acde 100644 --- a/contrib/tools/dummysigner/Cargo.toml +++ b/contrib/tools/dummysigner/Cargo.toml @@ -17,7 +17,7 @@ iced_futures = "0.3.0" iced_native = "0.4.0" serde = { version = "1.0", features = ["derive"] } serde_json = "1" -tokio = {version = "1.9.0", features = ["net", "io-util"]} +tokio = {version = "1.9.0", features = ["net", "io-util", "signal"]} tokio-util = { version = "0.6", features = ["codec"] } tokio-serde = {version = "0.8", features = ["json"]} toml = "0.5" diff --git a/contrib/tools/dummysigner/src/app.rs b/contrib/tools/dummysigner/src/app.rs index 27276b52..87dd55c8 100644 --- a/contrib/tools/dummysigner/src/app.rs +++ b/contrib/tools/dummysigner/src/app.rs @@ -1,6 +1,7 @@ use std::net::SocketAddr; -use iced::{executor, Application, Clipboard, Command, Element, Settings}; +use iced::{executor, Application, Clipboard, Command, Element, Settings, Subscription}; +use iced_native::{window, Event}; use revault_tx::bitcoin::util::bip32::ExtendedPrivKey; use serde_json::json; @@ -14,11 +15,13 @@ use crate::{ }; pub fn run(cfg: Config) -> iced::Result { - let settings = Settings::with_flags(cfg); + let mut settings = Settings::with_flags(cfg); + settings.exit_on_close_request = false; App::run(settings) } pub struct App { + exit: bool, keys: Vec, signer: sign::Signer, status: AppStatus, @@ -35,6 +38,8 @@ pub enum AppStatus { #[derive(Debug)] pub enum Message { + CtrlC, + Event(Event), Server(server::ServerMessage), View(view::ViewMessage), } @@ -47,6 +52,7 @@ impl Application for App { fn new(cfg: Config) -> (App, Command) { ( App { + exit: false, signer: sign::Signer::new( cfg.descriptors.map(|d| sign::Descriptors { deposit_descriptor: d.deposit_descriptor, @@ -58,7 +64,7 @@ impl Application for App { keys: cfg.keys, status: AppStatus::Waiting, }, - Command::none(), + Command::perform(ctrl_c(), |_| Message::CtrlC), ) } @@ -66,8 +72,17 @@ impl Application for App { String::from("Dummy signer - Revault") } + fn should_exit(&self) -> bool { + self.exit + } + fn update(&mut self, message: Message, _clipboard: &mut Clipboard) -> Command { match message { + Message::CtrlC | Message::Event(Event::Window(window::Event::CloseRequested)) => { + self.exit = true; + self.status = AppStatus::Waiting {}; + Command::none() + } Message::Server(server::ServerMessage::NewConnection(addr, writer)) => { self.status = AppStatus::Connected { addr, @@ -111,7 +126,8 @@ impl Application for App { } Command::none() } - Message::Server(server::ServerMessage::ConnectionDropped) => { + Message::Server(server::ServerMessage::ConnectionDropped) + | Message::Server(server::ServerMessage::Stopped) => { self.status = AppStatus::Waiting {}; Command::none() } @@ -273,7 +289,14 @@ impl Application for App { } fn subscription(&self) -> iced::Subscription { - iced::Subscription::batch(vec![server::listen("0.0.0.0:8080").map(Message::Server)]) + if !self.exit { + Subscription::batch(vec![ + iced_native::subscription::events().map(Message::Event), + server::listen("0.0.0.0:8080").map(Message::Server), + ]) + } else { + Subscription::none() + } } fn view(&mut self) -> Element { @@ -287,6 +310,11 @@ impl Application for App { } } +async fn ctrl_c() -> Result<(), ()> { + tokio::signal::ctrl_c().await.unwrap(); + Ok(()) +} + pub struct Key { name: String, xpriv: ExtendedPrivKey,