use async_trait::async_trait;
use std::{
net::{IpAddr, SocketAddr},
sync::Arc,
};
use tokio::net::{TcpStream, UdpSocket};
use crate::{
NetworkInterface, RestartPolicy,
network::{run_tcp, run_udp},
supervisor::{SupervisedTask, Task},
};
pub struct Tcp;
pub struct Udp;
#[async_trait]
pub trait TcpHandler: Send + Sync + 'static {
fn name(&self) -> &'static str;
fn port(&self) -> u16;
fn bind_mode(&self) -> crate::BindMode {
crate::BindMode::PreferInterface
}
async fn on_connection(&self, stream: TcpStream, peer: &SocketAddr);
}
#[async_trait]
pub trait UdpHandler: Send + Sync + 'static {
fn name(&self) -> &'static str;
fn port(&self) -> u16;
fn bind_mode(&self) -> crate::BindMode {
crate::BindMode::PreferInterface
}
fn multicast_addrs(&self) -> &[IpAddr] {
&[]
}
async fn on_packet(&self, data: &[u8], socket: Arc<UdpSocket>, peer: &SocketAddr);
}
pub trait Service<Kind> {
fn into_task(self, iface: Arc<NetworkInterface>, policy: RestartPolicy) -> Box<dyn Task>;
}
impl<T> Service<Tcp> for T
where
T: TcpHandler,
{
fn into_task(self, iface: Arc<NetworkInterface>, policy: RestartPolicy) -> Box<dyn Task> {
let handler = Arc::new(self);
Box::new(SupervisedTask::new(handler.name(), policy, move || {
let h = handler.clone();
let i = iface.clone();
Box::pin(async move { run_tcp(h, i).await })
}))
}
}
impl<T> Service<Udp> for T
where
T: UdpHandler,
{
fn into_task(self, iface: Arc<NetworkInterface>, policy: RestartPolicy) -> Box<dyn Task> {
let handler = Arc::new(self);
Box::new(SupervisedTask::new(handler.name(), policy, move || {
let h = handler.clone();
let i = iface.clone();
Box::pin(async move { run_udp(h, i).await })
}))
}
}
#[cfg(test)]
mod tests {
use std::str::FromStr;
use super::*;
struct MockTcp;
#[async_trait]
impl TcpHandler for MockTcp {
fn name(&self) -> &'static str {
"MockTcp"
}
fn port(&self) -> u16 {
0
}
async fn on_connection(&self, _s: TcpStream, _p: &SocketAddr) {}
}
struct MockUdp;
#[async_trait]
impl UdpHandler for MockUdp {
fn name(&self) -> &'static str {
"MockUdp"
}
fn port(&self) -> u16 {
0
}
async fn on_packet(&self, _data: &[u8], _socket: Arc<UdpSocket>, _peer: &SocketAddr) {}
}
#[test]
fn test_tcp_into_task() {
let iface = Arc::new(NetworkInterface::from_str("lo").unwrap());
let service = MockTcp;
let _task = Service::<Tcp>::into_task(service, iface, RestartPolicy::default());
}
#[test]
fn test_udp_into_task() {
let iface = Arc::new(NetworkInterface::from_str("lo").unwrap());
let service = MockUdp;
let _task = Service::<Udp>::into_task(service, iface, RestartPolicy::default());
}
}