From 8ed23b9461b8394d6a60529a84ce0b1af848728e Mon Sep 17 00:00:00 2001 From: ennucore Date: Fri, 20 Mar 2020 13:00:53 +0300 Subject: [PATCH] IF --- Cargo.toml | 5 +++++ src/ironforce.rs | 31 ++++++++++++++++++++++++++----- src/transport.rs | 12 +++++++----- 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3ef687f..74131e2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,11 @@ authors = ["ennucore "] edition = "2018" +[features] +default = [] +std = [] + + [dependencies] rand_os = "0.2.2" x25519-dalek = "0.6.0" diff --git a/src/ironforce.rs b/src/ironforce.rs index 4fb83ce..0868d9d 100644 --- a/src/ironforce.rs +++ b/src/ironforce.rs @@ -15,19 +15,40 @@ impl IronForce { IronForce { transport: Transport::new(), key_pack: KeyPack::gen() } } - fn new_message(&self, msg_type: MsgType, body: Vec) -> Message { - Message::new(msg_type, body, &self.key_pack) + fn is_valid_message(&self, msg: &Message) -> bool { + // todo: some kind of PoW + msg.verify() } - fn service_msg(&self, body: Vec) { + fn new_message(&self, msg_type: MsgType, body: &Vec) -> Message { + Message::new(msg_type, body.clone(), &self.key_pack) + } + + fn service_msg(&self, body: &Vec) { self.transport.send(&self.new_message(MsgType::Service, body)) } - pub fn multicast(&self, body: Vec) { + pub fn multicast(&self, body: &Vec) { self.transport.send(&self.new_message(MsgType::MultiCast, body)) } - pub fn send_message_to(&self, to: PublicKey, body: Vec) { + pub fn send_message_to(&self, to: PublicKey, body: &Vec) { self.transport.send(&self.new_message(MsgType::ToTarget(to), body)) } + + fn handle_message(&self, msg: &Message) { + if self.is_valid_message(msg) { + match &msg.msg_type { + MsgType::Service => { + self.service_msg(&msg.body) + }, + MsgType::MultiCast => { + + }, + MsgType::ToTarget(Target) => { + + } + } + } + } } diff --git a/src/transport.rs b/src/transport.rs index 866400e..1590e63 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -4,15 +4,15 @@ use crate::way::Way; use alloc::vec::Vec; use alloc::boxed::Box; -#[cfg(no_std)] +#[cfg(not(feature = "std"))] pub struct Transport {} -#[cfg(not(no_std))] +#[cfg(feature = "std")] pub struct Transport { ways: Vec> } -#[cfg(no_std)] +#[cfg(not(feature = "std"))] impl Transport { pub fn new() -> Self { Self {} @@ -29,14 +29,16 @@ impl Transport { } } -#[cfg(not(no_std))] +#[cfg(feature = "std")] impl Transport { pub fn new() -> Self { Self {ways: Vec::>::new()} } pub fn send(&self, msg: &Message) { - + for way in &self.ways { + way.send(&msg); + } } pub fn receive(&self) -> Message {