rename 'encoded_message' to 'decoded_message'
This commit is contained in:
parent
e5150af666
commit
0c4496ae07
4 changed files with 20 additions and 19 deletions
|
@ -1,7 +1,7 @@
|
||||||
[package]
|
[package]
|
||||||
name = "nebkor-maelstrom"
|
name = "nebkor-maelstrom"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
version = "1.0.0"
|
version = "1.0.1"
|
||||||
license-file = "LICENSE.md"
|
license-file = "LICENSE.md"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
description = "An easy-to-use and synchronous client for creating Maelstrom distributed clients."
|
description = "An easy-to-use and synchronous client for creating Maelstrom distributed clients."
|
||||||
|
|
|
@ -1,6 +1,4 @@
|
||||||
use crate::Value;
|
use crate::{check_err, mk_payload, Body, RpcResult, Runner, Value};
|
||||||
|
|
||||||
use crate::{check_err, mk_payload, Body, RpcResult, Runner};
|
|
||||||
|
|
||||||
/// A convenient way to do RPC with Maelstrom's KV services.
|
/// A convenient way to do RPC with Maelstrom's KV services.
|
||||||
#[derive(Debug, Default, Clone)]
|
#[derive(Debug, Default, Clone)]
|
||||||
|
|
26
src/lib.rs
26
src/lib.rs
|
@ -49,9 +49,9 @@ impl Runner {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Start processing messages from stdin and sending them to your node. The `on_init` argument
|
/// Start processing messages from stdin and sending them to your node. The
|
||||||
/// is an optional callback that will be called with `&self` after the `init` message from
|
/// `on_init` argument is an optional callback that will be called with
|
||||||
/// Maelstrom has been processed.
|
/// `&self` after the `init` message from Maelstrom has been processed.
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
|
@ -88,9 +88,10 @@ impl Runner {
|
||||||
self.process_input(on_init);
|
self.process_input(on_init);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get a Sender that will send Messages to the node as input. Useful for triggering periodic
|
/// Get a Sender that will send Messages to the node as input. Useful for
|
||||||
/// behavior from a separate thread, or for sending a Message to the node from `on_init`. See
|
/// triggering periodic behavior from a separate thread, or for sending
|
||||||
/// the `broadcast` example for a use of it.
|
/// a Message to the node from `on_init`. See the `broadcast` example
|
||||||
|
/// for a use of it.
|
||||||
pub fn get_backdoor(&self) -> Sender<Message> {
|
pub fn get_backdoor(&self) -> Sender<Message> {
|
||||||
self.backdoor.get().unwrap().clone()
|
self.backdoor.get().unwrap().clone()
|
||||||
}
|
}
|
||||||
|
@ -123,7 +124,8 @@ impl Runner {
|
||||||
self.outbound_tx.get().unwrap().send(msg).unwrap();
|
self.outbound_tx.get().unwrap().send(msg).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a Receiver<Message> that will receive the reply from the request.
|
/// Returns a Receiver<Message> that will receive the reply from the
|
||||||
|
/// request.
|
||||||
pub fn rpc(&self, dest: &str, body: Body) -> RpcPromise {
|
pub fn rpc(&self, dest: &str, body: Body) -> RpcPromise {
|
||||||
let msg = self.mk_msg(dest, body);
|
let msg = self.mk_msg(dest, body);
|
||||||
let (tx, rx) = channel();
|
let (tx, rx) = channel();
|
||||||
|
@ -164,8 +166,8 @@ impl Runner {
|
||||||
|
|
||||||
fn process_input(&self, on_init: Option<OnInit>) {
|
fn process_input(&self, on_init: Option<OnInit>) {
|
||||||
// for sending Messages to the node's inputs
|
// for sending Messages to the node's inputs
|
||||||
let (encoded_input_tx, encoded_input_rx) = channel();
|
let (decoded_input_tx, decoded_input_rx) = channel();
|
||||||
let _ = self.backdoor.get_or_init(|| encoded_input_tx.clone());
|
let _ = self.backdoor.get_or_init(|| decoded_input_tx.clone());
|
||||||
|
|
||||||
// decouple stdin from processing
|
// decouple stdin from processing
|
||||||
let proms = self.promises.clone();
|
let proms = self.promises.clone();
|
||||||
|
@ -179,13 +181,13 @@ impl Runner {
|
||||||
promise.send(msg).unwrap();
|
promise.send(msg).unwrap();
|
||||||
} else {
|
} else {
|
||||||
// just let the node's `handle()` method handle it
|
// just let the node's `handle()` method handle it
|
||||||
encoded_input_tx.send(msg).unwrap();
|
decoded_input_tx.send(msg).unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// first Message is always `init`:
|
// first Message is always `init`:
|
||||||
let msg = encoded_input_rx.recv().unwrap();
|
let msg = decoded_input_rx.recv().unwrap();
|
||||||
{
|
{
|
||||||
self.init(&msg);
|
self.init(&msg);
|
||||||
let body = Body::from_type("init_ok");
|
let body = Body::from_type("init_ok");
|
||||||
|
@ -197,7 +199,7 @@ impl Runner {
|
||||||
|
|
||||||
// every other message is for the node's handle() method
|
// every other message is for the node's handle() method
|
||||||
let mut node = self.node.lock().unwrap();
|
let mut node = self.node.lock().unwrap();
|
||||||
for msg in encoded_input_rx {
|
for msg in decoded_input_rx {
|
||||||
node.handle(self, msg);
|
node.handle(self, msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,9 +52,10 @@ impl Message {
|
||||||
m
|
m
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The Maelstrom type of a Body (and hence a Message) is just a string. This is for the sake of
|
/// The Maelstrom type of a Body (and hence a Message) is just a string.
|
||||||
/// ease of use for doing the Gossip Glomers challenges; this crate is not meant to be a real
|
/// This is for the sake of ease of use for doing the Gossip Glomers
|
||||||
/// network client framework.
|
/// challenges; this crate is not meant to be a real network client
|
||||||
|
/// framework.
|
||||||
pub fn typ(&self) -> &str {
|
pub fn typ(&self) -> &str {
|
||||||
self.body.typ.as_str()
|
self.body.typ.as_str()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue