use crate::{
channel::mpsc::create_unbounded_channel,
network::{
block_handler::{BlockHandler, create_incoming_sections_channel},
com_hub::{ComHub, ComInterfaceReconnectRequest},
},
prelude::*,
runtime::{
CustomRuntimeInfo, Runtime, RuntimeConfig, RuntimeInternal,
cache::shared_references_cache::SharedReferencesCache,
pointer_address_provider::SelfOwnedPointerAddressProvider,
},
time::now_ms,
utils::task_manager::TaskManager,
};
use async_select::select;
use core::{cell::RefCell, pin::Pin};
use futures::channel::oneshot;
use futures_util::{
future,
future::{Join, join},
join,
};
use log::info;
pub struct RuntimeRunner {
pub runtime: Runtime,
pub task_future: Pin<Box<dyn Future<Output = ()>>>,
}
impl RuntimeRunner {
pub fn new(
config: RuntimeConfig,
info: CustomRuntimeInfo,
) -> RuntimeRunner {
let endpoint = config.endpoint.clone();
let (task_manager, runtime_task_future) = TaskManager::create();
let (incoming_sections_sender, incoming_sections_receiver) =
create_incoming_sections_channel();
let (
com_interface_reconnect_request_sender,
com_interface_reconnect_request_receiver,
) = create_unbounded_channel::<ComInterfaceReconnectRequest>();
let (com_hub, com_hub_task_future) = ComHub::create(
endpoint.clone(),
incoming_sections_sender,
com_interface_reconnect_request_sender,
BlockHandler::DEFAULT_HISTORY_CAPACITY,
);
let memory = RefCell::new(SharedReferencesCache::default());
let pointer_address_provider = Rc::new(RefCell::new(
SelfOwnedPointerAddressProvider::new(endpoint.clone()),
));
let runtime = Runtime::new(RuntimeInternal::new(
endpoint,
memory,
pointer_address_provider,
config,
com_hub.clone(),
task_manager,
incoming_sections_receiver,
com_interface_reconnect_request_receiver,
info,
));
*com_hub.runtime.borrow_mut() = Some(runtime.clone());
let runtime_internal = runtime.internal.clone();
let task_future = async {
join!(
com_hub_task_future,
runtime_task_future,
runtime_internal.clone().handle_incoming_sections_task(),
runtime_internal.handle_reconnect_requests_task(),
);
};
RuntimeRunner {
runtime,
task_future: Box::pin(task_future),
}
}
pub async fn run<AppReturn, AppFuture>(
self,
app_logic: impl FnOnce(Runtime) -> AppFuture,
) -> AppReturn
where
AppFuture: Future<Output = AppReturn>,
{
let (runtime_future, app_future) =
self.create_runtime_and_app_future(app_logic);
select! {
_ = runtime_future => {
unreachable!("Runtime task future exited unexpectedly");
},
exit_value = app_future => {
exit_value
},
}
}
pub async fn run_forever<AppReturn, AppFuture>(
self,
app_logic: impl FnOnce(Runtime) -> AppFuture,
) -> !
where
AppFuture: Future<Output = AppReturn>,
{
let (runtime_future, app_future) =
self.create_runtime_and_app_future(app_logic);
future::join(runtime_future, app_future).await;
unreachable!("Both runtime and app logic futures exited unexpectedly");
}
fn create_runtime_and_app_future<AppReturn, AppFuture>(
self,
app_logic: impl FnOnce(Runtime) -> AppFuture,
) -> (
Join<impl Future<Output = ()>, impl Future<Output = ()>>,
impl Future<Output = AppReturn>,
)
where
AppFuture: Future<Output = AppReturn>,
{
let (init_ready_sender, init_ready_receiver) = oneshot::channel();
let runtime = self.runtime.clone();
let version = runtime.version().to_string();
let runtime_future = join(
async move {
runtime.internal.init_async().await;
init_ready_sender.send(()).unwrap();
},
self.task_future,
);
let app_future = async move {
init_ready_receiver.await.unwrap();
info!("Runtime initialized - Version {version} Time: {}", now_ms());
app_logic(self.runtime).await
};
(runtime_future, app_future)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn runtime_runner() {
let runner = RuntimeRunner::new(
RuntimeConfig::default(),
CustomRuntimeInfo::default(),
);
let mut runtime = None;
runner
.run(async |runtime_inner| {
runtime = Some(runtime_inner);
})
.await;
assert!(runtime.is_some());
let com_hub = runtime.unwrap().com_hub();
let interface_map = com_hub.interfaces_manager().interfaces.borrow();
let local_loopback_interface =
interface_map.iter().find(|(_uuid, interface)| {
interface.properties.interface_type == "local"
});
let (local_loopback_interface_uuid, _) = local_loopback_interface
.expect("Local loopback interface not found in com hub");
let socket_map = com_hub.socket_manager().sockets.borrow();
socket_map
.values()
.find(|socket| {
&socket.interface_uuid == local_loopback_interface_uuid
})
.expect("Local loopback socket not found in com hub");
}
}