mpstthree 0.1.17

A library implementing Multiparty Session Types for 2 or more participants
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
#![allow(dead_code, non_camel_case_types, unused_variables)]

use mpstthree::binary::struct_trait::end::End;
use mpstthree::binary_atmp::struct_trait::{recv::RecvTimed, send::SendTimed};
use mpstthree::generate_atmp;
use mpstthree::role::broadcast::RoleBroadcast;
use mpstthree::role::end::RoleEnd;
use std::collections::HashMap;
use std::error::Error;
use std::time::Instant;

generate_atmp!(MeshedChannels, A, B, C);

// Types of the payloads
struct Payload;
struct Test0;
struct TestInit;

struct Test2 {
    payload: Payload,
}
struct Test1;
struct Test3;
struct Test4 {
    payload: Payload,
}

// Binary sessions in depth 0
// Binary sessions for A
type Message_0_v_0_FromAToB =
    SendTimed<TestInit, 'a', 0, true, 1, true, ' ', Message_0_v_1_FromAToB>;
type Message_0_v_1_FromAToB = SendTimed<Test0, 'a', 0, true, 1, true, ' ', Message_0_v_2_FromAToB>;
type Message_0_v_2_FromAToB = SendTimed<Choice_0_FromAToB, ' ', -2, false, -1, false, ' ', End>;
type Message_0_v_0_FromAToC = SendTimed<Choice_0_FromAToC, ' ', -2, false, -1, false, ' ', End>;

// Binary sessions for B
type Message_0_v_0_FromBToA =
    RecvTimed<TestInit, 'a', 0, true, 1, true, ' ', Message_0_v_1_FromBToA>;
type Message_0_v_1_FromBToA = RecvTimed<Test0, 'a', 0, true, 1, true, ' ', Message_0_v_2_FromBToA>;
type Message_0_v_2_FromBToA = RecvTimed<Choice_0_FromAToB, ' ', -2, false, -1, false, ' ', End>;
type Message_0_v_0_FromBToC = End;

// Binary sessions for C
type Message_0_v_0_FromCToA = RecvTimed<Choice_0_FromAToC, ' ', -2, false, -1, false, ' ', End>;
type Message_0_v_0_FromCToB = End;

// Binary sessions in depth 0.1
// Binary sessions for A
type Message_0_1_v_0_FromAToB =
    SendTimed<Test1, 'a', 0, true, 1, true, ' ', Message_0_1_v_1_FromAToB>;
type Message_0_1_v_1_FromAToB =
    SendTimed<Test2, 'a', 0, true, 1, true, ' ', Message_0_1_v_2_FromAToB>;
type Message_0_1_v_2_FromAToB =
    SendTimed<Test3, 'a', 0, true, 1, true, 'a', Message_0_1_v_3_FromAToB>;
type Message_0_1_v_3_FromAToB =
    SendTimed<Test4, 'a', 0, true, 1, true, 'a', Message_0_1_v_4_FromAToB>;
type Message_0_1_v_4_FromAToB = End;
type Message_0_1_v_0_FromAToC = End;

// Binary sessions for B
type Message_0_1_v_0_FromBToA =
    RecvTimed<Test1, 'a', 0, true, 1, true, ' ', Message_0_1_v_1_FromBToA>;
type Message_0_1_v_1_FromBToA =
    RecvTimed<Test2, 'a', 0, true, 1, true, ' ', Message_0_1_v_2_FromBToA>;
type Message_0_1_v_2_FromBToA =
    RecvTimed<Test3, 'a', 0, true, 1, true, 'a', Message_0_1_v_3_FromBToA>;
type Message_0_1_v_3_FromBToA =
    RecvTimed<Test4, 'a', 0, true, 1, true, 'a', Message_0_1_v_4_FromBToA>;
type Message_0_1_v_4_FromBToA = End;
type Message_0_1_v_0_FromBToC = End;

// Binary sessions for C
type Message_0_1_v_0_FromCToA = End;
type Message_0_1_v_0_FromCToB = End;

// Binary sessions in depth 0.0
// Binary sessions for C
type Message_0_0_v_0_FromCToA = RecvTimed<Choice_0_FromAToC, ' ', -2, false, -1, false, ' ', End>;
type Message_0_0_v_0_FromCToB = End;

