use std::sync::Once;
use crate::kernel_api::{register_kernel, Kernel, KernelContract, KernelCtx};
use crate::runtime::{LOG_DEBUG, LOG_INFO};
use crate::status::Result;
#[derive(Default)]
pub struct PassThrough;
impl Kernel for PassThrough {
fn get_contract(c: &mut KernelContract) {
c.input_any(0);
c.output_any(0);
}
fn process(&mut self, cc: &mut KernelCtx) -> Result<()> {
cc.forward(0, 0)
}
}
#[derive(Default)]
pub struct Sink {
count: i64,
}
impl Kernel for Sink {
fn get_contract(c: &mut KernelContract) {
c.input_any(0);
}
fn process(&mut self, cc: &mut KernelCtx) -> Result<()> {
if cc.log_enabled() {
let ts = cc.input_timestamp();
cc.log(LOG_DEBUG, &format!("received packet @ ts={}", ts.0));
}
cc.counter_add("sink.packets", 1);
self.count += 1;
Ok(())
}
fn close(&mut self, cc: &mut KernelCtx) -> Result<()> {
cc.log(
LOG_INFO,
&format!("processed {} packets in total", self.count),
);
cc.counter_add("sink.closed", 1);
Ok(())
}
}
pub fn register_defaults() {
static ONCE: Once = Once::new();
ONCE.call_once(|| {
let _ = register_kernel::<PassThrough>("PassThrough");
let _ = register_kernel::<Sink>("Sink");
});
}