use meadow::prelude::*;
use std::thread;
use std::time::Duration;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
struct Pose {
pub x: f32,
pub y: f32,
}
const LABELS: usize = 36;
fn main() -> Result<(), meadow::Error> {
type N = Tcp;
let mut host: Host = HostConfig::default().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 node: Node<Blocking, N, Idle, Pose> = NodeConfig::new("pose").build().unwrap();
let node = match node.activate() {
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 = node.request().unwrap();
println!("From thread {}, got: {:?}", thread_num, result.data);
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(())
}