use bus::ExampleBus;
use lifeline::prelude::*;
use message::{ExampleRecv, ExampleSend};
use service::ExampleService;
use std::time::Duration;
#[async_std::main]
pub async fn main() -> anyhow::Result<()> {
let bus = ExampleBus::default();
let _service = ExampleService::spawn(&bus)?;
let rx = bus.rx::<ExampleSend>()?;
let tx = bus.tx::<ExampleRecv>()?;
drop(bus);
tx.send(ExampleRecv::Hello).await?;
tx.send(ExampleRecv::Goodbye).await?;
let oh_hello = rx.recv().await;
assert_eq!(Ok(ExampleSend::OhHello), oh_hello);
println!("Service says {:?}", oh_hello.unwrap());
let aww_ok = rx.recv().await;
assert_eq!(Ok(ExampleSend::AwwOk), aww_ok);
println!("Service says {:?}", aww_ok.unwrap());
println!("All done.");
async_std::task::sleep(Duration::from_millis(100)).await;
Ok(())
}
mod message {
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum ExampleSend {
OhHello,
AwwOk,
}
#[derive(Debug, Clone)]
pub enum ExampleRecv {
Hello,
Goodbye,
}
}
mod bus {
use crate::message::{ExampleRecv, ExampleSend};
use lifeline::{lifeline_bus, Message};
lifeline_bus!(pub struct ExampleBus);
impl Message<ExampleBus> for ExampleSend {
type Channel = async_std::channel::Sender<Self>;
}
impl Message<ExampleBus> for ExampleRecv {
type Channel = async_std::channel::Sender<Self>;
}
}
mod service {
use super::bus::ExampleBus;
use crate::message::{ExampleRecv, ExampleSend};
use lifeline::prelude::*;
pub struct ExampleService {
_greet: Lifeline,
}
impl Service for ExampleService {
type Bus = ExampleBus;
type Lifeline = anyhow::Result<Self>;
fn spawn(bus: &Self::Bus) -> Self::Lifeline {
let rx = bus.rx::<ExampleRecv>()?;
let tx = bus.tx::<ExampleSend>()?;
let _greet = Self::try_task("greet", async move {
while let Ok(recv) = rx.recv().await {
match recv {
ExampleRecv::Hello => {
tx.send(ExampleSend::OhHello).await?;
}
ExampleRecv::Goodbye => {
tx.send(ExampleSend::AwwOk).await?;
}
}
}
Ok(())
});
Ok(Self { _greet })
}
}
}