use tokio::sync::{mpsc, oneshot};
use super::RpcServerError;
use crate::peer_manager::NodeId;
#[derive(Debug)]
pub enum RpcServerRequest {
GetNumActiveSessions(oneshot::Sender<usize>),
GetNumActiveSessionsForPeer(NodeId, oneshot::Sender<usize>),
CloseAllSessionsForPeer(NodeId, oneshot::Sender<usize>),
}
#[derive(Debug, Clone)]
pub struct RpcServerHandle {
sender: mpsc::Sender<RpcServerRequest>,
}
impl RpcServerHandle {
pub(super) fn new(sender: mpsc::Sender<RpcServerRequest>) -> Self {
Self { sender }
}
pub async fn get_num_active_sessions(&mut self) -> Result<usize, RpcServerError> {
let (req, resp) = oneshot::channel();
self.sender
.send(RpcServerRequest::GetNumActiveSessions(req))
.await
.map_err(|_| RpcServerError::RequestCanceled)?;
resp.await.map_err(Into::into)
}
pub async fn get_num_active_sessions_for(&mut self, peer: NodeId) -> Result<usize, RpcServerError> {
let (req, resp) = oneshot::channel();
self.sender
.send(RpcServerRequest::GetNumActiveSessionsForPeer(peer, req))
.await
.map_err(|_| RpcServerError::RequestCanceled)?;
resp.await.map_err(Into::into)
}
pub async fn close_all_sessions_for(&mut self, peer: NodeId) -> Result<usize, RpcServerError> {
let (req, resp) = oneshot::channel();
self.sender
.send(RpcServerRequest::CloseAllSessionsForPeer(peer, req))
.await
.map_err(|_| RpcServerError::RequestCanceled)?;
resp.await.map_err(Into::into)
}
}