use timely_communication::Bytesable;
pub struct Message {
pub payload: String,
}
impl Bytesable for Message {
fn from_bytes(bytes: timely_bytes::arc::Bytes) -> Self {
Message { payload: std::str::from_utf8(&bytes[..]).unwrap().to_string() }
}
fn length_in_bytes(&self) -> usize {
self.payload.len()
}
fn into_bytes<W: ::std::io::Write>(&self, writer: &mut W) {
writer.write_all(self.payload.as_bytes()).unwrap();
}
}
fn main() {
let (config, _free) = timely_communication::Config::from_args(std::env::args()).unwrap();
let guards = timely_communication::initialize(config, |mut allocator| {
println!("worker {} of {} started", allocator.index(), allocator.peers());
let (mut senders, mut receiver) = allocator.allocate(0);
for (i, sender) in senders.iter_mut().enumerate() {
sender.send(Message { payload: format!("hello, {}", i)});
sender.done();
}
let mut received = 0;
while received < allocator.peers() {
allocator.receive();
if let Some(message) = receiver.recv() {
println!("worker {}: received: <{}>", allocator.index(), message.payload);
received += 1;
}
allocator.release();
}
allocator.index()
});
if let Ok(guards) = guards {
for guard in guards.join() {
println!("result: {:?}", guard);
}
}
else { println!("error in computation"); }
}