From 8a31a707d99c20d08e8c780241082e1e1a24bde9 Mon Sep 17 00:00:00 2001 From: hinto-janaiyo Date: Thu, 17 Nov 2022 22:45:57 -0500 Subject: [PATCH] node: implement async ping --- Cargo.toml | 1 + src/gupax.rs | 4 +-- src/node.rs | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++- src/update.rs | 2 +- 4 files changed, 76 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d69ebb3..888f0a3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/gupax.rs b/src/gupax.rs index 8eed214..266c86f 100644 --- a/src/gupax.rs +++ b/src/gupax.rs @@ -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|| { diff --git a/src/node.rs b/src/node.rs index f2fb2aa..18ee5ab 100644 --- a/src/node.rs +++ b/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 { 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>, og: Arc>) -> Result, 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::builder() + .build(hyper::client::HttpConnector::new()); + + // Random User Agent + let rand_user_agent = crate::Pkg::get_user_agent(); + // Handle vector +// let mut handles: Vec>> = 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> = 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, request: hyper::Request, ip: &'static str) -> Result { + 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: diff --git a/src/update.rs b/src/update.rs index c04f525..550bbdd 100644 --- a/src/update.rs +++ b/src/update.rs @@ -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);