#[macro_use] extern crate clap; extern crate time; extern crate task1; extern crate sys_info; use std::sync::mpsc::channel; use time::get_time; use clap::App; use std::{process, thread}; use task1::verify_product; use std::sync::Arc; pub fn main() { let yaml = load_yaml!("cli.yml"); let matches = App::from_yaml(yaml).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 sync = matches.is_present("sync"); let wait = matches.is_present("wait"); let verbosity = matches.occurrences_of("verbose"); let mut time_measurement = false; // Falls irgendein Zeichen nicht hexadezimal ist, breche ab. if diff.chars().any(|c| !c.is_digit(16)) { println!("Difficulty is not hexadecimal."); process::exit(1) } // Falls das Unterkommando timings angegeben wurde aktiviere die Zeitmessung. if let Some(ref sub_command) = matches.subcommand { if sub_command.name.eq("timings") { time_measurement = true; } } if verbosity >= 1 { 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!("Please wait..."); if verbosity >= 1 { println!("Searching with {} threads", t); } let start = get_time(); let max = ::max_value(); let mut children = vec![]; let (tx, rx) = channel(); let mut found = &false; for i in 0..t { let d = diff.clone(); let tx = tx.clone(); children.push(thread::spawn(move || { let mut n = i; while n < max { if n % (100000 + i) == 0 { println!("Thread {}: {}", i, n); } if *found && sync { return n; } if let Some(solution) = verify_product(b, n, &d) { *found = true; tx.send(solution).unwrap(); if sync { break; } } n += t; } n })); } match rx.recv() { Ok(sol) => { let end = get_time(); println!("Number: {} --> hash: {}", sol.number, sol.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); } } Err(_) => {} } if wait { for child in children { let loops = child.join(); println!("Loops: {}", loops.unwrap()); } } } (_, Err(_)) => { println!("Number of threads is not integer."); process::exit(1) } (Err(_), _) => { println!("Base is not integer."); process::exit(1) } }; }