Have burrow gtk use new rpc

This commit is contained in:
dav 2024-07-13 14:04:59 -07:00 committed by David Zhong
parent aa634d03e2
commit 90468d5518
10 changed files with 364 additions and 1621 deletions

1903
burrow-gtk/Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -8,10 +8,15 @@ edition = "2021"
[dependencies] [dependencies]
anyhow = "1.0" anyhow = "1.0"
relm4 = { version = "0.6", features = ["libadwaita", "gnome_44"]} relm4 = { version = "0.6", features = ["libadwaita", "gnome_44"]}
burrow = { version = "*", path = "../burrow/" }
tokio = { version = "1.35.0", features = ["time", "sync"] } tokio = { version = "1.35.0", features = ["time", "sync"] }
gettext-rs = { version = "0.7.0", features = ["gettext-system"] } gettext-rs = { version = "0.7.0", features = ["gettext-system"] }
tonic = "0.12"
prost = "0.13"
prost-types = "0.13"
hyper-util = "0.1.6"
tower = "0.4.13"
[build-dependencies] [build-dependencies]
anyhow = "1.0" anyhow = "1.0"
glib-build-tools = "0.18.0" glib-build-tools = "0.18.0"
tonic-build = "0.12"

View file

@ -27,6 +27,5 @@ CFLAGS="-I/usr/local/include -I/usr/include/$MUSL_TARGET -fPIE"
meson setup $BURROW_GTK_BUILD --bindir bin --prefix /usr --buildtype $BURROW_BUILD_TYPE meson setup $BURROW_GTK_BUILD --bindir bin --prefix /usr --buildtype $BURROW_BUILD_TYPE
meson compile -C $BURROW_GTK_BUILD meson compile -C $BURROW_GTK_BUILD
DESTDIR=AppDir meson install -C $BURROW_GTK_BUILD DESTDIR=AppDir meson install -C $BURROW_GTK_BUILD
cargo b --$BURROW_BUILD_TYPE --manifest-path=../Cargo.toml
/tmp/linuxdeploy --appimage-extract-and-run --appdir $BURROW_GTK_BUILD/AppDir -e $BURROW_GTK_BUILD/../../target/$BURROW_BUILD_TYPE/burrow --output appimage /tmp/linuxdeploy --appimage-extract-and-run --appdir $BURROW_GTK_BUILD/AppDir -e $BURROW_GTK_BUILD/../../target/$BURROW_BUILD_TYPE/burrow --output appimage
mv *.AppImage $BURROW_GTK_BUILD mv *.AppImage $BURROW_GTK_BUILD

View file

@ -2,6 +2,7 @@ use anyhow::Result;
fn main() -> Result<()> { fn main() -> Result<()> {
compile_gresources()?; compile_gresources()?;
tonic_build::compile_protos("../proto/burrow.proto")?;
Ok(()) Ok(())
} }

View file

