-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtime.rs
246 lines (200 loc) · 7.51 KB
/
runtime.rs
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
//! Defines the runtime for a Maelstrom node
use std::{
io::{stdin, stdout, BufRead, Write},
sync::mpsc::{channel, Receiver, Sender},
thread::{self, JoinHandle},
};
const EOI: &str = "EOI";
use anyhow::bail;
use crate::{
network::Network,
node::Node,
types::{Init, Message, Payload, Try},
};
pub struct Runtime<P, N>(std::marker::PhantomData<P>, std::marker::PhantomData<N>);
impl<P, N> Runtime<P, N>
where
P: Payload,
N: Node<P>,
{
/// Run a node using stdin/stdout.
/// This is the standard entrypoint for use with Maelstrom.
pub fn run() -> Try {
let (stdin_tx, stdin_rx) = channel();
let (stdout_tx, stdout_rx) = channel();
// stdin thread: decouples stdin reads from node message processing
thread::spawn(move || {
let stdin = stdin().lock().lines();
for line in stdin {
let line = line.unwrap();
stdin_tx.send(line).unwrap();
}
});
// stdout thread: decouples stdout writes from node message processing
thread::spawn(move || {
let mut stdout = stdout().lock();
for message in stdout_rx {
writeln!(&mut stdout, "{message}").unwrap();
}
});
// we give the node a Sender so it can pass outbound messages to stdout
// and a receiver so it can pull inbound messages from stdin
eprintln!("Starting runtime...\nWaiting for init message");
Runtime::<P, N>::run_internal(stdout_tx, stdin_rx)?;
Ok(())
}
fn run_internal(tx: Sender<String>, rx: Receiver<String>) -> Try {
let init = &rx.recv()?;
eprintln!("Got init: {init}");
let init: Message<Init> = serde_json::from_str(init)?;
let Init::Init { node_id, node_ids } = &init.body.payload else {
bail!("expected init as first message");
};
// the network is how the node communicates with the runtime
let (network, node_receiver) = Network::new();
let node = N::from_init(network.clone(), node_id.clone(), node_ids.clone());
// we are using a msg_id here that might be used by the node,
// which is against protocol, but maelstrom doesn't seem to mind
let reply = init.into_reply(Init::InitOk);
eprintln!("Starting outbound processing and sending init_ok");
Runtime::<P, N>::process_output(reply, tx, node_receiver);
eprintln!("Starting inbound processing");
if let Err(e) = Runtime::process_input(rx, network, node) {
eprintln!("failed to process input: {e:#?}");
}
eprintln!("Shutting down...");
Ok(())
}
fn process_output(
reply: Message<Init>,
tx: Sender<String>,
node_receiver: Receiver<Message<P>>,
) -> JoinHandle<Try> {
// output thread: decouples node sending outbound messages from
// node receiving inbound messages. This way, a node may be sending messages
// even if it isn't receiving any.
thread::spawn::<_, Try>(move || {
// send the init_ok
let mut json = serde_json::to_string(&reply)?;
eprintln!("Writing init_ok: {json}");
tx.send(json)?;
// reply to other messages
loop {
let outbound = node_receiver.recv()?;
json = serde_json::to_string(&outbound)?;
eprintln!("Writing outbound message: {json}");
tx.send(json)?;
}
})
}
fn process_input(rx: Receiver<String>, network: Network<P>, mut node: N) -> Try {
let (json_tx, json_rx) = channel();
// callback thread: allows us to process input and check for pending
// rpc callbacks even if the node is still handling a message.
thread::spawn(move || {
for line in rx {
if line == EOI {
eprintln!("Got EOI");
break;
}
eprintln!("Got message: {line}");
let message: Message<P> = serde_json::from_str(&line).unwrap();
// we try checking for pending callbacks for the message, if not,
// check_callback returns ownership of the message so that we may deliver
// it to the node as a regular message rather than an RPC response
if let Some(message) = network.check_callback(message) {
json_tx.send(message).unwrap();
}
}
});
for message in json_rx {
node.handle_message(message)?;
}
eprintln!("done processing input");
Ok(())
}
}
#[cfg(test)]
mod tests {
use std::{sync::mpsc::Sender, thread::JoinHandle};
use crate::{payload, types::BodyBuilder};
use super::*;
payload!(
enum EchoPayload {
Echo { echo: String },
EchoOk { echo: String },
}
);
struct EchoNode {
network: Network<EchoPayload>,
seq: usize,
}
impl Node<EchoPayload> for EchoNode {
fn from_init(network: Network<EchoPayload>, _: String, _: Vec<String>) -> Self {
EchoNode { network, seq: 0 }
}
fn handle_message(&mut self, msg: Message<EchoPayload>) -> Try {
let EchoPayload::Echo { echo } = &msg.body.payload else {
bail!("expected echo");
};
let echo = echo.clone();
let reply = msg.into_reply(EchoPayload::EchoOk { echo });
self.seq += 1;
self.network.send(reply)?;
Ok(())
}
}
#[test]
fn test_basic_init() -> Try {
let (_, input, output) = run_node();
let init = Message::new(
"c2",
"n1",
BodyBuilder::new(Init::Init {
node_id: "n1".into(),
node_ids: vec!["n1".into()],
})
.msg_id(3)
.build(),
);
input.send(serde_json::to_string(&init)?)?;
let _: Message<Init> = serde_json::from_str(&output.recv()?)?;
Ok(())
}
#[test]
fn test_basic_echo() -> Try {
let (_, input, output) = run_node();
let init = Message::new(
"c2",
"n1",
BodyBuilder::new(Init::Init {
node_id: "n1".into(),
node_ids: vec!["n1".into()],
})
.msg_id(3)
.build(),
);
input.send(serde_json::to_string(&init)?)?;
let _: Message<Init> = serde_json::from_str(&output.recv()?)?;
let echo = Message::new(
"c2",
"n1",
BodyBuilder::new(EchoPayload::Echo {
echo: "ding-dong!".into(),
})
.msg_id(3)
.build(),
);
input.send(serde_json::to_string(&echo)?)?;
let _: Message<EchoPayload> = serde_json::from_str(&output.recv()?)?;
Ok(())
}
fn run_node() -> (JoinHandle<()>, Sender<String>, Receiver<String>) {
let (stdout_tx, stdout_rx) = channel();
let (stdin_tx, stdin_rx) = channel();
let runtime = thread::spawn(move || {
Runtime::<EchoPayload, EchoNode>::run_internal(stdout_tx, stdin_rx).unwrap();
});
(runtime, stdin_tx, stdout_rx)
}
}