|
|
|
@ -1,18 +1,24 @@
|
|
|
|
|
use crate::transport::Transport; |
|
|
|
|
use crate::crypto::{PublicKey, Keys}; |
|
|
|
|
use crate::message::{Message, MsgType}; |
|
|
|
|
use crate::tunnel::Tunnel; |
|
|
|
|
|
|
|
|
|
use alloc::vec::Vec; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const UNICAST_TUNNELS: u32 = 2; |
|
|
|
|
const MULTICAST_TUNNELS: u32 = 5; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub struct IronForce { |
|
|
|
|
transport: Transport, |
|
|
|
|
tunnels: Vec<Tunnel>, |
|
|
|
|
key_pack: Keys, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl 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 { |
|
|
|
@ -24,16 +30,43 @@ impl IronForce {
|
|
|
|
|
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>) { |
|
|
|
|
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>) { |
|
|
|
|
self.transport.send(&self.new_message(MsgType::MultiCast, body)) |
|
|
|
|
pub fn multicast(&self, msg: &Message) -> u32 { |
|
|
|
|
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>) { |
|
|
|
|
self.transport.send(&self.new_message(MsgType::UniCast(to), body)) |
|
|
|
|
pub fn send_message_to(&self, msg: &Message, to: PublicKey) -> u32 { |
|
|
|
|
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) { |
|
|
|
@ -41,10 +74,10 @@ impl IronForce {
|
|
|
|
|
match &msg.content.msg_type { |
|
|
|
|
MsgType::Service => { |
|
|
|
|
self.service_msg(&msg.content.body) |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
MsgType::MultiCast => { |
|
|
|
|
// todo
|
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
MsgType::UniCast(target) => { |
|
|
|
|
// todo
|
|
|
|
|
} |
|
|
|
|