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
#![allow(unused_variables)]
use colink::{
    extensions::instant_server::{InstantRegistry, InstantServer},
    CoLink, Participant, ProtocolEntry,
};

struct Initiator;
#[colink::async_trait]
impl ProtocolEntry for Initiator {
    async fn start(
        &self,
        cl: CoLink,
        param: Vec<u8>,
        participants: Vec<Participant>,
    ) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
        println!("initiator");
        Ok(())
    }
}

struct Receiver;
#[colink::async_trait]
impl ProtocolEntry for Receiver {
    async fn start(
        &self,
        cl: CoLink,
        param: Vec<u8>,
        participants: Vec<Participant>,
    ) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
        println!("{}", String::from_utf8_lossy(&param));
        cl.create_entry(&format!("tasks:{}:output", cl.get_task_id()?), &param)
            .await?;
        Ok(())
    }
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
    let ir = InstantRegistry::new();
    // The instant server will automatically use the instant registry when there is one.
    let is0 = InstantServer::new();
    let is1 = InstantServer::new();
    let cl0 = is0.get_colink().switch_to_generated_user().await?;
    let cl1 = is1.get_colink().switch_to_generated_user().await?;
    colink::protocol_attach!(
        cl0,
        ("greetings:initiator", Initiator),
        ("greetings:receiver", Receiver)
    );
    colink::protocol_attach!(
        cl1,
        ("greetings:initiator", Initiator),
        ("greetings:receiver", Receiver)
    );
    let participants = vec![
        Participant {
            user_id: cl0.get_user_id()?,
            role: "initiator".to_string(),
        },
        Participant {
            user_id: cl1.get_user_id()?,
            role: "receiver".to_string(),
        },
    ];
    let task_id = cl0
        .run_task("greetings", "test".as_bytes(), &participants, true)
        .await?;
    let res = cl1
        .read_or_wait(&format!("tasks:{}:output", task_id))
        .await?;
    println!("{}", String::from_utf8_lossy(&res));
    Ok(())
}