use crate::{
ChannelOptions, Signal,
config::Config,
export::{Channel, Executor, Extender},
package,
scheduler::Runtime,
};
use std::sync::Arc;
use tracing::info;
#[derive(Clone)]
pub struct Engine {
runtime: Arc<Runtime>,
extender: Arc<Extender>,
}
impl Default for Engine {
fn default() -> Self {
Self::new()
}
}
impl Engine {
pub fn new() -> Self {
Self::new_with_config(&Config::default())
}
pub fn config(&self) -> Arc<Config> {
self.runtime.config().clone()
}
pub fn executor(&self) -> Arc<Executor> {
Arc::new(Executor::new(&self.runtime))
}
pub fn channel(&self) -> Arc<Channel> {
Arc::new(Channel::new(&self.runtime))
}
pub fn channel_with_options(&self, matcher: &ChannelOptions) -> Arc<Channel> {
Arc::new(Channel::channel(&self.runtime, matcher))
}
pub fn extender(&self) -> Arc<Extender> {
self.extender.clone()
}
pub(crate) fn runtime(&self) -> Arc<Runtime> {
self.runtime.clone()
}
pub fn close(&self) {
info!("close");
self.runtime.scher().close();
}
pub fn signal<T: Clone>(&self, init: T) -> Signal<T> {
Signal::new(init)
}
pub fn is_running(&self) -> bool {
self.runtime.is_running()
}
fn init(&self) {
info!("init");
self.runtime.init(self);
package::init(self);
}
pub(crate) fn new_with_config(config: &Config) -> Self {
info!("config: {:?}", config);
let runtime = Runtime::new(config);
let extender = Arc::new(Extender::new(&runtime));
Self { runtime, extender }
}
pub fn start(self) -> Self {
self.init();
self
}
}