From 7de282fd075aeb816aaa5cf4433c513e09a15359 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Thu, 16 Jan 2020 15:55:15 -0800 Subject: [PATCH] mux: make Domain::attach an async fn --- Cargo.lock | 12 +++++ Cargo.toml | 1 + src/main.rs | 2 +- src/mux/domain.rs | 10 ++-- src/server/domain.rs | 111 ++++++++++++++++++------------------------ src/ssh.rs | 6 ++- src/termwiztermtab.rs | 6 ++- 7 files changed, 75 insertions(+), 73 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0615259b5..cf3c08dc4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -166,6 +166,17 @@ dependencies = [ "winapi 0.3.8", ] +[[package]] +name = "async-trait" +version = "0.1.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8df72488e87761e772f14ae0c2480396810e51b2c2ade912f97f0f7e5b95e3c" +dependencies = [ + "proc-macro2 1.0.7", + "quote 1.0.2", + "syn 1.0.13", +] + [[package]] name = "atty" version = "0.2.14" @@ -3238,6 +3249,7 @@ dependencies = [ "allsorts", "anyhow", "async-task", + "async-trait", "base64", "base91", "bitflags 1.2.1", diff --git a/Cargo.toml b/Cargo.toml index e1e26b2c2..3b30733a2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,7 @@ embed-resource = "1.3" [dependencies] allsorts = "0.1" async-task = "1.2" +async-trait = "0.1" anyhow = "1.0" thiserror = "1.0" base64 = "0.10" diff --git a/src/main.rs b/src/main.rs index f78762cc7..2db4f973a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -426,7 +426,7 @@ fn run_serial(config: config::ConfigHandle, opts: &SerialCommand) -> anyhow::Res let front_end = opts.front_end.unwrap_or(config.front_end); let gui = front_end.try_new()?; - domain.attach().wait()?; + promise::spawn::block_on(domain.attach())?; // FIXME: blocking let window_id = mux.new_empty_window(); let tab = domain.spawn(PtySize::default(), None, None, window_id)?; diff --git a/src/mux/domain.rs b/src/mux/domain.rs index 57155802b..6d5ff96b5 100644 --- a/src/mux/domain.rs +++ b/src/mux/domain.rs @@ -11,11 +11,11 @@ use crate::mux::tab::Tab; use crate::mux::window::WindowId; use crate::mux::Mux; use anyhow::{bail, Error}; +use async_trait::async_trait; use downcast_rs::{impl_downcast, Downcast}; use log::info; use portable_pty::cmdbuilder::CommandBuilder; use portable_pty::{PtySize, PtySystem}; -use promise::Future; use std::rc::Rc; static DOMAIN_ID: ::std::sync::atomic::AtomicUsize = ::std::sync::atomic::AtomicUsize::new(0); @@ -31,6 +31,7 @@ pub fn alloc_domain_id() -> DomainId { DOMAIN_ID.fetch_add(1, ::std::sync::atomic::Ordering::Relaxed) } +#[async_trait(?Send)] pub trait Domain: Downcast { /// Spawn a new command within this domain fn spawn( @@ -49,7 +50,7 @@ pub trait Domain: Downcast { fn domain_name(&self) -> &str; /// Re-attach to any tabs that might be pre-existing in this domain - fn attach(&self) -> Future<()>; + async fn attach(&self) -> anyhow::Result<()>; /// Detach all tabs fn detach(&self) -> anyhow::Result<()>; @@ -81,6 +82,7 @@ impl LocalDomain { } } +#[async_trait(?Send)] impl Domain for LocalDomain { fn spawn( &self, @@ -126,8 +128,8 @@ impl Domain for LocalDomain { &self.name } - fn attach(&self) -> Future<()> { - Future::ok(()) + async fn attach(&self) -> anyhow::Result<()> { + Ok(()) } fn detach(&self) -> anyhow::Result<()> { diff --git a/src/server/domain.rs b/src/server/domain.rs index 9517cd851..f418bc18f 100644 --- a/src/server/domain.rs +++ b/src/server/domain.rs @@ -9,8 +9,9 @@ use crate::server::client::Client; use crate::server::codec::{GetCodecVersion, ListTabsResponse, Spawn, CODEC_VERSION}; use crate::server::tab::ClientTab; use anyhow::{anyhow, bail}; +use async_trait::async_trait; use portable_pty::{CommandBuilder, PtySize}; -use promise::{Future, Promise}; +use promise::spawn::{join_handle_result, spawn_into_new_thread}; use std::cell::RefCell; use std::collections::HashMap; use std::rc::Rc; @@ -203,6 +204,7 @@ impl ClientDomain { } } +#[async_trait(?Send)] impl Domain for ClientDomain { fn domain_id(&self) -> DomainId { self.local_domain_id @@ -246,76 +248,57 @@ impl Domain for ClientDomain { Ok(tab) } - fn attach(&self) -> Future<()> { + async fn attach(&self) -> anyhow::Result<()> { let domain_id = self.local_domain_id; let config = self.config.clone(); - let mut promise = Promise::new(); - let future = promise.get_future().unwrap(); let activity = crate::frontend::activity::Activity::new(); - std::thread::spawn(move || { - let client = match &config { - ClientDomainConfig::Unix(unix) => { - let initial = true; - Client::new_unix_domain(domain_id, unix, initial) - } - ClientDomainConfig::Tls(tls) => Client::new_tls(domain_id, tls), - ClientDomainConfig::Ssh(ssh) => Client::new_ssh(domain_id, ssh), - }; - - match client { - Err(err) => promise.result(Err(err)), - Ok(client) => { - match client.get_codec_version(GetCodecVersion {}).wait() { - Ok(info) if info.codec_vers == CODEC_VERSION => log::info!( - "Server version is {} (codec version {})", - info.version_string, - info.codec_vers - ), - Ok(info) => { - promise.result(Err(anyhow!( - "Please install the same version of wezterm on both \ - the client and server! \ - The server verson is {} (codec version {}), which is not \ - compatible with our version {} (codec version {}).", - info.version_string, - info.codec_vers, - crate::wezterm_version(), - CODEC_VERSION - ))); - return; - } - Err(err) => { - promise.result(Err(anyhow!( - "Please install the same version of wezterm on both \ - the client and server! \ - The server reported error {} while being asked for its \ - version. This likely means that the server is older \ - than the client.", - err - ))); - return; - } - }; - - let tabs = match client.list_tabs().wait() { - Ok(tabs) => tabs, - Err(err) => { - promise.result(Err(err)); - return; - } - }; - - promise::spawn::spawn_into_main_thread(async move { - promise.result(ClientDomain::finish_attach(domain_id, client, tabs)); - drop(activity); - }); - } + let client = join_handle_result(spawn_into_new_thread(move || match &config { + ClientDomainConfig::Unix(unix) => { + let initial = true; + Client::new_unix_domain(domain_id, unix, initial) } - }); + ClientDomainConfig::Tls(tls) => Client::new_tls(domain_id, tls), + ClientDomainConfig::Ssh(ssh) => Client::new_ssh(domain_id, ssh), + })) + .await?; - future + match client.get_codec_version(GetCodecVersion {}).await { + Ok(info) if info.codec_vers == CODEC_VERSION => log::info!( + "Server version is {} (codec version {})", + info.version_string, + info.codec_vers + ), + Ok(info) => { + bail!( + "Please install the same version of wezterm on both \ + the client and server! \ + The server verson is {} (codec version {}), which is not \ + compatible with our version {} (codec version {}).", + info.version_string, + info.codec_vers, + crate::wezterm_version(), + CODEC_VERSION + ); + } + Err(err) => { + bail!( + "Please install the same version of wezterm on both \ + the client and server! \ + The server reported error {} while being asked for its \ + version. This likely means that the server is older \ + than the client.", + err + ); + } + }; + + let tabs = client.list_tabs().await?; + + ClientDomain::finish_attach(domain_id, client, tabs)?; + drop(activity); + Ok(()) } fn detach(&self) -> anyhow::Result<()> { diff --git a/src/ssh.rs b/src/ssh.rs index 595218902..63a51e3fd 100644 --- a/src/ssh.rs +++ b/src/ssh.rs @@ -5,6 +5,7 @@ use crate::mux::window::WindowId; use crate::mux::Mux; use crate::termwiztermtab; use anyhow::{anyhow, bail, Context, Error}; +use async_trait::async_trait; use portable_pty::cmdbuilder::CommandBuilder; use portable_pty::{PtySize, PtySystem}; use promise::{Future, Promise}; @@ -329,6 +330,7 @@ impl RemoteSshDomain { } } +#[async_trait(?Send)] impl Domain for RemoteSshDomain { fn spawn( &self, @@ -370,8 +372,8 @@ impl Domain for RemoteSshDomain { &self.name } - fn attach(&self) -> promise::Future<()> { - promise::Future::ok(()) + async fn attach(&self) -> anyhow::Result<()> { + Ok(()) } fn detach(&self) -> anyhow::Result<()> { diff --git a/src/termwiztermtab.rs b/src/termwiztermtab.rs index d42865bb0..c822056ed 100644 --- a/src/termwiztermtab.rs +++ b/src/termwiztermtab.rs @@ -12,6 +12,7 @@ use crate::mux::tab::{alloc_tab_id, Tab, TabId}; use crate::mux::window::WindowId; use crate::mux::Mux; use anyhow::{bail, Error}; +use async_trait::async_trait; use crossbeam_channel::{unbounded as channel, Receiver, Sender}; use filedescriptor::Pipe; use portable_pty::*; @@ -146,6 +147,7 @@ impl TermWizTerminalDomain { } } +#[async_trait(?Send)] impl Domain for TermWizTerminalDomain { fn spawn( &self, @@ -164,8 +166,8 @@ impl Domain for TermWizTerminalDomain { fn domain_name(&self) -> &str { "TermWizTerminalDomain" } - fn attach(&self) -> promise::Future<()> { - promise::Future::ok(()) + async fn attach(&self) -> anyhow::Result<()> { + Ok(()) } fn detach(&self) -> anyhow::Result<()> {