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
//! [Maelstrom](https://github.com/jepsen-io/maelstrom) is a workbench for testing toy implementations of distributed systems.
//!
//! This crate abstracts away the boilerplate of setting up the stdin/stdout for a node
//! in a distributed system, and provides a few useful utilities for writing handlers.
//!
//! This crate is inspired from and primarily written for the [Fly.io Distributed Systems challenges](https://fly.io/dist-sys/).
//!
//! # Usage
//!
//! To use this crate, you'll create a node that is capable of handling
//! some rpcs. Define the rpc messages with a serializable `Message` enum
//! and define any meaningful error type that can stop the maelstrom test early
//! in case of something going terribly wrong with the node.
//!
//! The node must implement the [HandleMessage] trait, which requires
//! a `handle_message` function that takes an [Envelope] and a [Sender] for optionally
//! sending any messages.
//!
//! ## Example
//!
//! Let's create a simple echo node that responds to `init` and `echo` messages.
//! This also corresponds to the [Echo challenge](https://fly.io/dist-sys/1/) in the [Fly.io Distributed Systems challenge set](https://fly.io/dist-sys/).
//!
//! ```no_run
//! use maelstrom_common::{run, HandleMessage, Envelope};
//! use serde::{Deserialize, Serialize};
//! use core::panic;
//!
//! #[derive(Debug, Serialize, Deserialize)]
//! #[serde(tag = "type")]
//! pub enum Message {
//! #[serde(rename = "init")]
//! Init {
//! #[serde(skip_serializing_if = "Option::is_none")]
//! msg_id: Option<usize>,
//! node_id: String
//! },
//! #[serde(rename = "echo")]
//! Echo {
//! echo: String,
//! #[serde(skip_serializing_if = "Option::is_none")]
//! msg_id: Option<usize>
//! },
//! #[serde(rename = "init_ok")]
//! InitOk {
//! #[serde(skip_serializing_if = "Option::is_none")]
//! in_reply_to: Option<usize>
//! },
//! #[serde(rename = "echo_ok")]
//! EchoOk {
//! echo: String,
//! #[serde(skip_serializing_if = "Option::is_none")]
//! in_reply_to: Option<usize>
//! },
//! }
//!
//! #[derive(Debug, Default)]
//! pub struct Echo {
//! // Store our ID when a client initializes us.
//! node_id: Option<String>,
//! }
//!
//! impl HandleMessage for Echo {
//! type Message = Message;
//! type Error = std::io::Error;
//!
//! fn handle_message(
//! &mut self,
//! msg: Envelope<Self::Message>,
//! outbound_msg_tx: std::sync::mpsc::Sender<Envelope<Self::Message>>,
//! ) -> Result<(), Self::Error> {
//! match msg.body {
//! Message::Init { msg_id, ref node_id } => {
//! self.node_id = Some(node_id.clone());
//! outbound_msg_tx.send(
//! msg.reply(Message::InitOk { in_reply_to: msg_id })
//! ).unwrap();
//! Ok(())
//! },
//! Message::Echo { ref echo, msg_id } => {
//! outbound_msg_tx.send(
//! msg.reply(
//! Message::EchoOk { echo: echo.to_owned(), in_reply_to: msg_id }
//! )
//! ).unwrap();
//! Ok(())
//! },
//! _ => panic!("{}", format!("Unexpected message: {:#?}", serde_json::to_string_pretty(&msg)))
//! }
//! }
//! }
//!
//! # pub fn main() -> Result<(), Box<dyn std::error::Error>> {
//! run(Echo::default())?;
//! # Ok(())
//! # }
//! ```
use ;
use io;
use Write;
use channel;
use Sender;
use spawn;
/// A formal structure for any message sent between nodes
/// or clients in a maelstrom orchestrated distributed system.
/// A single node should be able to handle messages
/// of a given type, and return an error if something goes wrong.
/// A thin wrapper around a node that handles
/// rpcs and dumps outbound messages to stdout
/// and reads inbound messages from stdin.
/// Run the Maelstrom runtime implicitly
/// for the given node that can handle rpcs.