ennucore 5 years ago
parent
commit
8ed23b9461
  1. 5
      Cargo.toml
  2. 31
      src/ironforce.rs
  3. 12
      src/transport.rs

5
Cargo.toml

@ -5,6 +5,11 @@ authors = ["ennucore <ennucore@gmail.com>"]
edition = "2018"
[features]
default = []
std = []
[dependencies]
rand_os = "0.2.2"
x25519-dalek = "0.6.0"

31
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<u8>) -> 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<u8>) {
fn new_message(&self, msg_type: MsgType, body: &Vec<u8>) -> Message {
Message::new(msg_type, body.clone(), &self.key_pack)
}
fn service_msg(&self, body: &Vec<u8>) {
self.transport.send(&self.new_message(MsgType::Service, body))
}
pub fn multicast(&self, body: Vec<u8>) {
pub fn multicast(&self, body: &Vec<u8>) {
self.transport.send(&self.new_message(MsgType::MultiCast, body))
}
pub fn send_message_to(&self, to: PublicKey, body: Vec<u8>) {
pub fn send_message_to(&self, to: PublicKey, body: &Vec<u8>) {
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) => {
}
}
}
}
}

12
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<Box<dyn Way>>
}
#[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::<Box<dyn Way>>::new()}
}
pub fn send(&self, msg: &Message) {
for way in &self.ways {
way.send(&msg);
}
}
pub fn receive(&self) -> Message {

Loading…
Cancel
Save