work on slot mgr/ctrl [refs #24]
This commit is contained in:
parent
371db8141b
commit
2af8ff5946
3 changed files with 65 additions and 22 deletions
|
|
@ -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<Mutex<JoinHandle<()>>>,
|
||||
commander: Commander<SlotCtrlCmd, SlotCtrlCmdRsp>,
|
||||
reviewer: Reviewer<SlotCtrlStat>
|
||||
}
|
||||
impl SlotCtrl {
|
||||
pub fn new(slot_id: usize,
|
||||
sc: ShutdownControl,
|
||||
app_state: AppState,
|
||||
cmd_rx: SlotCtrlCmdRx,
|
||||
stat_tx: SlotCtrlStatTx,) -> Result<Self, SsloError> {
|
||||
let thread_handle = match std::thread::Builder::new()
|
||||
db: DatabaseManager
|
||||
) -> Result<Self, SsloError> {
|
||||
|
||||
// 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<SlotCtrlCmd, SlotCtrlCmdRsp>,
|
||||
reporter: Reporter<SlotCtrlStat>,
|
||||
}
|
||||
|
||||
fn worker(state: SlotControllerState) {
|
||||
|
|
|
|||
|
|
@ -41,7 +41,7 @@ pub async fn handler_slot(State(app_state): State<AppState>,
|
|||
|
||||
// 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<AppState>,
|
|||
}
|
||||
|
||||
// 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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue