#![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); // Types // A enum Branching0fromCtoA { Forward( MeshedChannels< SendTimed<(), 'a', 0, true, 10, true, ' ', End>, RecursAtoC, RoleB>, NameA, >, ), Backward( MeshedChannels< RecvTimed<(), 'a', 0, true, 10, true, ' ', End>, RecursAtoC, RoleB>, NameA, >, ), Done(MeshedChannels), } type RecursAtoC = RecvTimed; // B enum Branching0fromCtoB { Forward( MeshedChannels< RecvTimed<(), 'a', 0, true, 10, true, ' ', End>, SendTimed<(), 'a', 0, true, 10, true, ' ', RecursBtoC>, RoleA>>, NameB, >, ), Backward( MeshedChannels< SendTimed<(), 'a', 0, true, 10, true, ' ', End>, RecvTimed<(), 'a', 0, true, 10, true, ' ', RecursBtoC>, RoleC>>, NameB, >, ), Done(MeshedChannels), } type RecursBtoC = RecvTimed; // C type Choose0fromCtoA = SendTimed; type Choose0fromCtoB = SendTimed; type EndpointForwardC = MeshedChannels< Choose0fromCtoA, RecvTimed<(), 'a', 0, true, 10, true, ' ', Choose0fromCtoB>, RoleB, NameC, >; type EndpointBackwardC = MeshedChannels< Choose0fromCtoA, SendTimed<(), 'a', 0, true, 10, true, ' ', Choose0fromCtoB>, RoleB, NameC, >; // Creating the MP sessions type EndpointA = MeshedChannels, NameA>; type EndpointB = MeshedChannels, NameB>; type EndpointC = MeshedChannels; fn endpoint_a(s: EndpointA, all_clocks: &mut HashMap) -> Result<(), Box> { all_clocks.insert('a', Instant::now()); offer_mpst!(s, all_clocks, { Branching0fromCtoA::Done(s) => { s.close() }, Branching0fromCtoA::Forward(s) => { let s = s.send((), all_clocks)?; endpoint_a(s, all_clocks) }, Branching0fromCtoA::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, { Branching0fromCtoB::Done(s) => { s.close() }, Branching0fromCtoB::Forward(s) => { let ((), s) = s.recv(all_clocks)?; let s = s.send((), all_clocks)?; endpoint_b(s, all_clocks) }, Branching0fromCtoB::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()); recurs_c(s, LOOPS, all_clocks) } fn recurs_c( s: EndpointC, index: i64, all_clocks: &mut HashMap, ) -> Result<(), Box> { match index { 0 => { let s = choose_mpst_c_to_all!( s, all_clocks, Branching0fromCtoA::Done, Branching0fromCtoB::Done ); s.close() } i if i % 2 == 0 => { let s: EndpointForwardC = choose_mpst_c_to_all!( s, all_clocks, Branching0fromCtoA::Forward, Branching0fromCtoB::Forward ); let (_, s) = s.recv(all_clocks)?; recurs_c(s, i - 1, all_clocks) } i => { let s: EndpointBackwardC = choose_mpst_c_to_all!( s, all_clocks, Branching0fromCtoA::Backward, Branching0fromCtoB::Backward ); let s = s.send((), all_clocks)?; recurs_c(s, i - 1, all_clocks) } } } fn main() { let (thread_a, thread_b, thread_c) = fork_mpst(endpoint_a, endpoint_b, endpoint_c); thread_a.join().unwrap(); thread_b.join().unwrap(); thread_c.join().unwrap(); }