diff --git a/Cargo.lock b/Cargo.lock index 9493ff1..679c1fa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,20 +2,50 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + [[package]] name = "distributed-systems-flyio" version = "0.1.0" dependencies = [ + "rand", "serde", "serde_json", ] +[[package]] +name = "getrandom" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe9006bed769170c11f845cf00c7c1e9092aeb3f268e007c3e760ac68008070f" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "itoa" version = "1.0.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c" +[[package]] +name = "libc" +version = "0.2.151" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "302d7ab3130588088d277783b1e2d2e10c9e9e4a16dd9050e6ec93fb3e7048f4" + +[[package]] +name = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + [[package]] name = "proc-macro2" version = "1.0.73" @@ -34,6 +64,36 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + [[package]] name = "ryu" version = "1.0.16" @@ -87,3 +147,9 @@ name = "unicode-ident" version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" diff --git a/Cargo.toml b/Cargo.toml index b00f412..bec9802 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,5 +6,6 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +rand = { version = "0.8.5", features = ["small_rng"] } serde = { version = "1.0.193", features = ["serde_derive"] } serde_json = "1.0.109" diff --git a/src/lib.rs b/src/lib.rs index 1c3377e..ab07bc3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,10 +1,13 @@ pub mod types; + use crate::types::Message; +use rand::{rngs::SmallRng, Rng, SeedableRng}; use std::{ cell::RefCell, collections::HashMap, io::{stdin, stdout, BufRead, Error as IoError, Stdin, Stdout, Write}, rc::Rc, + time::{SystemTime, UNIX_EPOCH}, }; pub struct Malestorm<'a> { @@ -16,6 +19,7 @@ pub struct Malestorm<'a> { struct Node { id: String, nodes: Vec, + rng: SmallRng, } struct MalestormIo { @@ -25,10 +29,15 @@ struct MalestormIo { impl<'a> Default for Malestorm<'a> { fn default() -> Self { + let seed = SystemTime::now() + .duration_since(UNIX_EPOCH) + .expect("time went backwards??"); + Self { node: RefCell::new(Node { id: String::new(), nodes: Vec::new(), + rng: SmallRng::seed_from_u64(seed.as_secs()), }), io: MalestormIo { stdin: stdin(), @@ -112,6 +121,15 @@ impl<'a> Malestorm<'a> { node.nodes = nodes; } + pub fn generate_client_id(&self) -> String { + let mut node = self.node.borrow_mut(); + + let s2: u64 = node.rng.gen(); + let s1 = &node.id; + + format!("{}_{}", s1, s2) + } + pub fn register( &mut self, name: &'a str, diff --git a/src/main.rs b/src/main.rs index 58a748b..618b687 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ use distributed_systems_flyio::{ - types::{EchoMessage, InitMessage, Message}, + types::{EchoMessage, GenerateMessage, InitMessage, Message}, Malestorm, }; use std::rc::Rc; @@ -40,5 +40,19 @@ fn main() { }, ); + program.register( + "generate", + |mut msg: Message, program: Rc| -> Result<(), String> { + msg.body.message_type = "generate_ok".into(); + msg.body.message_body = serde_json::to_value(GenerateMessage { + id: program.generate_client_id(), + }) + .unwrap(); + + program + .reply(msg) + .map_err(|e| format!("generate: error in writing response: {}", e)) + }, + ); program.run(); } diff --git a/src/types.rs b/src/types.rs index 6c6763a..a2a01ed 100644 --- a/src/types.rs +++ b/src/types.rs @@ -35,6 +35,12 @@ pub struct InitMessage { pub nodes: Vec, } +#[derive(Debug, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct GenerateMessage { + pub id: String, +} + #[cfg(test)] mod test {