mod termination;
use std::{
ops::ControlFlow,
sync::{Arc, atomic::AtomicBool},
time::{Duration, Instant},
};
pub use self::termination::VmiTerminationHandler;
use crate::{VmiContext, VmiEventResponse, VmiOs, VmiSession};
pub trait VmiHandler<Os>
where
Os: VmiOs,
{
type Output;
fn handle_event(&mut self, event: VmiContext<Os>) -> VmiEventResponse<Os::Architecture>;
fn handle_timeout(&mut self, _session: &VmiSession<Os>) {}
fn handle_interrupted(&mut self, _session: &VmiSession<Os>) -> ControlFlow<()> {
ControlFlow::Break(())
}
fn cleanup(&mut self, _session: &VmiSession<Os>) {}
fn poll(&mut self) -> Option<Self::Output> {
None
}
}
pub trait VmiHandlerExt<Os>: VmiHandler<Os> + Sized
where
Os: VmiOs,
{
fn with_deadline(self, deadline: Instant) -> VmiTerminationHandler<Self> {
VmiTerminationHandler::new(self).with_deadline(deadline)
}
fn with_termination_flag(
self,
termination_flag: Arc<AtomicBool>,
) -> VmiTerminationHandler<Self> {
VmiTerminationHandler::new(self).with_termination_flag(termination_flag)
}
fn with_termination_timeout(
self,
termination_timeout: Duration,
) -> VmiTerminationHandler<Self> {
VmiTerminationHandler::new(self).with_termination_timeout(termination_timeout)
}
}
impl<Os, Handler> VmiHandlerExt<Os> for Handler
where
Os: VmiOs,
Handler: VmiHandler<Os>,
{
}