Skip to content

Commit

Permalink
dummysigner: add low level exit handling
Browse files Browse the repository at this point in the history
- do not rely on iced close_on_exit
- listen to window event and ctrl-c signal to exit
- drop any connection on exit

close revault#278
  • Loading branch information
edouardparis committed Dec 16, 2021
1 parent 02e480c commit 5769708
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
11 changes: 11 additions & 0 deletions contrib/tools/dummysigner/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion contrib/tools/dummysigner/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
38 changes: 33 additions & 5 deletions contrib/tools/dummysigner/src/app.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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<config::Key>,
signer: sign::Signer,
status: AppStatus,
Expand All @@ -35,6 +38,8 @@ pub enum AppStatus {

#[derive(Debug)]
pub enum Message {
CtrlC,
Event(Event),
Server(server::ServerMessage),
View(view::ViewMessage),
}
Expand All @@ -47,6 +52,7 @@ impl Application for App {
fn new(cfg: Config) -> (App, Command<Message>) {
(
App {
exit: false,
signer: sign::Signer::new(
cfg.descriptors.map(|d| sign::Descriptors {
deposit_descriptor: d.deposit_descriptor,
Expand All @@ -58,16 +64,25 @@ impl Application for App {
keys: cfg.keys,
status: AppStatus::Waiting,
},
Command::none(),
Command::perform(ctrl_c(), |_| Message::CtrlC),
)
}

fn title(&self) -> String {
String::from("Dummy signer - Revault")
}

fn should_exit(&self) -> bool {
self.exit
}

fn update(&mut self, message: Message, _clipboard: &mut Clipboard) -> Command<Message> {
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,
Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -273,7 +289,14 @@ impl Application for App {
}

fn subscription(&self) -> iced::Subscription<Message> {
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<Message> {
Expand All @@ -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,
Expand Down

0 comments on commit 5769708

Please sign in to comment.