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

use std::sync::Arc;

macro_rules! sleep {
    () => {
        pub fn sleep(&mut self, amount: core::time::Duration) -> &mut Self {
            self.ops
                .push(crate::operation::Connection::Sleep { amount });
            self
        }
    };
}

macro_rules! trace {
    () => {
        pub fn trace(&mut self, name: &str) -> &mut Self {
            let trace_id = self.state.trace(name);
            self.ops
                .push(crate::operation::Connection::Trace { trace_id });
            self
        }

        pub fn profile<F: FnOnce(&mut Self)>(&mut self, name: &str, f: F) -> &mut Self {
            let trace_id = self.state.trace(name);
            let mut builder = self.child_scope();
            f(&mut builder);
            let operations = builder.finish_scope();

            self.ops.push(crate::operation::Connection::Profile {
                trace_id,
                operations,
            });

            self
        }
    };
}

macro_rules! iterate {
    () => {
        pub fn iterate<I: Into<crate::operation::IterateValue>, F: FnOnce(&mut Self)>(
            &mut self,
            count: I,
            f: F,
        ) -> &mut Self {
            let mut builder = self.child_scope();
            f(&mut builder);
            let mut operations = builder.finish_scope();

            let count = count.into();

            if operations.is_empty() || count.is_zero() {
                return self;
            }

            let mut trace_id = None;

            // optimize out nested iterate/profile statements
            if operations.len() == 1 {
                if let Some(crate::operation::Connection::Profile {
                    trace_id: child_id,
                    operations: child,
                }) = operations.get_mut(0)
                {
                    trace_id = Some(*child_id);
                    operations = core::mem::take(child);
                }
            }

            self.ops.push(crate::operation::Connection::Iterate {
                value: count,
                operations,
                trace_id,
            });

            self
        }
    };
}

#[macro_use]
pub mod checkpoint;

pub mod certificate;
pub mod client;
pub mod connection;
pub mod scope;
pub mod server;
mod state;
pub mod stream;

pub use client::Client;
pub use connection::Connection;
pub use scope::Scope;
pub use server::Server;
pub use stream::Stream;

use state::State;

#[derive(Debug)]
pub struct Builder {
    state: State,
}

impl Builder {
    pub(super) fn new() -> Self {
        Self {
            state: Default::default(),
        }
    }

    pub fn create_ca(&mut self) -> certificate::Authority {
        self.create_ca_with(|_| {})
    }

    pub fn create_ca_with<F: FnOnce(&mut certificate::AuthorityBuilder)>(
        &mut self,
        f: F,
    ) -> certificate::Authority {
        self.state.create_ca_with(f)
    }

    pub fn create_server(&mut self) -> Server {
        self.create_server_with(|_| {})
    }

    pub fn create_server_with<F: FnOnce(&mut server::Builder)>(&mut self, f: F) -> Server {
        Server::new(self.state.clone(), f)
    }

    pub fn create_client<F: FnOnce(&mut client::Builder)>(&mut self, f: F) {
        let id = self.state.clients.push(super::Client {
            name: String::new(),
            scenario: vec![],
            connections: vec![],
            configuration: Default::default(),
            certificate_authorities: vec![],
        }) as u64;

        let mut builder = client::Builder::new(id, self.state.clone());
        f(&mut builder);

        let client = &mut self.state.clients.borrow_mut()[id as usize];

        client.scenario = builder.finish();
    }

    pub(super) fn finish(self) -> super::Scenario {
        let clients = self
            .state
            .clients
            .take()
            .into_iter()
            .map(|mut client| {
                client.certificate_authorities.sort_unstable();

                Arc::new(client)
            })
            .collect();
        let servers = self
            .state
            .servers
            .take()
            .into_iter()
            .map(Arc::new)
            .collect();
        let mut traces = self.state.trace.take().into_iter().collect::<Vec<_>>();
        traces.sort_by(|(_, a), (_, b)| a.cmp(b));
        let traces = Arc::new(traces.into_iter().map(|(value, _)| value).collect());
        let certificates = self.state.certificates.take();

        let mut scenario = super::Scenario {
            id: Default::default(),
            clients,
            servers,
            // TODO implement router builder
            routers: vec![],
            traces,
            certificates: vec![],
        };

        let mut hash = crate::scenario::Id::hasher();
        core::hash::Hash::hash(&scenario, &mut hash);
        core::hash::Hash::hash(&certificates, &mut hash);
        scenario.id = hash.finish();

        scenario.certificates = certificate::Certificate::build_all(certificates, &scenario.id);

        scenario
    }
}

pub trait Endpoint {
    type Peer: Endpoint;
}

impl Endpoint for Client {
    type Peer = Server;
}

impl Endpoint for Server {
    type Peer = Client;
}

#[derive(Debug)]
pub struct Local;

#[derive(Debug)]
pub struct Remote;

#[cfg(test)]
mod tests;