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 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<tokio::task::JoinHandle<()>, ServiceError> {
fn start(&mut self) -> Result<Option<JoinHandle<()>>, ServiceError> {
Err(ServiceError::AlreadyStarted)
}
@ -105,11 +117,46 @@ pub mod Service {
true
}
fn stop(&mut self) -> Result<(), ServiceError> {
fn stop(&mut self) -> Result<Option<JoinHandle<()>>, ServiceError> {
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)
}
}

View File

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