use rand::{thread_rng, Rng}; use mpstthree::binary::struct_trait::{end::End, recv::Recv, send::Send, session::Session}; use mpstthree::functionmpst::fork::fork_mpst; use mpstthree::meshedchannels::MeshedChannels; use mpstthree::role::broadcast::RoleBroadcast; use mpstthree::{checker_concat, checker_concat_impl}; // use std::boxed::Box; use std::error::Error; // Get roles use mpstthree::role::a::RoleA; use mpstthree::role::b::RoleB; use mpstthree::role::c::RoleC; use mpstthree::role::end::RoleEnd; use mpstthree::name::a::NameA; use mpstthree::name::b::NameB; use mpstthree::name::c::NameC; use mpstthree::choose_mpst_c_to_all; use mpstthree::offer_mpst_a_to_c; use mpstthree::offer_mpst_b_to_c; use petgraph::dot::Dot; // Test our usecase // Simple types // Client = C // Authenticator = A // Server = B type AtoCClose = End; type AtoBClose = End; type AtoBVideo = Send>; type AtoCVideo = Recv>; type InitA = Recv>; type BtoAClose = ::Dual; type BtoCClose = End; type BtoAVideo = ::Dual; type RecursAtoC = Recv; type RecursBtoC = Recv; enum Branches0AtoC { End(MeshedChannels), Video(MeshedChannels), } enum Branches0BtoC { End(MeshedChannels), Video(MeshedChannels), } type Choose0fromCtoA = Send; type Choose0fromCtoB = Send; type InitC = Send>; // Stacks type StackAEnd = RoleEnd; type StackAVideo = RoleC>>>>; type StackARecurs = RoleC; type StackAInit = RoleC>>; type StackBEnd = RoleEnd; type StackBVideo = RoleA>>; type StackBRecurs = RoleC; type StackCRecurs = RoleBroadcast; type StackCFull = RoleA>; // Creating the MP sessions // For C type EndpointCEnd = MeshedChannels; type EndpointCVideo = MeshedChannels< Send>>, Send, RoleA>, NameC, >; type EndpointCRecurs = MeshedChannels; type EndpointCFull = MeshedChannels; // For A type EndpointARecurs = MeshedChannels; type EndpointAFull = MeshedChannels; // For B type EndpointBFull = MeshedChannels; // Functions related to endpoints fn server(s: EndpointBFull) -> Result<(), Box> { offer_mpst_b_to_c!(s, { Branches0BtoC::End(s) => { s.close() }, Branches0BtoC::Video(s) => { let (request, s) = s.recv()?; let s = s.send(request + 1); server(s) }, }) } fn authenticator(s: EndpointAFull) -> Result<(), Box> { let (id, s) = s.recv()?; let s = s.send(id + 1); authenticator_recurs(s) } fn authenticator_recurs(s: EndpointARecurs) -> Result<(), Box> { offer_mpst_a_to_c!(s, { Branches0AtoC::End(s) => { s.close() }, Branches0AtoC::Video(s) => { let (request, s) = s.recv()?; let (video, s) = s.send(request + 1).recv()?; let s = s.send(video + 1); authenticator_recurs(s) }, }) } fn client(s: EndpointCFull) -> Result<(), Box> { let xs: Vec = (1..100).map(|_| thread_rng().gen()).collect(); let (_, s) = s.send(0).recv()?; client_recurs(s, xs, 1) } fn client_recurs(s: EndpointCRecurs, mut xs: Vec, index: i32) -> Result<(), Box> { match xs.pop() { Option::Some(_) => { let s: EndpointCFull = choose_mpst_c_to_all!(s, Branches0AtoC::Video, Branches0BtoC::Video); let (_, s) = s.send(1).recv()?; client_recurs(s, xs, index + 1) } Option::None => { let s = choose_mpst_c_to_all!(s, Branches0AtoC::End, Branches0BtoC::End); assert_eq!(index, 100); s.close() } } } ///////////////////////////////////////// pub fn run_c_usecase_recursive() { let (thread_a, thread_b, thread_c) = fork_mpst(authenticator, server, client); assert!(thread_a.join().is_ok()); assert!(thread_b.join().is_ok()); assert!(thread_c.join().is_ok()); } checker_concat_impl!( [Branches0AtoC, Video, Branches0BtoC, Video], [Branches0AtoC, End, Branches0BtoC, End] ); pub fn run_c_usecase_recursive_checker() { let (graphs, kmc) = checker_concat!( "", EndpointAFull, EndpointCFull, EndpointBFull => [ EndpointCVideo, Branches0AtoC, Video, Branches0BtoC, Video ], [ EndpointCEnd, Branches0AtoC, End, Branches0BtoC, End ] ) .unwrap(); ////////////// Test graph A let graph_a = &graphs["RoleA"]; assert_eq!( format!("{:?}", Dot::new(&graph_a)), "digraph {\n \ 0 [ label = \"\\\"0\\\"\" ]\n \ 1 [ label = \"\\\"1\\\"\" ]\n \ 2 [ label = \"\\\"2\\\"\" ]\n \ 3 [ label = \"\\\"2.1\\\"\" ]\n \ 4 [ label = \"\\\"2.1\\\"\" ]\n \ 5 [ label = \"\\\"2.2\\\"\" ]\n \ 6 [ label = \"\\\"2.3\\\"\" ]\n \ 7 [ label = \"\\\"2.4\\\"\" ]\n \ 0 -> 1 [ label = \"\\\"RoleA?RoleC: i32\\\"\" ]\n \ 1 -> 2 [ label = \"\\\"RoleA!RoleC: i32\\\"\" ]\n \ 2 -> 3 [ label = \"\\\"0\\\"\" ]\n \ 2 -> 4 [ label = \"\\\"RoleA?RoleC: i32\\\"\" ]\n \ 4 -> 5 [ label = \"\\\"RoleA!RoleB: i32\\\"\" ]\n \ 5 -> 6 [ label = \"\\\"RoleA?RoleB: i32\\\"\" ]\n \ 6 -> 7 [ label = \"\\\"RoleA!RoleC: i32\\\"\" ]\n \ 7 -> 2 [ label = \"\\\"µ\\\"\" ]\n\ }\n" ); ////////////// Test graph B let graph_b = &graphs["RoleB"]; assert_eq!( format!("{:?}", Dot::new(&graph_b)), "digraph {\n \ 0 [ label = \"\\\"0\\\"\" ]\n \ 1 [ label = \"\\\"0.1\\\"\" ]\n \ 2 [ label = \"\\\"0.1\\\"\" ]\n \ 3 [ label = \"\\\"0.2\\\"\" ]\n \ 0 -> 1 [ label = \"\\\"0\\\"\" ]\n \ 0 -> 2 [ label = \"\\\"RoleB?RoleA: i32\\\"\" ]\n \ 2 -> 3 [ label = \"\\\"RoleB!RoleA: i32\\\"\" ]\n \ 3 -> 0 [ label = \"\\\"µ\\\"\" ]\n\ }\n" ); ////////////// Test graph C let graph_c = &graphs["RoleC"]; assert_eq!( format!("{:?}", Dot::new(&graph_c)), "digraph {\n \ 0 [ label = \"\\\"0\\\"\" ]\n \ 1 [ label = \"\\\"1\\\"\" ]\n \ 2 [ label = \"\\\"2\\\"\" ]\n \ 3 [ label = \"\\\"2.1\\\"\" ]\n \ 4 [ label = \"\\\"2.1\\\"\" ]\n \ 5 [ label = \"\\\"2.2\\\"\" ]\n \ 0 -> 1 [ label = \"\\\"RoleC!RoleA: i32\\\"\" ]\n \ 1 -> 2 [ label = \"\\\"RoleC?RoleA: i32\\\"\" ]\n \ 2 -> 3 [ label = \"\\\"0\\\"\" ]\n \ 2 -> 4 [ label = \"\\\"RoleC!RoleA: i32\\\"\" ]\n \ 4 -> 5 [ label = \"\\\"RoleC?RoleA: i32\\\"\" ]\n \ 5 -> 2 [ label = \"\\\"µ\\\"\" ]\n\ }\n" ); ////////////// Test KMC output assert_eq!(kmc, None); }