use crate::mailbox::client::MailboxClient;
use crate::mailbox::types::ServerMsg;
use crate::control::PhaseIO;
pub struct MailboxPhaseIO<'a> {
pub client: &'a mut MailboxClient,
pub phase: String,
pub our_side: String,
}
use async_trait::async_trait;
#[async_trait]
impl<'a> PhaseIO for MailboxPhaseIO<'a> {
async fn send(&mut self, bytes: &[u8]) {
let hex = hex::encode(bytes);
let phase = self.phase.clone();
self.client.add(&phase, &hex).await.ok();
}
async fn recv(&mut self) -> Option<Vec<u8>> {
loop {
let msg = self.client.next().await;
if msg.is_none() { return None; }
match msg.unwrap() {
ServerMsg::Message { side, phase, body } => {
if phase == self.phase && side != self.our_side {
if let Ok(bytes) = hex::decode(body) { return Some(bytes); }
}
}
_ => {}
}
}
}
}