Files
unshell/src/protocol/tree/call.rs
T

392 lines
12 KiB
Rust
Raw Normal View History

2026-04-25 15:35:08 -06:00
//! Stateful application-layer call runtime built on top of `ProtocolEndpoint`.
use alloc::{string::String, vec::Vec};
use core::fmt;
use rkyv::{Archive, Serialize, rancor::Error, to_bytes, util::AlignedVec};
use crate::protocol::{
CallMessage, DataMessage, FrameBytes, FrameError, HookTarget, PacketHeader, ProtocolFault,
};
use super::{
Endpoint, EndpointError, HookKey, Ingress, LocalEvent, ProtocolEndpoint, ProtocolLeaf,
};
/// One typed incoming `Call` passed to a leaf procedure.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Call<T> {
2026-04-25 20:47:37 -06:00
/// Decoded application input payload.
2026-04-25 15:35:08 -06:00
pub input: T,
2026-04-25 20:47:37 -06:00
/// Endpoint path of the caller that opened this call.
2026-04-25 15:35:08 -06:00
pub caller_path: Vec<String>,
2026-04-25 20:47:37 -06:00
/// Canonical procedure identifier chosen by the caller.
2026-04-25 15:35:08 -06:00
pub procedure_id: String,
2026-04-25 20:47:37 -06:00
/// Optional destination leaf targeted by the call.
2026-04-25 15:35:08 -06:00
pub dst_leaf: Option<String>,
2026-04-25 20:47:37 -06:00
/// Hook key declared by the caller when it expects a response.
2026-04-25 15:35:08 -06:00
pub response_hook: Option<HookKey>,
}
/// One incoming local call event that already passed protocol validation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IncomingCall {
2026-04-25 20:47:37 -06:00
/// Validated protocol header for the call.
2026-04-25 15:35:08 -06:00
pub header: PacketHeader,
2026-04-25 20:47:37 -06:00
/// Application payload for the call.
2026-04-25 15:35:08 -06:00
pub message: CallMessage,
}
/// One incoming local data event tied to an active hook.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IncomingData {
2026-04-25 20:47:37 -06:00
/// Validated protocol header for the data packet.
2026-04-25 15:35:08 -06:00
pub header: PacketHeader,
2026-04-25 20:47:37 -06:00
/// Hook-associated data payload.
2026-04-25 15:35:08 -06:00
pub message: DataMessage,
2026-04-25 20:47:37 -06:00
/// Resolved hook key for the active session.
2026-04-25 15:35:08 -06:00
pub hook_key: HookKey,
}
/// One incoming local fault event tied to a pending or active hook.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IncomingFault {
2026-04-25 20:47:37 -06:00
/// Validated protocol header for the fault packet.
2026-04-25 15:35:08 -06:00
pub header: PacketHeader,
2026-04-25 20:47:37 -06:00
/// Fault payload emitted by the peer.
2026-04-25 15:35:08 -06:00
pub fault: crate::protocol::FaultMessage,
2026-04-25 20:47:37 -06:00
/// Hook key for the pending or active session that faulted.
2026-04-25 15:35:08 -06:00
pub hook_key: HookKey,
}
/// Outcome of one generated initial call procedure.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CallResult<T> {
2026-04-25 20:47:37 -06:00
/// Return one reply payload to the caller.
2026-04-25 15:35:08 -06:00
Reply(T),
2026-04-25 20:47:37 -06:00
/// Complete the call without any response data.
2026-04-25 15:35:08 -06:00
NoReply,
}
/// One hook-associated `Data` packet emitted by leaf code.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OutgoingData {
2026-04-25 20:47:37 -06:00
/// Destination endpoint path for the hook packet.
2026-04-25 15:35:08 -06:00
pub dst_path: Vec<String>,
2026-04-25 20:47:37 -06:00
/// Hook identifier scoped to the receiving endpoint.
2026-04-25 15:35:08 -06:00
pub hook_id: u64,
2026-04-25 20:47:37 -06:00
/// Procedure identifier that owns this hook stream.
2026-04-25 15:35:08 -06:00
pub procedure_id: String,
2026-04-25 20:47:37 -06:00
/// Serialized application data to send.
2026-04-25 15:35:08 -06:00
pub data: Vec<u8>,
2026-04-25 20:47:37 -06:00
/// Whether this packet closes the local side of the hook.
2026-04-25 15:35:08 -06:00
pub end_hook: bool,
}
/// One runtime-normalized reply produced by generated call dispatch.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CallReply {
2026-04-25 20:47:37 -06:00
/// Serialized reply bytes that should be returned upstream.
2026-04-25 15:35:08 -06:00
Reply(Vec<u8>),
2026-04-25 20:47:37 -06:00
/// Complete without emitting any reply packet.
2026-04-25 15:35:08 -06:00
NoReply,
}
/// Error surfaced while decoding one incoming call or encoding one generated reply.
#[derive(Debug)]
pub enum DispatchError<E> {
2026-04-25 20:47:37 -06:00
/// Failed to decode the typed call input.
2026-04-25 15:35:08 -06:00
Decode(FrameError),
2026-04-25 20:47:37 -06:00
/// Failed to encode the typed call output.
2026-04-25 15:35:08 -06:00
Encode(FrameError),
2026-04-25 20:47:37 -06:00
/// The leaf-specific call handler returned an error.
2026-04-25 15:35:08 -06:00
Handler(E),
}
impl<E> fmt::Display for DispatchError<E>
where
E: fmt::Display,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Decode(error) => write!(f, "call decode failed: {error}"),
Self::Encode(error) => write!(f, "call reply encode failed: {error}"),
Self::Handler(error) => write!(f, "call handler failed: {error}"),
}
}
}
impl<E> core::error::Error for DispatchError<E> where E: core::error::Error + 'static {}
/// Error surfaced by the stateful leaf runtime.
#[derive(Debug)]
pub enum LeafRuntimeError<E> {
2026-04-25 20:47:37 -06:00
/// Protocol endpoint routing or framing failed.
2026-04-25 15:35:08 -06:00
Endpoint(EndpointError),
2026-04-25 20:47:37 -06:00
/// Typed call dispatch failed.
2026-04-25 15:35:08 -06:00
Dispatch(DispatchError<E>),
2026-04-25 20:47:37 -06:00
/// Leaf-local data or fault handling failed.
2026-04-25 15:35:08 -06:00
Leaf(E),
}
impl<E> fmt::Display for LeafRuntimeError<E>
where
E: fmt::Display,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Endpoint(error) => write!(f, "{error}"),
Self::Dispatch(error) => write!(f, "{error}"),
Self::Leaf(error) => write!(f, "{error}"),
}
}
}
impl<E> core::error::Error for LeafRuntimeError<E> where E: core::error::Error + 'static {}
impl<E> From<EndpointError> for LeafRuntimeError<E> {
fn from(value: EndpointError) -> Self {
Self::Endpoint(value)
}
}
/// High-level leaf behavior layered on top of validated protocol events.
pub trait CallLeaf: ProtocolLeaf {
2026-04-25 20:47:37 -06:00
/// Leaf-specific error surfaced by call, data, or fault handling.
2026-04-25 15:35:08 -06:00
type Error;
/// Handles hook-associated inbound `Data` after protocol validation.
fn on_data(&mut self, _data: IncomingData) -> Result<Vec<OutgoingData>, Self::Error> {
Ok(Vec::new())
}
/// Observes one inbound `Fault` after protocol validation.
fn on_fault(&mut self, _fault: IncomingFault) -> Result<(), Self::Error> {
Ok(())
}
/// Polls the leaf for locally-generated hook traffic.
fn poll(&mut self) -> Result<Vec<OutgoingData>, Self::Error> {
Ok(Vec::new())
}
}
/// Stateful runtime that combines a protocol endpoint with one leaf instance.
#[derive(Debug)]
pub struct LeafRuntime<L> {
endpoint: ProtocolEndpoint,
leaf: L,
}
/// Frames emitted by the runtime after one receive or poll step.
#[derive(Debug, Default)]
pub struct RuntimeOutcome {
2026-04-25 20:47:37 -06:00
/// Frames emitted while processing the step.
2026-04-25 15:35:08 -06:00
pub frames: Vec<FrameBytes>,
2026-04-25 20:47:37 -06:00
/// Whether the endpoint dropped the incoming packet.
2026-04-25 15:35:08 -06:00
pub dropped: bool,
}
impl<L> LeafRuntime<L> {
2026-04-25 20:47:37 -06:00
/// Builds a runtime from one endpoint and one leaf instance.
2026-04-25 15:35:08 -06:00
#[must_use]
pub fn new(endpoint: ProtocolEndpoint, leaf: L) -> Self {
Self { endpoint, leaf }
}
2026-04-25 20:47:37 -06:00
/// Returns the underlying protocol endpoint.
2026-04-25 15:35:08 -06:00
#[must_use]
pub fn endpoint(&self) -> &ProtocolEndpoint {
&self.endpoint
}
2026-04-25 20:47:37 -06:00
/// Returns a mutable reference to the underlying endpoint.
2026-04-25 15:35:08 -06:00
pub fn endpoint_mut(&mut self) -> &mut ProtocolEndpoint {
&mut self.endpoint
}
2026-04-25 20:47:37 -06:00
/// Returns the hosted leaf instance.
2026-04-25 15:35:08 -06:00
#[must_use]
pub fn leaf(&self) -> &L {
&self.leaf
}
2026-04-25 20:47:37 -06:00
/// Returns a mutable reference to the hosted leaf instance.
2026-04-25 15:35:08 -06:00
pub fn leaf_mut(&mut self) -> &mut L {
&mut self.leaf
}
}
impl<L> LeafRuntime<L>
where
L: CallLeaf + super::CallProcedures<Error = <L as CallLeaf>::Error>,
{
pub fn receive(
&mut self,
ingress: &Ingress,
frame: FrameBytes,
) -> Result<RuntimeOutcome, LeafRuntimeError<<L as CallLeaf>::Error>> {
let outcome = self.endpoint.receive(ingress, frame)?;
self.process_endpoint_outcome(outcome)
}
pub fn poll(&mut self) -> Result<RuntimeOutcome, LeafRuntimeError<<L as CallLeaf>::Error>> {
let outgoing = self.leaf.poll().map_err(LeafRuntimeError::Leaf)?;
self.emit_outgoing(outgoing)
}
fn process_endpoint_outcome(
&mut self,
outcome: crate::protocol::tree::EndpointOutcome,
) -> Result<RuntimeOutcome, LeafRuntimeError<<L as CallLeaf>::Error>> {
2026-04-25 20:47:37 -06:00
match outcome {
crate::protocol::tree::EndpointOutcome::Forward { frame, .. } => {
let mut frames = Vec::with_capacity(1);
frames.push(frame);
Ok(RuntimeOutcome {
frames,
dropped: false,
})
}
crate::protocol::tree::EndpointOutcome::Dropped => Ok(RuntimeOutcome {
frames: Vec::new(),
dropped: true,
}),
crate::protocol::tree::EndpointOutcome::Local(event) => {
let mut runtime = RuntimeOutcome::default();
match event {
2026-04-25 15:35:08 -06:00
LocalEvent::Call { header, message } => {
let incoming = IncomingCall {
header,
message: message.clone(),
};
match self.leaf.dispatch_call(incoming) {
Ok(CallReply::Reply(bytes)) => {
if let Some(hook) = message.response_hook {
runtime.frames.extend(self.send_reply_data(
hook,
message.procedure_id,
bytes,
true,
)?);
}
}
Ok(CallReply::NoReply) => {}
Err(error) => {
runtime
.frames
.extend(self.emit_internal_fault_if_possible(&message)?);
return Err(LeafRuntimeError::Dispatch(error));
}
}
}
LocalEvent::Data {
header,
message,
hook_key,
} => {
let outgoing = self
.leaf
.on_data(IncomingData {
header,
message,
hook_key,
})
.map_err(LeafRuntimeError::Leaf)?;
runtime.frames.extend(self.emit_outgoing(outgoing)?.frames);
}
LocalEvent::Fault {
header,
message,
hook_key,
} => {
self.leaf
.on_fault(IncomingFault {
header,
fault: message,
hook_key,
})
.map_err(LeafRuntimeError::Leaf)?;
}
}
2026-04-25 20:47:37 -06:00
Ok(runtime)
}
}
2026-04-25 15:35:08 -06:00
}
fn emit_outgoing(
&mut self,
outgoing: Vec<OutgoingData>,
) -> Result<RuntimeOutcome, LeafRuntimeError<<L as CallLeaf>::Error>> {
let mut runtime = RuntimeOutcome::default();
for packet in outgoing {
let endpoint_outcome = self.endpoint.send_data(
packet.dst_path,
packet.hook_id,
packet.procedure_id,
packet.data,
packet.end_hook,
)?;
runtime
.frames
.extend(self.process_endpoint_outcome(endpoint_outcome)?.frames);
}
Ok(runtime)
}
fn send_reply_data(
&mut self,
hook: HookTarget,
procedure_id: String,
bytes: Vec<u8>,
end_hook: bool,
) -> Result<Vec<FrameBytes>, LeafRuntimeError<<L as CallLeaf>::Error>> {
let endpoint_outcome = self.endpoint.send_data(
hook.return_path,
hook.hook_id,
procedure_id,
bytes,
end_hook,
)?;
Ok(self.process_endpoint_outcome(endpoint_outcome)?.frames)
}
fn emit_internal_fault_if_possible(
&mut self,
message: &CallMessage,
) -> Result<Vec<FrameBytes>, LeafRuntimeError<<L as CallLeaf>::Error>> {
let Some(hook) = message.response_hook.as_ref() else {
return Ok(Vec::new());
};
let key = HookKey::new(hook.return_path.clone(), hook.hook_id);
let outcome = self
.endpoint
.emit_fault_if_possible(Some(key), ProtocolFault::INTERNAL_ERROR)?;
Ok(self.process_endpoint_outcome(outcome)?.frames)
}
}
/// Decodes one archived call payload into a typed application request.
pub fn decode_call_input<T>(bytes: &[u8]) -> Result<T, FrameError>
where
T: Archive,
<T as Archive>::Archived: rkyv::Portable
+ for<'b> rkyv::bytecheck::CheckBytes<rkyv::api::high::HighValidator<'b, Error>>
+ rkyv::Deserialize<T, rkyv::api::high::HighDeserializer<Error>>,
{
crate::protocol::deserialize_archived_bytes::<<T as Archive>::Archived, T>(bytes)
}
/// Encodes one typed application reply into hook `Data` bytes.
pub fn encode_call_reply<T>(value: &T) -> Result<Vec<u8>, FrameError>
where
T: for<'a> Serialize<
rkyv::api::high::HighSerializer<AlignedVec, rkyv::ser::allocator::ArenaHandle<'a>, Error>,
>,
{
let bytes = to_bytes::<Error>(value).map_err(FrameError::Serialize)?;
Ok(bytes.as_slice().to_vec())
}