Skip to content

Commit

Permalink
misc: small fix or general refactoring i did not bother commenting
Browse files Browse the repository at this point in the history
  • Loading branch information
evilsocket committed Nov 16, 2023
1 parent da5a324 commit adae139
Showing 1 changed file with 27 additions and 30 deletions.
57 changes: 27 additions & 30 deletions src/session/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ use std::time;

pub(crate) type Error = String;

async fn periodic_saver(session: Arc<Session>, persistent: bool) {
async fn periodic_saver(session: Arc<Session>) {
let one_sec = time::Duration::from_millis(1000);
let mut last_done: usize = 0;
let persistent = session.options.session.is_some();

while !session.is_stop() {
std::thread::sleep(one_sec);
Expand Down Expand Up @@ -80,17 +81,6 @@ impl Session {
parse_target(target, 0)?;
}

let num_targets = targets.len();
log::info!(
"target{}: {}",
if num_targets > 1 {
format!("s ({})", num_targets)
} else {
"".to_owned()
},
options.target.as_ref().unwrap()
);

let runtime = Runtime::new(options.concurrency);
let total = AtomicUsize::new(0);
let done = AtomicUsize::new(0);
Expand All @@ -108,35 +98,42 @@ impl Session {
}))
}

fn from_disk(path: &str) -> Result<Arc<Self>, Error> {
log::debug!("restoring session from {}", path);
fn from_disk(path: &str, options: Options) -> Result<Arc<Self>, Error> {
if Path::new(path).exists() {
log::info!("restoring session from {}", path);

let file = fs::File::open(path).map_err(|e| e.to_string())?;
let mut session: Session = serde_json::from_reader(file).map_err(|e| e.to_string())?;
let file = fs::File::open(path).map_err(|e| e.to_string())?;
let mut session: Session = serde_json::from_reader(file).map_err(|e| e.to_string())?;

session.runtime = Runtime::new(session.options.concurrency);
session.runtime = Runtime::new(session.options.concurrency);

Ok(Arc::new(session))
Ok(Arc::new(session))
} else {
Self::from_options(options)
}
}

pub fn new(options: Options) -> Result<Arc<Self>, Error> {
let mut persistent = false;
// if a session file has been specified
let session = if let Some(path) = &options.session {
persistent = true;
// if it already exists
if Path::new(path).exists() {
// restore from disk
Self::from_disk(path)?
} else {
// create new with persistency
Self::from_options(options)?
}
let session = if let Some(path) = options.session.as_ref() {
// load from disk if file exists, or from options and save to disk
Self::from_disk(path, options.clone())?
} else {
// create new without persistency
Self::from_options(options)?
};

let num_targets = session.targets.len();
log::info!(
"target{}: {}",
if num_targets > 1 {
format!("s ({})", num_targets)
} else {
"".to_owned()
},
session.options.target.as_ref().unwrap()
);

// set ctrl-c handler
let le_session = session.clone();
ctrlc::set_handler(move || {
Expand All @@ -145,7 +142,7 @@ impl Session {
})
.expect("error setting ctrl-c handler");

tokio::task::spawn(periodic_saver(session.clone(), persistent));
tokio::task::spawn(periodic_saver(session.clone()));

Ok(session)
}
Expand Down

0 comments on commit adae139

Please sign in to comment.