// Binary sessions for A
type Message_0_0_v_0_FromAToB =
    SendTimed<Test1, 'a', 0, true, 1, true, ' ', Message_0_0_v_1_FromAToB>;
type Message_0_0_v_1_FromAToB =
    SendTimed<Test2, 'a', 0, true, 1, true, ' ', Message_0_0_v_2_FromAToB>;
type Message_0_0_v_2_FromAToB =
    SendTimed<Test3, 'a', 0, true, 1, true, 'a', Message_0_0_v_3_FromAToB>;
type Message_0_0_v_3_FromAToB =
    SendTimed<Test4, 'a', 0, true, 1, true, 'a', Message_0_0_v_4_FromAToB>;
type Message_0_0_v_4_FromAToB =
    SendTimed<Test0, 'a', 0, true, 1, true, ' ', Message_0_0_v_5_FromAToB>;
type Message_0_0_v_5_FromAToB = SendTimed<Choice_0_FromAToB, ' ', -2, false, -1, false, ' ', End>;
type Message_0_0_v_0_FromAToC = SendTimed<Choice_0_FromAToC, ' ', -2, false, -1, false, ' ', End>;

// Binary sessions for B
type Message_0_0_v_0_FromBToC = End;
type Message_0_0_v_0_FromBToA =
    RecvTimed<Test1, 'a', 0, true, 1, true, ' ', Message_0_0_v_1_FromBToA>;
type Message_0_0_v_1_FromBToA =
    RecvTimed<Test2, 'a', 0, true, 1, true, ' ', Message_0_0_v_2_FromBToA>;
type Message_0_0_v_2_FromBToA =
    RecvTimed<Test3, 'a', 0, true, 1, true, 'a', Message_0_0_v_3_FromBToA>;
type Message_0_0_v_3_FromBToA =
    RecvTimed<Test4, 'a', 0, true, 1, true, 'a', Message_0_0_v_4_FromBToA>;
type Message_0_0_v_4_FromBToA =
    RecvTimed<Test0, 'a', 0, true, 1, true, ' ', Message_0_0_v_5_FromBToA>;
type Message_0_0_v_5_FromBToA = RecvTimed<Choice_0_FromAToB, ' ', -2, false, -1, false, ' ', End>;

// Stacks in depth 0
// Stacks for C
type Ordering_0_v_0_ForC = RoleA<RoleEnd>;

// Stacks for B
type Ordering_0_v_0_ForB = RoleA<Ordering_0_v_1_ForB>;
type Ordering_0_v_1_ForB = RoleA<Ordering_0_v_2_ForB>;
type Ordering_0_v_2_ForB = RoleA<RoleEnd>;

// Stacks for A
type Ordering_0_v_0_ForA = RoleB<Ordering_0_v_1_ForA>;
type Ordering_0_v_1_ForA = RoleB<Ordering_0_v_2_ForA>;
type Ordering_0_v_2_ForA = RoleBroadcast;

// Stacks in depth 0.1
// Stacks for B
type Ordering_0_1_v_0_ForB = RoleA<Ordering_0_1_v_1_ForB>;
type Ordering_0_1_v_1_ForB = RoleA<Ordering_0_1_v_2_ForB>;
type Ordering_0_1_v_2_ForB = RoleA<Ordering_0_1_v_3_ForB>;
type Ordering_0_1_v_3_ForB = RoleA<Ordering_0_1_v_4_ForB>;
type Ordering_0_1_v_4_ForB = RoleEnd;

// Stacks for C
type Ordering_0_1_v_0_ForC = RoleEnd;

// Stacks for A
type Ordering_0_1_v_0_ForA = RoleB<Ordering_0_1_v_1_ForA>;
type Ordering_0_1_v_1_ForA = RoleB<Ordering_0_1_v_2_ForA>;
type Ordering_0_1_v_2_ForA = RoleB<Ordering_0_1_v_3_ForA>;
type Ordering_0_1_v_3_ForA = RoleB<Ordering_0_1_v_4_ForA>;
type Ordering_0_1_v_4_ForA = RoleEnd;

