Skip to main content

pong_rs/task/icmp/
icmp_executor.rs

1use crate::executor::Executor;
2use crate::ping_error::PingError;
3use crate::task::icmp::icmp_ping::IcmpPing;
4use async_trait::async_trait;
5use log::trace;
6use std::net::IpAddr;
7use std::time::Duration;
8use wheel_rs::dns_utils::parse_host;
9
10#[derive(Clone)]
11pub struct IcmpExecutor {
12    ip_addr: IpAddr,
13    timeout: Duration,
14    icmp_ping: IcmpPing,
15}
16
17impl IcmpExecutor {
18    /// 构造函数
19    /// # 参数
20    /// * `host` - 要ping的主机名或 IP 地址
21    /// * `timeout` - 一个 `Duration`,表示超时时间
22    pub fn new(host: String, timeout: Duration) -> Self {
23        // 解析主机的字符串成IP地址
24        let ip_addr = parse_host(&host).unwrap();
25        Self {
26            ip_addr,
27            timeout,
28            icmp_ping: IcmpPing::new(),
29        }
30    }
31}
32
33#[async_trait]
34impl Executor for IcmpExecutor {
35    fn get_name(&self) -> String {
36        String::from("ICMP")
37    }
38
39    async fn exec(&self) -> Result<(), PingError> {
40        trace!("开始执行 ICMP 任务: ping {}", self.ip_addr);
41        self.icmp_ping.ping(self.ip_addr, self.timeout)
42    }
43}