|
|
@ -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
|
|
|
|
} |
|
|
|
} |
|
|
|