Compare commits
2 commits
e6d76ba37c
...
2793508a93
Author | SHA1 | Date | |
---|---|---|---|
|
2793508a93 | ||
|
6f45521660 |
5 changed files with 184 additions and 184 deletions
|
@ -1,7 +1,6 @@
|
||||||
use std::{
|
use std::{
|
||||||
collections::{HashMap, HashSet},
|
collections::{HashMap, HashSet},
|
||||||
io::BufRead,
|
sync::{Arc, Mutex},
|
||||||
sync::{mpsc::channel, Arc, Mutex},
|
|
||||||
thread,
|
thread,
|
||||||
time::Duration,
|
time::Duration,
|
||||||
};
|
};
|
||||||
|
@ -10,28 +9,18 @@ use nebkor_maelstrom::{protocol::Payload, Body, Message, Node, Runner};
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let out = std::io::stdout();
|
|
||||||
let std_in = Arc::new(std::io::stdin());
|
|
||||||
|
|
||||||
let (tx, rx) = channel();
|
|
||||||
let input = tx.clone();
|
|
||||||
|
|
||||||
let i = thread::spawn(move || {
|
|
||||||
let g = std_in.lock();
|
|
||||||
for line in g.lines().map_while(Result::ok) {
|
|
||||||
if let Ok(msg) = serde_json::from_str::<Message>(&line) {
|
|
||||||
input.send(msg).unwrap();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
let node = BCaster::default();
|
let node = BCaster::default();
|
||||||
let node = Arc::new(Mutex::new(node));
|
let node = Arc::new(Mutex::new(node));
|
||||||
|
|
||||||
let runner = Runner::new(node, out);
|
let runner = Runner::new(node);
|
||||||
let runner = &runner;
|
let runner = &runner;
|
||||||
|
|
||||||
let g = thread::spawn(move || loop {
|
runner.run(Some(Box::new(on_init)));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn on_init(runner: &Runner) {
|
||||||
|
let tx = runner.get_backdoor();
|
||||||
|
thread::spawn(move || loop {
|
||||||
let millis = rand::thread_rng().gen_range(400..=800);
|
let millis = rand::thread_rng().gen_range(400..=800);
|
||||||
thread::sleep(Duration::from_millis(millis));
|
thread::sleep(Duration::from_millis(millis));
|
||||||
let body = Body::from_type("do_gossip");
|
let body = Body::from_type("do_gossip");
|
||||||
|
@ -41,10 +30,6 @@ fn main() {
|
||||||
};
|
};
|
||||||
tx.send(msg).unwrap();
|
tx.send(msg).unwrap();
|
||||||
});
|
});
|
||||||
|
|
||||||
runner.run(rx, None);
|
|
||||||
let _ = i.join();
|
|
||||||
let _ = g.join();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Default)]
|
#[derive(Clone, Default)]
|
||||||
|
|
|
@ -1,8 +1,4 @@
|
||||||
use std::{
|
use std::sync::{Arc, Mutex};
|
||||||
io::BufRead,
|
|
||||||
sync::{mpsc::channel, Arc, Mutex},
|
|
||||||
thread,
|
|
||||||
};
|
|
||||||
|
|
||||||
use nebkor_maelstrom::{Body, Message, Node, Runner};
|
use nebkor_maelstrom::{Body, Message, Node, Runner};
|
||||||
|
|
||||||
|
@ -19,26 +15,11 @@ impl Node for Echo {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let out = std::io::stdout();
|
|
||||||
let std_in = Arc::new(std::io::stdin());
|
|
||||||
|
|
||||||
let (tx, rx) = channel();
|
|
||||||
|
|
||||||
let i = thread::spawn(move || {
|
|
||||||
let g = std_in.lock();
|
|
||||||
for line in g.lines().map_while(Result::ok) {
|
|
||||||
if let Ok(msg) = serde_json::from_str::<Message>(&line) {
|
|
||||||
tx.send(msg).unwrap();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
let node = Echo;
|
let node = Echo;
|
||||||
|
|
||||||
let node = Arc::new(Mutex::new(node));
|
let node = Arc::new(Mutex::new(node));
|
||||||
|
|
||||||
let runner = Runner::new(node, out);
|
let runner = Runner::new(node);
|
||||||
|
|
||||||
runner.run(rx, None);
|
runner.run(None);
|
||||||
i.join().unwrap();
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,17 +1,17 @@
|
||||||
use std::{
|
use std::sync::{Arc, Mutex};
|
||||||
rc::Rc,
|
|
||||||
sync::{Arc, Mutex},
|
|
||||||
};
|
|
||||||
|
|
||||||
use nebkor_maelstrom::{mk_payload, mk_stdin, Body, Message, Node, Runner};
|
use nebkor_maelstrom::{mk_payload, Body, Message, Node, Runner};
|
||||||
|
|
||||||
|
const KEY: &str = "COUNTER";
|
||||||
|
|
||||||
|
#[derive(Clone, Default)]
|
||||||
|
struct Counter;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let node = Counter;
|
let node = Counter;
|
||||||
let node = Arc::new(Mutex::new(node));
|
let node = Arc::new(Mutex::new(node));
|
||||||
|
|
||||||
let runner = Rc::new(Runner::new(node));
|
let runner = Runner::new(node);
|
||||||
|
|
||||||
let (i, _, rx) = mk_stdin();
|
|
||||||
|
|
||||||
let on_init = |rnr: &Runner| {
|
let on_init = |rnr: &Runner| {
|
||||||
let payload = mk_payload(&[
|
let payload = mk_payload(&[
|
||||||
|
@ -26,62 +26,56 @@ fn main() {
|
||||||
|
|
||||||
let on_init = Box::new(on_init);
|
let on_init = Box::new(on_init);
|
||||||
|
|
||||||
runner.run(rx, Some(on_init));
|
runner.run(Some(on_init));
|
||||||
i.join().unwrap();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const KEY: &str = "COUNTER";
|
|
||||||
|
|
||||||
#[derive(Clone, Default)]
|
|
||||||
struct Counter;
|
|
||||||
|
|
||||||
impl Node for Counter {
|
impl Node for Counter {
|
||||||
fn handle<'slf>(&'slf mut self, runner: &'slf Rc<Runner>, req: Message) {
|
fn handle<'slf>(&'slf mut self, runner: &'slf Runner, req: Message) {
|
||||||
|
let read_payload = mk_payload(&[("key", KEY.into())]);
|
||||||
|
let read_body = Body::from_type("read").with_payload(read_payload);
|
||||||
let typ = req.body.typ.as_str();
|
let typ = req.body.typ.as_str();
|
||||||
let frm = req.src.clone();
|
//let frm = req.src.clone();
|
||||||
let msg_id = req.body.msg_id.to_owned();
|
//let msg_id = req.body.msg_id.to_owned();
|
||||||
match typ {
|
match typ {
|
||||||
"add" => {
|
"add" => {
|
||||||
let read_runner = runner.clone();
|
|
||||||
let cas_runner = runner.clone();
|
|
||||||
let add_req = req.clone();
|
|
||||||
|
|
||||||
let cas_handler = move |_msg: Message| {
|
|
||||||
let req = add_req;
|
|
||||||
cas_runner.reply(&req, Body::from_type("add_ok"));
|
|
||||||
};
|
|
||||||
|
|
||||||
let delta = req.body.payload.get("delta").unwrap().as_i64().unwrap();
|
let delta = req.body.payload.get("delta").unwrap().as_i64().unwrap();
|
||||||
let read_handler = move |msg: Message| {
|
let cur = runner
|
||||||
let value = msg.body.payload.get("value").unwrap().as_i64().unwrap();
|
.rpc("seq-kv", read_body)
|
||||||
let payload = mk_payload(&[
|
.recv()
|
||||||
("from", value.into()),
|
.unwrap()
|
||||||
("to", (value + delta).into()),
|
.body
|
||||||
|
.payload
|
||||||
|
.get("value")
|
||||||
|
.cloned()
|
||||||
|
.unwrap()
|
||||||
|
.as_i64()
|
||||||
|
.unwrap();
|
||||||
|
let cas_payload = mk_payload(&[
|
||||||
("key", KEY.into()),
|
("key", KEY.into()),
|
||||||
|
("from", cur.into()),
|
||||||
|
("to", (cur + delta).into()),
|
||||||
]);
|
]);
|
||||||
let body = Body::from_type("cas").with_payload(payload);
|
// ERRORS BE HERE
|
||||||
read_runner.rpc("seq-kv", body, Box::new(cas_handler));
|
runner
|
||||||
};
|
.rpc("seq-kv", Body::from_type("cas").with_payload(cas_payload))
|
||||||
// kick it off by calling "read" on seq-kv:
|
.recv()
|
||||||
let payload = mk_payload(&[("key", KEY.into())]);
|
.unwrap();
|
||||||
let body = Body::from_type("read").with_payload(payload);
|
runner.reply(&req, Body::from_type("add_ok"));
|
||||||
runner.rpc("seq-kv", body, Box::new(read_handler));
|
|
||||||
}
|
}
|
||||||
"read" => {
|
"read" => {
|
||||||
let rn = runner.clone();
|
|
||||||
let h = move |msg: Message| {
|
|
||||||
let src = frm.clone();
|
|
||||||
let value = msg.body.payload.get("value").unwrap().as_i64().unwrap();
|
|
||||||
let irt = msg_id;
|
|
||||||
let payload = mk_payload(&[("value", value.into())]);
|
|
||||||
let body = Body::from_type("read_ok")
|
|
||||||
.with_in_reply_to(irt)
|
|
||||||
.with_payload(payload);
|
|
||||||
rn.send(&src, body);
|
|
||||||
};
|
|
||||||
let payload = mk_payload(&[("key", KEY.into())]);
|
let payload = mk_payload(&[("key", KEY.into())]);
|
||||||
let body = Body::from_type("read").with_payload(payload);
|
let body = Body::from_type("read").with_payload(payload);
|
||||||
runner.rpc("seq-kv", body, Box::new(h));
|
let val = runner
|
||||||
|
.rpc("seq-kv", body)
|
||||||
|
.recv()
|
||||||
|
.unwrap()
|
||||||
|
.body
|
||||||
|
.payload
|
||||||
|
.get("value")
|
||||||
|
.cloned()
|
||||||
|
.unwrap();
|
||||||
|
let body = Body::from_type("read_ok").with_payload(mk_payload(&[("value", val)]));
|
||||||
|
runner.reply(&req, body);
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
eprintln!("unknown type: {req:?}");
|
eprintln!("unknown type: {req:?}");
|
||||||
|
|
|
@ -1,33 +1,14 @@
|
||||||
use std::{
|
use std::sync::{Arc, Mutex};
|
||||||
io::BufRead,
|
|
||||||
sync::{mpsc::channel, Arc, Mutex},
|
|
||||||
thread,
|
|
||||||
};
|
|
||||||
|
|
||||||
use nebkor_maelstrom::{protocol::Payload, Body, Message, Node, Runner};
|
use nebkor_maelstrom::{protocol::Payload, Body, Message, Node, Runner};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let out = std::io::stdout();
|
|
||||||
let std_in = Arc::new(std::io::stdin());
|
|
||||||
|
|
||||||
let (tx, rx) = channel();
|
|
||||||
|
|
||||||
let i = thread::spawn(move || {
|
|
||||||
let g = std_in.lock();
|
|
||||||
for line in g.lines().map_while(Result::ok) {
|
|
||||||
if let Ok(msg) = serde_json::from_str::<Message>(&line) {
|
|
||||||
tx.send(msg).unwrap();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
let node = GenUid;
|
let node = GenUid;
|
||||||
let node = Arc::new(Mutex::new(node));
|
let node = Arc::new(Mutex::new(node));
|
||||||
|
|
||||||
let runner = Runner::new(node, out);
|
let runner = Runner::new(node);
|
||||||
|
|
||||||
runner.run(rx, None);
|
runner.run(None);
|
||||||
i.join().unwrap();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Default)]
|
#[derive(Clone, Default)]
|
||||||
|
|
|
@ -1,13 +1,12 @@
|
||||||
use std::{
|
use std::{
|
||||||
collections::HashMap,
|
collections::HashMap,
|
||||||
io::{BufRead, Stdout, Write},
|
io::{BufRead, Write},
|
||||||
rc::Rc,
|
|
||||||
sync::{
|
sync::{
|
||||||
atomic::{AtomicU64, AtomicUsize, Ordering},
|
atomic::{AtomicU64, AtomicUsize, Ordering},
|
||||||
mpsc::{channel, Receiver, Sender},
|
mpsc::{channel, Receiver, Sender},
|
||||||
Arc, LazyLock, Mutex, OnceLock,
|
Arc, Mutex, OnceLock,
|
||||||
},
|
},
|
||||||
thread::{self, JoinHandle},
|
thread::{self},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub mod protocol;
|
pub mod protocol;
|
||||||
|
@ -18,25 +17,57 @@ use serde_json::Value;
|
||||||
pub mod kv;
|
pub mod kv;
|
||||||
|
|
||||||
pub type DynNode = Arc<Mutex<dyn Node>>;
|
pub type DynNode = Arc<Mutex<dyn Node>>;
|
||||||
pub type Handler = Box<dyn FnOnce(Message)>; // -> Result<Option<Value>>>;
|
|
||||||
pub type OnInit = Box<dyn Fn(&Runner)>;
|
pub type OnInit = Box<dyn Fn(&Runner)>;
|
||||||
|
|
||||||
pub type Result<T> = std::result::Result<T, ErrorCode>;
|
pub type Result<T> = std::result::Result<T, ErrorCode>;
|
||||||
|
|
||||||
|
pub type RpcPromise = Receiver<Message>;
|
||||||
|
|
||||||
static MSG_ID: AtomicU64 = AtomicU64::new(0);
|
static MSG_ID: AtomicU64 = AtomicU64::new(0);
|
||||||
static OUTPUT: LazyLock<Stdout> = LazyLock::new(std::io::stdout);
|
|
||||||
|
|
||||||
pub trait Node {
|
pub trait Node {
|
||||||
fn handle(&mut self, runner: &Rc<Runner>, msg: Message);
|
fn handle(&mut self, runner: &Runner, msg: Message);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct Network {
|
||||||
|
promises: Arc<Mutex<HashMap<u64, Sender<Message>>>>,
|
||||||
|
node_output_tx: Sender<Message>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Network {
|
||||||
|
pub fn new() -> (Self, Receiver<Message>) {
|
||||||
|
let (node_output_tx, node_output_rx) = channel();
|
||||||
|
let net = Self {
|
||||||
|
node_output_tx,
|
||||||
|
promises: Arc::new(Mutex::new(HashMap::default())),
|
||||||
|
};
|
||||||
|
(net, node_output_rx)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn send(&self, msg: Message) {
|
||||||
|
self.node_output_tx.send(msg).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn rpc(&self, msg: Message) -> RpcPromise {
|
||||||
|
let (tx, rx) = channel();
|
||||||
|
{
|
||||||
|
let msg_id = msg.body.msg_id;
|
||||||
|
let mut g = self.promises.lock().unwrap();
|
||||||
|
g.insert(msg_id, tx);
|
||||||
|
}
|
||||||
|
self.send(msg);
|
||||||
|
rx
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct Runner {
|
pub struct Runner {
|
||||||
node: DynNode,
|
node: DynNode,
|
||||||
node_id: OnceLock<String>,
|
node_id: OnceLock<String>,
|
||||||
nodes: OnceLock<Vec<String>>,
|
nodes: OnceLock<Vec<String>>,
|
||||||
|
network: OnceLock<Network>,
|
||||||
|
backdoor: OnceLock<Sender<Message>>,
|
||||||
steps: Arc<AtomicUsize>,
|
steps: Arc<AtomicUsize>,
|
||||||
handlers: Arc<Mutex<HashMap<u64, Handler>>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Runner {
|
impl Runner {
|
||||||
|
@ -45,44 +76,87 @@ impl Runner {
|
||||||
node,
|
node,
|
||||||
nodes: OnceLock::new(),
|
nodes: OnceLock::new(),
|
||||||
node_id: OnceLock::new(),
|
node_id: OnceLock::new(),
|
||||||
|
network: OnceLock::new(),
|
||||||
|
backdoor: OnceLock::new(),
|
||||||
steps: Arc::new(AtomicUsize::new(0)),
|
steps: Arc::new(AtomicUsize::new(0)),
|
||||||
handlers: Default::default(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run(self: &Rc<Self>, input: Receiver<Message>, on_init: Option<OnInit>) {
|
pub fn run(&self, on_init: Option<OnInit>) {
|
||||||
for msg in input.iter() {
|
let (stdin_tx, stdin_rx) = channel();
|
||||||
let typ = &msg.body.typ;
|
let (stdout_tx, stdout_rx) = channel();
|
||||||
if let "init" = typ.as_str() {
|
|
||||||
|
thread::spawn(move || {
|
||||||
|
let stdin = std::io::stdin().lock().lines();
|
||||||
|
for line in stdin {
|
||||||
|
stdin_tx.send(line.unwrap()).unwrap();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
thread::spawn(move || {
|
||||||
|
let mut stdout = std::io::stdout().lock();
|
||||||
|
for msg in stdout_rx {
|
||||||
|
writeln!(&mut stdout, "{msg}").unwrap();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
self.run_internal(stdout_tx, stdin_rx, on_init);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run_internal(
|
||||||
|
&self,
|
||||||
|
stdout_tx: Sender<String>,
|
||||||
|
stdin_rx: Receiver<String>,
|
||||||
|
on_init: Option<OnInit>,
|
||||||
|
) {
|
||||||
|
let (network, node_receiver) = Network::new();
|
||||||
|
let _ = self.network.get_or_init(|| network.clone());
|
||||||
|
self.run_output(stdout_tx, node_receiver);
|
||||||
|
self.run_input(stdin_rx, network, on_init);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run_input(&self, stdin_rx: Receiver<String>, network: Network, on_init: Option<OnInit>) {
|
||||||
|
let (json_tx, json_rx) = channel();
|
||||||
|
let _ = self.backdoor.get_or_init(|| json_tx.clone());
|
||||||
|
|
||||||
|
thread::spawn(move || {
|
||||||
|
for line in stdin_rx {
|
||||||
|
let msg: Message = serde_json::from_str(&line).unwrap();
|
||||||
|
let irt = msg.body.in_reply_to;
|
||||||
|
if let Some(tx) = network.promises.lock().unwrap().remove(&irt) {
|
||||||
|
tx.send(msg).unwrap();
|
||||||
|
} else {
|
||||||
|
json_tx.send(msg).unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
for msg in json_rx {
|
||||||
|
let mut node = self.node.lock().unwrap();
|
||||||
|
if msg.body.typ.as_str() == "init" {
|
||||||
self.init(&msg);
|
self.init(&msg);
|
||||||
let body = Body::from_type("init_ok");
|
let body = Body::from_type("init_ok");
|
||||||
self.reply(&msg, body);
|
self.reply(&msg, body);
|
||||||
if let Some(ref h) = on_init {
|
if let Some(ref on_init) = on_init {
|
||||||
h(self);
|
on_init(self);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let irt = msg.body.in_reply_to;
|
node.handle(self, msg);
|
||||||
{
|
|
||||||
let mut g = self.handlers.lock().unwrap();
|
|
||||||
if let Some(h) = g.remove(&irt) {
|
|
||||||
h(msg.clone());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let mut n = self.node.lock().unwrap();
|
|
||||||
n.handle(self, msg);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn rpc(&self, dest: &str, body: Body, handler: Handler) {
|
fn run_output(&self, stdout_tx: Sender<String>, node_output_rx: Receiver<Message>) {
|
||||||
let mut body = body;
|
thread::spawn(move || {
|
||||||
let msg_id = self.next_msg_id();
|
while let Ok(msg) = node_output_rx.recv() {
|
||||||
body.msg_id = msg_id;
|
let msg = serde_json::to_string(&msg).unwrap();
|
||||||
{
|
stdout_tx.send(msg).unwrap();
|
||||||
let mut g = self.handlers.lock().unwrap();
|
|
||||||
g.insert(msg_id, handler);
|
|
||||||
}
|
}
|
||||||
self.send(dest, body);
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_backdoor(&self) -> Sender<Message> {
|
||||||
|
self.backdoor.get().unwrap().clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn node_id(&self) -> String {
|
pub fn node_id(&self) -> String {
|
||||||
|
@ -124,10 +198,10 @@ impl Runner {
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.iter()
|
.iter()
|
||||||
.map(|s| s.as_str().unwrap().to_string())
|
.map(|s| s.as_str().unwrap().to_string())
|
||||||
.collect::<Vec<_>>();
|
.collect();
|
||||||
|
|
||||||
let _ = self.node_id.get_or_init(|| node_id.to_owned());
|
let _ = self.node_id.get_or_init(|| node_id);
|
||||||
let _ = self.nodes.get_or_init(|| nodes.to_vec());
|
let _ = self.nodes.get_or_init(|| nodes);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reply(&self, req: &Message, body: Body) {
|
pub fn reply(&self, req: &Message, body: Body) {
|
||||||
|
@ -148,36 +222,21 @@ impl Runner {
|
||||||
dest: dest.to_string(),
|
dest: dest.to_string(),
|
||||||
body,
|
body,
|
||||||
};
|
};
|
||||||
let msg = serde_json::to_string(&msg).unwrap();
|
self.network.get().unwrap().send(msg);
|
||||||
self.writeln(&msg);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn writeln(&self, msg: &str) {
|
pub fn rpc(&self, dest: &str, body: Body) -> RpcPromise {
|
||||||
let mut out = OUTPUT.lock();
|
let mut body = body;
|
||||||
let msg = format!("{msg}\n");
|
if body.msg_id == 0 {
|
||||||
out.write_all(msg.as_bytes()).unwrap();
|
body.msg_id = self.next_msg_id();
|
||||||
out.flush().unwrap();
|
|
||||||
}
|
}
|
||||||
}
|
let msg = Message {
|
||||||
|
src: self.node_id().to_string(),
|
||||||
/// Feeds lines from stdin to the MPSC Sender, so that the Receiver can be used
|
dest: dest.to_string(),
|
||||||
/// in the Runner::run() method. Clone the Sender if you want to inject messages
|
body,
|
||||||
/// into the Runner. Join the handle after `run()`.
|
};
|
||||||
pub fn mk_stdin() -> (JoinHandle<()>, Sender<Message>, Receiver<Message>) {
|
self.network.get().unwrap().rpc(msg)
|
||||||
let stdin = std::io::stdin();
|
|
||||||
let (tx, rx) = channel();
|
|
||||||
let xtra_input = tx.clone();
|
|
||||||
|
|
||||||
let join = thread::spawn(move || {
|
|
||||||
let g = stdin.lock();
|
|
||||||
for line in g.lines().map_while(std::result::Result::ok) {
|
|
||||||
if let Ok(msg) = serde_json::from_str(&line) {
|
|
||||||
tx.send(msg).unwrap();
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
(join, xtra_input, rx)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn mk_payload(payload: &[(&str, Value)]) -> Payload {
|
pub fn mk_payload(payload: &[(&str, Value)]) -> Payload {
|
||||||
|
|
Loading…
Reference in a new issue