// Stacks in depth 0.0
// Stacks for A
type Ordering_0_0_v_0_ForA = RoleB<Ordering_0_0_v_1_ForA>;
type Ordering_0_0_v_1_ForA = RoleB<Ordering_0_0_v_2_ForA>;
type Ordering_0_0_v_2_ForA = RoleB<Ordering_0_0_v_3_ForA>;
type Ordering_0_0_v_3_ForA = RoleB<Ordering_0_0_v_4_ForA>;
type Ordering_0_0_v_4_ForA = RoleB<Ordering_0_0_v_5_ForA>;
type Ordering_0_0_v_5_ForA = RoleBroadcast;

// Stacks for B
type Ordering_0_0_v_0_ForB = RoleA<Ordering_0_0_v_1_ForB>;
type Ordering_0_0_v_1_ForB = RoleA<Ordering_0_0_v_2_ForB>;
type Ordering_0_0_v_2_ForB = RoleA<Ordering_0_0_v_3_ForB>;
type Ordering_0_0_v_3_ForB = RoleA<Ordering_0_0_v_4_ForB>;
type Ordering_0_0_v_4_ForB = RoleA<Ordering_0_0_v_5_ForB>;
type Ordering_0_0_v_5_ForB = RoleA<RoleEnd>;

// Stacks for C
type Ordering_0_0_v_0_ForC = RoleA<RoleEnd>;

// Enums (Branchings) in depth 0

// Enums (Branchings) for B
enum Choice_0_FromAToB {
    Branching0(Endpoint_0_0_ForB),
    Branching1(Endpoint_0_1_ForB),
}

// Enums (Branchings) for C
enum Choice_0_FromAToC {
    Branching0(Endpoint_0_0_ForC),
    Branching1(Endpoint_0_1_ForC),
}

// Endpoints in depth 0
// Endpoint for role A
type Endpoint_0_ForA =
    MeshedChannels<Message_0_v_0_FromAToB, Message_0_v_0_FromAToC, Ordering_0_v_0_ForA, NameA>;

// Endpoint for role B
type Endpoint_0_ForB =
    MeshedChannels<Message_0_v_0_FromBToA, Message_0_v_0_FromBToC, Ordering_0_v_0_ForB, NameB>;

// Endpoint for role C
type Endpoint_0_ForC =
    MeshedChannels<Message_0_v_0_FromCToA, Message_0_v_0_FromCToB, Ordering_0_v_0_ForC, NameC>;

// Endpoints in depth 0.1
// Endpoint for role A
type Endpoint_0_1_ForA = MeshedChannels<
    Message_0_1_v_0_FromAToB,
    Message_0_1_v_0_FromAToC,
    Ordering_0_1_v_0_ForA,
    NameA,
>;

// Endpoint for role B
type Endpoint_0_1_ForB = MeshedChannels<
    Message_0_1_v_0_FromBToA,
    Message_0_1_v_0_FromBToC,
    Ordering_0_1_v_0_ForB,
    NameB,
>;

// Endpoint for role C
type Endpoint_0_1_ForC = MeshedChannels<
    Message_0_1_v_0_FromCToA,
    Message_0_1_v_0_FromCToB,
    Ordering_0_1_v_0_ForC,
    NameC,
>;

// Endpoints in depth 0.0
// Endpoint for role A
type Endpoint_0_0_ForA = MeshedChannels<
    Message_0_0_v_0_FromAToB,
    Message_0_0_v_0_FromAToC,
    Ordering_0_0_v_0_ForA,
    NameA,
>;
type Endpoint_0_0_ForA_Loop = MeshedChannels<
    SendTimed<
        Test0,
        'a',
        0,
        true,
        1,
        true,
        ' ',
        SendTimed<Choice_0_FromAToB, ' ', -2, false, -1, false, ' ', End>,
    >,
    SendTimed<Choice_0_FromAToC, ' ', -2, false, -1, false, ' ', End>,
    RoleB<RoleBroadcast>,
    NameA,
>;

// Endpoint for role B
type Endpoint_0_0_ForB_Loop = MeshedChannels<
    RecvTimed<
        Test0,
        'a',
        0,
        true,
        1,
        true,
        ' ',
        RecvTimed<Choice_0_FromAToB, ' ', -2, false, -1, false, ' ', End>,
    >,
    Message_0_0_v_0_FromBToC,
    RoleA<RoleA<RoleEnd>>,
    NameB,
>;

type Endpoint_0_0_ForB = MeshedChannels<
    Message_0_0_v_0_FromBToA,
    Message_0_0_v_0_FromBToC,
    Ordering_0_0_v_0_ForB,
    NameB,
