use futures::{
channel::mpsc::{unbounded, UnboundedSender},
Stream,
};
use netlink_packet_core::NetlinkMessage;
use std::fmt::Debug;
use crate::{errors::Error, sys::SocketAddr, Request};
#[derive(Clone, Debug)]
pub struct ConnectionHandle<T>
where
T: Debug,
{
requests_tx: UnboundedSender<Request<T>>,
}
impl<T> ConnectionHandle<T>
where
T: Debug,
{
pub(crate) fn new(requests_tx: UnboundedSender<Request<T>>) -> Self {
ConnectionHandle { requests_tx }
}
pub fn request(
&self,
message: NetlinkMessage<T>,
destination: SocketAddr,
) -> Result<impl Stream<Item = NetlinkMessage<T>>, Error<T>> {
let (tx, rx) = unbounded::<NetlinkMessage<T>>();
let request = Request::from((message, destination, tx));
debug!("handle: forwarding new request to connection");
UnboundedSender::unbounded_send(&self.requests_tx, request).map_err(
|e| {
if e.is_full() {
panic!("internal error: unbounded channel full?!");
} else if e.is_disconnected() {
Error::ConnectionClosed
} else {
panic!("unknown error: {:?}", e);
}
},
)?;
Ok(rx)
}
pub fn notify(
&self,
message: NetlinkMessage<T>,
destination: SocketAddr,
) -> Result<(), Error<T>> {
let (tx, _rx) = unbounded::<NetlinkMessage<T>>();
let request = Request::from((message, destination, tx));
debug!("handle: forwarding new request to connection");
UnboundedSender::unbounded_send(&self.requests_tx, request)
.map_err(|_| Error::ConnectionClosed)
}
}