messagebus/examples/producer.rs

111 lines
3.0 KiB
Rust
Raw Normal View History

2023-02-17 21:40:45 +04:00
#![feature(type_alias_impl_trait)]
2023-03-16 18:27:31 +04:00
use std::sync::Arc;
use std::time::Duration;
2023-02-17 21:40:45 +04:00
use futures::Future;
use messagebus::{
bus::{Bus, MaskMatch},
2023-03-15 22:56:48 +04:00
cell::MsgCell,
derive_message_clone,
2023-02-17 21:40:45 +04:00
error::Error,
2023-02-24 11:59:30 +04:00
handler::{Handler, MessageProducer},
receivers::{producer::ProducerWrapper, wrapper::HandlerWrapper},
2023-02-17 21:40:45 +04:00
};
#[derive(Debug, Clone)]
struct Msg(pub u64);
2023-03-15 22:56:48 +04:00
derive_message_clone!(EXAMPLE_MSG, Msg, "example::Msg");
2023-02-17 21:40:45 +04:00
#[derive(Debug, Clone)]
2023-03-15 22:56:48 +04:00
struct StartMsg(u64);
derive_message_clone!(EXAMPLE_START_MSG, StartMsg, "example::StartMsg");
2023-02-17 21:40:45 +04:00
2023-03-15 22:56:48 +04:00
struct Test {}
2023-02-17 21:40:45 +04:00
impl MessageProducer<StartMsg> for Test {
type Message = Msg;
2023-03-15 22:56:48 +04:00
type Context = u64;
2023-02-17 21:40:45 +04:00
type NextFuture<'a> = impl Future<Output = Result<Self::Message, Error>> + 'a;
2023-03-15 22:56:48 +04:00
type StartFuture<'a> = impl Future<Output = Result<Self::Context, Error>> + 'a;
2023-03-07 15:18:55 +04:00
type CloseFuture<'a> = impl Future<Output = Result<(), Error>> + 'a;
2023-02-17 21:40:45 +04:00
2023-03-15 22:56:48 +04:00
fn start(&self, msg: &mut MsgCell<StartMsg>, _: &Bus) -> Self::StartFuture<'_> {
let start_from = msg.get().0;
2023-02-17 21:40:45 +04:00
async move {
2023-03-15 22:56:48 +04:00
println!("start {}", start_from);
2023-02-17 21:40:45 +04:00
2023-03-15 22:56:48 +04:00
tokio::time::sleep(Duration::from_millis(1000)).await;
Ok(start_from)
2023-02-17 21:40:45 +04:00
}
}
2023-03-15 22:56:48 +04:00
fn next<'a>(&'a self, ctx: &'a mut Self::Context, _: &Bus) -> Self::NextFuture<'a> {
2023-02-17 21:40:45 +04:00
async move {
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
2023-03-15 22:56:48 +04:00
let curr = *ctx;
*ctx += 1;
let msg = Msg(curr);
2023-02-24 11:59:30 +04:00
println!("next #{}", msg.0);
2023-03-15 22:56:48 +04:00
if msg.0 == 25 || msg.0 == 125 {
2023-03-16 18:27:31 +04:00
println!(">>>>> stopping");
2023-02-24 11:59:30 +04:00
return Err(Error::ProducerFinished);
}
Ok(msg)
2023-02-17 21:40:45 +04:00
}
}
2023-03-07 15:18:55 +04:00
2023-03-15 22:56:48 +04:00
fn close(&self, _ctx: Self::Context) -> Self::CloseFuture<'_> {
2023-03-07 15:18:55 +04:00
async move { Ok(()) }
}
2023-02-17 21:40:45 +04:00
}
2023-02-24 11:59:30 +04:00
impl Handler<Msg> for Test {
2023-03-16 18:27:31 +04:00
type Response = ();
2023-02-24 11:59:30 +04:00
type HandleFuture<'a> = impl Future<Output = Result<Self::Response, Error>> + 'a;
type FlushFuture<'a> = impl Future<Output = Result<(), Error>> + 'a;
2023-03-07 15:18:55 +04:00
type CloseFuture<'a> = impl Future<Output = Result<(), Error>> + 'a;
2023-02-24 11:59:30 +04:00
2023-03-07 15:18:55 +04:00
fn handle(&self, msg: &mut MsgCell<Msg>, _bus: &Bus) -> Self::HandleFuture<'_> {
2023-02-28 14:51:32 +04:00
let msg = msg.get();
2023-02-24 11:59:30 +04:00
async move {
tokio::time::sleep(Duration::from_millis(100)).await;
println!("handing #{}", msg.0);
2023-03-16 18:27:31 +04:00
Ok(())
2023-02-24 11:59:30 +04:00
}
}
2023-03-07 15:18:55 +04:00
fn flush(&mut self, _bus: &Bus) -> Self::FlushFuture<'_> {
async move { Ok(()) }
}
fn close(&mut self) -> Self::CloseFuture<'_> {
2023-02-24 11:59:30 +04:00
async move { Ok(()) }
}
}
2023-02-17 21:40:45 +04:00
async fn run() -> Result<(), Error> {
let bus = Bus::new();
2023-03-15 22:56:48 +04:00
let test = Arc::new(Test {});
2023-02-24 11:59:30 +04:00
bus.register(ProducerWrapper::new(test.clone()), MaskMatch::all());
bus.register(HandlerWrapper::new(test), MaskMatch::all());
2023-02-17 21:40:45 +04:00
2023-03-15 22:56:48 +04:00
println!("111");
bus.start_producer(StartMsg(0)).await?;
println!("222");
bus.start_producer(StartMsg(100)).await?;
2023-02-24 11:59:30 +04:00
2023-03-15 22:56:48 +04:00
println!("333");
2023-02-24 11:59:30 +04:00
bus.close().await;
2023-02-17 21:40:45 +04:00
bus.wait().await;
Ok(())
}
#[tokio::main]
async fn main() {
run().await.unwrap();
}