>;

// Endpoint for role C
type Endpoint_0_0_ForC = MeshedChannels<
    Message_0_0_v_0_FromCToA,
    Message_0_0_v_0_FromCToB,
    Ordering_0_0_v_0_ForC,
    NameC,
>;

type Endpoint_0_0_ForC_Loop = MeshedChannels<
    Message_0_0_v_0_FromCToA,
    Message_0_0_v_0_FromCToB,
    Ordering_0_0_v_0_ForC,
    NameC,
>;

// Fill in the functions here.
fn endpoint_a_0_v_0(
    s: Endpoint_0_ForA,
    all_clocks: &mut HashMap<char, Instant>,
) -> Result<(), Box<dyn Error>> {
    all_clocks.insert('a', Instant::now());

    let s = s.send(TestInit, all_clocks)?;
    let s = s.send(Test0, all_clocks)?;

    let s: Endpoint_0_0_ForA = choose_mpst_a_to_all!(
        s,
        all_clocks,
        Choice_0_FromAToB::Branching0,
        Choice_0_FromAToC::Branching0
    );
    let s = s.send(Test1, all_clocks)?;
    let s = s.send(Test2 { payload: Payload }, all_clocks)?;
    let s = s.send(Test3, all_clocks)?;
    let s = s.send(Test4 { payload: Payload }, all_clocks)?;
    rec_endpoint_a_0_v_0(s, all_clocks, 5)
}

fn rec_endpoint_a_0_v_0(
    s: Endpoint_0_0_ForA_Loop,
    all_clocks: &mut HashMap<char, Instant>,
    loops: i32,
) -> Result<(), Box<dyn Error>> {
    all_clocks.insert('a', Instant::now());
    let s = s.send(Test0, all_clocks)?;

    if loops > 0 {
        let s: Endpoint_0_0_ForA = choose_mpst_a_to_all!(
            s,
            all_clocks,
            Choice_0_FromAToB::Branching0,
            Choice_0_FromAToC::Branching0
        );
        let s = s.send(Test1, all_clocks)?;
        let s = s.send(Test2 { payload: Payload }, all_clocks)?;
        let s = s.send(Test3, all_clocks)?;
        let s = s.send(Test4 { payload: Payload }, all_clocks)?;
        rec_endpoint_a_0_v_0(s, all_clocks, loops - 1)
    } else {
        let s: Endpoint_0_1_ForA = choose_mpst_a_to_all!(
            s,
            all_clocks,
            Choice_0_FromAToB::Branching1,
            Choice_0_FromAToC::Branching1
        );
        let s = s.send(Test1, all_clocks)?;
        let s = s.send(Test2 { payload: Payload }, all_clocks)?;
        let s = s.send(Test3, all_clocks)?;
        let s = s.send(Test4 { payload: Payload }, all_clocks)?;
        s.close()
    }
}

fn endpoint_b_0_v_0(
    s: Endpoint_0_ForB,
    all_clocks: &mut HashMap<char, Instant>,
) -> Result<(), Box<dyn Error>> {
    all_clocks.insert('a', Instant::now());

    let (_, s) = s.recv(all_clocks)?;
    let (_, s) = s.recv(all_clocks)?;

    offer_mpst!(s, all_clocks, {
        Choice_0_FromAToB::Branching0(s) => {
            let (_, s) = s.recv(all_clocks)?;
            let (_, s) = s.recv(all_clocks)?;
            let (_, s) = s.recv(all_clocks)?;
            let (_, s) = s.recv(all_clocks)?;

            aux_endpoint_b_0_v_0(s, all_clocks)
        },
        Choice_0_FromAToB::Branching1(s) => {
            let (_, s) = s.recv(all_clocks)?;
            let (_, s) = s.recv(all_clocks)?;
            let (_, s) = s.recv(all_clocks)?;
            let (_, s) = s.recv(all_clocks)?;

            s.close()
        },
    })
}

