use mpstthree::binary::struct_trait::{end::End, recv::Recv, send::Send}; use mpstthree::generate; use mpstthree::role::broadcast::RoleBroadcast; use mpstthree::role::end::RoleEnd; use rand::{thread_rng, Rng}; use std::error::Error; use std::marker; // See the folder scribble_protocols for the related Scribble protocol // Create new MeshedChannels for two participants generate!("recursive", MeshedChannels, Controller, Logs); // RoleController enum Branching0fromLtoC { Success( MeshedChannels>, RoleLogs>, NameController>, ), Failure(MeshedChannels>, RoleLogs, NameController>), } type Recurs0fromCtoL = Recv, End>; type Choose1fromCtoL = Send, End>; // RoleLogs type Choose0fromLtoC = Send, End>; enum Branching1fromCtoL { Restart(MeshedChannels>, RoleController, NameLogs>), Stop(MeshedChannels, RoleController, NameLogs>), } type Recurs1fromLtoC = Recv, End>; // Creating the MP sessions // RoleController type EndpointController1Stop = MeshedChannels, RoleLogs, NameController>; type EndpointController1Restart = MeshedChannels>, RoleLogs>, NameController>; type EndpointController0 = MeshedChannels, RoleLogs, NameController>; type EndpointController1 = MeshedChannels, RoleBroadcast, NameController>; type EndpointControllerInit = MeshedChannels>, RoleLogs>, NameController>; // RoleLogs type EndpointLogs0Success = MeshedChannels>, RoleController, NameLogs>; type EndpointLogs0Failure = MeshedChannels>, RoleController>, NameLogs>; type EndpointLogs0 = MeshedChannels, RoleBroadcast, NameLogs>; type EndpointLogs1 = MeshedChannels, RoleController, NameLogs>; type EndpointLogsInit = MeshedChannels>, RoleController, NameLogs>; fn endpoint_controller(s: EndpointControllerInit) -> Result<(), Box> { let start: i32 = thread_rng().gen_range(5..100); let s = s.send(start); recurs_0_controller(s, start) } fn recurs_0_controller(s: EndpointController0, loops: i32) -> Result<(), Box> { offer_mpst!(s, { Branching0fromLtoC::Success(s) => { let (_, s) = s.recv(); recurs_0_controller(s, loops) }, Branching0fromLtoC::Failure(s) => { let (_, s) = s.recv(); recurs_1_controller(s, loops) }, }) } fn recurs_1_controller(s: EndpointController1, loops: i32) -> Result<(), Box> { match loops { i if i <= 0 => { // Stop let s: EndpointController1Stop = choose_mpst_controller_to_all!(s, Branching1fromCtoL::Stop); let s = s.send(loops - 1); s.close() } _ => { // Restart let s: EndpointController1Restart = choose_mpst_controller_to_all!(s, Branching1fromCtoL::Restart); let s = s.send(loops - 1); recurs_0_controller(s, loops - 1) } } } fn endpoint_logs(s: EndpointLogsInit) -> Result<(), Box> { let (status, s) = s.recv(); recurs_0_logs(s, status) } fn recurs_0_logs(s: EndpointLogs0, loops: i32) -> Result<(), Box> { match loops { i if i % 2 == 0 && i > 0 => { // Success let s: EndpointLogs0Success = choose_mpst_logs_to_all!(s, Branching0fromLtoC::Success); let s = s.send(loops - 1); recurs_0_logs(s, loops - 1) } _ => { // Failure let s: EndpointLogs0Failure = choose_mpst_logs_to_all!(s, Branching0fromLtoC::Failure); let s = s.send(loops - 1); recurs_1_logs(s) } } } fn recurs_1_logs(s: EndpointLogs1) -> Result<(), Box> { offer_mpst!(s, { Branching1fromCtoL::Restart(s) => { let (loops, s) = s.recv(); recurs_0_logs(s, loops - 1) }, Branching1fromCtoL::Stop(s) => { let (_, s) = s.recv(); s.close() }, }) } ///////////////////////// fn main() { let (thread_controller, thread_logs) = fork_mpst(endpoint_controller, endpoint_logs); thread_controller.join().unwrap(); thread_logs.join().unwrap(); }