Skip to content

Commit

Permalink
Add: basic network test
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Zephira58 committed Sep 15, 2023
1 parent 74a84b6 commit 55a3539
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 13 deletions.
64 changes: 64 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -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}"
}
]
}
64 changes: 51 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand All @@ -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(),
Expand All @@ -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");

Expand All @@ -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);
Expand Down Expand Up @@ -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| {
Expand All @@ -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!")
}
}
}
});
Expand Down

0 comments on commit 55a3539

Please sign in to comment.