pong_rs/task/icmp/
icmp_executor.rs1use 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 pub fn new(host: String, timeout: Duration) -> Self {
23 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}