From 55a3539f7b16f3c9fc61c391f8f6376d771a4475 Mon Sep 17 00:00:00 2001 From: Aviion Gibbs Date: Fri, 15 Sep 2023 22:06:30 +0930 Subject: [PATCH] Add: basic network test Added a basic network test to probe the google website at boot. This ensures that the user has a stable and functional internet connection at boot which is a requirement for the application to function correctly, will work on making a warning message if failed. --- .vscode/launch.json | 64 +++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 64 ++++++++++++++++++++++++++++++++++++--------- 2 files changed, 115 insertions(+), 13 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..4a675c7 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,64 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "lldb", + "request": "launch", + "name": "Debug unit tests in library 'vault_gui'", + "cargo": { + "args": [ + "test", + "--no-run", + "--lib", + "--package=vault_gui" + ], + "filter": { + "name": "vault_gui", + "kind": "lib" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + }, + { + "type": "lldb", + "request": "launch", + "name": "Debug executable 'vault_gui'", + "cargo": { + "args": [ + "build", + "--bin=vault_gui", + "--package=vault_gui" + ], + "filter": { + "name": "vault_gui", + "kind": "bin" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + }, + { + "type": "lldb", + "request": "launch", + "name": "Debug unit tests in executable 'vault_gui'", + "cargo": { + "args": [ + "test", + "--no-run", + "--bin=vault_gui", + "--package=vault_gui" + ], + "filter": { + "name": "vault_gui", + "kind": "bin" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + } + ] +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index c3a83ae..6ffce80 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,7 +24,10 @@ struct MyApp { closable: bool, duration: f32, - server_valid: bool, + network_alive: bool, + connect: bool, + + db_ip_valid: bool, ip: String, port: i32, @@ -44,7 +47,10 @@ impl Default for MyApp { closable: true, duration: 3.5, - server_valid: false, + network_alive: false, + connect: false, + + db_ip_valid: false, login_bool: false, ip: "127.0.0.1".to_owned(), @@ -70,7 +76,35 @@ impl eframe::App for MyApp { .set_duration(Some(Duration::from_millis((1000. * self.duration) as u64))); }; - if !self.server_valid { + if self.network_alive == false { + //Tests if the end user has internet access. + let iptest = "142.250.70.142".to_owned(); + let ip: IpAddr = IpAddr::from_str(&iptest).unwrap(); + let port = 80; + + // Create a channel to communicate the result of the ping test + let tx = self.tx.clone(); + + thread::spawn(move || { + let result = tokio::runtime::Runtime::new() + .unwrap() + .block_on(is_server_alive(ip, port as u16, 30)); + tx.send(dbg!(result)).expect("Failed to send result"); + }); + + if let Ok(result) = self.rx.try_recv() { + if result { + cb(self.toasts.success("Network Connection Established!")); + println!("Network Connection Established"); + self.network_alive = true + } else { + cb(self.toasts.error("Network Connection Failed!!")); + println!("Network Connection Failed!"); + } + } + } + + if !self.db_ip_valid && self.network_alive { ui.heading("Vault GUI"); ui.label("Please enter the ip and port of the sql server below"); @@ -93,6 +127,7 @@ impl eframe::App for MyApp { }); if ui.button("Connect").clicked() { + self.connect = true; cb(self.toasts.info("Testing connection...")); let ip_verified = validate_ip_address(&self.ip); @@ -121,7 +156,7 @@ impl eframe::App for MyApp { } } - if !self.login_bool && self.server_valid { + if !self.login_bool && self.db_ip_valid { ui.label("Please enter your username and password below"); ui.horizontal(|ui| { @@ -141,19 +176,22 @@ impl eframe::App for MyApp { } if ui.button("Return").clicked() { - self.server_valid = false; + self.connect = false; + self.db_ip_valid = false; } }); } - if let Ok(result) = self.rx.try_recv() { - if result { - cb(self.toasts.success("Connection Successful!")); - self.server_valid = true; - println!("Connection Successful!") - } else { - cb(self.toasts.error("Connection Failed!")); - println!("Connection Failed!") + if self.network_alive && self.connect { + if let Ok(result) = self.rx.try_recv() { + if result && !self.db_ip_valid { + cb(self.toasts.success("Connection Successful!")); + self.db_ip_valid = true; + println!("Connection Successful!") + } else if !self.db_ip_valid { + cb(self.toasts.error("Connection Failed!")); + println!("Connection Failed!") + } } } });