2026-06-23 15:42:28 +00:00
|
|
|
//! Generic datastream emission helpers.
|
2026-06-06 17:53:25 +00:00
|
|
|
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
use std::sync::OnceLock;
|
|
|
|
|
|
|
|
|
|
use swactor::actor::ActorAddress;
|
|
|
|
|
use swactor::process_observer::ProcessOutputObserver;
|
|
|
|
|
use swactor::runtime::Runtime;
|
|
|
|
|
|
2026-07-18 09:38:16 +00:00
|
|
|
use super::frame::{ChannelId, Frame, Lifetime, NodeId, StreamId};
|
2026-06-06 17:53:25 +00:00
|
|
|
use super::mux::Mux;
|
2026-06-23 15:42:28 +00:00
|
|
|
use super::record::Record;
|
|
|
|
|
use super::wire::{DatastreamFrame, encode_delivery};
|
2026-06-06 17:53:25 +00:00
|
|
|
|
2026-07-18 09:38:16 +00:00
|
|
|
/// Legacy sink for frames after mux drain has assigned positions.
|
2026-06-06 17:53:25 +00:00
|
|
|
pub trait FrameSink: Send {
|
2026-07-18 09:38:16 +00:00
|
|
|
/// Ship one positioned frame for `stream`. Best-effort: a sink may drop.
|
2026-06-06 17:53:25 +00:00
|
|
|
fn ship(&mut self, stream: &StreamId, frame: &Frame);
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-23 15:42:28 +00:00
|
|
|
/// Static identity a node needs to build its mux.
|
2026-06-06 17:53:25 +00:00
|
|
|
pub struct EmitterConfig {
|
|
|
|
|
pub node_hex: String,
|
|
|
|
|
pub life: u64,
|
|
|
|
|
pub mux_capacity: usize,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct MuxProcObserver {
|
|
|
|
|
mux: Arc<Mux>,
|
2026-06-23 15:42:28 +00:00
|
|
|
channel_for: Arc<dyn Fn(&str, bool) -> ChannelId + Send + Sync>,
|
2026-06-06 17:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ProcessOutputObserver for MuxProcObserver {
|
|
|
|
|
fn on_output(&self, label: &str, is_stderr: bool, data: &[u8]) {
|
|
|
|
|
self.mux
|
2026-06-23 15:42:28 +00:00
|
|
|
.submit((self.channel_for)(label, is_stderr), data.to_vec());
|
2026-06-06 17:53:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-12 06:14:34 +00:00
|
|
|
/// Legacy per-node emitter retained while runtime callsites move to
|
|
|
|
|
/// [`crate::DatastreamEndpoint`]. New code should register channels on the
|
|
|
|
|
/// endpoint and submit through [`crate::DatastreamProducer`].
|
2026-06-06 17:53:25 +00:00
|
|
|
pub struct DatastreamEmitter {
|
|
|
|
|
stream_id: StreamId,
|
|
|
|
|
mux: Arc<Mux>,
|
|
|
|
|
sink: Box<dyn FrameSink>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl DatastreamEmitter {
|
|
|
|
|
pub fn new(cfg: EmitterConfig, sink: Box<dyn FrameSink>) -> Self {
|
|
|
|
|
let stream_id = StreamId::new(NodeId::new(&cfg.node_hex), Lifetime(cfg.life));
|
|
|
|
|
let mux = Arc::new(Mux::new(stream_id.clone(), cfg.mux_capacity));
|
|
|
|
|
Self {
|
|
|
|
|
stream_id,
|
|
|
|
|
mux,
|
|
|
|
|
sink,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-23 15:42:28 +00:00
|
|
|
pub fn stream_id(&self) -> &StreamId {
|
|
|
|
|
&self.stream_id
|
2026-06-21 19:54:41 +00:00
|
|
|
}
|
|
|
|
|
|
2026-06-06 17:53:25 +00:00
|
|
|
pub fn mux(&self) -> &Arc<Mux> {
|
|
|
|
|
&self.mux
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-23 15:42:28 +00:00
|
|
|
pub fn assigned(&self) -> u64 {
|
|
|
|
|
self.mux.assigned()
|
2026-06-06 17:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
2026-06-23 15:42:28 +00:00
|
|
|
pub fn dropped(&self) -> u64 {
|
|
|
|
|
self.mux.dropped()
|
2026-06-09 09:29:07 +00:00
|
|
|
}
|
2026-06-06 17:53:25 +00:00
|
|
|
|
2026-07-18 09:38:16 +00:00
|
|
|
pub fn submit_record<R: Record>(&self, channel: ChannelId, record: &R) -> bool {
|
2026-07-12 06:14:34 +00:00
|
|
|
self.mux.submit(channel, record.encode())
|
2026-06-09 09:29:07 +00:00
|
|
|
}
|
|
|
|
|
|
2026-07-18 09:38:16 +00:00
|
|
|
pub fn submit_text(&self, channel: ChannelId, text: impl AsRef<[u8]>) -> bool {
|
2026-06-23 15:42:28 +00:00
|
|
|
self.mux.submit(channel, text.as_ref().to_vec())
|
2026-06-09 09:29:07 +00:00
|
|
|
}
|
|
|
|
|
|
2026-07-18 09:38:16 +00:00
|
|
|
pub fn submit_bytes(&self, channel: ChannelId, bytes: Vec<u8>) -> bool {
|
2026-06-23 15:42:28 +00:00
|
|
|
self.mux.submit(channel, bytes)
|
2026-06-21 19:54:41 +00:00
|
|
|
}
|
|
|
|
|
|
2026-07-18 09:38:16 +00:00
|
|
|
pub fn submit_text_owned(&self, channel: ChannelId, text: String) -> bool {
|
|
|
|
|
self.mux.submit(channel, text.into_bytes())
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-23 15:42:28 +00:00
|
|
|
pub fn process_observer_with<F>(&self, channel_for: F) -> Arc<dyn ProcessOutputObserver>
|
|
|
|
|
where
|
|
|
|
|
F: Fn(&str, bool) -> ChannelId + Send + Sync + 'static,
|
|
|
|
|
{
|
|
|
|
|
Arc::new(MuxProcObserver {
|
|
|
|
|
mux: self.mux.clone(),
|
|
|
|
|
channel_for: Arc::new(channel_for),
|
|
|
|
|
})
|
2026-06-21 19:54:41 +00:00
|
|
|
}
|
|
|
|
|
|
2026-06-23 15:42:28 +00:00
|
|
|
pub fn tick(&mut self) {
|
|
|
|
|
for frame in self.mux.drain() {
|
|
|
|
|
self.sink.ship(&self.stream_id, &frame);
|
|
|
|
|
}
|
2026-06-09 09:29:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn event_sink(&self) -> DatastreamEventSink {
|
|
|
|
|
DatastreamEventSink {
|
|
|
|
|
mux: self.mux.clone(),
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-06-06 17:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
2026-07-12 06:14:34 +00:00
|
|
|
/// A thread-safe submit handle. Legacy; prefer [`crate::DatastreamProducer`].
|
2026-06-09 09:29:07 +00:00
|
|
|
#[derive(Clone)]
|
|
|
|
|
pub struct DatastreamEventSink {
|
|
|
|
|
mux: Arc<Mux>,
|
2026-06-06 17:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
2026-06-09 09:29:07 +00:00
|
|
|
impl DatastreamEventSink {
|
2026-07-18 09:38:16 +00:00
|
|
|
pub fn submit_record<R: Record>(&self, channel: ChannelId, record: &R) -> bool {
|
2026-07-12 06:14:34 +00:00
|
|
|
self.mux.submit(channel, record.encode())
|
2026-06-23 15:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
2026-07-18 09:38:16 +00:00
|
|
|
pub fn submit_text(&self, channel: ChannelId, text: impl AsRef<[u8]>) -> bool {
|
2026-06-23 15:42:28 +00:00
|
|
|
self.mux.submit(channel, text.as_ref().to_vec())
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-18 09:38:16 +00:00
|
|
|
pub fn submit_bytes(&self, channel: ChannelId, bytes: Vec<u8>) -> bool {
|
2026-06-23 15:42:28 +00:00
|
|
|
self.mux.submit(channel, bytes)
|
2026-06-06 17:53:25 +00:00
|
|
|
}
|
2026-07-18 09:38:16 +00:00
|
|
|
|
|
|
|
|
pub fn submit_text_owned(&self, channel: ChannelId, text: String) -> bool {
|
|
|
|
|
self.mux.submit(channel, text.into_bytes())
|
|
|
|
|
}
|
2026-06-06 17:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
2026-07-12 06:14:34 +00:00
|
|
|
/// A sink that drops everything.
|
2026-06-09 09:29:07 +00:00
|
|
|
pub struct NoopSink;
|
|
|
|
|
|
|
|
|
|
impl FrameSink for NoopSink {
|
|
|
|
|
fn ship(&mut self, _stream: &StreamId, _frame: &Frame) {}
|
2026-06-06 17:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
2026-07-12 06:14:34 +00:00
|
|
|
/// Legacy swactor frame sink retained until MVP runtime cutover removes it.
|
2026-06-06 17:53:25 +00:00
|
|
|
pub struct ClusterFrameSink {
|
|
|
|
|
rt: Arc<Runtime>,
|
|
|
|
|
sink: Arc<OnceLock<ActorAddress>>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ClusterFrameSink {
|
|
|
|
|
pub fn new(rt: Arc<Runtime>, sink: Arc<OnceLock<ActorAddress>>) -> Self {
|
|
|
|
|
Self { rt, sink }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl FrameSink for ClusterFrameSink {
|
|
|
|
|
fn ship(&mut self, stream: &StreamId, frame: &Frame) {
|
|
|
|
|
if let Some(addr) = self.sink.get() {
|
|
|
|
|
let _ = self.rt.send_to(
|
|
|
|
|
*addr,
|
|
|
|
|
DatastreamFrame {
|
|
|
|
|
payload: encode_delivery(stream, frame),
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|