Browse Source

Some implementations for multicast and unicast

master
ennucore 5 years ago
parent
commit
880c49cd71
  1. 49
      src/ironforce.rs
  2. 27
      src/transport.rs
  3. 11
      src/tunnel.rs
  4. 1
      src/way.rs

49
src/ironforce.rs

@ -1,18 +1,24 @@
use crate::transport::Transport; use crate::transport::Transport;
use crate::crypto::{PublicKey, Keys}; use crate::crypto::{PublicKey, Keys};
use crate::message::{Message, MsgType}; use crate::message::{Message, MsgType};
use crate::tunnel::Tunnel;
use alloc::vec::Vec; use alloc::vec::Vec;
const UNICAST_TUNNELS: u32 = 2;
const MULTICAST_TUNNELS: u32 = 5;
pub struct IronForce { pub struct IronForce {
transport: Transport, transport: Transport,
tunnels: Vec<Tunnel>,
key_pack: Keys, key_pack: Keys,
} }
impl IronForce { impl IronForce {
pub fn new() -> IronForce { pub fn new() -> IronForce {
IronForce { transport: Transport::new(), key_pack: Keys::gen() } IronForce { transport: Transport::new(), tunnels: Vec::<Tunnel>::new(), key_pack: Keys::gen() }
} }
fn is_valid_message(&self, msg: &Message) -> bool { fn is_valid_message(&self, msg: &Message) -> bool {
@ -24,16 +30,43 @@ impl IronForce {
Message::new(msg_type, body.clone(), &self.key_pack) Message::new(msg_type, body.clone(), &self.key_pack)
} }
fn send_through_tunnel_to(&self, msg: &Message, tunnel: &Tunnel, to: &PublicKey) {
self.transport.send_to(msg, &tunnel.next(&self.key_pack.get_public(), &to).unwrap());
}
fn service_msg(&self, body: &Vec<u8>) { fn service_msg(&self, body: &Vec<u8>) {
self.transport.send(&self.new_message(MsgType::Service, body)) self.transport.send_service(&self.new_message(MsgType::Service, body))
} }
pub fn multicast(&self, body: &Vec<u8>) { pub fn multicast(&self, msg: &Message) -> u32 {
self.transport.send(&self.new_message(MsgType::MultiCast, body)) let mut count = 0;
for tunnel in &self.tunnels {
for node in tunnel.next_nodes(&self.key_pack.get_public()) {
count += 1;
self.send_through_tunnel_to(msg, tunnel, &node)
}
if count == MULTICAST_TUNNELS {
break;
}
}
count
} }
pub fn send_message_to(&self, to: PublicKey, body: &Vec<u8>) { pub fn send_message_to(&self, msg: &Message, to: PublicKey) -> u32 {
self.transport.send(&self.new_message(MsgType::UniCast(to), body)) let mut count = 0;
for tunnel in &self.tunnels {
if tunnel.is_in_tunnel(&to) {
count += 1;
self.send_through_tunnel_to(msg, tunnel, &to)
}
if count == UNICAST_TUNNELS {
break;
}
}
if count == 0 {
// todo: send service message and create new tunnel
}
count
} }
fn handle_message(&self, msg: &Message) { fn handle_message(&self, msg: &Message) {
@ -41,10 +74,10 @@ impl IronForce {
match &msg.content.msg_type { match &msg.content.msg_type {
MsgType::Service => { MsgType::Service => {
self.service_msg(&msg.content.body) self.service_msg(&msg.content.body)
}, }
MsgType::MultiCast => { MsgType::MultiCast => {
// todo // todo
}, }
MsgType::UniCast(target) => { MsgType::UniCast(target) => {
// todo // todo
} }

27
src/transport.rs

@ -1,14 +1,17 @@
use crate::message::Message; use crate::message::Message;
#[cfg(not(feature = "std"))] use crate::message::MsgType; #[cfg(not(feature = "std"))]
use crate::message::MsgType;
use crate::crypto::PublicKey; use crate::crypto::PublicKey;
#[cfg(feature = "std")] use crate::way::Way; #[cfg(feature = "std")]
use crate::way::Way;
#[cfg(feature = "std")] #[cfg(feature = "std")]
use std::thread; use std::thread;
#[cfg(feature = "std")] #[cfg(feature = "std")]
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use alloc::vec::Vec; use alloc::vec::Vec;
#[cfg(feature = "std")] use alloc::boxed::Box; #[cfg(feature = "std")]
use alloc::boxed::Box;
#[cfg(not(feature = "std"))] #[cfg(not(feature = "std"))]
@ -35,6 +38,14 @@ impl Transport {
Vec::<u8>::new(), Vec::<u8>::new(),
&crate::crypto::Keys::gen()), PublicKey { key: [0u8; 32] }) &crate::crypto::Keys::gen()), PublicKey { key: [0u8; 32] })
} }
pub fn send_service(&self, msg: &Message) {
}
pub fn send_to(&self, msg: &Message, to: &PublicKey) {
}
} }
#[cfg(feature = "std")] #[cfg(feature = "std")]
@ -44,12 +55,18 @@ impl Transport {
transport transport
} }
pub fn send(&self, msg: &Message) { pub fn send_service(&self, msg: &Message) {
for way in &self.ways { for way in &self.ways { // todo: do it in threads
way.send(&msg); way.send(&msg);
} }
} }
pub fn send_to(&self, msg: &Message, to: &PublicKey) {
for way in &self.ways {
way.send_to(&msg, to);
}
}
pub fn receive(&mut self) -> (Message, PublicKey) { pub fn receive(&mut self) -> (Message, PublicKey) {
if self.msg_pool.len() != 0 { if self.msg_pool.len() != 0 {
let msg = self.msg_pool[0].clone(); let msg = self.msg_pool[0].clone();

11
src/tunnel.rs

@ -60,4 +60,15 @@ impl Tunnel {
Option::from(self.nodes[my_index + 1].clone()) Option::from(self.nodes[my_index + 1].clone())
}; };
} }
pub fn next_nodes(&self, me: &PublicKey) -> Vec<PublicKey> {
let my_index = self.nodes
.iter()
.enumerate()
.find(|&node| node.1 == me).unwrap().0;
let mut result = Vec::<PublicKey>::new();
if my_index > 0 { result.push(self.nodes[my_index - 1].clone()) };
if my_index < self.nodes.len() - 1 { result.push(self.nodes[my_index + 1].clone()) };
result
}
} }

1
src/way.rs

@ -3,5 +3,6 @@ use crate::crypto::PublicKey;
pub trait Way { pub trait Way {
fn send(&self, msg: &Message); fn send(&self, msg: &Message);
fn send_to(&self, msg: &Message, to: &PublicKey);
fn receive(&self) -> (Message, PublicKey); fn receive(&self) -> (Message, PublicKey);
} }

Loading…
Cancel
Save