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

View file

@ -1,11 +1,17 @@
use super::*;
use anyhow::Context;
use anyhow::Result;
use hyper_util::rt::TokioIo;
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);
pub struct App {
daemon_client: Arc<Mutex<Option<DaemonClient>>>,
daemon_client: Arc<Mutex<Option<Channel>>>,
settings_screen: Controller<settings_screen::SettingsScreen>,
switch_screen: AsyncController<switch_screen::SwitchScreen>,
}
@ -58,7 +64,8 @@ impl AsyncComponent for App {
root: Self::Root,
sender: AsyncComponentSender<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()
.launch(switch_screen::SwitchScreenInit {
@ -128,7 +135,8 @@ impl AsyncComponent for App {
let mut disconnected_daemon_client = false;
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;
self.switch_screen
.emit(switch_screen::SwitchScreenMsg::DaemonDisconnect);
@ -138,7 +146,7 @@ impl AsyncComponent for App {
}
if disconnected_daemon_client || daemon_client.is_none() {
match DaemonClient::new().await {
match daemon_connect().await {
Ok(new_daemon_client) => {
*daemon_client = Some(new_daemon_client);
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 adw::prelude::*;
use burrow::{DaemonClient, DaemonCommand, DaemonResponseData};
use gtk::Align;
use relm4::{
component::{
@ -12,6 +11,12 @@ use relm4::{
use std::sync::Arc;
use tokio::sync::Mutex;
pub mod burrow_rpc {
tonic::include_proto!("burrow");
}
use burrow_rpc::tunnel_client;
use tonic::transport::Channel;
mod app;
mod settings;
mod settings_screen;

View file

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

View file

@ -2,7 +2,7 @@ use super::*;
#[derive(Debug)]
pub struct DiagGroup {
daemon_client: Arc<Mutex<Option<DaemonClient>>>,
daemon_client: Arc<Mutex<Option<Channel>>>,
system_setup: SystemSetup,
service_installed: StatusTernary,
@ -12,12 +12,12 @@ pub struct DiagGroup {
}
pub struct DiagGroupInit {
pub daemon_client: Arc<Mutex<Option<DaemonClient>>>,
pub daemon_client: Arc<Mutex<Option<Channel>>>,
pub system_setup: SystemSetup,
}
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 daemon_running = daemon_client.lock().await.is_some();

View file

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

View file

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