2021-06-28 17:41:00 +04:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2021-06-24 13:32:19 +04:00
|
|
|
use async_trait::async_trait;
|
2021-07-16 20:21:27 +04:00
|
|
|
use messagebus::{
|
|
|
|
derive::{Error as MbError, Message},
|
|
|
|
error, AsyncBatchHandler, BatchHandler, Bus, Message,
|
|
|
|
};
|
2021-06-28 17:41:00 +04:00
|
|
|
use thiserror::Error;
|
|
|
|
|
2021-07-16 20:21:27 +04:00
|
|
|
#[derive(Debug, Error, Clone, MbError)]
|
2021-06-28 17:41:00 +04:00
|
|
|
enum Error {
|
|
|
|
#[error("Error({0})")]
|
2021-07-02 20:21:53 +04:00
|
|
|
Error(Arc<anyhow::Error>),
|
2021-06-28 17:41:00 +04:00
|
|
|
}
|
2021-06-24 13:32:19 +04:00
|
|
|
|
2021-06-28 17:41:00 +04:00
|
|
|
impl<M: Message> From<error::Error<M>> for Error {
|
|
|
|
fn from(err: error::Error<M>) -> Self {
|
|
|
|
Self::Error(Arc::new(err.into()))
|
|
|
|
}
|
|
|
|
}
|
2021-07-16 20:21:27 +04:00
|
|
|
|
|
|
|
#[derive(Debug, Clone, Message)]
|
|
|
|
#[message(clone)]
|
|
|
|
struct MsgI32(i32);
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Message)]
|
|
|
|
#[message(clone)]
|
|
|
|
struct MsgI16(i16);
|
|
|
|
|
2021-06-24 13:32:19 +04:00
|
|
|
struct TmpReceiver;
|
|
|
|
|
|
|
|
#[async_trait]
|
2021-07-16 20:21:27 +04:00
|
|
|
impl AsyncBatchHandler<MsgI32> for TmpReceiver {
|
2021-06-24 13:32:19 +04:00
|
|
|
type Error = Error;
|
|
|
|
type Response = ();
|
2021-07-16 20:21:27 +04:00
|
|
|
type InBatch = Vec<MsgI32>;
|
2021-07-02 20:21:53 +04:00
|
|
|
type OutBatch = Vec<()>;
|
2021-06-24 13:32:19 +04:00
|
|
|
|
2021-07-16 20:21:27 +04:00
|
|
|
async fn handle(
|
|
|
|
&self,
|
|
|
|
msg: Vec<MsgI32>,
|
|
|
|
_bus: &Bus,
|
|
|
|
) -> Result<Vec<Self::Response>, Self::Error> {
|
2021-06-24 13:32:19 +04:00
|
|
|
println!("---> [i32; {}] {:?}", msg.len(), msg);
|
|
|
|
|
|
|
|
Ok(vec![])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-16 20:21:27 +04:00
|
|
|
impl BatchHandler<MsgI16> for TmpReceiver {
|
2021-06-24 13:32:19 +04:00
|
|
|
type Error = Error;
|
|
|
|
type Response = ();
|
2021-07-16 20:21:27 +04:00
|
|
|
type InBatch = Vec<MsgI16>;
|
2021-07-02 20:21:53 +04:00
|
|
|
type OutBatch = Vec<()>;
|
2021-06-24 13:32:19 +04:00
|
|
|
|
2021-07-16 20:21:27 +04:00
|
|
|
fn handle(&self, msg: Vec<MsgI16>, _bus: &Bus) -> Result<Vec<Self::Response>, Self::Error> {
|
2021-06-24 13:32:19 +04:00
|
|
|
println!("---> [i16; {}] {:?}", msg.len(), msg);
|
|
|
|
Ok(vec![])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
|
|
|
let (b, poller) = Bus::build()
|
|
|
|
.register(TmpReceiver)
|
2021-07-16 20:21:27 +04:00
|
|
|
.subscribe_batch_async::<MsgI32>(16, Default::default())
|
|
|
|
.subscribe_batch_sync::<MsgI16>(16, Default::default())
|
2021-06-24 13:32:19 +04:00
|
|
|
.done()
|
|
|
|
.build();
|
|
|
|
|
2021-06-28 17:41:00 +04:00
|
|
|
for i in 1..100i32 {
|
2021-07-16 20:21:27 +04:00
|
|
|
b.send(MsgI32(i)).await.unwrap();
|
2021-06-24 13:32:19 +04:00
|
|
|
}
|
|
|
|
|
2021-07-16 20:21:27 +04:00
|
|
|
b.send(MsgI16(1i16)).await.unwrap();
|
|
|
|
b.send(MsgI16(2i16)).await.unwrap();
|
|
|
|
b.send(MsgI16(3i16)).await.unwrap();
|
|
|
|
b.send(MsgI16(4i16)).await.unwrap();
|
|
|
|
b.send(MsgI16(5i16)).await.unwrap();
|
|
|
|
b.send(MsgI16(6i16)).await.unwrap();
|
|
|
|
b.send(MsgI16(7i16)).await.unwrap();
|
2021-06-24 13:32:19 +04:00
|
|
|
|
|
|
|
println!("flush");
|
|
|
|
b.flush().await;
|
|
|
|
|
|
|
|
println!("close");
|
|
|
|
b.close().await;
|
|
|
|
|
|
|
|
println!("closed");
|
|
|
|
|
|
|
|
poller.await;
|
|
|
|
println!("[done]");
|
|
|
|
}
|