use either::Either; use mpstthree::binary::struct_trait::{end::End, recv::Recv, send::Send, session::Session}; use mpstthree::role::end::RoleEnd; use mpstthree::role::Role; use std::error::Error; use rand::{thread_rng, Rng}; use mpstthree::generate; // Create new roles generate!("basic", MeshedChannels, A, B, C); // Those types will be code generated type OfferMpst = Recv, MeshedChannels>, End>; type ChooseMpst = Send< Either< MeshedChannels<::Dual, ::Dual, ::Dual, N0>, MeshedChannels<::Dual, ::Dual, ::Dual, N0>, >, End, >; // Types type AtoCClose = End; type AtoBClose = End; type AtoCVideo = Recv>; type AtoBVideo = Send>; type BtoAClose = ::Dual; type BtoCClose = End; type BtoAVideo = as Session>::Dual; type CtoBClose = ::Dual; type CtoAClose = ::Dual; type CtoAVideo = as Session>::Dual; // Stacks type StackAEnd = RoleEnd; type StackAVideo = RoleC>>>; type StackAVideoDual = ::Dual; type StackAFull = RoleC>>; type StackBEnd = RoleEnd; type StackBVideo = RoleA>; type StackBVideoDual = ::Dual; type StackBFull = RoleAlltoC; type StackCEnd = RoleEnd; type StackCVideo = RoleA>; type StackCChoice = RoleCtoAll; type StackCFull = RoleA>; // Creating the MP sessions // For C type ChooseCtoA = ChooseMpst, CtoAVideo, BtoAClose, CtoAClose, StackAVideoDual, StackAEnd, NameA>; type ChooseCtoB = ChooseMpst, CtoBClose, AtoBClose, CtoBClose, StackBVideoDual, StackBEnd, NameB>; type InitC = Send>>; type EndpointCFull = MeshedChannels, ChooseCtoB, StackCFull, NameC>; // For A type EndpointAVideo = MeshedChannels, AtoCVideo, StackAVideo, NameA>; type EndpointAEnd = MeshedChannels; type OfferA = OfferMpst, AtoCVideo, AtoBClose, AtoCClose, StackAVideo, StackAEnd, NameA>; type InitA = Recv>>; type EndpointAFull = MeshedChannels, StackAFull, NameA>; // For B type EndpointBVideo = MeshedChannels, BtoCClose, StackBVideo, NameB>; type EndpointBEnd = MeshedChannels; type OfferB = OfferMpst, BtoCClose, BtoAClose, BtoCClose, StackBVideo, StackBEnd, NameB>; type EndpointBFull = MeshedChannels, StackBFull, NameB>; // Functions related to endpoints fn server(s: EndpointBFull) -> Result<(), Box> { s.offer( |s: EndpointBVideo| { let (request, s) = s.recv(); s.send(request + 1).close() }, |s: EndpointBEnd| s.close(), ) } fn authenticator(s: EndpointAFull) -> Result<(), Box> { let (id, s) = s.recv(); s.send(id + 1).offer( |s: EndpointAVideo| { let (request, s) = s.recv(); let (video, s) = s.send(request + 1).recv(); assert_eq!(request, id + 1); assert_eq!(video, id + 3); s.send(video + 1).close() }, |s: EndpointAEnd| s.close(), ) } fn client_video(s: EndpointCFull) -> Result<(), Box> { let mut rng = thread_rng(); let id: i32 = rng.gen(); let (accept, s) = s.send(id).recv(); assert_eq!(accept, id + 1); let (result, s) = s.choose_left().send(accept).recv(); assert_eq!(result, accept + 3); s.close() } fn client_close(s: EndpointCFull) -> Result<(), Box> { let mut rng = thread_rng(); let id: i32 = rng.gen(); let (accept, s) = s.send(id).recv(); assert_eq!(accept, id + 1); s.choose_right().close() } ///////////////////////////////////////// pub fn run_usecase_right() { // Test end branch. let (thread_a, thread_b, thread_c) = fork_mpst(authenticator, server, client_close); assert!(thread_a.join().is_ok()); assert!(thread_b.join().is_ok()); assert!(thread_c.join().is_ok()); } pub fn run_usecase_left() { // Test video branch. let (thread_a, thread_b, thread_c) = fork_mpst(authenticator, server, client_video); assert!(thread_a.join().is_ok()); assert!(thread_b.join().is_ok()); assert!(thread_c.join().is_ok()); }