2021-10-12 15:59:46 +00:00
|
|
|
use std::marker::PhantomData;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use arc_swap::ArcSwapOption;
|
|
|
|
use async_trait::async_trait;
|
|
|
|
|
|
|
|
use crate::error::Error;
|
2022-07-21 15:34:53 +00:00
|
|
|
use crate::message::*;
|
2021-10-12 15:59:46 +00:00
|
|
|
use crate::netapp::*;
|
|
|
|
use crate::util::*;
|
|
|
|
|
2021-10-14 09:35:05 +00:00
|
|
|
/// This trait should be implemented by an object of your application
|
|
|
|
/// that can handle a message of type `M`.
|
|
|
|
///
|
|
|
|
/// The handler object should be in an Arc, see `Endpoint::set_handler`
|
2021-10-12 15:59:46 +00:00
|
|
|
#[async_trait]
|
|
|
|
pub trait EndpointHandler<M>: Send + Sync
|
|
|
|
where
|
|
|
|
M: Message,
|
|
|
|
{
|
2022-07-21 18:22:56 +00:00
|
|
|
async fn handle(self: &Arc<Self>, m: Req<M>, from: NodeID) -> Resp<M>;
|
2021-10-12 15:59:46 +00:00
|
|
|
}
|
|
|
|
|
2021-10-14 09:35:05 +00:00
|
|
|
/// If one simply wants to use an endpoint in a client fashion,
|
|
|
|
/// without locally serving requests to that endpoint,
|
|
|
|
/// use the unit type `()` as the handler type:
|
|
|
|
/// it will panic if it is ever made to handle request.
|
|
|
|
#[async_trait]
|
|
|
|
impl<M: Message + 'static> EndpointHandler<M> for () {
|
2022-07-21 18:22:56 +00:00
|
|
|
async fn handle(self: &Arc<()>, _m: Req<M>, _from: NodeID) -> Resp<M> {
|
2021-10-14 09:35:05 +00:00
|
|
|
panic!("This endpoint should not have a local handler.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This struct represents an endpoint for message of type `M`.
|
|
|
|
///
|
|
|
|
/// Creating a new endpoint is done by calling `NetApp::endpoint`.
|
|
|
|
/// An endpoint is identified primarily by its path, which is specified
|
|
|
|
/// at creation time.
|
|
|
|
///
|
|
|
|
/// An `Endpoint` is used both to send requests to remote nodes,
|
|
|
|
/// and to specify the handler for such requests on the local node.
|
|
|
|
/// The type `H` represents the type of the handler object for
|
|
|
|
/// endpoint messages (see `EndpointHandler`).
|
2021-10-12 15:59:46 +00:00
|
|
|
pub struct Endpoint<M, H>
|
|
|
|
where
|
|
|
|
M: Message,
|
|
|
|
H: EndpointHandler<M>,
|
|
|
|
{
|
|
|
|
phantom: PhantomData<M>,
|
|
|
|
netapp: Arc<NetApp>,
|
|
|
|
path: String,
|
|
|
|
handler: ArcSwapOption<H>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<M, H> Endpoint<M, H>
|
|
|
|
where
|
|
|
|
M: Message,
|
|
|
|
H: EndpointHandler<M>,
|
|
|
|
{
|
|
|
|
pub(crate) fn new(netapp: Arc<NetApp>, path: String) -> Self {
|
|
|
|
Self {
|
|
|
|
phantom: PhantomData::default(),
|
|
|
|
netapp,
|
|
|
|
path,
|
|
|
|
handler: ArcSwapOption::from(None),
|
|
|
|
}
|
|
|
|
}
|
2021-10-14 09:35:05 +00:00
|
|
|
|
2022-02-16 12:00:26 +00:00
|
|
|
/// Get the path of this endpoint
|
|
|
|
pub fn path(&self) -> &str {
|
|
|
|
&self.path
|
|
|
|
}
|
|
|
|
|
2021-10-14 09:35:05 +00:00
|
|
|
/// Set the object that is responsible of handling requests to
|
|
|
|
/// this endpoint on the local node.
|
2021-10-12 15:59:46 +00:00
|
|
|
pub fn set_handler(&self, h: Arc<H>) {
|
|
|
|
self.handler.swap(Some(h));
|
|
|
|
}
|
2021-10-14 09:35:05 +00:00
|
|
|
|
|
|
|
/// Call this endpoint on a remote node (or on the local node,
|
|
|
|
/// for that matter)
|
2022-07-21 18:22:56 +00:00
|
|
|
pub async fn call_full<T>(
|
2021-10-12 15:59:46 +00:00
|
|
|
&self,
|
|
|
|
target: &NodeID,
|
2022-07-21 18:22:56 +00:00
|
|
|
req: T,
|
2021-10-12 15:59:46 +00:00
|
|
|
prio: RequestPriority,
|
2022-07-21 18:22:56 +00:00
|
|
|
) -> Result<Resp<M>, Error>
|
|
|
|
where
|
|
|
|
T: IntoReq<M>,
|
|
|
|
{
|
2021-10-12 15:59:46 +00:00
|
|
|
if *target == self.netapp.id {
|
|
|
|
match self.handler.load_full() {
|
|
|
|
None => Err(Error::NoHandler),
|
2022-07-21 18:22:56 +00:00
|
|
|
Some(h) => Ok(h.handle(req.into_req_local(), self.netapp.id).await),
|
2021-10-12 15:59:46 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let conn = self
|
|
|
|
.netapp
|
|
|
|
.client_conns
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.get(target)
|
|
|
|
.cloned();
|
|
|
|
match conn {
|
|
|
|
None => Err(Error::Message(format!(
|
|
|
|
"Not connected: {}",
|
2022-02-21 15:57:07 +00:00
|
|
|
hex::encode(&target[..8])
|
2021-10-12 15:59:46 +00:00
|
|
|
))),
|
2022-07-21 18:22:56 +00:00
|
|
|
Some(c) => c.call(req.into_req()?, self.path.as_str(), prio).await,
|
2021-10-12 15:59:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-21 18:22:56 +00:00
|
|
|
|
|
|
|
/// Call this endpoint on a remote node, without the possibility
|
|
|
|
/// of adding or receiving a body
|
|
|
|
pub async fn call(
|
|
|
|
&self,
|
|
|
|
target: &NodeID,
|
|
|
|
req: M,
|
|
|
|
prio: RequestPriority,
|
|
|
|
) -> Result<<M as Message>::Response, Error> {
|
|
|
|
Ok(self.call_full(target, req, prio).await?.into_msg())
|
|
|
|
}
|
2021-10-12 15:59:46 +00:00
|
|
|
}
|
|
|
|
|
2021-10-14 09:35:05 +00:00
|
|
|
// ---- Internal stuff ----
|
|
|
|
|
|
|
|
pub(crate) type DynEndpoint = Box<dyn GenericEndpoint + Send + Sync>;
|
|
|
|
|
2021-10-12 15:59:46 +00:00
|
|
|
#[async_trait]
|
|
|
|
pub(crate) trait GenericEndpoint {
|
2022-06-05 13:33:43 +00:00
|
|
|
async fn handle(
|
|
|
|
&self,
|
|
|
|
buf: &[u8],
|
2022-07-21 15:34:53 +00:00
|
|
|
stream: ByteStream,
|
2022-06-05 13:33:43 +00:00
|
|
|
from: NodeID,
|
2022-07-21 15:34:53 +00:00
|
|
|
) -> Result<(Vec<u8>, Option<ByteStream>), Error>;
|
2021-10-14 15:33:12 +00:00
|
|
|
fn drop_handler(&self);
|
2021-10-12 15:59:46 +00:00
|
|
|
fn clone_endpoint(&self) -> DynEndpoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub(crate) struct EndpointArc<M, H>(pub(crate) Arc<Endpoint<M, H>>)
|
|
|
|
where
|
|
|
|
M: Message,
|
|
|
|
H: EndpointHandler<M>;
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl<M, H> GenericEndpoint for EndpointArc<M, H>
|
|
|
|
where
|
|
|
|
M: Message + 'static,
|
|
|
|
H: EndpointHandler<M> + 'static,
|
|
|
|
{
|
2022-06-05 13:33:43 +00:00
|
|
|
async fn handle(
|
|
|
|
&self,
|
|
|
|
buf: &[u8],
|
2022-07-21 15:34:53 +00:00
|
|
|
stream: ByteStream,
|
2022-06-05 13:33:43 +00:00
|
|
|
from: NodeID,
|
2022-07-21 15:34:53 +00:00
|
|
|
) -> Result<(Vec<u8>, Option<ByteStream>), Error> {
|
2021-10-12 15:59:46 +00:00
|
|
|
match self.0.handler.load_full() {
|
|
|
|
None => Err(Error::NoHandler),
|
|
|
|
Some(h) => {
|
2022-06-07 22:30:56 +00:00
|
|
|
let req = rmp_serde::decode::from_read_ref(buf)?;
|
2022-07-21 18:22:56 +00:00
|
|
|
let req = Req {
|
|
|
|
_phantom: Default::default(),
|
|
|
|
msg: Arc::new(req),
|
|
|
|
msg_ser: None,
|
|
|
|
body: BodyData::Stream(stream),
|
|
|
|
};
|
2022-07-21 17:05:51 +00:00
|
|
|
let res = h.handle(req, from).await;
|
2022-07-21 18:22:56 +00:00
|
|
|
let Resp {
|
|
|
|
msg,
|
|
|
|
body,
|
|
|
|
_phantom,
|
|
|
|
} = res;
|
|
|
|
let res_bytes = rmp_to_vec_all_named(&msg)?;
|
|
|
|
Ok((res_bytes, body.into_stream()))
|
2021-10-12 15:59:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-14 15:33:12 +00:00
|
|
|
fn drop_handler(&self) {
|
2021-10-12 15:59:46 +00:00
|
|
|
self.0.handler.swap(None);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn clone_endpoint(&self) -> DynEndpoint {
|
|
|
|
Box::new(Self(self.0.clone()))
|
|
|
|
}
|
|
|
|
}
|