use crate::{FrequencyBand, PacketMetadata};
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(transparent)]
pub struct DeviceId(String);
impl DeviceId {
#[must_use]
pub fn new(value: impl Into<String>) -> Self {
Self(value.into())
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
impl From<&str> for DeviceId {
fn from(value: &str) -> Self {
Self::new(value)
}
}
impl From<String> for DeviceId {
fn from(value: String) -> Self {
Self::new(value)
}
}
impl fmt::Display for DeviceId {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(formatter)
}
}
#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(transparent)]
pub struct ChannelId(String);
impl ChannelId {
#[must_use]
pub fn new(value: impl Into<String>) -> Self {
Self(value.into())
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
impl From<&str> for ChannelId {
fn from(value: &str) -> Self {
Self::new(value)
}
}
impl From<String> for ChannelId {
fn from(value: String) -> Self {
Self::new(value)
}
}
impl fmt::Display for ChannelId {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(formatter)
}
}
#[derive(Clone, Copy, Debug, Default, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(transparent)]
pub struct SimTime(u64);
impl SimTime {
pub const ZERO: Self = Self(0);
#[must_use]
pub const fn from_nanos(nanoseconds: u64) -> Self {
Self(nanoseconds)
}
#[must_use]
pub const fn as_nanos(self) -> u64 {
self.0
}
}
impl fmt::Display for SimTime {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "{} ns", self.0)
}
}
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(transparent)]
pub struct PacketId(u64);
impl PacketId {
pub(crate) const fn new(value: u64) -> Self {
Self(value)
}
#[must_use]
pub const fn get(self) -> u64 {
self.0
}
}
impl fmt::Display for PacketId {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(formatter)
}
}
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct Packet {
id: PacketId,
source: DeviceId,
destination: DeviceId,
payload: Vec<u8>,
hops_remaining: u16,
#[serde(default, skip_serializing_if = "PacketMetadata::is_default")]
metadata: PacketMetadata,
}
impl Packet {
pub(crate) fn new(
id: PacketId,
source: DeviceId,
destination: DeviceId,
payload: Vec<u8>,
hops_remaining: u16,
metadata: PacketMetadata,
) -> Self {
Self {
id,
source,
destination,
payload,
hops_remaining,
metadata,
}
}
#[must_use]
pub const fn id(&self) -> PacketId {
self.id
}
#[must_use]
pub const fn source(&self) -> &DeviceId {
&self.source
}
#[must_use]
pub const fn destination(&self) -> &DeviceId {
&self.destination
}
#[must_use]
pub fn payload(&self) -> &[u8] {
&self.payload
}
#[must_use]
pub const fn hops_remaining(&self) -> u16 {
self.hops_remaining
}
#[must_use]
pub const fn metadata(&self) -> &PacketMetadata {
&self.metadata
}
pub(crate) fn expired_at(&self, at: SimTime) -> bool {
self.metadata
.expires_at
.is_some_and(|expires| at >= expires)
}
pub(crate) fn forwarded(&self) -> Option<Self> {
self.hops_remaining.checked_sub(1).map(|remaining| {
let mut packet = self.clone();
packet.hops_remaining = remaining;
packet
})
}
}
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum DropReason {
EndpointRetired,
ChannelSevered {
channel: ChannelId,
},
OutOfRange {
channel: ChannelId,
},
ReceiverInterference {
channel: ChannelId,
},
QueueOverflow {
channel: ChannelId,
},
MtuExceeded {
channel: ChannelId,
mtu_bytes: usize,
wire_bytes: usize,
},
ChannelLoss {
channel: ChannelId,
},
Expired,
NoForwardingRule,
WrongDestination,
UnsupportedDeviceRole,
HopLimitExceeded,
}
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum NetworkEvent {
TransmissionStarted {
at: SimTime,
packet: Packet,
channel: ChannelId,
from: DeviceId,
to: DeviceId,
receive_at: SimTime,
distance_mm: Option<u64>,
effective_bit_rate_bps: u64,
frequency_band: Option<FrequencyBand>,
},
DataReceived {
at: SimTime,
packet: Packet,
channel: ChannelId,
from: DeviceId,
device: DeviceId,
},
PacketDelivered {
at: SimTime,
packet: Packet,
sink: DeviceId,
},
PacketDropped {
at: SimTime,
packet: Packet,
device: DeviceId,
reason: DropReason,
},
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ChannelMetrics {
pub at: SimTime,
pub distance_m: Option<f64>,
pub propagation_delay_ns: u64,
pub effective_bit_rate_bps: Option<u64>,
pub available: bool,
}
#[derive(Clone, Debug, PartialEq)]
pub struct TransmissionMetrics {
pub at: SimTime,
pub receiver: DeviceId,
pub distance_m: Option<f64>,
pub propagation_delay_ns: u64,
pub frequency_band: Option<FrequencyBand>,
pub jammed: f64,
pub base_bit_rate_bps: Option<u64>,
pub effective_bit_rate_bps: Option<u64>,
pub available: bool,
}
impl NetworkEvent {
#[must_use]
pub const fn time(&self) -> SimTime {
match self {
Self::TransmissionStarted { at, .. }
| Self::DataReceived { at, .. }
| Self::PacketDelivered { at, .. }
| Self::PacketDropped { at, .. } => *at,
}
}
}