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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

use super::connection;
use crate::{
    operation as op,
    units::{Byte, Rate},
};
use core::marker::PhantomData;

macro_rules! send_stream {
    () => {
        pub fn send(&mut self, bytes: Byte) -> &mut Self {
            self.ops.push(crate::operation::Connection::Send {
                stream_id: self.id,
                bytes,
            });
            self
        }

        pub fn set_send_rate(&mut self, rate: Rate) -> &mut Self {
            self.ops.push(crate::operation::Connection::SendRate {
                stream_id: self.id,
                rate,
            });
            self
        }
    };
}

macro_rules! receive_stream {
    () => {
        pub fn receive(&mut self, bytes: Byte) -> &mut Self {
            self.ops.push(crate::operation::Connection::Receive {
                stream_id: self.id,
                bytes,
            });
            self
        }

        pub fn set_receive_rate(&mut self, rate: Rate) -> &mut Self {
            self.ops.push(crate::operation::Connection::ReceiveRate {
                stream_id: self.id,
                rate,
            });
            self
        }

        pub fn receive_all(&mut self) -> &mut Self {
            self.ops
                .push(crate::operation::Connection::ReceiveAll { stream_id: self.id });
            self
        }
    };
}

pub struct Stream<Endpoint, Location> {
    id: u64,
    ops: Vec<op::Connection>,
    state: connection::State,
    endpoint: PhantomData<Endpoint>,
    location: PhantomData<Location>,
}

impl<Endpoint, Location> Stream<Endpoint, Location> {
    send_stream!();
    receive_stream!();
    sync!(Endpoint, Location);
    sleep!();
    trace!();
    iterate!();

    pub(crate) fn new(id: u64, state: connection::State) -> Self {
        Self {
            id,
            ops: vec![],
            state,
            endpoint: PhantomData,
            location: PhantomData,
        }
    }

    fn child_scope(&self) -> Self {
        Self::new(self.id, self.state.clone())
    }

    fn finish_scope(self) -> Vec<op::Connection> {
        self.ops
    }

    pub fn concurrently<
        S: FnOnce(&mut SendStream<Endpoint, Location>),
        R: FnOnce(&mut ReceiveStream<Endpoint, Location>),
    >(
        &mut self,
        send: S,
        receive: R,
    ) -> &mut Self {
        let mut send_stream = SendStream::new(self.id, self.state.clone());
        let mut receive_stream = ReceiveStream::new(self.id, self.state.clone());
        send(&mut send_stream);
        receive(&mut receive_stream);
        let threads = vec![send_stream.ops, receive_stream.ops];
        self.ops.push(op::Connection::Scope { threads });
        self
    }

    pub(crate) fn finish(mut self) -> Vec<op::Connection> {
        let stream_id = self.id;
        self.ops.push(op::Connection::SendFinish { stream_id });
        self.ops.push(op::Connection::ReceiveFinish { stream_id });
        self.ops
    }
}

pub struct SendStream<Endpoint, Location> {
    id: u64,
    ops: Vec<op::Connection>,
    state: connection::State,
    endpoint: PhantomData<Endpoint>,
    location: PhantomData<Location>,
}

impl<Endpoint, Location> SendStream<Endpoint, Location> {
    send_stream!();
    sync!(Endpoint, Location);
    sleep!();
    trace!();
    iterate!();

    pub(crate) fn new(id: u64, state: connection::State) -> Self {
        Self {
            id,
            ops: vec![],
            state,
            endpoint: PhantomData,
            location: PhantomData,
        }
    }

    fn child_scope(&self) -> Self {
        Self::new(self.id, self.state.clone())
    }

    fn finish_scope(self) -> Vec<op::Connection> {
        self.ops
    }

    pub(crate) fn finish(mut self) -> Vec<op::Connection> {
        let stream_id = self.id;
        self.ops.push(op::Connection::SendFinish { stream_id });
        self.ops
    }
}

pub struct ReceiveStream<Endpoint, Location> {
    id: u64,
    ops: Vec<op::Connection>,
    state: connection::State,
    endpoint: PhantomData<Endpoint>,
    location: PhantomData<Location>,
}

impl<Endpoint, Location> ReceiveStream<Endpoint, Location> {
    receive_stream!();
    sync!(Endpoint, Location);
    sleep!();
    trace!();
    iterate!();

    pub(crate) fn new(id: u64, state: connection::State) -> Self {
        Self {
            id,
            ops: vec![],
            state,
            endpoint: PhantomData,
            location: PhantomData,
        }
    }

    fn child_scope(&self) -> Self {
        Self::new(self.id, self.state.clone())
    }

    fn finish_scope(self) -> Vec<op::Connection> {
        self.ops
    }

    pub(crate) fn finish(mut self) -> Vec<op::Connection> {
        let stream_id = self.id;
        self.ops.push(op::Connection::ReceiveFinish { stream_id });
        self.ops
    }
}