mirror of
https://github.com/Cyrix126/gupaxx.git
synced 2024-12-22 14:49:21 +00:00
node: implement async ping
This commit is contained in:
parent
adaaca394a
commit
8a31a707d9
4 changed files with 76 additions and 4 deletions
|
@ -30,6 +30,7 @@ figment = { version = "0.10.8", features = ["toml"] }
|
|||
flate2 = "1.0"
|
||||
hex-literal = "0.3.4"
|
||||
hyper = "0.14.20"
|
||||
#hyper = { version = "0.14.20", features = ["full"] }
|
||||
hyper-tls = "0.5.0"
|
||||
image = { version = "0.24.4", features = ["png"] }
|
||||
log = "0.4.17"
|
||||
|
|
|
@ -153,7 +153,7 @@ impl Gupax {
|
|||
}
|
||||
ui.spacing_mut().text_edit_width = ui.available_width() - SPACE;
|
||||
ui.set_enabled(!file_window.lock().unwrap().thread);
|
||||
if ui.button("Select").on_hover_text(GUPAX_SELECT).clicked() {
|
||||
if ui.button("Open").on_hover_text(GUPAX_SELECT).clicked() {
|
||||
file_window.lock().unwrap().thread = true;
|
||||
let file_window = Arc::clone(file_window);
|
||||
thread::spawn(move|| {
|
||||
|
@ -187,7 +187,7 @@ impl Gupax {
|
|||
}
|
||||
ui.spacing_mut().text_edit_width = ui.available_width() - SPACE;
|
||||
ui.set_enabled(!file_window.lock().unwrap().thread);
|
||||
if ui.button("Select").on_hover_text(GUPAX_SELECT).clicked() {
|
||||
if ui.button("Open ").on_hover_text(GUPAX_SELECT).clicked() {
|
||||
file_window.lock().unwrap().thread = true;
|
||||
let file_window = Arc::clone(file_window);
|
||||
thread::spawn(move|| {
|
||||
|
|
73
src/node.rs
73
src/node.rs
|
@ -22,6 +22,11 @@ use std::collections::HashMap;
|
|||
use std::sync::{Arc,Mutex};
|
||||
use egui::Color32;
|
||||
use log::*;
|
||||
//use hyper::{
|
||||
// Client,Body,Request,
|
||||
// header::{HeaderValue,LOCATION},
|
||||
//};
|
||||
|
||||
use reqwest::blocking::ClientBuilder;
|
||||
|
||||
//---------------------------------------------------------------------------------------------------- Node list
|
||||
|
@ -75,7 +80,7 @@ pub struct NodeData {
|
|||
impl NodeData {
|
||||
pub fn new_vec() -> Vec<Self> {
|
||||
let mut vec = Vec::new();
|
||||
for ip in NODE_IPS.iter() {
|
||||
for ip in NODE_IPS {
|
||||
vec.push(Self {
|
||||
id: ip_to_enum(ip),
|
||||
ip,
|
||||
|
@ -188,6 +193,72 @@ pub fn format_enum(id: NodeEnum) -> String {
|
|||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------------------- Main Ping function
|
||||
#[tokio::main]
|
||||
pub async fn start(ping: Arc<Mutex<Ping>>, og: Arc<Mutex<State>>) -> Result<Vec<NodeData>, anyhow::Error> {
|
||||
// Start ping
|
||||
ping.lock().unwrap().pinging = true;
|
||||
ping.lock().unwrap().prog = 0.0;
|
||||
|
||||
// Create HTTP client
|
||||
let info = format!("{}", "Creating HTTP Client");
|
||||
ping.lock().unwrap().msg = info;
|
||||
let client: hyper::client::Client<hyper::client::HttpConnector> = hyper::Client::builder()
|
||||
.build(hyper::client::HttpConnector::new());
|
||||
|
||||
// Random User Agent
|
||||
let rand_user_agent = crate::Pkg::get_user_agent();
|
||||
// Handle vector
|
||||
// let mut handles: Vec<tokio::task::JoinHandle<Result<NodeData, anyhow::Error>>> = vec![];
|
||||
let mut handles = vec![];
|
||||
|
||||
for ip in NODE_IPS {
|
||||
let client = client.clone();
|
||||
let request = hyper::Request::builder()
|
||||
.method("POST")
|
||||
.uri("http://".to_string() + ip + "/json_rpc")
|
||||
.header("User-Agent", rand_user_agent)
|
||||
.body(hyper::Body::from(r#"{"jsonrpc":"2.0","id":"0","method":"get_info"}"#))
|
||||
.unwrap();
|
||||
let handle = tokio::spawn(async move { response(client, request, ip).await });
|
||||
// let handle: tokio::task::JoinHandle<Result<NodeData, anyhow::Error>> = tokio::spawn(async move { response(client, request, ip).await });
|
||||
handles.push(handle);
|
||||
}
|
||||
|
||||
let mut node_vec = vec![];
|
||||
for handle in handles {
|
||||
match handle.await {
|
||||
Ok(data) => match data { Ok(data) => node_vec.push(data), _ => return Err(anyhow::Error::msg("fail")) },
|
||||
_ => return Err(anyhow::Error::msg("fail")),
|
||||
};
|
||||
}
|
||||
|
||||
Ok(node_vec)
|
||||
}
|
||||
|
||||
async fn response(client: hyper::client::Client<hyper::client::HttpConnector>, request: hyper::Request<hyper::Body>, ip: &'static str) -> Result<NodeData, anyhow::Error> {
|
||||
let now = Instant::now();
|
||||
let response = tokio::time::timeout(Duration::from_secs(5), client.request(request)).await?;
|
||||
let ms = now.elapsed().as_millis();
|
||||
let mut color = Color32::BLACK;
|
||||
if ms < 300 {
|
||||
// GREEN
|
||||
color = Color32::from_rgb(100, 230, 100);
|
||||
} else if ms < 1000 {
|
||||
// YELLOW
|
||||
color = Color32::from_rgb(230, 230, 100);
|
||||
} else if ms < 5000 {
|
||||
// RED
|
||||
color = Color32::from_rgb(230, 50, 50);
|
||||
}
|
||||
Ok(NodeData {
|
||||
id: ip_to_enum(ip),
|
||||
ip,
|
||||
ms,
|
||||
color,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
// This is for pinging the community nodes to
|
||||
// find the fastest/slowest one for the user.
|
||||
// The process:
|
||||
|
|
|
@ -673,7 +673,7 @@ impl Pkg {
|
|||
|
||||
//---------------------------------------------------------------------------------------------------- Pkg functions
|
||||
// Generate fake [User-Agent] HTTP header
|
||||
fn get_user_agent() -> &'static str {
|
||||
pub fn get_user_agent() -> &'static str {
|
||||
let rand = thread_rng().gen_range(0..50);
|
||||
let user_agent = FAKE_USER_AGENT[rand];
|
||||
info!("Update | Randomly selecting User-Agent ({}/50) ... {}", rand, user_agent);
|
||||
|
|
Loading…
Reference in a new issue