Add MinecraftService

...and change some other trait impl bits, too
This commit is contained in:
Elias Schriefer 2020-09-14 20:15:22 +02:00
parent 4bcb78164d
commit e25cfe94d2
2 changed files with 67 additions and 15 deletions

View File

@ -1,13 +1,21 @@
use dbus::tree::DataType; use dbus::{
tree::DataType,
Path
};
use std::sync::Arc; 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)] #[allow(non_camel_case_types)]
pub mod traits; pub mod traits;
mod structs; mod structs;
mod errors; mod errors;
pub trait Interface { pub trait Interface {
fn path() -> dbus::Path<'static>; fn path() -> Path<'static>;
} }
#[derive(Clone, Debug, Default)] #[derive(Clone, Debug, Default)]
@ -77,27 +85,31 @@ macro_rules! handle_org_ddnss_sfs_mc_Service {
#[allow(non_snake_case)] #[allow(non_snake_case)]
pub mod Service { pub mod Service {
use super::{
Interface,
JoinHandle, Path, Arc, Command, info, warn
};
pub use super::{ pub use super::{
traits::org_ddnss_sfs_mc_Service, traits::org_ddnss_sfs_mc_Service,
structs::DaemonService, structs::{ DaemonService, MinecraftService },
errors::ServiceError errors::ServiceError
}; };
pub use crate::handle_org_ddnss_sfs_mc_Service as handle; pub use crate::handle_org_ddnss_sfs_mc_Service as handle;
impl super::Interface for DaemonService { impl Interface for DaemonService {
fn path() -> dbus::Path<'static> { fn path() -> Path<'static> {
dbus::Path::new("/").unwrap() Path::new("/").unwrap()
} }
} }
impl Default for DaemonService { impl Interface for MinecraftService {
fn default() -> Self { fn path() -> Path<'static> {
Self Path::new("/minecraft").unwrap()
} }
} }
impl org_ddnss_sfs_mc_Service for DaemonService { impl org_ddnss_sfs_mc_Service for DaemonService {
fn start(&mut self) -> Result<tokio::task::JoinHandle<()>, ServiceError> { fn start(&mut self) -> Result<Option<JoinHandle<()>>, ServiceError> {
Err(ServiceError::AlreadyStarted) Err(ServiceError::AlreadyStarted)
} }
@ -105,11 +117,46 @@ pub mod Service {
true true
} }
fn stop(&mut self) -> Result<(), ServiceError> { fn stop(&mut self) -> Result<Option<JoinHandle<()>>, ServiceError> {
std::process::exit(0); std::process::exit(0);
} }
fn restart(&mut self) -> Result<tokio::task::JoinHandle<()>, ServiceError> { fn restart(&mut self) -> Result<Option<JoinHandle<()>>, ServiceError> {
Err(ServiceError::NoPermission)
}
}
impl org_ddnss_sfs_mc_Service for MinecraftService {
fn start(&mut self) -> Result<Option<JoinHandle<()>>, 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<Option<JoinHandle<()>>, ServiceError> {
Err(ServiceError::NoPermission) Err(ServiceError::NoPermission)
} }
} }

View File

@ -1,2 +1,7 @@
#[derive(Debug)] use std::sync::Arc;
// use tokio::task::JoinHandle;
#[derive(Debug, Default)]
pub struct DaemonService; pub struct DaemonService;
#[derive(Debug, Default)]
pub struct MinecraftService;