implements PLN-0156
This commit is contained in:
@@ -6,6 +6,6 @@ mod window;
|
||||
|
||||
pub use fs::FsFacade;
|
||||
pub use lifecycle::LifecycleFacade;
|
||||
pub use sessions::SessionsFacade;
|
||||
pub use sessions::{SessionError, SessionsFacade};
|
||||
pub use vm::VmFacade;
|
||||
pub use window::WindowFacade;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use crate::CrashReport;
|
||||
use crate::os::LifecycleError;
|
||||
use crate::os::SystemOS;
|
||||
use crate::process::ProcessId;
|
||||
use crate::task::TaskId;
|
||||
use crate::vm_session::{VmSession, VmSessionError};
|
||||
use prometeu_hal::app_mode::AppMode;
|
||||
@@ -14,21 +15,55 @@ pub struct LoadedVmCartridge {
|
||||
pub reused_existing_session: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum SessionError {
|
||||
Lifecycle(LifecycleError),
|
||||
VmSession(VmSessionError),
|
||||
TaskNotFound(TaskId),
|
||||
ProcessNotFound(ProcessId),
|
||||
MissingVmSession(TaskId),
|
||||
Initialization(CrashReport),
|
||||
}
|
||||
|
||||
impl SessionError {
|
||||
pub fn into_crash_report(self, operation: &str) -> CrashReport {
|
||||
match self {
|
||||
Self::Initialization(report) => report,
|
||||
error => {
|
||||
CrashReport::VmPanic { message: format!("{operation} failed: {error:?}"), pc: None }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<LifecycleError> for SessionError {
|
||||
fn from(error: LifecycleError) -> Self {
|
||||
Self::Lifecycle(error)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<VmSessionError> for SessionError {
|
||||
fn from(error: VmSessionError) -> Self {
|
||||
Self::VmSession(error)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct SessionsFacade<'a> {
|
||||
pub(in crate::os) os: &'a mut SystemOS,
|
||||
}
|
||||
|
||||
impl<'a> SessionsFacade<'a> {
|
||||
#[cfg(test)]
|
||||
pub fn create_vm_game_task(&mut self, app_id: u32, title: impl Into<String>) -> TaskId {
|
||||
self.try_create_vm_game_task(app_id, title)
|
||||
.expect("newly created game task should have an associated process")
|
||||
.expect("test/setup VM game creation should succeed")
|
||||
}
|
||||
|
||||
pub fn try_create_vm_game_task(
|
||||
&mut self,
|
||||
app_id: u32,
|
||||
title: impl Into<String>,
|
||||
) -> Result<TaskId, LifecycleError> {
|
||||
) -> Result<TaskId, SessionError> {
|
||||
if let Some(session) = self.os.vm_sessions.resident_game_for_app(app_id) {
|
||||
return Ok(session.task_id);
|
||||
}
|
||||
@@ -37,42 +72,54 @@ impl<'a> SessionsFacade<'a> {
|
||||
let process_id = self.os.process_manager.spawn_vm_game(app_id, title.clone());
|
||||
let task_id = self.os.task_manager.create_game_task(process_id, app_id, title);
|
||||
self.os.lifecycle().set_game_foreground_task(task_id)?;
|
||||
self.create_vm_session_for_task(task_id)
|
||||
.expect("newly created game task should create a VM session");
|
||||
self.try_create_vm_session_for_task(task_id)?;
|
||||
|
||||
Ok(task_id)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub fn create_vm_shell_task(&mut self, app_id: u32, title: impl Into<String>) -> TaskId {
|
||||
self.try_create_vm_shell_task(app_id, title)
|
||||
.expect("test/setup VM shell creation should succeed")
|
||||
}
|
||||
|
||||
pub fn try_create_vm_shell_task(
|
||||
&mut self,
|
||||
app_id: u32,
|
||||
title: impl Into<String>,
|
||||
) -> Result<TaskId, SessionError> {
|
||||
let title = title.into();
|
||||
let process_id = self.os.process_manager.spawn_vm_shell(app_id, title.clone());
|
||||
let task_id = self.os.task_manager.create_shell_task(process_id, app_id, title);
|
||||
self.os
|
||||
.lifecycle()
|
||||
.set_shell_foreground_task(task_id)
|
||||
.expect("newly created shell task should have an associated process");
|
||||
self.create_vm_session_for_task(task_id)
|
||||
.expect("newly created shell task should create a VM session");
|
||||
self.os.lifecycle().set_shell_foreground_task(task_id)?;
|
||||
self.try_create_vm_session_for_task(task_id)?;
|
||||
|
||||
task_id
|
||||
Ok(task_id)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub fn create_native_shell_task(&mut self, app_id: u32, title: impl Into<String>) -> TaskId {
|
||||
self.try_create_native_shell_task(app_id, title)
|
||||
.expect("test/setup native shell creation should succeed")
|
||||
}
|
||||
|
||||
pub fn try_create_native_shell_task(
|
||||
&mut self,
|
||||
app_id: u32,
|
||||
title: impl Into<String>,
|
||||
) -> Result<TaskId, SessionError> {
|
||||
let title = title.into();
|
||||
let process_id = self.os.process_manager.spawn_native_shell(app_id, title.clone());
|
||||
let task_id = self.os.task_manager.create_shell_task(process_id, app_id, title);
|
||||
self.os
|
||||
.lifecycle()
|
||||
.set_shell_foreground_task(task_id)
|
||||
.expect("newly created native shell task should have an associated process");
|
||||
self.os.lifecycle().set_shell_foreground_task(task_id)?;
|
||||
|
||||
task_id
|
||||
Ok(task_id)
|
||||
}
|
||||
|
||||
pub fn load_vm_cartridge(
|
||||
&mut self,
|
||||
cartridge: &Cartridge,
|
||||
) -> Result<LoadedVmCartridge, CrashReport> {
|
||||
) -> Result<LoadedVmCartridge, SessionError> {
|
||||
let existing_game_task = if cartridge.app_mode == AppMode::Game {
|
||||
self.os
|
||||
.vm_sessions
|
||||
@@ -85,19 +132,15 @@ impl<'a> SessionsFacade<'a> {
|
||||
let (task_id, reused_existing_session) = if let Some(task_id) = existing_game_task {
|
||||
(task_id, true)
|
||||
} else if cartridge.app_mode == AppMode::Shell {
|
||||
(self.create_vm_shell_task(cartridge.app_id, cartridge.title.clone()), false)
|
||||
(self.try_create_vm_shell_task(cartridge.app_id, cartridge.title.clone())?, false)
|
||||
} else {
|
||||
let task_id = self
|
||||
.try_create_vm_game_task(cartridge.app_id, cartridge.title.clone())
|
||||
.map_err(|error| CrashReport::VmPanic {
|
||||
message: format!("failed to create VM game task: {error:?}"),
|
||||
pc: None,
|
||||
})?;
|
||||
let task_id =
|
||||
self.try_create_vm_game_task(cartridge.app_id, cartridge.title.clone())?;
|
||||
(task_id, false)
|
||||
};
|
||||
|
||||
if !reused_existing_session {
|
||||
self.initialize_session_cartridge(task_id, cartridge)?;
|
||||
self.try_initialize_session_cartridge(task_id, cartridge)?;
|
||||
}
|
||||
|
||||
Ok(LoadedVmCartridge { task_id, app_mode: cartridge.app_mode, reused_existing_session })
|
||||
@@ -123,13 +166,14 @@ impl<'a> SessionsFacade<'a> {
|
||||
self.os.vm_sessions.clear();
|
||||
}
|
||||
|
||||
fn create_vm_session_for_task(&mut self, task_id: TaskId) -> Result<(), VmSessionError> {
|
||||
let task = self.os.task_manager.get(task_id).expect("session task should exist").clone();
|
||||
pub fn try_create_vm_session_for_task(&mut self, task_id: TaskId) -> Result<(), SessionError> {
|
||||
let task =
|
||||
self.os.task_manager.get(task_id).ok_or(SessionError::TaskNotFound(task_id))?.clone();
|
||||
let process = self
|
||||
.os
|
||||
.process_manager
|
||||
.get(task.process_id)
|
||||
.expect("session process should exist")
|
||||
.ok_or(SessionError::ProcessNotFound(task.process_id))?
|
||||
.clone();
|
||||
|
||||
let cap_config = self.os.vm_runtime.certifier.config;
|
||||
@@ -138,24 +182,28 @@ impl<'a> SessionsFacade<'a> {
|
||||
.vm_sessions
|
||||
.create_for_task(&task, &process, "", Some(cap_config), logs_count)
|
||||
.map(|_| ())
|
||||
.map_err(SessionError::from)
|
||||
}
|
||||
|
||||
fn initialize_session_cartridge(
|
||||
pub fn try_initialize_session_cartridge(
|
||||
&mut self,
|
||||
task_id: TaskId,
|
||||
cartridge: &Cartridge,
|
||||
) -> Result<(), CrashReport> {
|
||||
) -> Result<(), SessionError> {
|
||||
let session = self
|
||||
.os
|
||||
.vm_sessions
|
||||
.for_task_mut(task_id)
|
||||
.expect("loaded VM task should have a VM session");
|
||||
.ok_or(SessionError::MissingVmSession(task_id))?;
|
||||
|
||||
session.app_id = cartridge.app_id;
|
||||
session.title = cartridge.title.clone();
|
||||
session.app_version = cartridge.app_version.clone();
|
||||
session.app_mode = cartridge.app_mode;
|
||||
session.clear_cartridge_service_state();
|
||||
session.runtime.initialize_vm(&mut self.os.log_service, &mut session.vm, cartridge)
|
||||
session
|
||||
.runtime
|
||||
.initialize_vm(&mut self.os.log_service, &mut session.vm, cartridge)
|
||||
.map_err(SessionError::Initialization)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,9 @@ mod facades;
|
||||
mod lifecycle;
|
||||
mod system_os;
|
||||
|
||||
pub use facades::{FsFacade, LifecycleFacade, SessionsFacade, VmFacade, WindowFacade};
|
||||
pub use facades::{
|
||||
FsFacade, LifecycleFacade, SessionError, SessionsFacade, VmFacade, WindowFacade,
|
||||
};
|
||||
pub use lifecycle::{
|
||||
DEFAULT_GAME_PAUSE_BUDGET_TICKS, GameLifecycleEvent, GameLifecycleEventKind, LifecycleError,
|
||||
LifecycleOperation,
|
||||
|
||||
@@ -143,17 +143,33 @@ impl SystemOS {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::os::{GameLifecycleEventKind, LifecycleError, LifecycleOperation};
|
||||
use crate::process::{ProcessKind, ProcessState};
|
||||
use crate::os::{GameLifecycleEventKind, LifecycleError, LifecycleOperation, SessionError};
|
||||
use crate::process::{ProcessId, ProcessKind, ProcessState};
|
||||
use crate::services::foreground::{ForegroundOwner, ResidentGame, ResidentGameState};
|
||||
use crate::task::{TaskId, TaskState};
|
||||
use prometeu_hal::app_mode::AppMode;
|
||||
use prometeu_hal::cartridge::{AssetsPayloadSource, Cartridge};
|
||||
use prometeu_hal::syscalls::caps;
|
||||
|
||||
fn process_state_for_task(os: &SystemOS, task_id: TaskId) -> ProcessState {
|
||||
let task = os.task_manager.get(task_id).expect("task should exist");
|
||||
os.process_manager.get(task.process_id).expect("process should exist").state
|
||||
}
|
||||
|
||||
fn session_test_cartridge(app_mode: AppMode, app_id: u32, title: &str) -> Cartridge {
|
||||
Cartridge {
|
||||
app_id,
|
||||
title: title.to_string(),
|
||||
app_version: "1.0.0".to_string(),
|
||||
app_mode,
|
||||
capabilities: caps::NONE,
|
||||
program: vec![],
|
||||
assets: AssetsPayloadSource::empty(),
|
||||
asset_table: vec![],
|
||||
preload: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_foreground_task_marks_task_and_process_running() {
|
||||
let mut os = SystemOS::new(None);
|
||||
@@ -453,6 +469,60 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn vm_shell_load_returns_typed_error_when_foreground_shell_exists() {
|
||||
let mut os = SystemOS::new(None);
|
||||
let first = os.sessions().create_vm_shell_task(7, "Settings");
|
||||
let cartridge = session_test_cartridge(AppMode::Shell, 8, "Files");
|
||||
|
||||
let error = os.sessions().load_vm_cartridge(&cartridge).expect_err("load should fail");
|
||||
|
||||
match error {
|
||||
SessionError::Lifecycle(LifecycleError::ForegroundShellAlreadyExists {
|
||||
existing,
|
||||
requested,
|
||||
}) => {
|
||||
assert_eq!(existing, first);
|
||||
assert_ne!(requested, first);
|
||||
}
|
||||
other => panic!("expected foreground shell lifecycle error, got {:?}", other),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn vm_session_creation_returns_typed_error_for_missing_task() {
|
||||
let mut os = SystemOS::new(None);
|
||||
|
||||
assert_eq!(
|
||||
os.sessions().try_create_vm_session_for_task(TaskId(999)),
|
||||
Err(SessionError::TaskNotFound(TaskId(999)))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn vm_session_creation_returns_typed_error_for_missing_process() {
|
||||
let mut os = SystemOS::new(None);
|
||||
let task_id = os.task_manager.create_game_task(ProcessId(999), 42, "Broken");
|
||||
|
||||
assert_eq!(
|
||||
os.sessions().try_create_vm_session_for_task(task_id),
|
||||
Err(SessionError::ProcessNotFound(ProcessId(999)))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn session_cartridge_initialization_returns_typed_error_for_missing_session() {
|
||||
let mut os = SystemOS::new(None);
|
||||
let process_id = os.process_manager.spawn_vm_game(42, "Sector Crawl");
|
||||
let task_id = os.task_manager.create_game_task(process_id, 42, "Sector Crawl");
|
||||
let cartridge = session_test_cartridge(AppMode::Game, 42, "Sector Crawl");
|
||||
|
||||
assert_eq!(
|
||||
os.sessions().try_initialize_session_cartridge(task_id, &cartridge),
|
||||
Err(SessionError::MissingVmSession(task_id))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn closing_foreground_shell_returns_to_hub() {
|
||||
let mut os = SystemOS::new(None);
|
||||
|
||||
Reference in New Issue
Block a user