Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): abstract everything in core
Feels right todo it this way. more feature guard needs to be added though.
- Loading branch information
Showing
16 changed files
with
175 additions
and
171 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,121 @@ | ||
use crate::constants::{DAEMON_BINARY, DAEMON_SOCKET_PATH}; | ||
use anyhow::Result; | ||
use std::io::Write; | ||
use std::net::Shutdown; | ||
use std::os::unix::net::UnixStream; | ||
use std::process::Command; | ||
|
||
/// Check if Unix socket is running | ||
pub fn is_running() -> bool { | ||
match UnixStream::connect(DAEMON_SOCKET_PATH) { | ||
Ok(s) => s.shutdown(Shutdown::Both).ok().is_some(), | ||
Err(_) => false, | ||
use anyhow::{bail, Context, Result}; | ||
|
||
pub struct Daemon { | ||
state: std::sync::Arc<tokio::sync::Mutex<crate::state::State>>, | ||
listener: tokio::net::UnixListener, | ||
} | ||
|
||
impl Daemon { | ||
pub fn default() -> Self { | ||
if std::fs::metadata(DAEMON_SOCKET_PATH).is_ok() { | ||
std::fs::remove_file(DAEMON_SOCKET_PATH).ok(); | ||
} | ||
|
||
tracing::info!("Started"); | ||
|
||
Self { | ||
state: Default::default(), | ||
listener: tokio::net::UnixListener::bind(DAEMON_SOCKET_PATH).unwrap(), | ||
} | ||
} | ||
|
||
/// Run Main daemon server loop | ||
pub async fn run(&mut self) -> ! { | ||
use tokio::io::AsyncReadExt; | ||
|
||
loop { | ||
let state = self.state.clone(); | ||
let (mut s, _) = self.listener.accept().await.unwrap(); | ||
tokio::spawn(async move { | ||
// let mut current_state = state.lock().await; | ||
// current_state.update_clients(); | ||
|
||
// trace!("Current State: {:?}", state.lock().await) | ||
let mut string = String::default(); | ||
|
||
if let Err(e) = s.read_to_string(&mut string).await { | ||
tracing::error!("[Read Error]: {:?}", e); | ||
return; | ||
}; | ||
|
||
if string.len() == 0 { | ||
return; | ||
} | ||
|
||
let msg = crate::Command::parse(string.as_str().trim()); | ||
|
||
if let Err(e) = msg { | ||
tracing::error!("[Parse Error]: {:?}", e); | ||
return; | ||
}; | ||
|
||
let msg = msg.unwrap(); | ||
if let Err(e) = msg.handle(state.clone()).await { | ||
tracing::error!("[Handling Error]: {:?}", e); | ||
return; | ||
}; | ||
|
||
crate::watch::update(state, msg).await; | ||
}); | ||
} | ||
} | ||
} | ||
|
||
/// Spawn new instance of the server via running binaray is a child process | ||
pub fn spawn() -> Result<()> { | ||
Command::new(DAEMON_BINARY).spawn()?; | ||
Ok(()) | ||
impl Daemon { | ||
/// Spawn new instance of the server via running binaray is a child process | ||
pub fn spawn() -> Result<()> { | ||
std::process::Command::new(DAEMON_BINARY) | ||
.spawn() | ||
.context("Unable to start background instance using daemon binaray") | ||
.map(|_| ()) | ||
} | ||
|
||
/// Pass args to running daemon | ||
pub fn execute(args: &[&str]) -> Result<()> { | ||
use std::io::Write; | ||
match std::os::unix::net::UnixStream::connect(DAEMON_SOCKET_PATH) { | ||
Ok(mut stream) => { | ||
stream.write_all(args.join(" ").as_str().as_ref())?; | ||
stream.flush().context("Fail to flush stream!") | ||
} | ||
Err(e) => bail!("Fail to execute {:#?}: {e}", args), | ||
} | ||
} | ||
} | ||
|
||
/// Execute argument in the runtime | ||
pub fn execute(args: &[&str]) -> Result<()> { | ||
let mut stream = UnixStream::connect(DAEMON_SOCKET_PATH)?; | ||
stream.write_all(args.join(" ").as_str().as_ref())?; | ||
stream.flush()?; | ||
Ok(()) | ||
#[cfg(feature = "lua")] | ||
use crate::mlua::LuaExtension; | ||
|
||
#[cfg(feature = "lua")] | ||
impl Daemon { | ||
/// Representation of daemon table in lua | ||
pub fn lua(lua: &mlua::Lua) -> mlua::Result<mlua::Table> { | ||
let table = lua.create_table()?; | ||
table.set("is_running", lua.create_function(Self::is_running)?)?; | ||
table.set("ensure", lua.create_function(Self::ensure)?)?; | ||
table.set("register", lua.create_function(crate::Register::lua)?)?; | ||
table.set("drop", lua.create_function(crate::Drop::lua)?)?; | ||
Ok(table) | ||
} | ||
|
||
/// Check if Daemon is running | ||
pub fn is_running(_: &mlua::Lua, _: ()) -> mlua::Result<bool> { | ||
match std::os::unix::net::UnixStream::connect(DAEMON_SOCKET_PATH) { | ||
Ok(stream) => Ok(stream.shutdown(std::net::Shutdown::Both).ok().is_some()), | ||
Err(_) => Ok(false), | ||
} | ||
} | ||
|
||
/// Ensure that daemon is currently running in background | ||
pub fn ensure(lua: &mlua::Lua, _: ()) -> mlua::Result<bool> { | ||
if Self::is_running(lua, ()).unwrap() { | ||
Ok(false) | ||
} else if Self::spawn().is_ok() { | ||
lua.info("Spawned Background Server")?; | ||
Ok(true) | ||
} else { | ||
panic!("Unable to spawn background server"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.