From d8c85c96aaed1595050dc9caa83012d474f695d4 Mon Sep 17 00:00:00 2001 From: ennucore Date: Sat, 11 Dec 2021 20:54:47 +0300 Subject: [PATCH] Add binary --- Cargo.toml | 5 +++++ src/bin/worker.rs | 8 ++++++++ src/interfaces/ip.rs | 3 +-- src/ironforce.rs | 7 ++++++- src/lib.rs | 4 ++-- 5 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 src/bin/worker.rs diff --git a/Cargo.toml b/Cargo.toml index 395ed48..7ccac09 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,3 +25,8 @@ spin = "0.9.2" [profile.dev.package.num-bigint-dig] opt-level = 3 + +[[bin]] +name = "worker" +src = "src/worker.rs" +required-features = ["std"] diff --git a/src/bin/worker.rs b/src/bin/worker.rs new file mode 100644 index 0000000..6a5f074 --- /dev/null +++ b/src/bin/worker.rs @@ -0,0 +1,8 @@ +use ironforce::IronForce; +use ironforce::res::IFResult; + +fn main() -> IFResult<()> { + let ironforce = IronForce::new(); + let (thread, _) = ironforce.launch_main_loop(500); + thread.join().unwrap() +} \ No newline at end of file diff --git a/src/interfaces/ip.rs b/src/interfaces/ip.rs index c196d65..6ae0176 100644 --- a/src/interfaces/ip.rs +++ b/src/interfaces/ip.rs @@ -3,7 +3,6 @@ use alloc::string::{String, ToString}; use alloc::vec; use alloc::vec::Vec; use core::ops::RangeInclusive; -#[cfg(test)] use core::str::FromStr; use core::time::Duration; use rayon::prelude::*; @@ -201,7 +200,7 @@ impl Interface for IPInterface { )); } // We do a peer exchange every 30 iterations - if self.main_loop_iterations % 30 == 0 { + if self.main_loop_iterations % 30 == 0 && !self.connections.is_empty() { let connection_index = (self.main_loop_iterations / 30) as usize % self.connections.len(); IPInterface::request_peers(&mut self.connections[connection_index])?; diff --git a/src/ironforce.rs b/src/ironforce.rs index 84428c8..41e15cf 100644 --- a/src/ironforce.rs +++ b/src/ironforce.rs @@ -83,7 +83,7 @@ impl IronForce { has_background_worker: false, processed_messages: vec![], tunnel_counters: Default::default(), - auto_save: false, + auto_save: true, } } @@ -353,6 +353,11 @@ impl IronForce { }) } + #[cfg(feature = "std")] + pub fn from_file(filename: alloc::string::String) -> IFResult { + Self::from_serialization_data(serde_json::from_str(std::fs::read_to_string(filename)?.as_str())?) + } + #[cfg(feature = "std")] pub fn save_to_file(&self, filename: Option) -> IFResult<()> { std::fs::write( diff --git a/src/lib.rs b/src/lib.rs index de516c5..b73191a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,8 +22,8 @@ pub mod interfaces; pub mod res; mod tunnel; -#[cfg(std)] -use crate::interfaces::ip; + +pub use ironforce::IronForce; #[cfg(test)] mod tests {