diff --git a/sslo_league/src/app_state/slot_mgr/ctrl_interface.rs b/sslo_league/src/app_state/slot_mgr/ctrl_interface.rs deleted file mode 100644 index e69de29..0000000 diff --git a/sslo_league/src/app_state/slot_mgr/slot_ctrl.rs b/sslo_league/src/app_state/slot_mgr/slot_ctrl.rs index cc683c1..48ab2c8 100644 --- a/sslo_league/src/app_state/slot_mgr/slot_ctrl.rs +++ b/sslo_league/src/app_state/slot_mgr/slot_ctrl.rs @@ -1,29 +1,71 @@ +use std::sync::{Arc, Mutex}; use std::thread::JoinHandle; use sslo_lib::error::SsloError; use sslo_lib::ndpc2::collection::NdpcCollection; +use sslo_lib::sync; +use sslo_lib::sync::command::{Commander, Executor}; +use sslo_lib::sync::state::{Reporter, Reviewer}; use crate::app_state::AppState; +use crate::app_state::db2::DatabaseManager; use crate::ndpc::coll_preset::CollPreset; use crate::app_state::shutdown_control::ShutdownControl; +/// possible commands for the slot controller +#[derive(Clone)] +pub enum SlotCtrlCmd { + + /// request to start a new server + StartServer(CollPreset), + + /// request to gracefully end a running server + CloseServer, + + /// forcefully kill a running server + KillServer, +} + +#[derive(Clone)] +pub enum SlotCtrlCmdRsp { + + /// Command has been received and will be executed + Ok, + + /// command cannot be executed for the specified reason + Rejected(String), +} + +#[derive(Clone)] +pub enum SlotCtrlStat { + Initialization, + Execution, +} + + +#[derive(Clone)] pub struct SlotCtrl { - thread_handle: JoinHandle<()>, + thread_handle: Arc>>, + commander: Commander, + reviewer: Reviewer } impl SlotCtrl { pub fn new(slot_id: usize, sc: ShutdownControl, - app_state: AppState, - cmd_rx: SlotCtrlCmdRx, - stat_tx: SlotCtrlStatTx,) -> Result { - let thread_handle = match std::thread::Builder::new() + db: DatabaseManager + ) -> Result { + + // create interface + let (commander, executor) = sync::command::create_channel(); + let (reporter, reviewer) = sync::state::create_channel(); + + let thread_handle = Arc::new(Mutex::new(match std::thread::Builder::new() .name(format!("Slot Controller {}", slot_id)) .spawn(move || { worker(SlotControllerState { slot_id, sc, - app_state, - cmd_rx, - stat_tx, - session: SlotCtrlSessionStat::Idle, + db, + executor, + reporter, }); }) { Ok(handle) => handle, @@ -31,10 +73,12 @@ impl SlotCtrl { log::error!("Failed to start Slot Controller SlotID{}: {}", slot_id, e); return Err(SsloError::ThreadSpawnError(e)) } - }; + })); Ok(Self{ thread_handle, + commander, + reviewer, }) } @@ -47,10 +91,9 @@ impl SlotCtrl { struct SlotControllerState { slot_id: usize, sc: ShutdownControl, - app_state: AppState, - session: SlotCtrlSessionStat, - cmd_rx: SlotCtrlCmdRx, - stat_tx: SlotCtrlStatTx, + db: DatabaseManager, + executor: Executor, + reporter: Reporter, } fn worker(state: SlotControllerState) { diff --git a/sslo_league/src/http/routes_html/slots.rs b/sslo_league/src/http/routes_html/slots.rs index 59f05c4..8612bec 100644 --- a/sslo_league/src/http/routes_html/slots.rs +++ b/sslo_league/src/http/routes_html/slots.rs @@ -41,7 +41,7 @@ pub async fn handler_slot(State(app_state): State, // get slot status let slot_states = app_state.substate().slot_states(); - let slot_status: SlotCtrlStat = match slot_states.into_iter().find(|ss| ss.slot_id == slot_id) { + let slot_status = match slot_states.into_iter().find(|ss| ss.slot_id == slot_id) { None => return Err(StatusCode::NOT_FOUND), Some(ss) => ss, }; @@ -57,13 +57,13 @@ pub async fn handler_slot(State(app_state): State, } // show current active preset - match &slot_status.session { - SlotCtrlSessionStat::Unknown => {}, - SlotCtrlSessionStat::Idle => {}, - SlotCtrlSessionStat::StartingSession(preset) => { html.push_body(&preset.html_view(&db).await) }, - SlotCtrlSessionStat::RunningSession(preset) => {html.push_body(&preset.html_view(&db).await) }, - SlotCtrlSessionStat::EndingSession(preset) => {html.push_body(&preset.html_view(&db).await) }, - } + // match &slot_status.session { + // SlotCtrlSessionStat::Unknown => {}, + // SlotCtrlSessionStat::Idle => {}, + // SlotCtrlSessionStat::StartingSession(preset) => { html.push_body(&preset.html_view(&db).await) }, + // SlotCtrlSessionStat::RunningSession(preset) => {html.push_body(&preset.html_view(&db).await) }, + // SlotCtrlSessionStat::EndingSession(preset) => {html.push_body(&preset.html_view(&db).await) }, + // } html.push_body(&slot_status.html_item().format()); Ok(html.into_response().await)