Implemented mock fountain code protocol (#45)

This commit is contained in:
Daniel Sanchez 2023-01-17 01:08:00 -08:00 committed by GitHub
parent cfcc664e9e
commit f1412b112e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 0 deletions

View File

@ -24,3 +24,4 @@ tokio = { version = "1.23", features = ["macros", "rt"] }
[features]
default = []
raptor = ["raptorq"]
mock = []

View File

@ -0,0 +1,38 @@
// std
// crates
use async_trait::async_trait;
use bytes::Bytes;
use futures::{Stream, StreamExt};
// internal
use crate::fountain::{FountainCode, FountainError};
/// Fountain code that does no protocol at all.
/// Just bypasses the raw bytes into a single chunk and reconstruct from it.
pub struct MockFountain;
#[async_trait]
impl FountainCode for MockFountain {
type Settings = ();
fn new(_: Self::Settings) -> Self {
Self
}
fn encode(&self, block: &[u8]) -> Box<dyn Stream<Item = Bytes> + Send + Sync + Unpin> {
let data = block.to_vec();
Box::new(futures::stream::once(Box::pin(
async move { Bytes::from(data) },
)))
}
async fn decode(
&self,
mut stream: impl Stream<Item = Bytes> + Send + Sync + Unpin,
) -> Result<Bytes, FountainError> {
if let Some(chunk) = stream.next().await {
Ok(chunk)
} else {
Err("Stream ended before decoding was complete".into())
}
}
}

View File

@ -1,3 +1,5 @@
#[cfg(feature = "mock")]
pub mod mock;
#[cfg(feature = "raptor")]
pub mod raptorq;