fn aux_endpoint_b_0_v_0(
    s: Endpoint_0_0_ForB_Loop,
    all_clocks: &mut HashMap<char, Instant>,
) -> Result<(), Box<dyn Error>> {
    all_clocks.insert('a', Instant::now());

    let (_, s) = s.recv(all_clocks)?;

    offer_mpst!(s, all_clocks, {
        Choice_0_FromAToB::Branching0(s) => {
            let (_, s) = s.recv(all_clocks)?;
            let (_, s) = s.recv(all_clocks)?;
            let (_, s) = s.recv(all_clocks)?;
            let (_, s) = s.recv(all_clocks)?;

            aux_endpoint_b_0_v_0(s, all_clocks)
        },
        Choice_0_FromAToB::Branching1(s) => {
            let (_, s) = s.recv(all_clocks)?;
            let (_, s) = s.recv(all_clocks)?;
            let (_, s) = s.recv(all_clocks)?;
            let (_, s) = s.recv(all_clocks)?;

            s.close()
        },
    })
}

fn endpoint_c_0_v_0(
    s: Endpoint_0_ForC,
    all_clocks: &mut HashMap<char, Instant>,
) -> Result<(), Box<dyn Error>> {
    offer_mpst!(s, all_clocks, {
        Choice_0_FromAToC::Branching0(s) => {

            aux_endpoint_c_0_v_0(s, all_clocks)
        },
        Choice_0_FromAToC::Branching1(s) => {

            s.close()
        },
    })
}

fn aux_endpoint_c_0_v_0(
    s: Endpoint_0_0_ForC_Loop,
    all_clocks: &mut HashMap<char, Instant>,
) -> Result<(), Box<dyn Error>> {
    offer_mpst!(s, all_clocks, {
        Choice_0_FromAToC::Branching0(s) => {

            aux_endpoint_c_0_v_0(s, all_clocks)
        },
        Choice_0_FromAToC::Branching1(s) => {

            s.close()
        },
    })
}

fn endpoint_a_0_1_v_0(
    s: Endpoint_0_1_ForA,
    all_clocks: &mut HashMap<char, Instant>,
) -> Result<(), Box<dyn Error>> {
    all_clocks.insert('a', Instant::now());

    Ok(())
}

fn endpoint_a_0_1_v_1(
    s: Endpoint_0_1_ForA,
    all_clocks: &mut HashMap<char, Instant>,
) -> Result<(), Box<dyn Error>> {
    all_clocks.insert('a', Instant::now());

    Ok(())
}

fn endpoint_b_0_1_v_0(
    s: Endpoint_0_1_ForB,
    all_clocks: &mut HashMap<char, Instant>,
) -> Result<(), Box<dyn Error>> {
    all_clocks.insert('a', Instant::now());

    Ok(())
}

fn endpoint_b_0_1_v_1(
    s: Endpoint_0_1_ForB,
    all_clocks: &mut HashMap<char, Instant>,
) -> Result<(), Box<dyn Error>> {
    all_clocks.insert('a', Instant::now());

    Ok(())
}

fn endpoint_c_0_1_v_0(
    s: Endpoint_0_1_ForC,
    all_clocks: &mut HashMap<char, Instant>,
) -> Result<(), Box<dyn Error>> {
    Ok(())
}

fn endpoint_c_0_1_v_1(
    s: Endpoint_0_1_ForC,
    all_clocks: &mut HashMap<char, Instant>,
) -> Result<(), Box<dyn Error>> {
    Ok(())
}

fn endpoint_a_0_0_v_0(
    s: Endpoint_0_0_ForA,
    all_clocks: &mut HashMap<char, Instant>,
) -> Result<(), Box<dyn Error>> {
    all_clocks.insert('a', Instant::now());

    Ok(())
}

fn endpoint_b_0_0_v_0(
    s: Endpoint_0_0_ForB,
    all_clocks: &mut HashMap<char, Instant>,
) -> Result<(), Box<dyn Error>> {
    all_clocks.insert('a', Instant::now());

    Ok(())
}

fn endpoint_c_0_0_v_0(
    s: Endpoint_0_0_ForC,
    all_clocks: &mut HashMap<char, Instant>,
) -> Result<(), Box<dyn Error>> {
    Ok(())
}

pub fn main() {
    let (thread_a, thread_b, thread_c) =
        fork_mpst(endpoint_a_0_v_0, endpoint_b_0_v_0, endpoint_c_0_v_0);

    println!("Thread A: {:?}", thread_a.join());
    println!("Thread B: {:?}", thread_b.join());
    println!("Thread C: {:?}", thread_c.join());
}