Run only one thread

This commit is contained in:
Henry Case 2019-07-25 19:03:01 +01:00
parent e4e305c83e
commit 2531ceba49
3 changed files with 70 additions and 35 deletions

62
rawhammer/Cargo.lock generated
View File

@ -1,5 +1,22 @@
# This file is automatically @generated by Cargo. # This file is automatically @generated by Cargo.
# It is not intended for manual editing. # It is not intended for manual editing.
[[package]]
name = "ansi_term"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "atty"
version = "0.2.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.60 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "bitflags" name = "bitflags"
version = "1.1.0" version = "1.1.0"
@ -15,6 +32,20 @@ name = "cfg-if"
version = "0.1.9" version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "clap"
version = "2.33.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
"atty 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)",
"bitflags 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
"vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "ctrlc" name = "ctrlc"
version = "3.1.3" version = "3.1.3"
@ -45,15 +76,39 @@ dependencies = [
name = "rawhammer" name = "rawhammer"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)",
"ctrlc 3.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "ctrlc 3.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"thread-control 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "thread-control 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "strsim"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "textwrap"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "thread-control" name = "thread-control"
version = "0.1.2" version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "unicode-width"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "vec_map"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "void" name = "void"
version = "1.0.2" version = "1.0.2"
@ -79,13 +134,20 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata] [metadata]
"checksum ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
"checksum atty 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)" = "1803c647a3ec87095e7ae7acfca019e98de5ec9a7d01343f611cf3152ed71a90"
"checksum bitflags 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3d155346769a6855b86399e9bc3814ab343cd3d62c7e985113d46a0ec3c281fd" "checksum bitflags 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3d155346769a6855b86399e9bc3814ab343cd3d62c7e985113d46a0ec3c281fd"
"checksum cc 1.0.38 (registry+https://github.com/rust-lang/crates.io-index)" = "ce400c638d48ee0e9ab75aef7997609ec57367ccfe1463f21bf53c3eca67bf46" "checksum cc 1.0.38 (registry+https://github.com/rust-lang/crates.io-index)" = "ce400c638d48ee0e9ab75aef7997609ec57367ccfe1463f21bf53c3eca67bf46"
"checksum cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "b486ce3ccf7ffd79fdeb678eac06a9e6c09fc88d33836340becb8fffe87c5e33" "checksum cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "b486ce3ccf7ffd79fdeb678eac06a9e6c09fc88d33836340becb8fffe87c5e33"
"checksum clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5067f5bb2d80ef5d68b4c87db81601f0b75bca627bc2ef76b141d7b846a3c6d9"
"checksum ctrlc 3.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c7dfd2d8b4c82121dfdff120f818e09fc4380b0b7e17a742081a89b94853e87f" "checksum ctrlc 3.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c7dfd2d8b4c82121dfdff120f818e09fc4380b0b7e17a742081a89b94853e87f"
"checksum libc 0.2.60 (registry+https://github.com/rust-lang/crates.io-index)" = "d44e80633f007889c7eff624b709ab43c92d708caad982295768a7b13ca3b5eb" "checksum libc 0.2.60 (registry+https://github.com/rust-lang/crates.io-index)" = "d44e80633f007889c7eff624b709ab43c92d708caad982295768a7b13ca3b5eb"
"checksum nix 0.14.1 (registry+https://github.com/rust-lang/crates.io-index)" = "6c722bee1037d430d0f8e687bbdbf222f27cc6e4e68d5caf630857bb2b6dbdce" "checksum nix 0.14.1 (registry+https://github.com/rust-lang/crates.io-index)" = "6c722bee1037d430d0f8e687bbdbf222f27cc6e4e68d5caf630857bb2b6dbdce"
"checksum strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
"checksum textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
"checksum thread-control 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "22e002431352cce4e76fd6fe77d5bf1b75ff790ea790602f0579ce58641c2eea" "checksum thread-control 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "22e002431352cce4e76fd6fe77d5bf1b75ff790ea790602f0579ce58641c2eea"
"checksum unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "882386231c45df4700b275c7ff55b6f3698780a650026380e72dabe76fa46526"
"checksum vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a"
"checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" "checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"
"checksum winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "f10e386af2b13e47c89e7236a7a14a086791a2b88ebad6df9bf42040195cf770" "checksum winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "f10e386af2b13e47c89e7236a7a14a086791a2b88ebad6df9bf42040195cf770"
"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"

