From c3ed23ec553fed3bd91319de1b370b7fc56ba8ba Mon Sep 17 00:00:00 2001 From: Yannick Poirier Date: Wed, 28 Feb 2024 13:08:49 +0100 Subject: [PATCH] Expose LCT Header to python --- py_tests/__init__.py | 29 ++++++++++++++++++++++++ src/py/receiver/lct.rs | 51 ++++++++++++++++++++++++++++++++++++++++++ src/py/receiver/mod.rs | 2 ++ 3 files changed, 82 insertions(+) create mode 100644 src/py/receiver/lct.rs diff --git a/py_tests/__init__.py b/py_tests/__init__.py index 4727e6c..87fb6e1 100644 --- a/py_tests/__init__.py +++ b/py_tests/__init__.py @@ -125,5 +125,34 @@ def test_remove_object(self): assert(success == True) assert(flute_sender.nb_objects() == 0) + + def test_lct(self): + from flute import sender, receiver + + print("------- test_lct--------") + + tsi = 1234 + + sender_config = sender.Config() + oti = sender.Oti.new_no_code(1400, 64) + flute_sender = sender.Sender(tsi, oti, sender_config) + + receiver_writer = receiver.ObjectWriterBuilder.new_buffer() + receiver_config = receiver.Config() + udp_endpoint = receiver.UDPEndpoint("224.0.0.1", 1234) + flute_receiver = receiver.Receiver(udp_endpoint, tsi, receiver_writer, receiver_config) + + buf = bytes(b'hello world') + flute_sender.add_object_from_buffer(buf, "text", "file://hello.txt", None) + flute_sender.publish() + + pkt = flute_sender.read() + lct = receiver.LCTHeader(bytes(pkt)) + assert(lct.cci == 0) + assert(lct.tsi == 1234) + assert(lct.toi == 0) + assert(lct.sbn == 0) + assert(lct.esi == 0) + if __name__ == '__main__': unittest.main() diff --git a/src/py/receiver/lct.rs b/src/py/receiver/lct.rs new file mode 100644 index 0000000..a72436d --- /dev/null +++ b/src/py/receiver/lct.rs @@ -0,0 +1,51 @@ +use pyo3::{exceptions::PyTypeError, prelude::*}; + +use crate::common::alc; + +#[pyclass(unsendable)] +#[derive(Debug)] +pub struct LCTHeader { + pub inner: crate::core::lct::LCTHeader, + pub payload_id: Option, +} + +#[pymethods] +impl LCTHeader { + #[new] + fn new(data: &[u8]) -> PyResult { + let alc = crate::core::alc::parse_alc_pkt(data) + .map_err(|e| PyTypeError::new_err(e.0.to_string()))?; + + let payload_id = alc::get_fec_inline_payload_id(&alc).ok(); + + Ok(LCTHeader { + inner: alc.lct, + payload_id, + }) + } + + #[getter] + fn cci(&self) -> PyResult { + Ok(self.inner.cci) + } + + #[getter] + fn toi(&self) -> PyResult { + Ok(self.inner.toi) + } + + #[getter] + fn tsi(&self) -> PyResult { + Ok(self.inner.tsi) + } + + #[getter] + fn sbn(&self) -> PyResult> { + Ok(self.payload_id.as_ref().map(|p| p.sbn)) + } + + #[getter] + fn esi(&self) -> PyResult> { + Ok(self.payload_id.as_ref().map(|p| p.esi)) + } +} diff --git a/src/py/receiver/mod.rs b/src/py/receiver/mod.rs index 2317f67..a973601 100644 --- a/src/py/receiver/mod.rs +++ b/src/py/receiver/mod.rs @@ -5,6 +5,7 @@ mod objectwriterbuilder; mod multireceiver; mod receiverpy; mod udpendpoint; +mod lct; #[pymodule] pub fn receiver(_py: Python, m: &PyModule) -> PyResult<()> { @@ -13,5 +14,6 @@ pub fn receiver(_py: Python, m: &PyModule) -> PyResult<()> { m.add_class::()?; m.add_class::()?; m.add_class::()?; + m.add_class::()?; Ok(()) }