1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
use std::any::type_name;
use async_trait::async_trait;
use interface::Update;
use crate::framework::model::{Task, TaskError};
use super::{Actor, PlainActor};
type Result<T> = std::result::Result<T, TaskError>;
#[async_trait]
impl<C, const NI: usize, const NO: usize> Task for Actor<C, NI, NO>
where
C: 'static + Update,
{
/// Run the actor loop
async fn task(mut self: Box<Self>) -> Result<()> {
/* match self.bootstrap().await {
Err(e) => crate::print_info(
format!("{} bootstrapping failed", Who::highlight(self)),
Some(&e),
),
Ok(_) => {
crate::print_info(
format!("{} loop started", Who::highlight(self)),
None::<&dyn std::error::Error>,
);
if let Err(e) = self.async_run().await {
println!(
"{}{:?}",
format!("{} loop ended", Who::highlight(self)),
Some(&e)
);
}
}
} */
self.async_run().await
}
/// Starts the actor infinite loop
async fn async_run(&mut self) -> Result<()> {
log::debug!("ACTOR LOOP ({NI}/{NO}): {}", type_name::<C>());
let _bootstrap = self.bootstrap().await?;
match (self.inputs.as_ref(), self.outputs.as_ref()) {
(Some(_), Some(_)) => {
if NO >= NI {
// Decimation
// if !bootstrap {
self.collect().await?.client.lock().await.update();
self.distribute().await?;
// } else {
// log::debug!("BOOTSTRAPPING ACTOR LOOP ({NI}/{NO}): {}", type_name::<C>());
// self.collect().await?.client.lock().await.update();
// self.distribute().await?;
// }
loop {
for _ in 0..NO / NI {
self.collect().await?.client.lock().await.update();
}
self.distribute().await?;
}
} else {
// Upsampling
loop {
self.collect().await?.client.lock().await.update();
for _ in 0..NI / NO {
self.distribute().await?;
}
}
}
}
(None, Some(_)) => loop {
// Initiator
self.client.lock().await.update();
self.distribute().await?;
},
(Some(_), None) => loop {
// Terminator
self.collect().await?.client.lock().await.update();
},
(None, None) => Ok(()),
}
}
fn as_plain(&self) -> PlainActor {
self.into()
}
}