#![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, F); // Types // A enum Branching0fromFtoA { Forward( MeshedChannels< SendTimed<(), 'a', 0, true, 10, true, ' ', End>, End, End, End, RecursAtoF, RoleB>, NameA, >, ), Backward( MeshedChannels< RecvTimed<(), 'a', 0, true, 10, true, ' ', End>, End, End, End, RecursAtoF, RoleB>, NameA, >, ), Done(MeshedChannels), } type RecursAtoF = RecvTimed; // B enum Branching0fromFtoB { Forward( MeshedChannels< RecvTimed<(), 'a', 0, true, 10, true, ' ', End>, SendTimed<(), 'a', 0, true, 10, true, ' ', End>, End, End, RecursBtoF, RoleA>>, NameB, >, ), Backward( MeshedChannels< SendTimed<(), 'a', 0, true, 10, true, ' ', End>, RecvTimed<(), 'a', 0, true, 10, true, ' ', End>, End, End, RecursBtoF, RoleC>>, NameB, >, ), Done(MeshedChannels), } type RecursBtoF = RecvTimed; // C enum Branching0fromFtoC { Forward( MeshedChannels< End, RecvTimed<(), 'a', 0, true, 10, true, ' ', End>, SendTimed<(), 'a', 0, true, 10, true, ' ', End>, End, RecursCtoF, RoleB>>, NameC, >, ), Backward( MeshedChannels< End, SendTimed<(), 'a', 0, true, 10, true, ' ', End>, RecvTimed<(), 'a', 0, true, 10, true, ' ', End>, End, RecursCtoF, RoleD>>, NameC, >, ), Done(MeshedChannels), } type RecursCtoF = RecvTimed; // D enum Branching0fromFtoD { Forward( MeshedChannels< End, End, RecvTimed<(), 'a', 0, true, 10, true, ' ', End>, SendTimed<(), 'a', 0, true, 10, true, ' ', End>, RecursDtoF, RoleC>>, NameD, >, ), Backward( MeshedChannels< End, End, SendTimed<(), 'a', 0, true, 10, true, ' ', End>, RecvTimed<(), 'a', 0, true, 10, true, ' ', End>, RecursDtoF, RoleE>>, NameD, >, ), Done(MeshedChannels), } type RecursDtoF = RecvTimed; // E enum Branching0fromFtoE { Forward( MeshedChannels< End, End, End, RecvTimed<(), 'a', 0, true, 10, true, ' ', End>, SendTimed<(), 'a', 0, true, 10, true, ' ', RecursEtoF>, RoleD>>, NameE, >, ), Backward( MeshedChannels< End, End, End, SendTimed<(), 'a', 0, true, 10, true, ' ', End>, RecvTimed<(), 'a', 0, true, 10, true, ' ', RecursEtoF>, RoleF>>, NameE, >, ), Done(MeshedChannels), } type RecursEtoF = RecvTimed; // F type Choose0fromFtoA = SendTimed; type Choose0fromFtoB = SendTimed; type Choose0fromFtoC = SendTimed; type Choose0fromFtoD = SendTimed; type Choose0fromFtoE = SendTimed; type EndpointForwardF = MeshedChannels< Choose0fromFtoA, Choose0fromFtoB, Choose0fromFtoC, Choose0fromFtoD, RecvTimed<(), 'a', 0, true, 10, true, ' ', Choose0fromFtoE>, RoleE, NameF, >; type EndpointBackwardF = MeshedChannels< Choose0fromFtoA, Choose0fromFtoB, Choose0fromFtoC, Choose0fromFtoD, SendTimed<(), 'a', 0, true, 10, true, ' ', Choose0fromFtoE>, RoleE, NameF, >; // Creating the MP sessions type EndpointA = MeshedChannels, NameA>; type EndpointB = MeshedChannels, NameB>; type EndpointC = MeshedChannels, NameC>; type EndpointD = MeshedChannels, NameD>; type EndpointE = MeshedChannels, NameE>; type EndpointF = MeshedChannels< Choose0fromFtoA, Choose0fromFtoB, Choose0fromFtoC, Choose0fromFtoD, Choose0fromFtoE, RoleBroadcast, NameF, >; fn endpoint_a(s: EndpointA, all_clocks: &mut HashMap) -> Result<(), Box> { all_clocks.insert('a', Instant::now()); offer_mpst!(s, all_clocks, { Branching0fromFtoA::Done(s) => { s.close() }, Branching0fromFtoA::Forward(s) => { let s = s.send((), all_clocks)?; endpoint_a(s, all_clocks) }, Branching0fromFtoA::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, { Branching0fromFtoB::Done(s) => { s.close() }, Branching0fromFtoB::Forward(s) => { let ((), s) = s.recv(all_clocks)?; let s = s.send((), all_clocks)?; endpoint_b(s, all_clocks) }, Branching0fromFtoB::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, { Branching0fromFtoC::Done(s) => { s.close() }, Branching0fromFtoC::Forward(s) => { let ((), s) = s.recv(all_clocks)?; let s = s.send((), all_clocks)?; endpoint_c(s, all_clocks) }, Branching0fromFtoC::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, { Branching0fromFtoD::Done(s) => { s.close() }, Branching0fromFtoD::Forward(s) => { let ((), s) = s.recv(all_clocks)?; let s = s.send((), all_clocks)?; endpoint_d(s, all_clocks) }, Branching0fromFtoD::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()); offer_mpst!(s, all_clocks, { Branching0fromFtoE::Done(s) => { s.close() }, Branching0fromFtoE::Forward(s) => { let ((), s) = s.recv(all_clocks)?; let s = s.send((), all_clocks)?; endpoint_e(s, all_clocks) }, Branching0fromFtoE::Backward(s) => { let ((), s) = s.recv(all_clocks)?; let s = s.send((), all_clocks)?; endpoint_e(s, all_clocks) }, }) } fn endpoint_f(s: EndpointF, all_clocks: &mut HashMap) -> Result<(), Box> { all_clocks.insert('a', Instant::now()); recurs_f(s, LOOPS, all_clocks) } fn recurs_f( s: EndpointF, index: i64, all_clocks: &mut HashMap, ) -> Result<(), Box> { match index { 0 => { let s = choose_mpst_f_to_all!( s, all_clocks, Branching0fromFtoA::Done, Branching0fromFtoB::Done, Branching0fromFtoC::Done, Branching0fromFtoD::Done, Branching0fromFtoE::Done ); s.close() } i if i % 2 == 0 => { let s: EndpointForwardF = choose_mpst_f_to_all!( s, all_clocks, Branching0fromFtoA::Forward, Branching0fromFtoB::Forward, Branching0fromFtoC::Forward, Branching0fromFtoD::Forward, Branching0fromFtoE::Forward ); let (_, s) = s.recv(all_clocks)?; recurs_f(s, i - 1, all_clocks) } i => { let s: EndpointBackwardF = choose_mpst_f_to_all!( s, all_clocks, Branching0fromFtoA::Backward, Branching0fromFtoB::Backward, Branching0fromFtoC::Backward, Branching0fromFtoD::Backward, Branching0fromFtoE::Backward ); let s = s.send((), all_clocks)?; recurs_f(s, i - 1, all_clocks) } } } fn main() { let (thread_a, thread_b, thread_c, thread_d, thread_e, thread_f) = fork_mpst( endpoint_a, endpoint_b, endpoint_c, endpoint_d, endpoint_e, endpoint_f, ); thread_a.join().unwrap(); thread_b.join().unwrap(); thread_c.join().unwrap(); thread_d.join().unwrap(); thread_e.join().unwrap(); thread_f.join().unwrap(); }