19 个版本
0.1.17 | 2024年4月27日 |
---|---|
0.1.16 | 2022年6月7日 |
0.1.15 | 2021年12月2日 |
0.0.13 | 2021年9月6日 |
0.0.1 | 2020年3月9日 |
#46 在 并发 中
1,062 每月下载量
750KB
12K SLoC
Rust 的多方会话类型
此库在 Rust 中实现了至少两个参与者使用的多方会话类型。它依赖于 sesh。
关于此库的简短视频介绍可在以下链接找到:https://youtu.be/ej1FetN31HE。
用法
将以下内容添加到您的 Cargo.toml
[dependencies]
mpstthree = "0.1.17"
示例
假设一个涉及3个参与者(A、B 和 C)的简单协议。 A 向 B 发送有效负载,然后从 C 接收另一个有效负载。在从 A 接收到有效负载后,B 向 C 发送有效负载。此协议可以写成 A!B.A?C.B!C.0。要实现此示例,首先,从库中获取正确的组件。
// Used for the functions that will process the protocol
use std::boxed::Box;
use std::error::Error;
// Used for creating the types
use mpstthree::binary::struct_trait::{end::End, recv::Recv, send::Send};
use mpstthree::meshedchannels::MeshedChannels;
// Used for creating the stack of each role
use mpstthree::role::a::RoleA;
use mpstthree::role::b::RoleB;
use mpstthree::role::c::RoleC;
use mpstthree::role::end::RoleEnd;
// Importing the names of the participants
use mpstthree::name::a::NameA;
use mpstthree::name::b::NameB;
use mpstthree::name::c::NameC;
// Used for connecting all the roles, represented as MeshedChannels, together
use mpstthree::functionmpst::fork::fork_mpst;
然后,您必须为每个参与者的交互创建 二进制会话类型。请注意,每个创建的类型可以根据需要重复使用多次。在我们的示例中,我们为了清晰起见多次创建了相同的二进制会话类型,但我们可以使用其中两种类型来替代整个协议。
// Creating the binary sessions
// for A
type AtoB = Send<i32, End>;
type AtoC = Recv<i32, End>;
// for B
type BtoA = Recv<i32, End>;
type BtoC = Send<i32, End>;
// for C
type CtoA = Send<i32, End>;
type CtoB = Recv<i32, End>;
添加 堆栈,它为每个参与者提供操作的正确顺序。
// Stacks
// for A
type StackA = RoleB<RoleC<RoleEnd>>;
// for B
type StackB = RoleA<RoleC<RoleEnd>>;
// for C
type StackC = RoleA<RoleB<RoleEnd>>;
现在,您可以将这些 二进制会话类型 和 堆栈 封装到每个参与者的 MeshedChannels 中。我们还添加了相关角色的名称。
// Creating the MP sessions
// for A
type EndpointA = MeshedChannels<AtoB, AtoC, StackA, NameA>;
// for B
type EndpointB = MeshedChannels<BtoA, BtoC, StackB, NameB>;
// for C
type EndpointC = MeshedChannels<CtoA, CtoB, StackC, NameC>;
要运行协议,我们需要使用定义上述端点的函数详细说明参与者的行为。
// Function to process Endpoint of A
fn endpoint_a(s: EndpointA) -> Result<(), Box<dyn Error>> {
let s = s.send(1);
let (_x, s) = s.recv()?;
s.close()
}
// Function to process Endpoint of B
fn endpoint_b(s: EndpointB) -> Result<(), Box<dyn Error>> {
let (_x, s) = s.recv()?;
let s = s.send(2);
s.close()
}
// Function to process Endpoint of C
fn endpoint_c(s: EndpointC) -> Result<(), Box<dyn Error>> {
let s = s.send(3);
let (_x, s) = s.recv()?;
s.close()
}
最后,您需要使用 fork_mpst() 将与上述函数相关的线程链接/分叉在一起。不要忘记 unwrap() 返回的线程。
// Fork all endpoints
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();
}
运行此示例
要运行此示例,假设它在 examples/ 文件夹中,请使用以下命令:
cargo run --example [name of your example] --features="mpst"
其中 --features="mpst"
用于构建此库的 mpst
功能,包括 MeshedChannels
和 role
类型等。
入门指南
以下说明将帮助您在本地计算机上创建项目副本,以便进行开发和测试。
先决条件
您需要安装 Rust。您将安装 cargo
。
构建
要构建库,请运行以下代码。
cargo build
运行测试
要运行测试,请运行以下代码。
cargo test
运行
要运行库的示例 [XXX],请运行以下代码。
cargo run --example [XXX]
根据您想要运行的示例,您可能需要将之前的命令行修改为
cargo run --example [XXX] --features="[YYY]"
其中 [YYY] 是以下提供的功能之一:可用功能。
进一步学习
使用此库,可以编写至少有两个参与者且使用方法来简化编写和检查的任何协议。您可以查看测试和示例,以了解此库提供的不同可能性。
可用功能
以下是一些可用功能:
default
:默认功能,用于实现上述基本示例。message
:使用库提供的 message 结构的功能。macros_simple
:用于实现三个参与者的协议的功能,无论其名称如何。macros_multiple
:用于实现任何数量参与者的协议的功能。包含macros_simple
。baking
:用于实现任何数量参与者且使用关联函数而不是函数的协议的功能。包含macros_multiple
。baking_atmp
:用于实现任何数量参与者且使用关联函数而不是函数的异步 atmp 协议的功能。transport_tcp
:包含用于与 TCP 通信的原始功能。**需要在您的机器上安装openssl
、pkg-config
和libssl-dev
**。transport_udp
:包含用于与 UDP 通信的原始功能。**需要在您的机器上安装openssl
、pkg-config
和libssl-dev
**。transport_http
:包含用于与 HTTP/HTTPS 通信的原始功能。**需要在您的机器上安装openssl
、pkg-config
和libssl-dev
**。transport
:包含transport_tcp
、transport_udp
和transport_http
的功能。checking
:用于自顶向下方法的功能。需要 [KMC
] 工具。full
:包含checking
、baking
和transport
的功能。
贡献
请阅读 CONTRIBUTING.md 了解我们的行为准则以及向我们提交拉取请求的过程。
版本管理
我们使用 SemVer 进行版本管理。
作者
- Nicolas Lagaillardie - 初始工作 - Nicolas Lagaillardie
- Rumyana Neykova - 初始工作 - Rumyana Neykova
- Nobuko Yoshida - 初始工作 - Nobuko Yoshida
请参阅参与此项目的 贡献者 列表。
许可
本项目根据您的选择,受Apache License 2.0或MIT许可证的许可。
致谢
本项目是我目前在Nobuko Yoshida教授指导下进行的博士项目的一部分。我对此表示衷心的感谢。我还得到了来自Imperial College London的同事的帮助。
依赖项
~24–37MB
~373K SLoC