feat: embedded daemon

This commit is contained in:
Ilya Zlobintsev
2023-01-05 17:40:29 +02:00
parent 1d89f39e31
commit ad0554fb5c
10 changed files with 65 additions and 13 deletions
+4
View File
@@ -1,3 +1,7 @@
/target
AppDir/
*.glade\~
*.AppImage
*.AppImage.zsync
appimage-build/
*.stignore
Generated
+2
View File
@@ -749,10 +749,12 @@ dependencies = [
"gtk4",
"indexmap",
"lact-client",
"lact-daemon",
"nix",
"once_cell",
"serde",
"serde_json",
"tokio",
"tracing",
"tracing-subscriber",
]
+6 -1
View File
@@ -17,6 +17,7 @@ use tracing::info;
#[derive(Clone)]
pub struct DaemonClient {
stream: Arc<Mutex<(BufReader<UnixStream>, UnixStream)>>,
pub embedded: bool,
}
impl DaemonClient {
@@ -25,10 +26,14 @@ impl DaemonClient {
get_socket_path().context("Could not connect to daemon: socket file not found")?;
info!("Connecting to service at {path:?}");
let stream = UnixStream::connect(path).context("Could not connect to daemon")?;
let reader = BufReader::new(stream.try_clone()?);
Self::from_stream(stream, false)
}
pub fn from_stream(stream: UnixStream, embedded: bool) -> anyhow::Result<Self> {
let reader = BufReader::new(stream.try_clone()?);
Ok(Self {
stream: Arc::new(Mutex::new((reader, stream))),
embedded,
})
}
+16 -1
View File
@@ -5,7 +5,8 @@ mod socket;
use anyhow::Context;
use config::Config;
use server::Server;
use server::{handle_stream, handler::Handler, Server};
use std::os::unix::net::UnixStream as StdUnixStream;
use std::str::FromStr;
use tokio::{runtime, signal::ctrl_c};
use tracing::{debug_span, Instrument, Level};
@@ -40,3 +41,17 @@ pub fn run() -> anyhow::Result<()> {
Ok(())
})
}
pub fn run_embedded(stream: StdUnixStream) -> anyhow::Result<()> {
let rt = runtime::Builder::new_current_thread()
.enable_all()
.build()
.expect("Could not initialize tokio runtime");
rt.block_on(async {
let config = Config::default();
let handler = Handler::new(config).await?;
let stream = stream.try_into()?;
handle_stream(stream, handler).await
})
}
+2 -2
View File
@@ -1,5 +1,5 @@
pub mod gpu_controller;
mod handler;
pub mod handler;
// mod pci;
mod vulkan;
@@ -46,7 +46,7 @@ impl Server {
}
#[instrument(level = "debug", skip(stream, handler))]
async fn handle_stream(stream: UnixStream, handler: Handler) -> anyhow::Result<()> {
pub async fn handle_stream(stream: UnixStream, handler: Handler) -> anyhow::Result<()> {
let mut stream = BufReader::new(stream);
let mut buf = String::new();
+2
View File
@@ -6,6 +6,7 @@ edition = "2021"
[dependencies]
lact-client = { path = "../lact-client" }
lact-daemon = { path = "../lact-daemon" }
gtk = { version = "0.5", package = "gtk4" }
once_cell = "1.17.0"
# pango = "0.16"
@@ -16,3 +17,4 @@ anyhow = "1.0"
serde_json = "1.0"
serde = "1.0"
indexmap = "1.9.2"
tokio = { version = "1", features = ["net"] }
+1 -1
View File
@@ -50,7 +50,7 @@ impl App {
// Inhibit(false)
// });
let root_stack = RootStack::new();
let root_stack = RootStack::new(daemon_client.embedded);
header.set_switcher_stack(&root_stack.container);
+2 -2
View File
@@ -20,7 +20,7 @@ pub struct RootStack {
}
impl RootStack {
pub fn new() -> Self {
pub fn new(embedded_daemon: bool) -> Self {
let container = Stack::builder().vexpand(true).build();
let info_page = InformationPage::new();
@@ -35,7 +35,7 @@ impl RootStack {
container.add_titled(&thermals_page.container, Some("thermals_page"), "Thermals");
let software_page = SoftwarePage::new();
let software_page = SoftwarePage::new(embedded_daemon);
container.add_titled(&software_page.container, Some("software_page"), "Software");
+6 -2
View File
@@ -8,7 +8,7 @@ pub struct SoftwarePage {
}
impl SoftwarePage {
pub fn new() -> Self {
pub fn new(embedded_daemon: bool) -> Self {
let container = Grid::new();
container.set_margin_start(5);
@@ -38,8 +38,12 @@ impl SoftwarePage {
true => "debug",
false => "release",
};
let mut version_text = format!("{lact_version}-{lact_release_type}");
if embedded_daemon {
version_text.push_str(" (embedded)");
}
lact_version_label.set_markup(&format!("<b>{}-{}</b>", lact_version, lact_release_type));
lact_version_label.set_markup(&format!("<b>{version_text}</b>"));
lact_version_label.set_hexpand(true);
lact_version_label.set_halign(Align::Start);
+24 -4
View File
@@ -1,9 +1,10 @@
mod app;
use anyhow::{anyhow, Context};
use anyhow::anyhow;
use app::App;
use lact_client::DaemonClient;
use tracing::metadata::LevelFilter;
use std::os::unix::net::UnixStream;
use tracing::{error, info, metadata::LevelFilter};
use tracing_subscriber::EnvFilter;
pub fn run() -> anyhow::Result<()> {
@@ -16,9 +17,28 @@ pub fn run() -> anyhow::Result<()> {
return Err(anyhow!("Cannot initialize GTK: {err}"));
}
let connection = DaemonClient::connect().context("Could not connect to daemon")?;
let connection = create_connection()?;
let app = App::new(connection);
app.run()
}
fn create_connection() -> anyhow::Result<DaemonClient> {
match DaemonClient::connect() {
Ok(connection) => Ok(connection),
Err(err) => {
info!("Could not connect to socket: {err}");
info!("Using a local daemon");
let (server_stream, client_stream) = UnixStream::pair()?;
std::thread::spawn(move || {
if let Err(err) = lact_daemon::run_embedded(server_stream) {
error!("Builtin daemon error: {err}");
}
});
DaemonClient::from_stream(client_stream, true)
}
}
}