mirror of
https://github.com/Cyrix126/gupaxx.git
synced 2024-11-16 23:37:47 +00:00
update/ping: consolidate code in *::spawn_thread()
This commit is contained in:
parent
d0ced90bb1
commit
f2852549c2
6 changed files with 188 additions and 207 deletions
|
@ -59,16 +59,19 @@ pub fn get_gupax_data_path() -> Result<PathBuf, TomlError> {
|
|||
Some(mut path) => {
|
||||
path.push(DIRECTORY);
|
||||
info!("OS | Data path ... OK ... {}", path.display());
|
||||
create_gupax_dir(&path)?;
|
||||
Ok(path)
|
||||
},
|
||||
None => { error!("OS | Data path ... FAIL"); Err(TomlError::Path(PATH_ERROR.to_string())) },
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_gupax_dir(path: PathBuf) -> Result<(), TomlError> {
|
||||
pub fn create_gupax_dir(path: &PathBuf) -> Result<(), TomlError> {
|
||||
// Create directory
|
||||
fs::create_dir_all(&path)?;
|
||||
Ok(())
|
||||
match fs::create_dir_all(path) {
|
||||
Ok(_) => { info!("OS | Create data path ... OK"); Ok(()) },
|
||||
Err(e) => { error!("OS | Create data path ... FAIL ... {}", e); Err(TomlError::Io(e)) },
|
||||
}
|
||||
}
|
||||
|
||||
// Convert a [File] path to a [String]
|
||||
|
|
29
src/gupax.rs
29
src/gupax.rs
|
@ -71,34 +71,7 @@ impl Gupax {
|
|||
ui.vertical(|ui| {
|
||||
ui.set_enabled(!updating);
|
||||
if ui.add_sized([width, height], egui::Button::new("Check for updates")).on_hover_text(GUPAX_UPDATE).clicked() {
|
||||
update.lock().unwrap().path_p2pool = og.lock().unwrap().gupax.absolute_p2pool_path.display().to_string();
|
||||
update.lock().unwrap().path_xmrig = og.lock().unwrap().gupax.absolute_xmrig_path.display().to_string();
|
||||
update.lock().unwrap().tor = og.lock().unwrap().gupax.update_via_tor;
|
||||
let og = Arc::clone(&og);
|
||||
let state_ver = Arc::clone(&state_ver);
|
||||
let update = Arc::clone(&update);
|
||||
let update_thread = Arc::clone(&update);
|
||||
let state_path = state_path.clone();
|
||||
thread::spawn(move|| {
|
||||
info!("Spawning update thread...");
|
||||
match Update::start(update_thread, og.clone(), state_ver.clone()) {
|
||||
Err(e) => {
|
||||
info!("Update ... FAIL ... {}", e);
|
||||
*update.lock().unwrap().msg.lock().unwrap() = format!("{} | {}", MSG_FAILED, e);
|
||||
},
|
||||
_ => {
|
||||
info!("Update | Saving state...");
|
||||
match State::save(&mut og.lock().unwrap(), &state_path) {
|
||||
Ok(_) => info!("Update ... OK"),
|
||||
Err(e) => {
|
||||
warn!("Update | Saving state ... FAIL ... {}", e);
|
||||
*update.lock().unwrap().msg.lock().unwrap() = format!("Saving new versions into state failed");
|
||||
},
|
||||
};
|
||||
}
|
||||
};
|
||||
*update.lock().unwrap().updating.lock().unwrap() = false;
|
||||
});
|
||||
Update::spawn_thread(og, update, state_ver, state_path);
|
||||
}
|
||||
});
|
||||
ui.vertical(|ui| {
|
||||
|
|
41
src/main.rs
41
src/main.rs
|
@ -440,6 +440,7 @@ fn init_options() -> NativeOptions {
|
|||
}
|
||||
|
||||
fn init_auto(app: &App) {
|
||||
// Return early if [--no-startup] was not passed
|
||||
if app.no_startup {
|
||||
info!("[--no-startup] flag passed, skipping init_auto()...");
|
||||
return
|
||||
|
@ -449,39 +450,10 @@ fn init_auto(app: &App) {
|
|||
} else {
|
||||
info!("Starting init_auto()...");
|
||||
}
|
||||
|
||||
// [Auto-Update]
|
||||
if app.state.gupax.auto_update {
|
||||
let path_p2pool = app.og.lock().unwrap().gupax.absolute_p2pool_path.display().to_string();
|
||||
let path_xmrig = app.og.lock().unwrap().gupax.absolute_xmrig_path.display().to_string();
|
||||
let tor = app.og.lock().unwrap().gupax.update_via_tor;
|
||||
app.update.lock().unwrap().path_p2pool = path_p2pool;
|
||||
app.update.lock().unwrap().path_xmrig = path_xmrig;
|
||||
app.update.lock().unwrap().tor = tor;
|
||||
let og = Arc::clone(&app.og);
|
||||
let state_ver = Arc::clone(&app.state.version);
|
||||
let update = Arc::clone(&app.update);
|
||||
let update_thread = Arc::clone(&app.update);
|
||||
let state_path = app.state_path.clone();
|
||||
thread::spawn(move|| {
|
||||
info!("Spawning update thread...");
|
||||
match Update::start(update_thread, og.clone(), state_ver.clone()) {
|
||||
Err(e) => {
|
||||
info!("Update ... FAIL ... {}", e);
|
||||
*update.lock().unwrap().msg.lock().unwrap() = format!("{} | {}", MSG_FAILED, e);
|
||||
},
|
||||
_ => {
|
||||
info!("Update | Saving state...");
|
||||
match State::save(&mut og.lock().unwrap(), &state_path) {
|
||||
Ok(_) => info!("Update ... OK"),
|
||||
Err(e) => {
|
||||
warn!("Update | Saving state ... FAIL ... {}", e);
|
||||
*update.lock().unwrap().msg.lock().unwrap() = format!("Saving new versions into state failed");
|
||||
},
|
||||
};
|
||||
}
|
||||
};
|
||||
*update.lock().unwrap().updating.lock().unwrap() = false;
|
||||
});
|
||||
Update::spawn_thread(&app.og, &app.update, &app.state.version, &app.state_path);
|
||||
} else {
|
||||
info!("Skipping auto-update...");
|
||||
}
|
||||
|
@ -490,12 +462,7 @@ fn init_auto(app: &App) {
|
|||
let auto_node = app.og.lock().unwrap().p2pool.auto_node;
|
||||
let simple = app.og.lock().unwrap().p2pool.simple;
|
||||
if auto_node && simple {
|
||||
let ping = Arc::clone(&app.ping);
|
||||
let og = Arc::clone(&app.og);
|
||||
thread::spawn(move|| {
|
||||
info!("Spawning ping thread...");
|
||||
crate::node::ping(ping, og);
|
||||
});
|
||||
Ping::spawn_thread(&app.ping, &app.og)
|
||||
} else {
|
||||
info!("Skipping auto-ping...");
|
||||
}
|
||||
|
|
116
src/node.rs
116
src/node.rs
|
@ -90,32 +90,6 @@ impl NodeData {
|
|||
}
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------------------- Ping data
|
||||
#[derive(Debug)]
|
||||
pub struct Ping {
|
||||
pub nodes: Vec<NodeData>,
|
||||
pub fastest: NodeEnum,
|
||||
pub pinging: bool,
|
||||
pub msg: String,
|
||||
pub prog: f32,
|
||||
pub pinged: bool,
|
||||
pub auto_selected: bool,
|
||||
}
|
||||
|
||||
impl Ping {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
nodes: NodeData::new_vec(),
|
||||
fastest: NodeEnum::C3pool,
|
||||
pinging: false,
|
||||
msg: "No ping in progress".to_string(),
|
||||
prog: 0.0,
|
||||
pinged: false,
|
||||
auto_selected: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------------------- IP <-> Enum functions
|
||||
use crate::NodeEnum::*;
|
||||
// Function for returning IP/Enum
|
||||
|
@ -191,26 +165,69 @@ pub fn format_enum(id: NodeEnum) -> String {
|
|||
}
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------------------- Main Ping function
|
||||
// This is for pinging the community nodes to
|
||||
// find the fastest/slowest one for the user.
|
||||
// The process:
|
||||
// - Send [get_info] JSON-RPC request over HTTP to all IPs
|
||||
// - Measure each request in milliseconds
|
||||
// - Timeout on requests over 5 seconds
|
||||
// - Add data to appropriate struct
|
||||
// - Sorting fastest to lowest is automatic (fastest nodes return ... the fastest)
|
||||
//
|
||||
// This used to be done 3x linearly but after testing, sending a single
|
||||
// JSON-RPC call to all IPs asynchronously resulted in the same data.
|
||||
//
|
||||
// <300ms = GREEN
|
||||
// <1000ms = YELLOW
|
||||
// >1000ms = RED
|
||||
// timeout = BLACK
|
||||
// default = GRAY
|
||||
#[tokio::main]
|
||||
pub async fn ping(ping: Arc<Mutex<Ping>>, og: Arc<Mutex<State>>) -> Result<(), anyhow::Error> {
|
||||
//---------------------------------------------------------------------------------------------------- Ping data
|
||||
#[derive(Debug)]
|
||||
pub struct Ping {
|
||||
pub nodes: Vec<NodeData>,
|
||||
pub fastest: NodeEnum,
|
||||
pub pinging: bool,
|
||||
pub msg: String,
|
||||
pub prog: f32,
|
||||
pub pinged: bool,
|
||||
pub auto_selected: bool,
|
||||
}
|
||||
|
||||
impl Ping {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
nodes: NodeData::new_vec(),
|
||||
fastest: NodeEnum::C3pool,
|
||||
pinging: false,
|
||||
msg: "No ping in progress".to_string(),
|
||||
prog: 0.0,
|
||||
pinged: false,
|
||||
auto_selected: true,
|
||||
}
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------------------- Main Ping function
|
||||
// Intermediate thread for spawning thread
|
||||
pub fn spawn_thread(ping: &Arc<Mutex<Self>>, og: &Arc<Mutex<State>>) {
|
||||
let ping = Arc::clone(&ping);
|
||||
let og = Arc::clone(og);
|
||||
std::thread::spawn(move|| {
|
||||
info!("Spawning ping thread...");
|
||||
match Self::ping(ping.clone(), og) {
|
||||
Ok(_) => info!("Ping ... OK"),
|
||||
Err(err) => {
|
||||
error!("Ping ... FAIL ... {}", err);
|
||||
ping.lock().unwrap().pinged = false;
|
||||
ping.lock().unwrap().msg = err.to_string();
|
||||
},
|
||||
};
|
||||
ping.lock().unwrap().pinging = false;
|
||||
});
|
||||
}
|
||||
|
||||
// This is for pinging the community nodes to
|
||||
// find the fastest/slowest one for the user.
|
||||
// The process:
|
||||
// - Send [get_info] JSON-RPC request over HTTP to all IPs
|
||||
// - Measure each request in milliseconds
|
||||
// - Timeout on requests over 5 seconds
|
||||
// - Add data to appropriate struct
|
||||
// - Sorting fastest to lowest is automatic (fastest nodes return ... the fastest)
|
||||
//
|
||||
// This used to be done 3x linearly but after testing, sending a single
|
||||
// JSON-RPC call to all IPs asynchronously resulted in the same data.
|
||||
//
|
||||
// <300ms = GREEN
|
||||
// <1000ms = YELLOW
|
||||
// >1000ms = RED
|
||||
// timeout = BLACK
|
||||
// default = GRAY
|
||||
#[tokio::main]
|
||||
pub async fn ping(ping: Arc<Mutex<Self>>, og: Arc<Mutex<State>>) -> Result<(), anyhow::Error> {
|
||||
// Timer
|
||||
let now = Instant::now();
|
||||
|
||||
|
@ -241,7 +258,7 @@ pub async fn ping(ping: Arc<Mutex<Ping>>, og: Arc<Mutex<State>>) -> Result<(), a
|
|||
.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, ping, percent, node_vec).await });
|
||||
let handle = tokio::spawn(async move { Self::response(client, request, ip, ping, percent, node_vec).await });
|
||||
handles.push(handle);
|
||||
}
|
||||
|
||||
|
@ -263,9 +280,9 @@ pub async fn ping(ping: Arc<Mutex<Ping>>, og: Arc<Mutex<State>>) -> Result<(), a
|
|||
ping.auto_selected = false;
|
||||
drop(ping);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
async fn response(client: hyper::client::Client<hyper::client::HttpConnector>, request: hyper::Request<hyper::Body>, ip: &'static str, ping: Arc<Mutex<Ping>>, percent: f32, node_vec: Arc<Mutex<Vec<NodeData>>>) {
|
||||
async fn response(client: hyper::client::Client<hyper::client::HttpConnector>, request: hyper::Request<hyper::Body>, ip: &'static str, ping: Arc<Mutex<Self>>, percent: f32, node_vec: Arc<Mutex<Vec<NodeData>>>) {
|
||||
let id = ip_to_enum(ip);
|
||||
let now = Instant::now();
|
||||
let ms;
|
||||
|
@ -297,4 +314,5 @@ async fn response(client: hyper::client::Client<hyper::client::HttpConnector>, r
|
|||
ping.prog += percent;
|
||||
drop(ping);
|
||||
node_vec.lock().unwrap().push(NodeData { id: ip_to_enum(ip), ip, ms, color, });
|
||||
}
|
||||
}
|
||||
|
|
|
@ -141,21 +141,7 @@ impl P2pool {
|
|||
// [Ping Button]
|
||||
ui.set_enabled(!ping.lock().unwrap().pinging);
|
||||
if ui.add_sized([width, height], Button::new("Ping community nodes")).on_hover_text(P2POOL_PING).clicked() {
|
||||
let ping1 = Arc::clone(&ping);
|
||||
let ping2 = Arc::clone(&ping);
|
||||
let og = Arc::clone(og);
|
||||
thread::spawn(move|| {
|
||||
info!("Spawning ping thread...");
|
||||
match crate::node::ping(ping1, og) {
|
||||
Ok(_) => info!("Ping ... OK"),
|
||||
Err(err) => {
|
||||
error!("Ping ... FAIL ... {}", err);
|
||||
ping2.lock().unwrap().pinged = false;
|
||||
ping2.lock().unwrap().msg = err.to_string();
|
||||
},
|
||||
};
|
||||
ping2.lock().unwrap().pinging = false;
|
||||
});
|
||||
Ping::spawn_thread(&ping, &og);
|
||||
}});
|
||||
|
||||
ui.vertical(|ui| {
|
||||
|
|
|
@ -288,6 +288,41 @@ impl Update {
|
|||
}
|
||||
}
|
||||
|
||||
// Intermediate function that spawns a new thread
|
||||
// which starts the async [start()] function that
|
||||
// actually contains the code. This is so that everytime
|
||||
// an update needs to happen (Gupax tab, auto-update), the
|
||||
// code only needs to be edited once, here.
|
||||
pub fn spawn_thread(og: &Arc<Mutex<State>>, update: &Arc<Mutex<Update>>, state_ver: &Arc<Mutex<Version>>, state_path: &PathBuf) {
|
||||
update.lock().unwrap().path_p2pool = og.lock().unwrap().gupax.absolute_p2pool_path.display().to_string();
|
||||
update.lock().unwrap().path_xmrig = og.lock().unwrap().gupax.absolute_xmrig_path.display().to_string();
|
||||
update.lock().unwrap().tor = og.lock().unwrap().gupax.update_via_tor;
|
||||
let og = Arc::clone(&og);
|
||||
let state_ver = Arc::clone(&state_ver);
|
||||
let update = Arc::clone(&update);
|
||||
let state_path = state_path.clone();
|
||||
std::thread::spawn(move|| {
|
||||
info!("Spawning update thread...");
|
||||
match Update::start(update.clone(), og.clone(), state_ver.clone()) {
|
||||
Err(e) => {
|
||||
info!("Update ... FAIL ... {}", e);
|
||||
*update.lock().unwrap().msg.lock().unwrap() = format!("{} | {}", MSG_FAILED, e);
|
||||
},
|
||||
_ => {
|
||||
info!("Update | Saving state...");
|
||||
match State::save(&mut og.lock().unwrap(), &state_path) {
|
||||
Ok(_) => info!("Update ... OK"),
|
||||
Err(e) => {
|
||||
warn!("Update | Saving state ... FAIL ... {}", e);
|
||||
*update.lock().unwrap().msg.lock().unwrap() = format!("Saving new versions into state failed");
|
||||
},
|
||||
};
|
||||
}
|
||||
};
|
||||
*update.lock().unwrap().updating.lock().unwrap() = false;
|
||||
});
|
||||
}
|
||||
|
||||
// Download process:
|
||||
// 0. setup tor, client, http, etc
|
||||
// 1. fill vector with all enums
|
||||
|
@ -295,7 +330,6 @@ impl Update {
|
|||
// 3. if current == version, remove from vec
|
||||
// 4. loop over vec, download links
|
||||
// 5. extract, upgrade
|
||||
|
||||
#[tokio::main]
|
||||
pub async fn start(update: Arc<Mutex<Self>>, og: Arc<Mutex<State>>, state_ver: Arc<Mutex<Version>>) -> Result<(), anyhow::Error> {
|
||||
//---------------------------------------------------------------------------------------------------- Init
|
||||
|
|
Loading…
Reference in a new issue