use std::{collections::HashMap, io};
use bytes::Bytes;
use super::{
assembly::FrameOutcome,
buffered_assembly,
core::DeserFailureTracker,
stream_assembly,
};
use crate::{
app::{Envelope, StreamingHandler},
message_assembler::{
AssembledMessage,
ContinuationFrameHeader,
EnvelopeRouting,
FirstFrameHeader,
FrameHeader,
MessageAssemblyState,
},
request::RequestParts,
};
pub(super) struct AssembledFrame<'a, 'b, 'c> {
pub(super) state: &'a mut MessageAssemblyState,
pub(super) failures: &'a mut DeserFailureTracker<'b>,
pub(super) correlation_id: Option<u64>,
pub(super) streaming_routes: &'c HashMap<u32, StreamingHandler>,
pub(super) stream_live: bool,
pub(super) envelope_id: u32,
pub(super) body: &'c [u8],
}
pub(super) fn dispatch_assembled(
header: FrameHeader,
frame: AssembledFrame<'_, '_, '_>,
) -> io::Result<FrameOutcome> {
match header {
FrameHeader::First(first) => dispatch_first(&first, frame),
FrameHeader::Continuation(continuation) => dispatch_continuation(&continuation, frame),
}
}
fn dispatch_first(
header: &FirstFrameHeader,
frame: AssembledFrame<'_, '_, '_>,
) -> io::Result<FrameOutcome> {
let AssembledFrame {
state,
failures,
correlation_id,
streaming_routes,
stream_live,
envelope_id,
body,
} = frame;
let routing = EnvelopeRouting {
envelope_id: envelope_id.into(),
correlation_id: correlation_id.map(Into::into),
};
if opens_stream(streaming_routes, envelope_id, stream_live) {
return match buffered_assembly::build_first_frame_input(header, body, routing) {
Ok(input) => {
stream_assembly::accept_streaming_first(state, failures, correlation_id, input)
}
Err(err) => record_malformed_first(failures, correlation_id, err),
};
}
let mut context = assembly_context(state, failures, correlation_id);
let admitted = buffered_assembly::admit_first_frame(&mut context, header, body, routing)?;
Ok(buffered_or_pending(admitted, streaming_routes))
}
fn dispatch_continuation(
header: &ContinuationFrameHeader,
frame: AssembledFrame<'_, '_, '_>,
) -> io::Result<FrameOutcome> {
let AssembledFrame {
state,
failures,
correlation_id,
streaming_routes,
body,
..
} = frame;
let mut context = assembly_context(state, failures, correlation_id);
if context.state.is_streaming_key(header.message_key) {
return accept_stream_chunk(header, body, context);
}
let admitted = buffered_assembly::admit_continuation_frame(&mut context, header, body)?;
Ok(buffered_or_pending(admitted, streaming_routes))
}
fn accept_stream_chunk(
header: &ContinuationFrameHeader,
body: &[u8],
context: buffered_assembly::AssemblyContext<'_, '_>,
) -> io::Result<FrameOutcome> {
let buffered_assembly::AssemblyContext {
state,
failures,
correlation_id,
} = context;
if body.len() != header.body_len {
return record_length_mismatch(failures, correlation_id, header.body_len, body.len());
}
let continuation = stream_assembly::ContinuationFrame { body, header };
stream_assembly::accept_streaming_continuation(state, failures, correlation_id, &continuation)
}
fn opens_stream(
streaming_routes: &HashMap<u32, StreamingHandler>,
envelope_id: u32,
stream_live: bool,
) -> bool {
streaming_routes.contains_key(&envelope_id) && !stream_live
}
const fn assembly_context<'a, 'b>(
state: &'a mut MessageAssemblyState,
failures: &'a mut DeserFailureTracker<'b>,
correlation_id: Option<u64>,
) -> buffered_assembly::AssemblyContext<'a, 'b> {
buffered_assembly::AssemblyContext {
state,
failures,
correlation_id,
}
}
fn buffered_or_pending(
admitted: Option<AssembledMessage>,
streaming_routes: &HashMap<u32, StreamingHandler>,
) -> FrameOutcome {
admitted.map_or_else(
|| FrameOutcome::Pending,
|msg| buffered_outcome(msg, streaming_routes),
)
}
fn buffered_outcome(
msg: AssembledMessage,
streaming_routes: &HashMap<u32, StreamingHandler>,
) -> FrameOutcome {
let route_id: u32 = msg.routing().envelope_id.into();
if streaming_routes.contains_key(&route_id) {
let parts = request_parts(msg.routing(), msg.metadata().to_vec());
return FrameOutcome::StreamFallback {
parts,
body: Bytes::from(msg.into_body()),
};
}
FrameOutcome::Buffered(Envelope::from_assembled(&msg))
}
fn request_parts(routing: EnvelopeRouting, metadata: Vec<u8>) -> RequestParts {
RequestParts::new(
routing.envelope_id.into(),
routing.correlation_id.map(Into::into),
metadata,
)
}
fn record_malformed_first(
failures: &mut DeserFailureTracker<'_>,
correlation_id: Option<u64>,
err: io::Error,
) -> io::Result<FrameOutcome> {
failures.record(
correlation_id,
"message assembly first frame malformed",
err,
)?;
Ok(FrameOutcome::Pending)
}
fn record_length_mismatch(
failures: &mut DeserFailureTracker<'_>,
correlation_id: Option<u64>,
expected: usize,
actual: usize,
) -> io::Result<FrameOutcome> {
failures.record(
correlation_id,
"message assembly continuation frame length mismatch",
io::Error::new(
io::ErrorKind::InvalidData,
format!(
"message assembly continuation length mismatch: expected {expected}, got {actual}"
),
),
)?;
Ok(FrameOutcome::Pending)
}