use std::sync::Arc;
use prost::Message;
use crate::cancellation::Cancellations;
use crate::error::CallError;
use crate::outbound::Outbound;
use crate::pending_calls::PendingCalls;
use crate::wire::v1::envelope::Kind;
use crate::wire::v1::request::Body;
use crate::wire::v1::{CallRequest, ErrorCode, Event, Permissions, Request, response};
pub(crate) struct Link {
pub(crate) outbound: Outbound,
pub(crate) pending: PendingCalls,
pub(crate) cancellations: Cancellations,
}
#[derive(Clone)]
pub struct Host {
link: Arc<Link>,
permissions: Arc<Permissions>,
}
impl Host {
pub(crate) fn new(link: Arc<Link>, permissions: Permissions) -> Self {
Self {
link,
permissions: Arc::new(permissions),
}
}
pub fn permissions(&self) -> &Permissions {
&self.permissions
}
pub fn call<Input, Output>(&self, method: &str, input: &Input) -> Result<Output, CallError>
where
Input: Message,
Output: Message + Default,
{
let (id, reply) = self
.link
.pending
.register()
.ok_or_else(CallError::disconnected)?;
let body = Body::Call(CallRequest {
method: method.to_owned(),
payload: input.encode_to_vec(),
});
let request = Request {
id,
body: Some(body),
};
if let Err(error) = self.link.outbound.send(Kind::Request(request)) {
self.link.pending.forget(id);
return Err(CallError::failed(format!(
"the call of `{method}` was not sent: {error}"
)));
}
let response = reply.recv().map_err(|_| CallError::disconnected())?;
match response.result {
Some(response::Result::Call(reply)) => Output::decode(reply.payload.as_slice())
.map_err(|error| {
CallError::new(
ErrorCode::InvalidArgument,
format!("the result of `{method}` is not valid: {error}"),
)
}),
Some(response::Result::Error(error)) => Err(error.into()),
_ => Err(CallError::failed(format!(
"the host did not answer `{method}` with a call result"
))),
}
}
pub fn emit(&self, name: &str, payload: &impl Message) -> Result<(), CallError> {
let event = Event {
name: name.to_owned(),
payload: payload.encode_to_vec(),
};
self.link
.outbound
.send(Kind::Event(event))
.map_err(|error| CallError::failed(format!("the event `{name}` was not sent: {error}")))
}
}