#![allow( clippy::type_complexity, clippy::too_many_arguments, clippy::large_enum_variant )] use mpstthree::binary::struct_trait::end::End; use mpstthree::binary_atmp::struct_trait::{recv::RecvTimed, send::SendTimed}; use mpstthree::generate_atmp; use mpstthree::role::broadcast::RoleBroadcast; use mpstthree::role::end::RoleEnd; use std::collections::HashMap; use std::error::Error; use std::time::Instant; static LOOPS: i64 = 100; // Create new roles generate_atmp!(MeshedChannels, A, B, C, D, E); // Types // A enum Branching0fromEtoA { Forward( MeshedChannels< SendTimed<(), 'a', 0, true, 10, true, ' ', End>, End, End, RecursAtoE, RoleB>, NameA, >, ), Backward( MeshedChannels< RecvTimed<(), 'a', 0, true, 10, true, ' ', End>, End, End, RecursAtoE, RoleB>, NameA, >, ), Done(MeshedChannels), } type RecursAtoE = RecvTimed; // B enum Branching0fromEtoB { Forward( MeshedChannels< RecvTimed<(), 'a', 0, true, 10, true, ' ', End>, SendTimed<(), 'a', 0, true, 10, true, ' ', End>, End, RecursBtoE, RoleA>>, NameB, >, ), Backward( MeshedChannels< SendTimed<(), 'a', 0, true, 10, true, ' ', End>, RecvTimed<(), 'a', 0, true, 10, true, ' ', End>, End, RecursBtoE, RoleC>>, NameB, >, ), Done(MeshedChannels), } type RecursBtoE = RecvTimed; // C enum Branching0fromEtoC { Forward( MeshedChannels< End, RecvTimed<(), 'a', 0, true, 10, true, ' ', End>, SendTimed<(), 'a', 0, true, 10, true, ' ', End>, RecursCtoE, RoleB>>, NameC, >, ), Backward( MeshedChannels< End, SendTimed<(), 'a', 0, true, 10, true, ' ', End>, RecvTimed<(), 'a', 0, true, 10, true, ' ', End>, RecursCtoE, RoleD>>, NameC, >, ), Done(MeshedChannels), } type RecursCtoE = RecvTimed; // D enum Branching0fromEtoD { Forward( MeshedChannels< End, End, RecvTimed<(), 'a', 0, true, 10, true, ' ', End>, SendTimed<(), 'a', 0, true, 10, true, ' ', RecursDtoE>, RoleC>>, NameD, >, ), Backward( MeshedChannels< End, End, SendTimed<(), 'a', 0, true, 10, true, ' ', End>, RecvTimed<(), 'a', 0, true, 10, true, ' ', RecursDtoE>, RoleE>>, NameD, >, ), Done(MeshedChannels), } type RecursDtoE = RecvTimed; // E type Choose0fromEtoA = SendTimed; type Choose0fromEtoB = SendTimed; type Choose0fromEtoC = SendTimed; type Choose0fromEtoD = SendTimed; type EndpointForwardE = MeshedChannels< Choose0fromEtoA, Choose0fromEtoB, Choose0fromEtoC, RecvTimed<(), 'a', 0, true, 10, true, ' ', Choose0fromEtoD>, RoleD, NameE, >; type EndpointBackwardE = MeshedChannels< Choose0fromEtoA, Choose0fromEtoB, Choose0fromEtoC, SendTimed<(), 'a', 0, true, 10, true, ' ', Choose0fromEtoD>, RoleD, NameE, >; // Creating the MP sessions type EndpointA = MeshedChannels, NameA>; type EndpointB = MeshedChannels, NameB>; type EndpointC = MeshedChannels, NameC>; type EndpointD = MeshedChannels, NameD>; type EndpointE = MeshedChannels< Choose0fromEtoA, Choose0fromEtoB, Choose0fromEtoC, Choose0fromEtoD, RoleBroadcast, NameE, >; fn endpoint_a(s: EndpointA, all_clocks: &mut HashMap) -> Result<(), Box> { all_clocks.insert('a', Instant::now()); offer_mpst!(s, all_clocks, { Branching0fromEtoA::Done(s) => { s.close() }, Branching0fromEtoA::Forward(s) => { let s = s.send((), all_clocks)?; endpoint_a(s, all_clocks) }, Branching0fromEtoA::Backward(s) => { let (_, s) = s.recv(all_clocks)?; endpoint_a(s, all_clocks) }, }) } fn endpoint_b(s: EndpointB, all_clocks: &mut HashMap) -> Result<(), Box> { all_clocks.insert('a', Instant::now()); offer_mpst!(s, all_clocks, { Branching0fromEtoB::Done(s) => { s.close() }, Branching0fromEtoB::Forward(s) => { let ((), s) = s.recv(all_clocks)?; let s = s.send((), all_clocks)?; endpoint_b(s, all_clocks) }, Branching0fromEtoB::Backward(s) => { let ((), s) = s.recv(all_clocks)?; let s = s.send((), all_clocks)?; endpoint_b(s, all_clocks) }, }) } fn endpoint_c(s: EndpointC, all_clocks: &mut HashMap) -> Result<(), Box> { all_clocks.insert('a', Instant::now()); offer_mpst!(s, all_clocks, { Branching0fromEtoC::Done(s) => { s.close() }, Branching0fromEtoC::Forward(s) => { let ((), s) = s.recv(all_clocks)?; let s = s.send((), all_clocks)?; endpoint_c(s, all_clocks) }, Branching0fromEtoC::Backward(s) => { let ((), s) = s.recv(all_clocks)?; let s = s.send((), all_clocks)?; endpoint_c(s, all_clocks) }, }) } fn endpoint_d(s: EndpointD, all_clocks: &mut HashMap) -> Result<(), Box> { all_clocks.insert('a', Instant::now()); offer_mpst!(s, all_clocks, { Branching0fromEtoD::Done(s) => { s.close() }, Branching0fromEtoD::Forward(s) => { let ((), s) = s.recv(all_clocks)?; let s = s.send((), all_clocks)?; endpoint_d(s, all_clocks) }, Branching0fromEtoD::Backward(s) => { let ((), s) = s.recv(all_clocks)?; let s = s.send((), all_clocks)?; endpoint_d(s, all_clocks) }, }) } fn endpoint_e(s: EndpointE, all_clocks: &mut HashMap) -> Result<(), Box> { all_clocks.insert('a', Instant::now()); recurs_e(s, LOOPS, all_clocks) } fn recurs_e( s: EndpointE, index: i64, all_clocks: &mut HashMap, ) -> Result<(), Box> { match index { 0 => { let s = choose_mpst_e_to_all!( s, all_clocks, Branching0fromEtoA::Done, Branching0fromEtoB::Done, Branching0fromEtoC::Done, Branching0fromEtoD::Done ); s.close() } i if i % 2 == 0 => { let s: EndpointForwardE = choose_mpst_e_to_all!( s, all_clocks, Branching0fromEtoA::Forward, Branching0fromEtoB::Forward, Branching0fromEtoC::Forward, Branching0fromEtoD::Forward ); let (_, s) = s.recv(all_clocks)?; recurs_e(s, i - 1, all_clocks) } i => { let s: EndpointBackwardE = choose_mpst_e_to_all!( s, all_clocks, Branching0fromEtoA::Backward, Branching0fromEtoB::Backward, Branching0fromEtoC::Backward, Branching0fromEtoD::Backward ); let s = s.send((), all_clocks)?; recurs_e(s, i - 1, all_clocks) } } } fn main() { let (thread_a, thread_b, thread_c, thread_d, thread_e) = fork_mpst(endpoint_a, endpoint_b, endpoint_c, endpoint_d, endpoint_e); thread_a.join().unwrap(); thread_b.join().unwrap(); thread_c.join().unwrap(); thread_d.join().unwrap(); thread_e.join().unwrap(); }