View File

@ -9,3 +9,4 @@ edition = "2018"
[dependencies] [dependencies]
ctrlc = "3.1.3" ctrlc = "3.1.3"
thread-control = "0.1.2" thread-control = "0.1.2"
clap = "2.33.0"

View File

@ -2,7 +2,6 @@ use std::io::Write;
use std::net::*; use std::net::*;
use std::env; use std::env;
use std::time::Duration; use std::time::Duration;
use std::sync::mpsc::sync_channel;
use std::thread; use std::thread;
use std::time; use std::time;
use ctrlc; use ctrlc;
@ -205,12 +204,10 @@ fn sleep_no_comment(millis: u64) {
fn main() -> std::io::Result<()> { fn main() -> std::io::Result<()> {
// Condition variables to control threads // Condition variables to control threads
let (flag_t1, control_t1) = make_pair(); let (flag_t1, control_t1) = make_pair();
let (flag_t2, control_t2) = make_pair();
ctrlc::set_handler(move || { ctrlc::set_handler(move || {
println!("Stopping..."); println!("Stopping...");
control_t1.stop(); control_t1.stop();
control_t2.stop();
}).expect("Error setting Ctrl-C handler"); }).expect("Error setting Ctrl-C handler");
// Parse arguments // Parse arguments
@ -224,23 +221,18 @@ fn main() -> std::io::Result<()> {
let ch_case: usize = _args[3].parse().unwrap(); let ch_case: usize = _args[3].parse().unwrap();
// Argument 4 - Sends per second // Argument 4 - Sends per second
let msg_per_sec: usize = _args[4].parse().unwrap(); let msg_per_sec: usize = _args[4].parse().unwrap();
// Argument 5 - Number of connections to keep open
let con_num: usize = _args[5].parse().unwrap();
// create FIFO
println!("We will keep {} connections open", con_num);
let (tx, rx) = sync_channel(con_num);
// Start a thread which sends CH // Start a thread which sends CH
let _t1 = thread::spawn(move || { let _t1 = thread::spawn(move || {
let mut sent = 0; let mut sent = 0;
while flag_t1.is_alive() { while flag_t1.is_alive() {
// wait until connections are produced let addr_ref = &addr_iter.as_slice()[0];
while rx.try_iter().next().is_none() { let sock = TcpStream::connect_timeout(addr_ref, dur);
sleep(100, "Waiting for connections"); if sock.is_err() {
sleep(10, "Can't connect, waiting...");
continue;
} }
// receive next connection and send CH let mut s = sock.unwrap();
let mut s: TcpStream = rx.recv().unwrap();
match ch_case { match ch_case {
1 => s.write(&CH_NTRU).unwrap(), 1 => s.write(&CH_NTRU).unwrap(),
2 => s.write(&CH_SIKE).unwrap(), 2 => s.write(&CH_SIKE).unwrap(),
@ -257,28 +249,8 @@ fn main() -> std::io::Result<()> {
} }
}); });
// Start a thread which produces connections
let _t2 = thread::spawn(move || {
// First thread owns tx
while flag_t2.is_alive() {
let addr_ref = &addr_iter.as_slice()[0];
let sock = TcpStream::connect_timeout(addr_ref, dur);
if sock.is_err() {
sleep(1000, "Can't connect, waiting...");
continue;
}
let s = sock.unwrap();
s.set_nonblocking(true).expect("set_nonblocking call failed");
match tx.try_send(s) {
Err(_) => sleep(1000, "Can't put in the queue"),
Ok(_) => continue,
}
}
});
// wait for TX thread to finish // wait for TX thread to finish
_t1.join().unwrap(); _t1.join().unwrap();
_t2.join().unwrap(); //_t2.join().unwrap();
Ok(()) Ok(())
} }