use std::collections::HashMap;
use std::time::Duration;
use std::time::SystemTime;
use crate::common::udpendpoint::UDPEndpoint;
use crate::core::lct::Cenc;
use crate::core::Oti;
use crate::tools::error::Result;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ObjectCacheControl {
NoCache,
MaxStale,
ExpiresAt(SystemTime),
ExpiresAtHint(SystemTime),
}
impl ObjectCacheControl {
pub fn should_update(&self, cache_control: ObjectCacheControl) -> bool {
match self {
ObjectCacheControl::NoCache => cache_control != ObjectCacheControl::NoCache,
ObjectCacheControl::MaxStale => cache_control != ObjectCacheControl::MaxStale,
ObjectCacheControl::ExpiresAt(expires_at) => {
if let ObjectCacheControl::ExpiresAt(d) = cache_control {
let diff = match d < *expires_at {
true => expires_at.duration_since(d).unwrap_or_default(),
false => d.duration_since(*expires_at).unwrap_or_default(),
};
return diff > std::time::Duration::from_secs(1);
}
true
}
ObjectCacheControl::ExpiresAtHint(expires_at) => {
if let ObjectCacheControl::ExpiresAtHint(d) = cache_control {
let diff = match d < *expires_at {
true => expires_at.duration_since(d).unwrap_or_default(),
false => d.duration_since(*expires_at).unwrap_or_default(),
};
return diff > std::time::Duration::from_secs(1);
}
true
}
}
}
}
#[derive(Debug, Clone)]
pub struct ObjectMetadata {
pub content_location: String,
pub content_length: Option<usize>,
pub transfer_length: Option<usize>,
pub content_type: Option<String>,
pub cache_control: ObjectCacheControl,
pub groups: Option<Vec<String>>,
pub md5: Option<String>,
pub optel_propagator: Option<HashMap<String, String>>,
pub oti: Option<Oti>,
pub cenc: Option<Cenc>,
pub e_tag: Option<String>,
}
#[derive(Debug)]
pub enum ObjectWriterBuilderResult {
StoreObject(Box<dyn ObjectWriter>),
ObjectAlreadyReceived,
Abort,
}
pub trait ObjectWriterBuilder {
fn new_object_writer(
&self,
endpoint: &UDPEndpoint,
tsi: &u64,
toi: &u128,
meta: &ObjectMetadata,
now: std::time::SystemTime,
) -> ObjectWriterBuilderResult;
fn update_cache_control(
&self,
endpoint: &UDPEndpoint,
tsi: &u64,
toi: &u128,
meta: &ObjectMetadata,
now: std::time::SystemTime,
);
fn fdt_received(
&self,
endpoint: &UDPEndpoint,
tsi: &u64,
fdt_xml: &str,
expires: std::time::SystemTime,
meta: &ObjectMetadata,
transfer_duration: Duration,
now: std::time::SystemTime,
ext_time: Option<std::time::SystemTime>,
);
}
pub trait ObjectWriter {
fn open(&self, now: SystemTime) -> Result<()>;
fn write(&self, sbn: u32, data: &[u8], now: SystemTime) -> Result<()>;
fn complete(&self, now: SystemTime);
fn error(&self, now: SystemTime);
fn interrupted(&self, now: SystemTime);
fn enable_md5_check(&self) -> bool;
}
impl std::fmt::Debug for dyn ObjectWriterBuilder {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "ObjectWriterBuilder {{ }}")
}
}
impl std::fmt::Debug for dyn ObjectWriter {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "ObjectWriter {{ }}")
}
}
mod objectwriterbuffer;
mod objectwriterfs;
pub use objectwriterbuffer::ObjectWriterBuffer;
pub use objectwriterbuffer::ObjectWriterBufferBuilder;
pub use objectwriterfs::ObjectWriterFS;
pub use objectwriterfs::ObjectWriterFSBuilder;