Add a constant for socket timeout.
authorGreg Burri <greg.burri@gmail.com>
Thu, 19 Oct 2023 20:15:31 +0000 (22:15 +0200)
committerGreg Burri <greg.burri@gmail.com>
Thu, 19 Oct 2023 20:15:31 +0000 (22:15 +0200)
Increase timeout from 5 s to 7 s.

src/main.rs

index 967eb5b..c7791a0 100644 (file)
@@ -19,6 +19,7 @@ const FILE_CONF: &str = "config.ron";
 const PING_PERIOD: Duration = Duration::from_secs(5); // 5 s.
 const EMAIL_RESEND_PERIOD: Duration = Duration::from_secs(2 * 60 * 60); // 2 h.
 const STATE_PRINT_PERIOD: Duration = Duration::from_secs(15 * 60); // 15 min.
+const SOCKET_TIMEOUT: Duration = Duration::from_secs(7);
 
 fn main() -> Result<()> {
     println!("Staking Watchdog Watchdog");
@@ -44,12 +45,9 @@ fn main() -> Result<()> {
 
     let socket = UdpSocket::bind("0.0.0.0:0").unwrap();
     socket.connect("192.168.2.102:8739").unwrap();
-    socket
-        .set_read_timeout(Some(Duration::from_secs(5)))
-        .unwrap();
-    socket
-        .set_write_timeout(Some(Duration::from_secs(5)))
-        .unwrap();
+    socket.set_nonblocking(false).unwrap();
+    socket.set_read_timeout(Some(SOCKET_TIMEOUT)).unwrap();
+    socket.set_write_timeout(Some(SOCKET_TIMEOUT)).unwrap();
 
     loop {
         let time_beginning_loop = time::Instant::now();
@@ -81,7 +79,7 @@ fn main() -> Result<()> {
                     // Send e-mail.
                     println!("Sending email...");
                     match send_email(
-                        "Watchdog ERROR",
+                        "Watchdog Watchdog ERROR",
                         &format!("Error: {:?}", error),
                         &config.smtp_login,
                         &config.smtp_password,
@@ -107,7 +105,8 @@ fn main() -> Result<()> {
 
 #[derive(Debug)]
 enum PingError {
-    SocketError(std::io::Error),
+    SocketReceiveError(std::io::Error),
+    SocketSendError(std::io::Error),
     WrongMessageReceived(String),
 }
 
@@ -136,10 +135,10 @@ fn ping(socket: &UdpSocket, rng: &mut ThreadRng) -> std::result::Result<Duration
                         )));
                     }
                 }
-                Err(error) => return Err(PingError::SocketError(error)),
+                Err(error) => return Err(PingError::SocketReceiveError(error)),
             }
         }
-        Err(error) => return Err(PingError::SocketError(error)),
+        Err(error) => return Err(PingError::SocketSendError(error)),
     }
 
     Ok(time::Instant::now() - now)
@@ -148,7 +147,7 @@ fn ping(socket: &UdpSocket, rng: &mut ThreadRng) -> std::result::Result<Duration
 fn send_email(title: &str, body: &str, login: &str, pass: &str) -> Result<()> {
     let email = Message::builder()
         .message_id(None)
-        .from("Staking Watchdog <redmine@d-lan.net>".parse()?)
+        .from("Staking Watchdog Watchdog <redmine@d-lan.net>".parse()?)
         .to("Greg Burri <greg.burri@gmail.com>".parse()?)
         .subject(title)
         .header(ContentType::TEXT_PLAIN)