pub mod any;
pub mod control;
pub mod coordinator_daemon;
pub mod daemon_node;
pub mod peer;
pub mod samples;
pub use any::AnyMessage;
pub use control::{
ControlReply, ControlRequest, DEFAULT_LOG_LIMIT, DataflowSource, ErrorCode, LogQuery,
ParamScope, RequestScope, TopicQuery,
};
pub use coordinator_daemon::{
BuildOutcome, BuildStep, CoordinatorEvent, DaemonEvent, DaemonRegistration, PeerRouteDirective,
SpawnOutcome, StateEntry, StateEntryKind,
};
pub use daemon_node::{
DEFAULT_EVENT_BATCH, DEFAULT_ZERO_COPY_THRESHOLD, ENV_NODE_CONFIG, ENV_RUN_PARENT_PID,
ExtensionKey, ExtensionNamespace, MAX_EXTENSION_NAME_LEN, NodeConfig, NodeConfigError,
NodeEvent, NodeHandshake, NodeRequest, OutputPayload,
};
pub use peer::PeerEvent;
use crate::codec::{WireDecode, WireEncode};
use crate::error::WireResult;
use crate::frame::{Frame, FrameFlags, FrameKind, FrameLimits, FrameView, write_message};
pub trait WireMessage: WireEncode + WireDecode + Sized {
const KIND: FrameKind;
const VARIANT_NAMES: &'static [&'static str];
fn variant_index(&self) -> u16;
fn variant_name(&self) -> &'static str {
Self::VARIANT_NAMES
.get(usize::from(self.variant_index()))
.copied()
.unwrap_or("<unknown>")
}
fn write_frame_into(
&self,
out: &mut Vec<u8>,
flags: FrameFlags,
limits: &FrameLimits,
) -> WireResult<usize> {
write_message(out, Self::KIND, flags, self, limits)
}
fn to_frame(&self, flags: FrameFlags, limits: &FrameLimits) -> WireResult<Vec<u8>> {
let mut out = Vec::new();
self.write_frame_into(&mut out, flags, limits)?;
Ok(out)
}
fn to_owned_frame(&self, flags: FrameFlags, limits: &FrameLimits) -> WireResult<Frame> {
Frame::from_message(Self::KIND, flags, self, limits)
}
fn from_frame(view: &FrameView<'_>) -> WireResult<Self> {
view.decode_as(Self::KIND)
}
fn from_payload(payload: &[u8]) -> WireResult<Self> {
Self::decode_exact(payload)
}
fn from_bytes(bytes: &[u8], limits: &FrameLimits) -> WireResult<Self> {
let view = crate::frame::decode_frame(bytes, limits)?;
Self::from_frame(&view)
}
}
macro_rules! impl_wire_message {
($ty:ty, $kind:expr, [$($name:literal),* $(,)?], $index:item) => {
impl $crate::messages::WireMessage for $ty {
const KIND: $crate::frame::FrameKind = $kind;
const VARIANT_NAMES: &'static [&'static str] = &[$($name),*];
$index
}
};
}
pub(crate) use impl_wire_message;
impl WireMessage for crate::common::DataFrame {
const KIND: FrameKind = FrameKind::Data;
const VARIANT_NAMES: &'static [&'static str] = &["DataFrame"];
fn variant_index(&self) -> u16 {
0
}
}
impl WireMessage for crate::common::LogFrame {
const KIND: FrameKind = FrameKind::Log;
const VARIANT_NAMES: &'static [&'static str] = &["LogFrame"];
fn variant_index(&self) -> u16 {
0
}
}
impl WireMessage for crate::common::TelemetryFrame {
const KIND: FrameKind = FrameKind::Telemetry;
const VARIANT_NAMES: &'static [&'static str] = &["TelemetryFrame"];
fn variant_index(&self) -> u16 {
0
}
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use astrs_time::HlcTimestamp;
use super::*;
use crate::common::{DataFrame, LogFrame, LogLevel, LogRecord};
use crate::error::WireError;
use crate::ids::{DataflowId, SubscriptionId};
use crate::metadata::Metadata;
fn data_frame() -> DataFrame {
DataFrame::new(
SubscriptionId::new(3),
DataflowId::from_u128(7),
"camera/image".parse().unwrap(),
Metadata::new(HlcTimestamp::new(1_000, 0)),
vec![1, 2, 3],
)
}
#[test]
fn fan_out_frames_carry_their_own_kinds() {
assert_eq!(DataFrame::KIND, FrameKind::Data);
assert_eq!(LogFrame::KIND, FrameKind::Log);
assert_eq!(
<crate::common::TelemetryFrame as WireMessage>::KIND,
FrameKind::Telemetry
);
}
#[test]
fn a_data_frame_round_trips_through_its_own_helpers() {
let limits = FrameLimits::uds();
let frame = data_frame();
let bytes = frame.to_frame(FrameFlags::EMPTY, &limits).unwrap();
let decoded = DataFrame::from_bytes(&bytes, &limits).unwrap();
assert!(decoded.bitwise_eq(&frame));
assert_eq!(frame.variant_name(), "DataFrame");
}
#[test]
fn a_family_mismatch_is_reported_not_guessed() {
let limits = FrameLimits::uds();
let bytes = data_frame().to_frame(FrameFlags::EMPTY, &limits).unwrap();
let view = crate::frame::decode_frame(&bytes, &limits).unwrap();
match LogFrame::from_frame(&view) {
Err(WireError::KindMismatch { expected, found }) => {
assert_eq!(expected, FrameKind::Log);
assert_eq!(found, FrameKind::Data);
}
other => panic!("expected KindMismatch, got {other:?}"),
}
}
#[test]
fn log_frames_frame_and_unframe() {
let limits = FrameLimits::network();
let record = LogRecord::new(HlcTimestamp::new(5, 1), LogLevel::Warn, "queue full");
let frame = LogFrame::new(SubscriptionId::FIRST, record);
let bytes = frame.to_frame(FrameFlags::CRC, &limits).unwrap();
assert_eq!(LogFrame::from_bytes(&bytes, &limits).unwrap(), frame);
}
#[test]
fn owned_frames_and_byte_frames_agree() {
let limits = FrameLimits::uds();
let frame = data_frame();
let owned = frame.to_owned_frame(FrameFlags::EMPTY, &limits).unwrap();
let bytes = frame.to_frame(FrameFlags::EMPTY, &limits).unwrap();
assert_eq!(owned.encode(&limits).unwrap(), bytes);
}
}