#![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; generate_atmp!(MeshedChannels, A, B, C, D, E, F, G, H); // Types // A enum Branching0fromHtoA { Forward( MeshedChannels< SendTimed<(), 'a', 0, true, 10, true, ' ', End>, End, End, End, End, End, RecursAtoH, RoleB>, NameA, >, ), Backward( MeshedChannels< RecvTimed<(), 'a', 0, true, 10, true, ' ', End>, End, End, End, End, End, RecursAtoH, RoleB>, NameA, >, ), Done(MeshedChannels), } type RecursAtoH = RecvTimed; // B enum Branching0fromHtoB { Forward( MeshedChannels< RecvTimed<(), 'a', 0, true, 10, true, ' ', End>, SendTimed<(), 'a', 0, true, 10, true, ' ', End>, End, End, End, End, RecursBtoH, RoleA>>, NameB, >, ), Backward( MeshedChannels< SendTimed<(), 'a', 0, true, 10, true, ' ', End>, RecvTimed<(), 'a', 0, true, 10, true, ' ', End>, End, End, End, End, RecursBtoH, RoleC>>, NameB, >, ), Done(MeshedChannels), } type RecursBtoH = RecvTimed; // C enum Branching0fromHtoC { Forward( MeshedChannels< End, RecvTimed<(), 'a', 0, true, 10, true, ' ', End>, SendTimed<(), 'a', 0, true, 10, true, ' ', End>, End, End, End, RecursCtoH, RoleB>>, NameC, >, ), Backward( MeshedChannels< End, SendTimed<(), 'a', 0, true, 10, true, ' ', End>, RecvTimed<(), 'a', 0, true, 10, true, ' ', End>, End, End, End, RecursCtoH, RoleD>>, NameC, >, ), Done(MeshedChannels), } type RecursCtoH = RecvTimed; // D enum Branching0fromHtoD { Forward( MeshedChannels< End, End, RecvTimed<(), 'a', 0, true, 10, true, ' ', End>, SendTimed<(), 'a', 0, true, 10, true, ' ', End>, End, End, RecursDtoH, RoleC>>, NameD, >, ), Backward( MeshedChannels< End, End, SendTimed<(), 'a', 0, true, 10, true, ' ', End>, RecvTimed<(), 'a', 0, true, 10, true, ' ', End>, End, End, RecursDtoH, RoleE>>, NameD, >, ), Done(MeshedChannels), } type RecursDtoH = RecvTimed; // E enum Branching0fromHtoE { Forward( MeshedChannels< End, End, End, RecvTimed<(), 'a', 0, true, 10, true, ' ', End>, SendTimed<(), 'a', 0, true, 10, true, ' ', End>, End, RecursEtoH, RoleD>>, NameE, >, ), Backward( MeshedChannels< End, End, End, SendTimed<(), 'a', 0, true, 10, true, ' ', End>, RecvTimed<(), 'a', 0, true, 10, true, ' ', End>, End, RecursEtoH, RoleF>>, NameE, >, ), Done(MeshedChannels), } type RecursEtoH = RecvTimed; // F enum Branching0fromHtoF { Forward( MeshedChannels< End, End, End, End, RecvTimed<(), 'a', 0, true, 10, true, ' ', End>, SendTimed<(), 'a', 0, true, 10, true, ' ', End>, RecursFtoH, RoleE>>, NameF, >, ), Backward( MeshedChannels< End, End, End, End, SendTimed<(), 'a', 0, true, 10, true, ' ', End>, RecvTimed<(), 'a', 0, true, 10, true, ' ', End>, RecursFtoH, RoleG>>, NameF, >, ), Done(MeshedChannels), } type RecursFtoH = RecvTimed; // G enum Branching0fromHtoG { Forward( MeshedChannels< End, End, End, End, End, RecvTimed<(), 'a', 0, true, 10, true, ' ', End>, SendTimed<(), 'a', 0, true, 10, true, ' ', RecursGtoH>, RoleF>>, NameG, >, ), Backward( MeshedChannels< End, End, End, End, End, SendTimed<(), 'a', 0, true, 10, true, ' ', End>, RecvTimed<(), 'a', 0, true, 10, true, ' ', RecursGtoH>, RoleH>>, NameG, >, ), Done(MeshedChannels), } type RecursGtoH = RecvTimed; // H type Choose0fromHtoA = SendTimed; type Choose0fromHtoB = SendTimed; type Choose0fromHtoC = SendTimed; type Choose0fromHtoD = SendTimed; type Choose0fromHtoE = SendTimed; type Choose0fromHtoF = SendTimed; type Choose0fromHtoG = SendTimed; type EndpointForwardH = MeshedChannels< Choose0fromHtoA, Choose0fromHtoB, Choose0fromHtoC, Choose0fromHtoD, Choose0fromHtoE, Choose0fromHtoF, RecvTimed<(), 'a', 0, true, 10, true, ' ', Choose0fromHtoG>, RoleG, NameH, >; type EndpointBackwardH = MeshedChannels< Choose0fromHtoA, Choose0fromHtoB, Choose0fromHtoC, Choose0fromHtoD, Choose0fromHtoE, Choose0fromHtoF, SendTimed<(), 'a', 0, true, 10, true, ' ', Choose0fromHtoG>, RoleG, NameH, >; // 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, NameF>; type EndpointG = MeshedChannels, NameG>; type EndpointH = MeshedChannels< Choose0fromHtoA, Choose0fromHtoB, Choose0fromHtoC, Choose0fromHtoD, Choose0fromHtoE, Choose0fromHtoF, Choose0fromHtoG, RoleBroadcast, NameH, >; fn endpoint_a(s: EndpointA, all_clocks: &mut HashMap) -> Result<(), Box> { all_clocks.insert('a', Instant::now()); offer_mpst!(s, all_clocks, { Branching0fromHtoA::Done(s) => { s.close() }, Branching0fromHtoA::Forward(s) => { let s = s.send((), all_clocks)?; endpoint_a(s, all_clocks) }, Branching0fromHtoA::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, { Branching0fromHtoB::Done(s) => { s.close() }, Branching0fromHtoB::Forward(s) => { let ((), s) = s.recv(all_clocks)?; let s = s.send((), all_clocks)?; endpoint_b(s, all_clocks) }, Branching0fromHtoB::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, { Branching0fromHtoC::Done(s) => { s.close() }, Branching0fromHtoC::Forward(s) => { let ((), s) = s.recv(all_clocks)?; let s = s.send((), all_clocks)?; endpoint_c(s, all_clocks) }, Branching0fromHtoC::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, { Branching0fromHtoD::Done(s) => { s.close() }, Branching0fromHtoD::Forward(s) => { let ((), s) = s.recv(all_clocks)?; let s = s.send((), all_clocks)?; endpoint_d(s, all_clocks) }, Branching0fromHtoD::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, { Branching0fromHtoE::Done(s) => { s.close() }, Branching0fromHtoE::Forward(s) => { let ((), s) = s.recv(all_clocks)?; let s = s.send((), all_clocks)?; endpoint_e(s, all_clocks) }, Branching0fromHtoE::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()); offer_mpst!(s, all_clocks, { Branching0fromHtoF::Done(s) => { s.close() }, Branching0fromHtoF::Forward(s) => { let ((), s) = s.recv(all_clocks)?; let s = s.send((), all_clocks)?; endpoint_f(s, all_clocks) }, Branching0fromHtoF::Backward(s) => { let ((), s) = s.recv(all_clocks)?; let s = s.send((), all_clocks)?; endpoint_f(s, all_clocks) }, }) } fn endpoint_g(s: EndpointG, all_clocks: &mut HashMap) -> Result<(), Box> { all_clocks.insert('a', Instant::now()); offer_mpst!(s, all_clocks, { Branching0fromHtoG::Done(s) => { s.close() }, Branching0fromHtoG::Forward(s) => { let ((), s) = s.recv(all_clocks)?; let s = s.send((), all_clocks)?; endpoint_g(s, all_clocks) }, Branching0fromHtoG::Backward(s) => { let ((), s) = s.recv(all_clocks)?; let s = s.send((), all_clocks)?; endpoint_g(s, all_clocks) }, }) } fn endpoint_h(s: EndpointH, all_clocks: &mut HashMap) -> Result<(), Box> { all_clocks.insert('a', Instant::now()); recurs_h(s, LOOPS, all_clocks) } fn recurs_h( s: EndpointH, index: i64, all_clocks: &mut HashMap, ) -> Result<(), Box> { match index { 0 => { let s = choose_mpst_h_to_all!( s, all_clocks, Branching0fromHtoA::Done, Branching0fromHtoB::Done, Branching0fromHtoC::Done, Branching0fromHtoD::Done, Branching0fromHtoE::Done, Branching0fromHtoF::Done, Branching0fromHtoG::Done ); s.close() } i if i % 2 == 0 => { let s: EndpointForwardH = choose_mpst_h_to_all!( s, all_clocks, Branching0fromHtoA::Forward, Branching0fromHtoB::Forward, Branching0fromHtoC::Forward, Branching0fromHtoD::Forward, Branching0fromHtoE::Forward, Branching0fromHtoF::Forward, Branching0fromHtoG::Forward ); let (_, s) = s.recv(all_clocks)?; recurs_h(s, i - 1, all_clocks) } i => { let s: EndpointBackwardH = choose_mpst_h_to_all!( s, all_clocks, Branching0fromHtoA::Backward, Branching0fromHtoB::Backward, Branching0fromHtoC::Backward, Branching0fromHtoD::Backward, Branching0fromHtoE::Backward, Branching0fromHtoF::Backward, Branching0fromHtoG::Backward ); let s = s.send((), all_clocks)?; recurs_h(s, i - 1, all_clocks) } } } fn main() { let (thread_a, thread_b, thread_c, thread_d, thread_e, thread_f, thread_g, thread_h) = fork_mpst( endpoint_a, endpoint_b, endpoint_c, endpoint_d, endpoint_e, endpoint_f, endpoint_g, endpoint_h, ); thread_a.join().unwrap(); thread_b.join().unwrap(); thread_c.join().unwrap(); thread_d.join().unwrap(); thread_e.join().unwrap(); thread_f.join().unwrap(); thread_g.join().unwrap(); thread_h.join().unwrap(); }