diff --git a/sfsmcd/src/ifaces/mod.rs b/sfsmcd/src/ifaces/mod.rs index 583fb58..878a6bf 100644 --- a/sfsmcd/src/ifaces/mod.rs +++ b/sfsmcd/src/ifaces/mod.rs @@ -1,13 +1,21 @@ -use dbus::tree::DataType; +use dbus::{ + tree::DataType, + Path +}; use std::sync::Arc; -use tokio::sync::RwLock; +use tokio::{ + sync::RwLock, + task::JoinHandle, + process::Command +}; +use log::{ info, warn }; #[allow(non_camel_case_types)] pub mod traits; mod structs; mod errors; pub trait Interface { - fn path() -> dbus::Path<'static>; + fn path() -> Path<'static>; } #[derive(Clone, Debug, Default)] @@ -77,27 +85,31 @@ macro_rules! handle_org_ddnss_sfs_mc_Service { #[allow(non_snake_case)] pub mod Service { + use super::{ + Interface, + JoinHandle, Path, Arc, Command, info, warn + }; pub use super::{ traits::org_ddnss_sfs_mc_Service, - structs::DaemonService, + structs::{ DaemonService, MinecraftService }, errors::ServiceError }; pub use crate::handle_org_ddnss_sfs_mc_Service as handle; - impl super::Interface for DaemonService { - fn path() -> dbus::Path<'static> { - dbus::Path::new("/").unwrap() + impl Interface for DaemonService { + fn path() -> Path<'static> { + Path::new("/").unwrap() } } - impl Default for DaemonService { - fn default() -> Self { - Self + impl Interface for MinecraftService { + fn path() -> Path<'static> { + Path::new("/minecraft").unwrap() } } impl org_ddnss_sfs_mc_Service for DaemonService { - fn start(&mut self) -> Result, ServiceError> { + fn start(&mut self) -> Result>, ServiceError> { Err(ServiceError::AlreadyStarted) } @@ -105,11 +117,46 @@ pub mod Service { true } - fn stop(&mut self) -> Result<(), ServiceError> { + fn stop(&mut self) -> Result>, ServiceError> { std::process::exit(0); } - fn restart(&mut self) -> Result, ServiceError> { + fn restart(&mut self) -> Result>, ServiceError> { + Err(ServiceError::NoPermission) + } + } + + impl org_ddnss_sfs_mc_Service for MinecraftService { + fn start(&mut self) -> Result>, ServiceError> { + if !self.is_running() { + // Configure the command + std::process::Command::new("screen") + .current_dir("/media/games/bukkit") + .args(r#"-dmS sfs-mc -t sfs-mc "java -server -Xmx2G -jar server.jar""#.split_whitespace()) + // Handle spawning + .spawn() + .map_err(|err| ServiceError::IOError(err))? + // Wait for execution + .wait() + .map(|status| info!("Started minecraft server {}successfully", if !status.success() { "un" } else { "" })) + .map_err(|err| warn!("Couldn't start minecraft server successfully ({})", err)) + .or(Ok(()))?; + Ok(None) + } else { + Err(ServiceError::AlreadyStarted) + } + } + + fn is_running(&self) -> bool { + std::process::Command::new("screen") + .args("-rqx sfs-mc -t sfs-mc -Q title".split_whitespace()) + .output() + .unwrap() + .status + .success() + } + + fn stop(&mut self) -> Result>, ServiceError> { Err(ServiceError::NoPermission) } } diff --git a/sfsmcd/src/ifaces/structs.rs b/sfsmcd/src/ifaces/structs.rs index 3e0384e..270e312 100644 --- a/sfsmcd/src/ifaces/structs.rs +++ b/sfsmcd/src/ifaces/structs.rs @@ -1,2 +1,7 @@ -#[derive(Debug)] -pub struct DaemonService; \ No newline at end of file +use std::sync::Arc; +// use tokio::task::JoinHandle; + +#[derive(Debug, Default)] +pub struct DaemonService; +#[derive(Debug, Default)] +pub struct MinecraftService; \ No newline at end of file