use mpstthree::binary::struct_trait::{end::End, recv::Recv, send::Send, session::Session}; use mpstthree::generate; use mpstthree::role::broadcast::RoleBroadcast; use mpstthree::role::end::RoleEnd; use rand::{thread_rng, Rng}; use std::error::Error; // See the folder scribble_protocols for the related Scribble protocol generate!("recursive", MeshedChannels, A, C, S); // Payloads struct Start; struct Redirect; struct Login; struct Auth; struct Password; struct Success; struct Token; struct Fail; struct Received; // Types // A type Choose0fromAtoC = ::Dual; type Choose0fromAtoS = ::Dual; // C enum Branching0fromAtoC { Success( MeshedChannels< Recv, Send>, RoleA>>, NameC, >, ), Fail( MeshedChannels< Recv, Send>, RoleA>>, NameC, >, ), } type Offer0fromCtoA = Recv; // S enum Branching0fromAtoS { Success( MeshedChannels< Send>, Recv>, RoleC>>>, NameS, >, ), Fail(MeshedChannels>, RoleC>, NameS>), } type Offer0fromStoA = Recv; // Creating the MP sessions // A type EndpointASuccess = MeshedChannels< Send, Recv>, RoleC>>, NameA, >; type EndpointAFail = MeshedChannels, End, RoleC, NameA>; type EndpointA = MeshedChannels< Recv>>, Choose0fromAtoS, RoleC>>, NameA, >; // C type EndpointC = MeshedChannels< Send>>, Send>, RoleS>>>>>, NameC, >; // S type EndpointS = MeshedChannels< Offer0fromStoA, Recv>, RoleC>>, NameS, >; // Functions fn endpoint_a(s: EndpointA) -> Result<(), Box> { let (_, s) = s.recv(); let s = s.send(Auth {}); let (_, s) = s.recv(); let expected: i32 = thread_rng().gen_range(1..=3); if 1 == expected { let s: EndpointASuccess = choose_mpst_a_to_all!(s, Branching0fromAtoC::Success, Branching0fromAtoS::Success); let s = s.send(Success {}); let (_, s) = s.recv(); let s = s.send(Token {}); s.close() } else { let s: EndpointAFail = choose_mpst_a_to_all!(s, Branching0fromAtoC::Fail, Branching0fromAtoS::Fail); let s = s.send(Fail {}); s.close() } } fn endpoint_c(s: EndpointC) -> Result<(), Box> { let s = s.send(Start {}); let (_, s) = s.recv(); let s = s.send(Login {}); let (_, s) = s.recv(); let s = s.send(Password {}); offer_mpst!(s, { Branching0fromAtoC::Success(s) => { let (_, s) = s.recv(); let s = s.send(Success { }); let (_, s) = s.recv(); s.close() }, Branching0fromAtoC::Fail(s) => { let (_, s) = s.recv(); let s = s.send(Fail { }); let (_, s) = s.recv(); s.close() }, }) } fn endpoint_s(s: EndpointS) -> Result<(), Box> { let (_, s) = s.recv(); let s = s.send(Redirect {}); offer_mpst!(s, { Branching0fromAtoS::Success(s) => { let (_, s) = s.recv(); let s = s.send(Token { }); let (_, s) = s.recv(); let s = s.send(Token { }); s.close() }, Branching0fromAtoS::Fail(s) => { let (_, s) = s.recv(); let s = s.send(Received { }); 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(); }