#![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); // Types // SendTimed/RecvTimed type RS = RecvTimed<(), 'a', 0, true, 10, true, ' ', SendTimed<(), 'a', 0, true, 10, true, ' ', End>>; type SR = SendTimed<(), 'a', 0, true, 10, true, ' ', RecvTimed<(), 'a', 0, true, 10, true, ' ', End>>; // Roles type R2A = RoleA>; type R2B = RoleB>; type R2C = RoleC>; type R2D = RoleD>; // A enum Branching0fromDtoA { More( MeshedChannels< RS, RS, RecvTimed< (), 'a', 0, true, 10, true, ' ', SendTimed<(), 'a', 0, true, 10, true, ' ', RecursAtoD>, >, R2D>>>, NameA, >, ), Done(MeshedChannels), } type RecursAtoD = RecvTimed; // B enum Branching0fromDtoB { More( MeshedChannels< SR, RS, RecvTimed< (), 'a', 0, true, 10, true, ' ', SendTimed<(), 'a', 0, true, 10, true, ' ', RecursBtoD>, >, R2D>>>, NameB, >, ), Done(MeshedChannels), } type RecursBtoD = RecvTimed; // C enum Branching0fromDtoC { More( MeshedChannels< SR, SR, RecvTimed< (), 'a', 0, true, 10, true, ' ', SendTimed<(), 'a', 0, true, 10, true, ' ', RecursCtoD>, >, R2D>>>, NameC, >, ), Done(MeshedChannels), } type RecursCtoD = RecvTimed; // D type Choose0fromDtoA = SendTimed; type Choose0fromDtoB = SendTimed; type Choose0fromDtoC = SendTimed; type EndpointMoreD = MeshedChannels< SendTimed< (), 'a', 0, true, 10, true, ' ', RecvTimed<(), 'a', 0, true, 10, true, ' ', Choose0fromDtoA>, >, SendTimed< (), 'a', 0, true, 10, true, ' ', RecvTimed<(), 'a', 0, true, 10, true, ' ', Choose0fromDtoB>, >, SendTimed< (), 'a', 0, true, 10, true, ' ', RecvTimed<(), 'a', 0, true, 10, true, ' ', Choose0fromDtoC>, >, R2A>>, NameD, >; // Creating the MP sessions type EndpointA = MeshedChannels, NameA>; type EndpointB = MeshedChannels, NameB>; type EndpointC = MeshedChannels, NameC>; type EndpointD = MeshedChannels; fn endpoint_a(s: EndpointA, all_clocks: &mut HashMap) -> Result<(), Box> { all_clocks.insert('a', Instant::now()); offer_mpst!(s, all_clocks, { Branching0fromDtoA::Done(s) => { s.close() }, Branching0fromDtoA::More(s) => { let (_, s) = s.recv(all_clocks)?; let s = s.send((), all_clocks)?; let (_, s) = s.recv(all_clocks)?; let s = s.send((), all_clocks)?; let (_, s) = s.recv(all_clocks)?; let s = s.send((), 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, { Branching0fromDtoB::Done(s) => { s.close() }, Branching0fromDtoB::More(s) => { let (_, s) = s.recv(all_clocks)?; let s = s.send((), all_clocks)?; let s = s.send((), all_clocks)?; let (_, s) = s.recv(all_clocks)?; 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, { Branching0fromDtoC::Done(s) => { s.close() }, Branching0fromDtoC::More(s) => { let (_, s) = s.recv(all_clocks)?; let s = s.send((), all_clocks)?; let s = s.send((), all_clocks)?; let (_, s) = s.recv(all_clocks)?; let s = s.send((), all_clocks)?; let (_, s) = s.recv(all_clocks)?; endpoint_c(s, all_clocks) }, }) } fn endpoint_d(s: EndpointD, all_clocks: &mut HashMap) -> Result<(), Box> { all_clocks.insert('a', Instant::now()); recurs_d(s, LOOPS, all_clocks) } fn recurs_d( s: EndpointD, index: i64, all_clocks: &mut HashMap, ) -> Result<(), Box> { match index { 0 => { let s = choose_mpst_d_to_all!( s, all_clocks, Branching0fromDtoA::Done, Branching0fromDtoB::Done, Branching0fromDtoC::Done ); s.close() } i => { let s: EndpointMoreD = choose_mpst_d_to_all!( s, all_clocks, Branching0fromDtoA::More, Branching0fromDtoB::More, Branching0fromDtoC::More ); let s = s.send((), all_clocks)?; let (_, s) = s.recv(all_clocks)?; let s = s.send((), all_clocks)?; let (_, s) = s.recv(all_clocks)?; let s = s.send((), all_clocks)?; let (_, s) = s.recv(all_clocks)?; recurs_d(s, i - 1, all_clocks) } } } fn main() { let (thread_a, thread_b, thread_c, thread_d) = fork_mpst(endpoint_a, endpoint_b, endpoint_c, endpoint_d); thread_a.join().unwrap(); thread_b.join().unwrap(); thread_c.join().unwrap(); thread_d.join().unwrap(); }