use core::future::Future;
use crate::error::*;
use crate::respond::ExchangeHandler;
use crate::transport::exchange::Exchange;
use super::{IMStatusCode, OpCode, StatusResp, PROTO_ID_INTERACTION_MODEL};
pub struct BusyInteractionModel(());
impl Default for BusyInteractionModel {
fn default() -> Self {
Self::new()
}
}
impl BusyInteractionModel {
#[inline(always)]
pub const fn new() -> Self {
Self(())
}
pub async fn handle(&self, mut exchange: Exchange<'_>) -> Result<(), Error> {
let meta = exchange.recv().await?.meta();
if meta.proto_id != PROTO_ID_INTERACTION_MODEL {
Err(ErrorCode::InvalidProto)?;
}
let status = match meta.opcode()? {
OpCode::ReadRequest
| OpCode::WriteRequest
| OpCode::SubscribeRequest
| OpCode::InvokeRequest => IMStatusCode::Busy,
_ => IMStatusCode::Failure,
};
exchange
.send_with(|_, wb| {
StatusResp::write(wb, status)?;
Ok(Some(OpCode::StatusResponse.meta()))
})
.await
}
}
impl ExchangeHandler for BusyInteractionModel {
fn handle(&self, exchange: Exchange<'_>) -> impl Future<Output = Result<(), Error>> {
BusyInteractionModel::handle(self, exchange)
}
}