extern crate chorus_lib;
use std::thread;
use chorus_lib::core::{ChoreoOp, Choreography, ChoreographyLocation, LocationSet, Projector};
use chorus_lib::transport::local::{LocalTransport, LocalTransportChannelBuilder};
#[derive(ChoreographyLocation)]
struct Alice;
#[derive(ChoreographyLocation)]
struct Bob;
struct HelloWorldChoreography;
impl Choreography for HelloWorldChoreography {
type L = LocationSet!(Alice, Bob);
fn run(self, op: &impl ChoreoOp<Self::L>) {
let msg_at_alice = op.locally(Alice, |_| {
println!("Hello from Alice!");
"Hello from Alice!".to_string()
});
let msg_at_bob = op.comm(Alice, Bob, &msg_at_alice);
op.locally(Bob, |un| {
let msg = un.unwrap(&msg_at_bob);
println!("Bob received a message: {}", msg);
msg
});
}
}
fn main() {
let mut handles: Vec<thread::JoinHandle<()>> = Vec::new();
let transport_channel = LocalTransportChannelBuilder::new()
.with(Alice)
.with(Bob)
.build();
{
let transport = LocalTransport::new(Alice, transport_channel.clone());
handles.push(thread::spawn(move || {
let p = Projector::new(Alice, transport);
p.epp_and_run(HelloWorldChoreography);
}));
}
{
let transport = LocalTransport::new(Bob, transport_channel.clone());
handles.push(thread::spawn(move || {
let p = Projector::new(Bob, transport);
p.epp_and_run(HelloWorldChoreography);
}));
}
for h in handles {
h.join().unwrap();
}
}