[fractal] utils: Add method to check if host is reachable



commit 753ccd6912abee6dc747818012e62576bf6e09f8
Author: Julian Sparber <julian sparber net>
Date:   Mon Aug 15 10:22:20 2022 +0200

    utils: Add method to check if host is reachable

 src/utils.rs | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
---
diff --git a/src/utils.rs b/src/utils.rs
index dcb190f0f..c17feba8d 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -446,3 +446,19 @@ pub fn freplace(s: String, args: &[(&str, &str)]) -> String {
 
     s
 }
+
+pub async fn check_if_reachable(hostname: &impl AsRef<str>) -> bool {
+    let address = gio::NetworkAddress::parse_uri(hostname.as_ref(), 80).unwrap();
+    let monitor = gio::NetworkMonitor::default();
+    match monitor.can_reach_future(&address).await {
+        Ok(()) => true,
+        Err(error) => {
+            log::error!(
+                "Homeserver {} isn't reachable: {}",
+                hostname.as_ref(),
+                error
+            );
+            false
+        }
+    }
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]