You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
1.0 KiB
Rust
39 lines
1.0 KiB
Rust
use crate::bus::{Response, RequestEnvelope};
|
|
use async_std::sync::{Arc, Mutex};
|
|
use std::fmt::{Formatter, Debug, Error};
|
|
use futures::Stream;
|
|
|
|
#[async_trait]
|
|
pub trait BusConnection: Send + Stream<Item=RequestEnvelope> + Unpin {
|
|
async fn close(&mut self);
|
|
async fn ack(&mut self, id: u64);
|
|
async fn send(&mut self, id: u64, message: Response);
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
pub struct Receiver(Arc<Mutex<Box<dyn RawReceiver>>>, String);
|
|
|
|
impl Debug for Receiver {
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
|
|
write!(f, "Receiver {{ {} }}", self.1)
|
|
}
|
|
}
|
|
|
|
impl Receiver {
|
|
pub fn new(receiver: Box<dyn RawReceiver>) -> Receiver {
|
|
let name = receiver.name();
|
|
Receiver(Arc::new(Mutex::new(receiver)), name.to_string())
|
|
}
|
|
|
|
pub async fn get(&mut self) -> Option<Box<dyn BusConnection>> {
|
|
self.0.lock().await.get().await
|
|
}
|
|
}
|
|
|
|
#[async_trait]
|
|
pub trait RawReceiver: Send {
|
|
fn name<'s>(&self) -> &'s str;
|
|
async fn disconnect(&mut self);
|
|
async fn get(&mut self) -> Option<Box<dyn BusConnection>>;
|
|
}
|