use std::{fmt::Debug, sync::Arc};
use wind_tunnel_core::prelude::{DelegatedShutdownListener, ShutdownHandle};
use wind_tunnel_instruments::Reporter;
use crate::executor::Executor;
pub trait UserValuesConstraint: Default + Debug + Send + Sync + 'static {}
#[derive(Debug)]
pub struct RunnerContext<RV: UserValuesConstraint> {
executor: Arc<Executor>,
reporter: Arc<Reporter>,
shutdown_handle: ShutdownHandle,
connection_string: String,
value: RV,
}
impl<RV: UserValuesConstraint> RunnerContext<RV> {
pub(crate) fn new(
executor: Arc<Executor>,
reporter: Arc<Reporter>,
shutdown_handle: ShutdownHandle,
connection_string: String,
) -> Self {
Self {
executor,
reporter,
shutdown_handle,
connection_string,
value: Default::default(),
}
}
pub fn executor(&self) -> &Arc<Executor> {
&self.executor
}
pub fn reporter(&self) -> Arc<Reporter> {
self.reporter.clone()
}
pub fn new_shutdown_listener(&self) -> DelegatedShutdownListener {
self.shutdown_handle.new_listener()
}
pub fn get_connection_string(&self) -> &str {
&self.connection_string
}
pub fn get_mut(&mut self) -> &mut RV {
&mut self.value
}
pub fn get(&self) -> &RV {
&self.value
}
pub fn force_stop_scenario(&self) {
self.shutdown_handle.shutdown();
}
}
#[derive(Debug)]
pub struct AgentContext<RV: UserValuesConstraint, V: UserValuesConstraint> {
agent_id: String,
runner_context: Arc<RunnerContext<RV>>,
shutdown_listener: DelegatedShutdownListener,
value: V,
}
impl<RV: UserValuesConstraint, V: UserValuesConstraint> AgentContext<RV, V> {
pub(crate) fn new(
agent_id: String,
runner_context: Arc<RunnerContext<RV>>,
shutdown_listener: DelegatedShutdownListener,
) -> Self {
Self {
agent_id,
runner_context,
shutdown_listener,
value: Default::default(),
}
}
pub fn agent_id(&self) -> &str {
&self.agent_id
}
pub fn runner_context(&self) -> &Arc<RunnerContext<RV>> {
&self.runner_context
}
pub fn shutdown_listener(&mut self) -> &mut DelegatedShutdownListener {
&mut self.shutdown_listener
}
pub fn get_mut(&mut self) -> &mut V {
&mut self.value
}
pub fn get(&self) -> &V {
&self.value
}
}