use mpstthree::binary::struct_trait::{end::End, recv::Recv, send::Send}; use mpstthree::generate; use mpstthree::role::broadcast::RoleBroadcast; use mpstthree::role::end::RoleEnd; use rand::{random, thread_rng, Rng}; use std::error::Error; // See the folder scribble_protocols for the related Scribble protocol // Create new MeshedChannels for four participants generate!("recursive", MeshedChannels, A, C, S); // Types // A type Choose0fromCtoA = Send; type Choose0fromCtoS = Send; // A enum Branching0fromCtoA { Accept(MeshedChannels, End, RoleC, NameA>), Quit(MeshedChannels), } // S enum Branching0fromCtoS { Accept(MeshedChannels>, RoleC>, NameS>), Quit(MeshedChannels), } // Creating the MP sessions // A type EndpointA = MeshedChannels< Send>, Send>, RoleS>>>, NameA, >; // C type EndpointC = MeshedChannels< Recv, Recv, RoleS>, NameC, >; type EndpointCAccept = MeshedChannels, Send>, RoleA>>, NameC>; // S type EndpointS = MeshedChannels< Recv>, Send>, RoleA>>>, NameS, >; // Functions fn endpoint_a(s: EndpointA) -> Result<(), Box> { let s = s.send(random()); let (_empty2, s) = s.recv(); let s = s.send(random()); offer_mpst!(s, { Branching0fromCtoA::Accept(s) => { let (_ok, s) = s.recv(); s.close() }, Branching0fromCtoA::Quit(s) => { s.close() }, }) } fn endpoint_c(s: EndpointC) -> Result<(), Box> { let (_empty3, s) = s.recv(); let (_empty4, s) = s.recv(); let choice: i32 = thread_rng().gen_range(1..=3); if choice != 1 { let s: EndpointCAccept = choose_mpst_c_to_all!(s, Branching0fromCtoA::Accept, Branching0fromCtoS::Accept); let s = s.send(random()); let s = s.send(random()); let (_empty5, s) = s.recv(); s.close() } else { let s = choose_mpst_c_to_all!(s, Branching0fromCtoA::Quit, Branching0fromCtoS::Quit); s.close() } } fn endpoint_s(s: EndpointS) -> Result<(), Box> { let (_empty1, s) = s.recv(); let s = s.send(random()); let s = s.send(random()); offer_mpst!(s, { Branching0fromCtoS::Accept(s) => { let (_ok, s) = s.recv(); let s = s.send(random()); s.close() }, Branching0fromCtoS::Quit(s) => { s.close() }, }) } ///////////////////////////////////////// fn main() { let (thread_a, thread_c, thread_s) = fork_mpst(endpoint_a, endpoint_c, endpoint_s); thread_a.join().unwrap(); thread_c.join().unwrap(); thread_s.join().unwrap(); }