extern crate clap; extern crate time; extern crate task1; extern crate sys_info; use std::sync::mpsc::channel; use time::get_time; use clap::{Arg, App, SubCommand}; use std::{process, thread}; use task1::verify_product; use std::sync::Arc; pub fn main() { let matches = create_app().get_matches(); let base = matches.value_of("base").unwrap_or("1"); let diff = Arc::new(matches.value_of("difficulty").unwrap_or("1").to_string()); let cpus = sys_info::cpu_num().unwrap_or(1).to_string(); let threads = matches.value_of("threads").unwrap_or(&cpus); let mut time_measurement = false; if diff.chars().any(|c| !c.is_digit(16)) { println!("Difficulty is not hexadecimal."); process::exit(1) } if let Some(ref sub_command) = matches.subcommand { if sub_command.name.eq("timings") { time_measurement = true; } } if true { println!("--------------------------------------------"); println!( "Container: : \"{}\"", sys_info::hostname().unwrap_or("-".to_string()) ); println!("Physical CPUs : {}", sys_info::cpu_num().unwrap_or(0)); println!("Logical CPUs : {}", sys_info::cpu_num().unwrap_or(0)); println!("CPU Speed : {}", sys_info::cpu_speed().unwrap_or(0)); println!("Load Average : {:?}", sys_info::loadavg().unwrap()); println!("Processes : {}", sys_info::proc_total().unwrap_or(0)); println!("--------------------------------------------"); } match (base.parse::(), threads.parse::()) { (Ok(b), Ok(t)) => { println!("Using base: {}", b); println!("Using difficulty: {}", diff); println!("Please wait..."); let start = get_time(); let max = ::max_value(); let mut children = vec![]; let (tx, rx) = channel(); for i in 0..t { let d = diff.clone(); let tx = tx.clone(); children.push(thread::spawn(move || { let from = max / t * i; let to = max / t * (i + 1); println!("{} - {} | Thread {} | Difficulty is: {}", from, to, i, d); for n in from..to { if let Some(x) = verify_product(b, n, &d) { let end = get_time(); println!("Number: {} --> hash: {}", x.number, x.hash); if time_measurement { let diff = end - start; let s = diff.num_seconds(); let ms = diff.num_milliseconds(); let us = diff.num_microseconds().unwrap_or(ms * 1000); println!("(Duration {}s / {}ms / {}us)", s, ms, us); } tx.send("Finished").unwrap(); } } })); } match rx.recv() { Ok(msg) => { println!("{}", msg); } Err(_) => {} } /* for child in children { let _ = child.join(); } */ } (_, Err(_)) => { println!("Number of threads is not integer."); process::exit(1) } (Err(_), _) => { println!("Base is not integer."); process::exit(1) } }; } fn create_app<'a, 'b>() -> App<'a, 'b> { App::new("Hash256") .version("1.0") .author("Lorenz Bung & Joshua Rutschmann") .about( "Calculates the Hashvalue of the given base, number and difficulty.", ) .arg( Arg::with_name("base") .value_name("base") .help("The base of the hash to be calculated on.") .takes_value(true) .required(true), ) .arg( Arg::with_name("difficulty") .value_name("difficulty") .help("The difficulty of the calculated hash.") .takes_value(true) .required(true), ) .arg( Arg::with_name("threads") .value_name("threads") .help( "Sets the number of the threads to use (default = number of cpus)", ) .takes_value(true) .required(false), ) .subcommand( SubCommand::with_name("timings") .about("controls timing features") .version("1.0") .author("Lorenz Bung & Joshua Rutschmann"), ) }