Skip to content

Commit

Permalink
scratch work
Browse files Browse the repository at this point in the history
Just a bunch of unfinished code written.
  • Loading branch information
danielpclark committed Dec 11, 2017
1 parent ac3cefe commit 476d7ad
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ clap = "~2.26"
digits = "~1.1.0"
rayon = "~0.8.2"
num_cpus = "~1.7"
serde = "1.0.24"
serde_json = "1.0.8"
17 changes: 14 additions & 3 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

use std::sync::Mutex;
use digits::Digits;
use std::io::{self, Write, Read};
use std::io::{Read};
use std::process::{Command, Output};
use rayon::prelude::*;
use super::result::Error;
Expand All @@ -18,11 +18,19 @@ use self::tempdir::TempDir;
use std::{fs,path,env};
use std::time::{Duration, Instant};
use ::WorkLoad;
use ::reporter::prelude::*;

fn has_five_minutes_passed(t: Instant) -> bool {
Instant::now().duration_since(t) > Duration::new(300,0)
}

fn update_report_data(five_min_iters: usize, last: &Digits, &fmp: Mutex<(usize, String>) {
let mut lock = fmp.try_lock();
if let Ok(ref mut mutex) = lock {
**mutex = (five_min_iters, last.to_s());
}
}

fn chunk_sequence(d: &mut Digits, adj: Option<String>, chunk: usize, step: Option<usize>) -> Vec<String> {
let qty: usize = num_cpus::get() * chunk;
let mut counter = 0;
Expand Down Expand Up @@ -72,8 +80,7 @@ fn unzip_command(value: &str, target: &str) -> Output {
}

fn progress_report<'a>(sequencer: &Digits) {
print!("{}..", sequencer.to_s()); // Verbose
io::stdout().flush().unwrap();
TickerTape::report(sequencer);
}

fn has_reached_end<'a>(sequencer: &Digits, max: usize) -> Result<(), Error> {
Expand Down Expand Up @@ -103,6 +110,8 @@ pub fn aescrypt_core_loop<'a>(work_load: WorkLoad) -> Result<(), Error> {
{
let output = aes_command(&value, &target);

ITERATIONS.fetch_add(1, Ordering::SeqCst);

if output.status.success() {
let mut code_mutex = code.lock().unwrap();
code_mutex.push(value.clone().to_string());
Expand Down Expand Up @@ -183,6 +192,8 @@ pub fn unzip_core_loop<'a>(work_load: WorkLoad) -> Result<(), Error> {
{
let output = unzip_command(&value, &target);

ITERATIONS.fetch_add(1, Ordering::SeqCst);

if output.status.success() {
if any_file_contents(&dir, &target) {
fs::read_dir(&dir).
Expand Down
4 changes: 4 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ mod resume;
mod result;
use result::Error;
use std::error::Error as StdError;
mod reporter;
mod process_input;
use process_input::*;
mod validators;
Expand All @@ -23,6 +24,9 @@ use core::*;
#[macro_use]
extern crate clap;
use clap::{Arg, App};
use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT};

static ITERATIONS: AtomicUsize = ATOMIC_USIZE_INIT;

pub struct WorkLoad(
pub String, // characters: String,
Expand Down
16 changes: 16 additions & 0 deletions src/reporter/json.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
extern crate serde;
extern crate serde_json;

#[macro_use]
extern crate serde_derive;

use serde_json::Error;
use ::reporter::{Reporter,ReportData};
use std::io::{self, Write};
pub struct JSON;

impl Reporter for JSON {
fn report(data: ReportData) {
let _a = serde_json::to_string(&ReportData)?;
}
}
60 changes: 60 additions & 0 deletions src/reporter/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
extern crate serde;
use serde::ser::{Serialize, Serializer, SerializeStruct};
extern crate serde_json;
use serde_json::Error;
use std::sync::{Arc, Mutex};
use ::Digits;
mod ticker_tape;
mod spinner;
mod json;

mod prelude {
pub use ticker_tape::*;
pub use spinner::*;
pub use json::*;
}

pub struct ReportData {
pub cores: u8,
pub chunk: usize,
pub cluster: Option<(usize,usize)>,
pub character_set: String,
pub start_time: SystemTime,
pub start_at: String,
pub adjacent_limit: Option<u8>,
pub iterations: &usize, // Atomic global
pub five_min_progress: Arc<Mutex<(usize, String)>>, // Mutex try_lock
}

impl Serialize for ReportData {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer {

let mut struct_fields = 8;
if self.cluster.is_some() { struct_fields += 2; }
if self.adjacent_limit.is_some() { struct_fields += 1; }

let mut state = serializer.serialize_struct("ReportData", struct_fields)?;
state.serialize_field("cores", &self.cores)?;
state.serialize_field("chunk", &self.chunk)?;
if let Some(node, cluster_size) = self.cluster {
state.serialize_field("cluster_node", &node)?;
state.serialize_field("cluster_size", &cluster_size)?;
}
state.serialize_field("character_set", &self.character_set)?;
state.serialize_field("start_time", &self.start_time)?;
state.serialize_field("start_at", &self.start_at)?;
if let Some(adj) = self.adjacent_limit {
state.serialize_field("adjacent_limit", &self.adjacent_limit)?;
}
state.serialize_field("iterations", &self.iterations.load(Ordering::SeqCst))?;
let (five_min_iters, last_string) = self.five_min_progress.lock().unwrap();
state.serialize_field("last_five_minute_iterations", &five_min_iters)?;
state.serialize_field("last_attempt", &last_string)?;
state.end()
}
}

pub trait Reporter {
fn report(data: ReportData);
}
13 changes: 13 additions & 0 deletions src/reporter/spinner.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use ::reporter::{Reporter,ReportData};
use std::io::{self, Write};

const SPINNER: &[char] = &['⠋','⠙','⠹','⠸','⠼','⠴','⠦','⠧','⠇','⠏'];

struct Spinner;

impl Reporter for Spinner {
fn report(data: ReportData) {
print!(":");
io::stdout().flush().unwrap();
}
}
9 changes: 9 additions & 0 deletions src/reporter/ticker_tape.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use std::io::{self, Write};
pub struct TickerTape;

impl Reporter for TickerTape {
fn report(data: ReportData) {
print!("{}..", ReportData.last.to_s());
io::stdout().flush().unwrap();
}
}

0 comments on commit 476d7ad

Please sign in to comment.