Code Monkey home page Code Monkey logo

mpst_rust_github's Introduction

Multiparty session types for Rust

Ubuntu Windows Mac Crate Minimum rustc version Documentation codecov dependency status

This library implements multiparty session types in Rust for at least two participants. It was inspired by sesh.

A short video presentation of the library can be found here: https://youtu.be/ej1FetN31HE.

Usage

Add this to your Cargo.toml:

[dependencies]
mpstthree = "0.1.17"

Example

Assume a simple protocol involving 3 participants, A, B and C. A sends a payload to B, then receives another payload from C. Upon receiving the payload from A, B sends a payload to C. This protocol can be written as A!B.A?C.B!C.0. To implement this example, first, get the right components from the library.

// 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;

Then, you have to create the binary session types defining the interactions for each pair of participants. Note that each created type can be reused as many times as needed. For our example, we create several times the same binary session type for clarity, but we could use only two of those types for the whole protocol instead.

// 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>;

Add the stacks which give the correct order of the operations for each participant.

// Stacks
// for A
type StackA = RoleB<RoleC<RoleEnd>>;
// for B
type StackB = RoleA<RoleC<RoleEnd>>;
// for C
type StackC = RoleA<RoleB<RoleEnd>>;

You can now encapsulate those binary session types and stacks into MeshedChannels for each participant. We also add the names of the related roles.

// 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>;

To run the protocol, we need to detail the behaviour of the participants with functions that input the Endpoints defined above.

// 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()
}

In the end, you have to link/fork the threads, related to the functions above, together with fork_mpst(). Do not forget to unwrap() the returned threads.

// 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();
}

Running this example

For running this example, assuming it is in the examples/ folder, use:

cargo run --example [name of your example] --features="mpst"

where --features="mpst" is used for building the mpst feature of this library, which includes the MeshedChannels and roles types, among other things.

Getting started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.

Prerequisites

You need to have Rust. You will get cargo installed.

Building

For building the library, run this code.

cargo build

Run test

For running the tests, run this code.

cargo test

Running

For running an example [XXX] of the library, run this code.

cargo run --example [XXX]

Depending on the example you would like to run, you may have to modify the previous command line into:

cargo run --example [XXX] --features="[YYY]"

where [YYY] is one or more of the features provided in Available features hereafter.

Going further

With this library, one can write any protocol with at least two participants and using methods to shorten the writing and checking. You can check the tests and examples to have a larger overview of the different possibilities provided by this library.

Available features

The different features available are:

  1. default: default features, for implementing the basic example above.
  2. message: feature for using the message structure provided by the library.
  3. macros_simple: feature for implementing protocols with three participants, whatever are their name.
  4. macros_multiple: feature for implementing protocols with any number of participants. Contains macros_simple.
  5. baking: feature for implementing protocols with any number of participants and using associated functions instead of functions. Contains macros_multiple.
  6. baking_atmp: feature for implementing asynchronous atmp protocols with any number of participants and using associated functions instead of functions.
  7. transport_tcp: feature containing primitives for communicating with TCP. Requires openssl, pkg-config and libssl-dev installed on your machine.
  8. transport_udp: feature containing primitives for communicating with UDP. Requires openssl, pkg-config and libssl-dev installed on your machine.
  9. transport_http: feature containing primitives for communicating with HTTP/HTTPS. Requires openssl, pkg-config and libssl-dev installed on your machine.
  10. transport: feature containing transport_tcp, transport_udp and transport_http.
  11. checking: feature for the top-down approach. Needs the [KMC] tool.
  12. full: feature containing checking, baking and transport.

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

Versioning

We use SemVer for versioning.

Authors

See also the list of contributors who participated in this project.

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.

Acknowledgment

This project is part of my current PhD under the supervision of Nobuko Yoshida, that I would like to thank. I was also helped by my colleagues from Imperial College London.

mpst_rust_github's People

Contributors

nicolaslagaillardie avatar nobukoyoshida avatar rkuhn avatar rumineykova avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

mpst_rust_github's Issues

Serialisation (hint)

In impl_a

fn get_graph() {
// initialise graph with a root node
// return aux_get_graph
}

fn aux_get_graph(
// graph and previous nodes/choices
) {
// depending on the current state of the meshed channel, create the correct edge and node
// create a new meshedchannel with all the continuations and return its aux_get_graph
}

Fix choose.rs

Order in parameters is not the same in every function

Some issues understanding tcp example

Hi, this might not be soi much of an issue, but maybe more a problem of understanding.
A little context first. I'm doing a project, where we should have an implementation of a specific protocol, that uses session types for the communication of two devices, on a network.

So, as a consequence of this, I was interested in looking at the tcp/client/server example of this page. Other session types repo's that I have been looking at have'nt had examples where session types was actually used by two processes acting away from each other.
And I had a hard time conceptually understanding how session types would work over a connection, since the definition of parties could not be in the same file, since the endpoints are usually run together by using fork_mpst

So I was thrilled to see your tcp example, in the example/tcp_and_others folder. And I have now studied it for a bit.

But it seems to me like the files in that folder does'nt actually use the session type specified in this library. The other examples in this repo follows the structure described in the README of the repo, defining roles and sending and receiving function with use mpstthree, but in the tcp example, another library called crossbeam_channel, which seems to be a more general.

So, is it possible to use the session types that this library uses over a connection? or maybe i'm completely misunderstanding the examples given

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.