use bissel::*;
use std::error::Error;
use std::thread;
use std::time::Duration;
const LABELS: usize = 36;
fn main() -> Result<(), Box<dyn Error>> {
let file_appender = tracing_appender::rolling::hourly("logs/", "multiple_nodes");
let (non_blocking, _guard) = tracing_appender::non_blocking(file_appender);
tracing_subscriber::fmt().with_writer(non_blocking).init();
let mut host: Host = HostConfig::new("lo").build()?;
host.start()?;
let mut handles: Vec<thread::JoinHandle<()>> = Vec::new();
for i in 0..LABELS {
let handle = thread::spawn(move || {
let thread_num = i;
let pose = Pose {
x: thread_num as f32,
y: thread_num as f32,
};
let name = format!("NODE_{}", i);
let node: Node<Idle, Pose> = NodeConfig::new(name).topic("pose").build().unwrap();
let node = match node.connect() {
Ok(node) => {
println!("NODE_{} connected successfully", i);
node
}
Err(_e) => {
panic!("NODE_{} did NOT connect successfully", i);
}
};
node.publish(pose).unwrap();
thread::sleep(Duration::from_millis((100 / (i + 1)) as u64));
let result: Pose = node.request().unwrap();
println!("From thread {}, got: {:?}", thread_num, result);
println!("Thread {} returning!", i);
});
handles.push(handle);
thread::sleep(Duration::from_millis(1));
}
for handle in handles {
handle.join().unwrap();
}
println!("All threads have joined!");
host.stop()?;
Ok(())
}