@ -1,11 +1,17 @@
use super::*; use super::*;
use anyhow::Context; use anyhow::Context;
use anyhow::Result;
use hyper_util::rt::TokioIo;
use std::time::Duration; use std::time::Duration;
use tokio::net::UnixStream;
use tonic::transport::{Channel, Endpoint, Uri};
use tower::service_fn;
const BURROW_RPC_SOCKET_PATH: &str = "/run/burrow.sock";
const RECONNECT_POLL_TIME: Duration = Duration::from_secs(5); const RECONNECT_POLL_TIME: Duration = Duration::from_secs(5);
pub struct App { pub struct App {
daemon_client: Arc<Mutex<Option<DaemonClient>>>, daemon_client: Arc<Mutex<Option<Channel>>>,
settings_screen: Controller<settings_screen::SettingsScreen>, settings_screen: Controller<settings_screen::SettingsScreen>,
switch_screen: AsyncController<switch_screen::SwitchScreen>, switch_screen: AsyncController<switch_screen::SwitchScreen>,
} }
@ -58,7 +64,8 @@ impl AsyncComponent for App {
root: Self::Root, root: Self::Root,
sender: AsyncComponentSender<Self>, sender: AsyncComponentSender<Self>,
) -> AsyncComponentParts<Self> { ) -> AsyncComponentParts<Self> {
let daemon_client = Arc::new(Mutex::new(DaemonClient::new().await.ok())); // TODO: RPC REFACTOR (Handle Error)
let daemon_client = Arc::new(Mutex::new(Some(daemon_connect().await.unwrap())));
let switch_screen = switch_screen::SwitchScreen::builder() let switch_screen = switch_screen::SwitchScreen::builder()
.launch(switch_screen::SwitchScreenInit { .launch(switch_screen::SwitchScreenInit {
@ -128,7 +135,8 @@ impl AsyncComponent for App {
let mut disconnected_daemon_client = false; let mut disconnected_daemon_client = false;
if let Some(daemon_client) = daemon_client.as_mut() { if let Some(daemon_client) = daemon_client.as_mut() {
if let Err(_e) = daemon_client.send_command(DaemonCommand::ServerInfo).await { let mut client = tunnel_client::TunnelClient::new(daemon_client);
if let Err(_e) = client.tunnel_status(burrow_rpc::Empty {}).await {
disconnected_daemon_client = true; disconnected_daemon_client = true;
self.switch_screen self.switch_screen
.emit(switch_screen::SwitchScreenMsg::DaemonDisconnect); .emit(switch_screen::SwitchScreenMsg::DaemonDisconnect);
@ -138,7 +146,7 @@ impl AsyncComponent for App {
} }
if disconnected_daemon_client || daemon_client.is_none() { if disconnected_daemon_client || daemon_client.is_none() {
match DaemonClient::new().await { match daemon_connect().await {
Ok(new_daemon_client) => { Ok(new_daemon_client) => {
*daemon_client = Some(new_daemon_client); *daemon_client = Some(new_daemon_client);
self.switch_screen self.switch_screen
@ -155,3 +163,13 @@ impl AsyncComponent for App {
} }
} }
} }
pub async fn daemon_connect() -> Result<Channel> {
Ok(Endpoint::try_from("http://[::]:50051")?
.connect_with_connector(service_fn(|_: Uri| async {
Ok::<_, std::io::Error>(TokioIo::new(
UnixStream::connect(BURROW_RPC_SOCKET_PATH).await?,
))
}))
.await?)
}

View file

@ -1,6 +1,5 @@
use super::*; use super::*;
use adw::prelude::*; use adw::prelude::*;
use burrow::{DaemonClient, DaemonCommand, DaemonResponseData};
use gtk::Align; use gtk::Align;
use relm4::{ use relm4::{
component::{ component::{
@ -12,6 +11,12 @@ use relm4::{
use std::sync::Arc; use std::sync::Arc;
use tokio::sync::Mutex; use tokio::sync::Mutex;
pub mod burrow_rpc {
tonic::include_proto!("burrow");
}
use burrow_rpc::tunnel_client;
use tonic::transport::Channel;
mod app; mod app;
mod settings; mod settings;
mod settings_screen; mod settings_screen;

View file

@ -4,12 +4,12 @@ use std::process::Command;
#[derive(Debug)] #[derive(Debug)]
pub struct DaemonGroup { pub struct DaemonGroup {
system_setup: SystemSetup, system_setup: SystemSetup,
daemon_client: Arc<Mutex<Option<DaemonClient>>>, daemon_client: Arc<Mutex<Option<Channel>>>,
already_running: bool, already_running: bool,
} }
pub struct DaemonGroupInit { pub struct DaemonGroupInit {
pub daemon_client: Arc<Mutex<Option<DaemonClient>>>, pub daemon_client: Arc<Mutex<Option<Channel>>>,
pub system_setup: SystemSetup, pub system_setup: SystemSetup,
} }

View file

@ -2,7 +2,7 @@ use super::*;
#[derive(Debug)] #[derive(Debug)]
pub struct DiagGroup { pub struct DiagGroup {
daemon_client: Arc<Mutex<Option<DaemonClient>>>, daemon_client: Arc<Mutex<Option<Channel>>>,
system_setup: SystemSetup, system_setup: SystemSetup,
service_installed: StatusTernary, service_installed: StatusTernary,
@ -12,12 +12,12 @@ pub struct DiagGroup {
} }
pub struct DiagGroupInit { pub struct DiagGroupInit {
pub daemon_client: Arc<Mutex<Option<DaemonClient>>>, pub daemon_client: Arc<Mutex<Option<Channel>>>,
pub system_setup: SystemSetup, pub system_setup: SystemSetup,
} }
impl DiagGroup { impl DiagGroup {
async fn new(daemon_client: Arc<Mutex<Option<DaemonClient>>>) -> Result<Self> { async fn new(daemon_client: Arc<Mutex<Option<Channel>>>) -> Result<Self> {
let system_setup = SystemSetup::new(); let system_setup = SystemSetup::new();
let daemon_running = daemon_client.lock().await.is_some(); let daemon_running = daemon_client.lock().await.is_some();

View file

@ -7,7 +7,7 @@ pub struct SettingsScreen {
} }
pub struct SettingsScreenInit { pub struct SettingsScreenInit {
pub daemon_client: Arc<Mutex<Option<DaemonClient>>>, pub daemon_client: Arc<Mutex<Option<Channel>>>,
} }
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]

View file

@ -1,14 +1,14 @@
use super::*; use super::*;
pub struct SwitchScreen { pub struct SwitchScreen {
daemon_client: Arc<Mutex<Option<DaemonClient>>>, daemon_client: Arc<Mutex<Option<Channel>>>,
switch: gtk::Switch, switch: gtk::Switch,
switch_screen: gtk::Box, switch_screen: gtk::Box,
disconnected_banner: adw::Banner, disconnected_banner: adw::Banner,
} }
pub struct SwitchScreenInit { pub struct SwitchScreenInit {
pub daemon_client: Arc<Mutex<Option<DaemonClient>>>, pub daemon_client: Arc<Mutex<Option<Channel>>>,
} }
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
@ -76,15 +76,13 @@ impl AsyncComponent for SwitchScreen {
let mut initial_daemon_server_down = false; let mut initial_daemon_server_down = false;
if let Some(daemon_client) = init.daemon_client.lock().await.as_mut() { if let Some(daemon_client) = init.daemon_client.lock().await.as_mut() {
if let Ok(res) = daemon_client let mut client = tunnel_client::TunnelClient::new(daemon_client);
.send_command(DaemonCommand::ServerInfo) if let Ok(res) = client.tunnel_status(burrow_rpc::Empty {}).await.as_mut() {
.await // TODO: RPC REFACTOR (Handle Error)
.as_ref() let res = res.get_mut().message().await.unwrap().unwrap();
{ initial_switch_status = match res.state() {
initial_switch_status = match res.result.as_ref() { burrow_rpc::State::Running => true,
Ok(DaemonResponseData::None) => false, burrow_rpc::State::Stopped => false,
Ok(DaemonResponseData::ServerInfo(_)) => true,
_ => false,
}; };
} else { } else {
initial_daemon_server_down = true; initial_daemon_server_down = true;
@ -123,17 +121,15 @@ impl AsyncComponent for SwitchScreen {
let mut disconnected_daemon_client = false; let mut disconnected_daemon_client = false;
if let Some(daemon_client) = self.daemon_client.lock().await.as_mut() { if let Some(daemon_client) = self.daemon_client.lock().await.as_mut() {
let mut client = tunnel_client::TunnelClient::new(daemon_client);
match msg { match msg {
Self::Input::Start => { Self::Input::Start => {
if let Err(_e) = daemon_client if let Err(_e) = client.tunnel_start(burrow_rpc::Empty {}).await {
.send_command(DaemonCommand::Start(Default::default()))
.await
{
disconnected_daemon_client = true; disconnected_daemon_client = true;
} }
} }
Self::Input::Stop => { Self::Input::Stop => {
if let Err(_e) = daemon_client.send_command(DaemonCommand::Stop).await { if let Err(_e) = client.tunnel_stop(burrow_rpc::Empty {}).await {
disconnected_daemon_client = true; disconnected_daemon_client = true;
} }
} }